Tuesday, February 24, 2009

Cisco IP phone directory from text file using JSP

This was a relatively simple excercise. I was approched by a co-worker who wanted to import a comman delimted text file from another system and create a Cisco IP phone directory from that file. As a one time operation, you could do this easily in a text editor. he wanted to create a nightly batch process that pulled the text file fomr another system, so the jsp seemed like the way to go.

Le me just state up front that I am a hack JSP guy, but this seems pretty simple. The environment is a linux distro using tomcat6 as a web server / jsp container.


Sorry about the formating here, i am really new to this editor!


The Cisco XML menu syntax is as follows:



<CiscoIPPhoneMenu>
<Prompt>Select an entry to dial</Prompt>
<MenuItem>
<Name>Wes</Name>
<URL>Dial:32081</URL>
</MenuItem>
<MenuItem>
<Name>Steve</Name>
<URL>Dial:32082</URL>
</MenuItem>
<MenuItem>
<Name>Mike</Name>
<URL>Dial:32538</URL>
</MenuItem>
</CiscoIPPhoneMenu>



The text file looks like this:

#Comment lines prefaced with #
Wes,32081
Steve,32082
Mike,32538

So the JSP does the following:


1. Create the XML header
2. Repeat for each line in the file:
a. Read and parse a line of text
b. Write the XML directory entry
3. Write the closing XML tags


Pretty simple. Here is the result:


<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>

<CiscoIPPhoneMenu>
<% try {
BufferedReader br = new BufferedReader(new FileReader("/srv/www/tomcat6/webapps/ROOT/teldir/pb.txt"));%>
<Prompt>Select an entry to dial</Prompt><%

try {
String line = null;
while (( line = br.readLine()) != null) {
if (!line.substring(0,1).equals("#")) {
String[] d=line.split(","); %>
<MenuItem><Name><%= d[0] %></Name>
<URL>Dial:<%= d[1] %></URL>
</MenuItem><%
}
}
} finally {
br.close();
}
} catch (IOException ex){%>
<Prompt>Directory File Not Found</Prompt><%
ex.printStackTrace(); }%>
</CiscoIPPhoneMenu>