Tutorial:Working as UAC

From Wesip

From Wesip

Whereas HTTP Servlets only response requests coming from clientes SIP Servlets can do more. SIP Servlets can act as a UAC (User Agent Client), a UAS (User Agent Server), a "proxy" or a "B2BUA"(Back to Back User Agent).

When acting as a UAC, that is,sending requests, the servlet has two ways to operate depending on wether the request is an initial request or a subsequent request.

To create an initial request from the scratch the servlet has to retrieve the SipFactory from the ServletContext, invoke to the createRequest method, modify the SipServletRequest as appropriate and then send the new created request.


//Retrieve SipFactory through the context
ServletContext sc = ServletContext.getServletContext();
SipFactory sf = (SipFactory) sc.getAttribute("javax.servlet.sip.SipFactory");
//Create a new application session (required for request creation)
SipApplicationSession sas = sf.createApplicationSession();
//Create the new request using the SipFactory
SipServletRequest req = sf.createRequest(sas, INVITE_METHOD, callerURI, calleeURI);
//Assign the sipservlet as handler of subsequent responses and requests
req.getSession().setHandler("myUACServlet");
//Push a route if needed
req.pushRoute("theURIPushed");
//Send the request
req.send();

To create a subsequent request the only thing we need is the SipSession. Then we can proceed like this

//Sending BYE because we're done 
mySipSession.createRequest("BYE");
//Send the request
req.send(); 

Other facilities that a UAC requires is a way to create ACKs and CANCELs.

ACKs to non-2xx responses are generated automatically by the container since tipically they aren't useful to the application but ACKs to 2xx responses are generated by the servlet. Since the ACK is sent upon receipt of the response, ACK requests are created using the SipServletResponse method createACK.

.....
doSuccessResponse(SipServletResponse resp) throws 
                javax.servlet.ServletException,java.io.IOException {
.....
resp.createACK().send();
.....
}

CANCELs are obtained from the request that we want to cancel.

SipServlet Request cancelRequest = inviteRequest.getCancel();
cancelRequest.send();

<< SipSessions and Dialogs | Working as UAS >>