Tutorial:Working as B2BUA

From Wesip

From Wesip

Many applications in SIP networks are implemented as a B2BUA (Back to Back User Agent). A good example of this kind of applications is a prepayed control system where the application must be able to tear down the session when it detects that the caller has run out of credit acting like a man in the middel.

To implement this kind of behaviour a SIP Servlet uses the SipFactory method createRequest(SipServletRequest origRequest, boolean sameCallId) when processing an initial request coming from the upstream to create the request that will be sent to the downstream.

The method returns a new request that is identical to the original in everything except the following:

  • The 'From tag' is recalculated and the 'To tag' is removed.
  • The 'To tag' is removed
  • The Record-Route and Via tags are removed
  • The Contact header is replaced by the containers contact address ('except for REGISTERS)
  • The Call-ID changes depending on the boolean parameter sameCallId passed to the method

The resultant request is by all means an initial request and as such is bounded to a new SipSession and send downstream. Both SipSessions the upstream" SipSession and the downstream SipSession share the same SipApplicationSession.


protected void doInvite(SipServletRequest req) throws
   javax.servlet.ServletException, java.io.IOException{
       ....
   //Retrieve SipFactory through the context
   ServletContext sc = ServletContext.getServletContext();
   SipFactory sf = (SipFactory) sc.getAttribute("javax.servlet.sip.SipFactory");
   //Clone incoming request
   SipServletRequest downstreamReq = sf.createRequest(req,true);
   //Retrieve SipApplicationSession and SipSessions
   SipApplicationSession sas = req.getSession().getApplicationSession();
   SipSession upstreamLeg = req.getSession();
   SipSession downstreamLeg = downstreamReq.getSession();
   //Save reference to SipSessions in the SipApplication
   sas.setAttribute("upstreamLeg",upstreamLeg);
   sas.setAttribute("downstreamLeg",downstreamLeg);
   //Save references to the initial request in the SipSession objects
   upstreamLeg.setAttribute("initialRequest",req);
   downstreamLeg.setAttribute("initialRequest", downstreamReq);
   //Send the new req downstream
   downstreamReq.send();
   ....
}
....
protected void doResponse(SipServletResponse resp) throws
   javax.servlet.ServletException,java.io.IOException{
   ......
   //Retrieve the upstream request to respond it
   SipSession upstreamLeg =
   (SipSession)resp.getSession().getApplicationSession().getAttribute("upstreamLeg");
   SipServletRequest upstreamRequest =
   (SipServletRequest)upstreamLeg.getAttribute("initialRequest");
   SipServletResponse upstreamResponse =
   upstreamRequest.createResponse(resp.getStatus(),resp.getReasonPhrase());
   //Copy the content from the downstream response to the upstream response
   if (resp.getContentType() != null) {
       upstreamResponse.setContent(resp.getRawContent(), resp.getContentType());
   }
   //Send the response upstream
   upstreamResponse.send();
   ......
}


<< Working as UAS | Working as Proxy >>