An Introduction to SIP, Part 2: SIP Servlets
Pages: 1, 2, 3, 4

3. Business logic code

The rest of the code consists of helper methods. The first two send messages out to instant messengers. To send a message, use a factory to create:

  • A SipApplicationSession (more on this in a bit)
  • A request message

At that point, we can massage the message any way we want. In our case, we add in the payload our instant message text. Finally, we send the message.

private void sendToAll(String from, String message)
        throws ServletParseException, IOException {
    SipFactory factory = (SipFactory)getServletContext().
        getAttribute("javax.servlet.sip.SipFactory");

    List list = (List)getServletContext().getAttribute(THE_LIST);
    Iterator users = list.iterator();
    while (users.hasNext()) { //Send this message to all on the list.
        String user = (String) users.next();

        SipApplicationSession session =
            factory.createApplicationSession();
        SipServletRequest request = factory.createRequest(session,
                "MESSAGE", serverAddress, user);
        String msg = from + " sent message: \n" + message;
        request.setContent(msg.getBytes(), "text/plain");
        request.send();
    }
}

private void sendToUser(String to, String message)
        throws ServletParseException, IOException {
    SipFactory factory = (SipFactory)getServletContext().
        getAttribute("javax.servlet.sip.SipFactory");
    SipApplicationSession session = factory.createApplicationSession();
    SipServletRequest request = factory.createRequest(session,
            "MESSAGE", serverAddress, to);
    request.setContent(message.getBytes(), "text/plain");
    request.send();
}

private boolean containsUser(String from) {
    List list = (List)getServletContext().getAttribute(THE_LIST);
    return list.contains(from);
}

private void addUser(String from) {
    List list = (List)getServletContext().getAttribute(THE_LIST);
    list.add(from);
}

private void removeUser(String from) {
    List list = (List)getServletContext().getAttribute(THE_LIST);
    list.remove(from);
}

}

Deployment Descriptor

When dealing with HTTP servlets, the infamous web.xml deployment descriptor must also be written. In the SIP servlet world, this corresponds to the file sip.xml, where we list the SIP servlets, initialization parameters, and mappings (which SIP message is to be processed by which SIP servlet). I'd like to provide links to more information about the syntax of this file, but the only data available is the DTD in the SIP Servlet Specification, section 15.5. The syntax is similar to web.xml, except for the <servlet-mapping> tag. This doesn't map a URL pattern to a servlet. Instead, it describes a condition (based on the content of fields and subfields) that a SIP request must fulfill in order to be mapped to a servlet. The SIP Servlet Specification, section 11, describes all the fields, subfields, and conditions used for this mapping.

Note that this mapping is used only for the initial requests; subsequent requests in the same session/dialog are handled by the same servlet as the initial one.

Here is the XML code we need for the ChatRoomServer:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sip-app
   PUBLIC "-//Java Community Process//DTD SIP Application 1.0//EN"
   "http://www.jcp.org/dtd/sip-app_1_0.dtd">
<sip-app>
   <servlet>
      <servlet-name>ChatRoomServer</servlet-name>
      <servlet-class>dev2dev.chatroomserver.ChatRoomServer</servlet-class>
      <init-param>
         <param-name>dev2dev.chatroomserver.name</param-name>
         <!-- This will be replaced by the build script -->
         <param-value>sip:chatroomname@serveraddress</param-value>
      </init-param>
   </servlet>

   <servlet-mapping>
      <servlet-name>ChatRoomServer</servlet-name>
      <pattern>
         <and>
            <equal>
               <var>request.uri.user</var>
               <!-- This will be replaced by the build script -->
               <value>chatroomname</value>
            </equal>
           <equal>
             <var>request.method</var>
             <value>MESSAGE</value>
           </equal>
         </and>
      </pattern>
   </servlet-mapping>

</sip-app>

This looks more complicated than it really is. The servlet mapping simply says:

Map incoming MESSAGE requests to the ChatRoomServer Servlet if the username part of the request URI is equal to chatroomname.

This chat room name is just a placeholder. The build process will replace the keyword "chatroomname" with the real name of the chat room.

How is this useful? Simply put, you can deploy the same service many times, each with its own unique chat room name, and messages will be routed automatically to the right servlet.

Pages: 1, 2, 3, 4

Next Page ยป