WAS v8.5 > Reference > Developer examples

Example: SIP servlet simple proxy

This is a servlet example of a simple proxy.


Simple proxy

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.sip.Proxy;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipServletResponse;
import javax.servlet.sip.SipSession;
import javax.servlet.sip.SipURI;
import javax.servlet.sip.URI;


public class SimpleProxy extends SipServlet implements Servlet {


 final static  private String  SHUTDOWN_KEY = new String("shutdown");
 final static  private String  STATE_KEY = new String("state");
 final static  private int   INVITE_RECEIVED = 1;

 /* (non-Java-doc)
  * @see javax.servlet.sip.SipServlet#SipServlet()
  */
 public SimpleProxy() {
  super();
 }

 /* (non-Javadoc)
  * @see javax.servlet.sip.SipServlet#doInvite(javax.servlet.sip.SipServletRequest)
  */
 protected void doInvite(SipServletRequest request) throws ServletException,
   IOException {

  //log("SimpleProxy: doInvite: TOP");

  try {
   if (request.isInitial() == true)
   {
    // This should cause the sip session to be created. This sample only uses the session on receiving     // a BYE but the Tivoli performance viewer can be used to track the creation of calls by viewing the     // active session count.
    Integer state = new Integer(INVITE_RECEIVED);
    SipSession session = request.getSession();
    session.setAttribute(STATE_KEY, state);
             log("SimpleProxy: doInvite: setting attribute");

    Proxy proxy = request.getProxy();

    SipFactory sipFactory = (SipFactory) getServletContext().getAttribute(SIP_FACTORY);
          if (sipFactory == null) {
              throw new ServletException("No SipFactory in context");
          }

          String callingNumber = request.getTo().toString();
          if (callingNumber != null)
          {
              String destStr = format_lookup(callingNumber);
              URI dest = sipFactory.createURI(destStr);

              //log("SimpleProxy: doInvite: Proxying to dest URI ="  + dest.toString());

           if (((SipURI)request.getRequestURI()).getTransportParam() != null)
            ((SipURI)dest).setTransportParam(((SipURI)request.getRequestURI()).getTransportParam());

           proxy.setRecordRoute(true);
     proxy.proxyTo(dest);
          }
    else
    {
              //log("SimpleProxy: doInvite: Request is invalid. Did not contain a To: field.");
     SipServletResponse sipresponse = request.createResponse(400);
     sipresponse.send();
    }
   }
   else
   {
          //log("SimpleProxy: doInvite: target refresh, let container handle invite");
    super.doInvite(request);
   }
  }
  catch (Exception e){
   e.printStackTrace();
  }
 }

 /* (non-Javadoc)
  * @see javax.servlet.sip.SipServlet#doResponse(javax.servlet.sip.SipServletResponse)
  */
 protected void doResponse(SipServletResponse response) throws ServletException,
   IOException {
   super.doResponse(response);

   // Example of using the session object to store session state.
  SipSession session = response.getSession();
  if (session.getAttribute(SHUTDOWN_KEY) != null)
  {
   //log("SimpleProxy: doResponse: invalidating session");
   session.invalidate();
  }
 }

 /* (non-Javadoc)
  * @see javax.servlet.sip.SipServlet#doBye(javax.servlet.sip.SipServletRequest)
  */
 protected void doBye(SipServletRequest request) throws ServletException,
   IOException {

  SipSession session = request.getSession();
  session.setAttribute(SHUTDOWN_KEY, new Boolean(true));

        //log("SimpleProxy: doBye: invalidate session when responses is received.");
  super.doBye(request);
 }

 protected String format_lookup(String toFormat){
  int start_index = toFormat.indexOf('<') + 1;
  int end_index = toFormat.indexOf('>');

  if(start_index == 0){
   //don't worry about it   }
  if(end_index == -1){
   end_index = toFormat.length();
  }

  return toFormat.substring(start_index, end_index);
 }}


Related


Browse all SIP topics
SIP servlets


Reference:

SIP SipServletRequest and SipServletResponse classes
SIP SipSession and SipApplicationSession classes
Example: SIP servlet Proxy servlet class
Example: SIP servlet SendOnServlet class


+

Search Tips   |   Advanced Search