The Sip Servlet Proxy example

This example shows how to create a simple SIP proxy servlet.
The proxy will just forward all SIP messages from SIP caller (UAC) to SIP callee (UAS).
In order to test this code you may use sipp SIP generator.

Note that this sample works best with sipp 1.1 .

The servlet code :

package com.ericsson.sip.servlet.example;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.sip.Proxy;
import javax.servlet.sip.SipErrorEvent;
import javax.servlet.sip.SipErrorListener;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipServletResponse;



public class SimpleProxyServlet 
            extends SipServlet 
            implements SipErrorListener,Servlet {
    
    /** Creates a new instance of SimpleProxyServlet */
    public SimpleProxyServlet() {
    }
    
    
    protected void doInvite(SipServletRequest request) 
        throws ServletException, IOException {
    	 
        if (request.isInitial()) {

           Proxy proxy = request.getProxy();
           proxy.setRecordRoute(true);
	   proxy.setSupervised(true);
	   proxy.proxyTo(request.getRequestURI()); // bobs uri
          
        }
        
        System.out.println("SimpleProxyServlet: Got request:\n" + request);
    }
    
    
    protected void doBye(SipServletRequest request) throws ServletException, IOException {
        
        System.out.println("SimpleProxyServlet: Got BYE request:\n" + request);
        super.doBye(request);
    }
    
    
    protected void doResponse(SipServletResponse response) 
        throws ServletException, IOException {
        
        System.out.println("SimpleProxyServlet: Got response:\n" + response);
 	super.doResponse(response);
    }
    
    
    // SipErrorListener
    
    public void noAckReceived(SipErrorEvent ee) {
         
        System.out.println("SimpleProxyServlet: Error: noAckReceived.");
    }
    
    public void noPrackReceived(SipErrorEvent ee) {
        
	System.out.println("SimpleProxyServlet: Error: noPrackReceived.");
    }
    
}

Archives for Sip Servlets must be of type .sar. In this case the archive can be called sip-proxy.sar.
Sip Servlet archives must also contain a deployment descriptor called sip.xml, in the WEB-INF archive directory.

sip-proxy
|-- WEB-INF
|   |-- classes
|   |   |-- com
|   |       |-- ericsson
|   |           |-- sip
|   |               |-- examples
|   |                   |-- SimpleProxyServlet.class
|   |-- lib
|   |-- sip.xml

Here is a deployment descriptor for this example:

<?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">
<!-- Some documentation needed here -->

<sip-app>
    <display-name>Simple Proxy Servlet</display-name>
    <description>Simple Proxy Servlet</description>

    <listener>
        <listener-class>com.ericsson.sip.servlet.example.SimpleProxyServlet</listener-class>
    </listener>

    <servlet>
        <servlet-name>SimpleProxyServlet</servlet-name>
        <display-name>SimpleProxyServlet</display-name>
        <description>Simple SIP proxy servlet</description>
        <servlet-class>com.ericsson.sip.servlet.example.SimpleProxyServlet</servlet-class>
     	<load-on-startup>1</load-on-startup>     
    </servlet>
    
    <servlet-mapping>
        <servlet-name>SimpleProxyServlet</servlet-name>
        <pattern>
	   <and>
           <equal><var>request.method</var><value>INVITE</value></equal>
	   </and>
       </pattern>
    </servlet-mapping> 

</sip-app>

To test the proxy, we will use sipp generator.
SIPp is a free Open Source test tool / traffic generator for the SIP protocol.
It can be downloaded from :

http://sipp.sourceforge.net/

In this test, we asume that :

  • the SIP proxy runs on a glassfish application server on a host with ip address 192.168.1.2
  • the SIP client (UAC) runs on a host with ip address 192.168.1.5 and accepts responses on port 5080
  • the SIP server (UAS) runs on a host with ip address 192.168.1.8 and accepts requests on port 5090

To run the test :

1) Build and deploy the SIP proxy

2) Start the SIP server (UAS) on the host with ip address 192.168.1.8 and port 5090 :

>> sipp -sn uas -p 5090

3) Start the SIP client (UAC) on the host with ip address 192.168.1.5 and port 5080.
All traffic between SIP client and server will pass through the proxy (-rsa flag) :

>> sipp -sn uac -rsa 192.168.1.2:5060 -p 5080 192.168.1.8:5090

4) Check the message flow on the text screens of SIP client and server and on sip.log
The output on the server side looks like this

[ps125818@prsad SIPp]$ ./sipp -sn uas -mp 6001 -p 5090
------------------------------ Scenario Screen -------- [1-5]: Change Screen --
  Port   Total-time  Total-calls  Transport
  5090      16.89 s          197  UDP

  8 new calls during 0.880 s period      2 ms scheduler resolution
  43 concurrent calls                    Peak was 71 calls, after 4 s
  1 open sockets

                                 Messages  Retrans   Timeout   Unexpected-Msg
  ----------> INVITE             197       0                   0

  <---------- 180                197       0
  <---------- 200                197       11        0
  ----------> ACK        E-RTD   195       0                   0

  ----------> BYE                197       0                   0
  <---------- 200                195       0
  [   4000ms] Pause              195                           0
------------------------------ Test Terminated --------------------------------

The output on the client side looks like this

[ps125818@prsad SIPp]$ ./sipp -sn uac -rsa 129.158.229.134:5060 -p 5072 129.158.229.134:5090
Resolving remote sending address 129.158.229.134...
Resolving remote host '129.158.229.134'... Done.
------------------------------ Scenario Screen -------- [1-5]: Change Screen --
  Call-rate(length)     Port   Total-time  Total-calls  Remote-host
  10.0(0 ms)/1.000s   5072      26.54 s          225  129.158.229.134:5090(UDP)

  0 new calls during 0.529 s period      2 ms scheduler resolution
  30 concurrent calls (limit 30)         Peak was 30 calls, after 3 s
  0 out-of-call msg (discarded)
  1 open sockets

                                 Messages  Retrans   Timeout   Unexpected-Msg
      INVITE ---------->         225       0         0
         100 <----------         225       0                   0
         180 <----------         197       0                   0
         183 <----------         0         0                   0
         200 <---------- E-RTD   197       11                  0
         ACK ---------->         197       11
   Var Pause [      0ms]         197                           0
         BYE ---------->         197       13        0
         200 <----------         195       0                   0

------------------------------ Test Terminated --------------------------------

Comments:

  • Choosing three different addresses is just to make everything more visual. It can also be run on a single machine.

List of terms

A terminology list can be found here.

Having trouble to run the demo ?

A list of known limitations and problems can be found here

Links page

Useful Sip Container related links can be found here.

Cannot resolve external resource into attachment.