Tutorial:Accessing the SIP message content

From Wesip

From Wesip

Often, developers need access to the content of the SIP messages. The SIP Servlet API provides a set of methods to read and write such content. They are all methods of the SipServletMessage interface. These methods are

int getContentLength(); 
void setContentLength(int len); 

to read or set the length of the message

String getContentType(); 
void setContentType(String type); 

to read or set the content type.

Object getContent(); 
byte[] getRawContent(); 
void setContent(Object obj, String type); 

used for direct access to the content. getContent will return a different type of object depending on the Content-Type. For "text/*" MIME types getContent will return a String unless the container has specific knowledge of the type. If the content type is unknown to the container and it is not of the text type, a byte[] is returned, just like when calling to getRawContent.

Likewise, the method setContent(Object obj, String type) accepts a String for text MIME types and a byte[] for others. If called with text type but not with a String the method will automatically convert the content object with a call to its toString() method.

The encoding used by those methods can be read or set via the getCharacterEncoding() and setCharacterEncoding(String enc) methods of SipServletMessage respectively. For incoming messages. if a charset attribute is present in the Content-Type header it is used, otherwise the default UTF-8 or the one set by setCharacterEncoding(String enc) will be used.

SDP Management

SIP applications very often have to deal with SDP. The JAIN familiy of specifications includes JAIN-SDP (JSR 141) which is designed to help developers with SDP multimedia session descriptionss. JAIN SDP offers a set of objects and interfaces wich developers can use to create,read or modify the SDP content of SIP messages. A free implementation can be found at https://jain-sip.dev.java.net/.

To use JAIN SDP within you SIP applications place the required libs in your /WEB-INF/lib directory and instantiate a SessionDescription as follows

....

protected void doInvite(SipServletRequest req)
                throws javax.servlet.ServletException,
                       java.io.IOException{
   ....
  
   //Save the SDP content in a String
   String sdpContent = 
             new String(req.getRawContent(),req.getCharacterEncoding());  
   //Use the static method of SdpFactory to
   //parse the content
   SessionDescription requestSDP = 
             SdpFactory. createSessionDescription(sdpContent);
   ....
}

....

<< Converged SIP & HTTP Applications | Layering techniques >>