Applications:Ringtone - Code
From Wesip
From Wesip
The Ringtone application is made of two SIP servlets.
- ProxyRingtoneServlet: Handles the incoming INVITE request, initiates and terminates the media server dialog when required. Prepares the proxy operation and processes proxy responses.
- MediaServletControlServlet: Handles the media server dialog and resumes the proxy operation.
ProxyRingtoneServlet
The servlet processes incoming requests preparing the proxy operation
protected void doInvite(SipServletRequest callerInvite) throws ServletException, IOException { .... //Get a proxy for this request Proxy callerProxy = callerInvite.getProxy(); callerProxy.setSupervised(true);
But after creating the proxy object it doesn't proceed to proxy the request. Instead it initiates the media server dialog and passes the control to the MediaServletControlServlet
//Create media server dialog SipServletRequest mediaServerInvite = sipFactory.createRequest(callerInvite.getApplicationSession(), "INVITE", callerInvite.getFrom(), sipFactory.createAddress(uriPlayToneUri)); //Delegate handling of the media server dialog to //the MediaServletControlServlet mediaServerInvite.getSession(). setHandler(MediaServletControlServlet.myServletName); .... mediaServerInvite.send();
The MediaServletControlServlet will setup the dialog with the media server and resume the proxy operation. Once proxying is resumed the responses are managed by ProxyRingtoneServlet. The only action required is the disposal of the media server dialog upon reception of a final response because final responses terminate the early media which is limited to provisional stages of the dialog
protected void doResponse(SipServletResponse response) throws ServletException, IOException {
//When we receive a final response from the caller //is time to finish the media server dialog
if (response.getMethod().equalsIgnoreCase("INVITE") && response.getStatus() >= 200){ SipSession mediaServerDialog = (SipSession)response.getApplicationSession(). getAttribute(MEDIA_SERVER_DIALOG); mediaServerDialog.createRequest("BYE").send();
MediaServletControlServlet
The MediaServletControlServlet manages the temporary dialog with the media server. In this dialog the application server acts like a UAC processing the responses that come from the UAS media server. Upon stablishment of the media server dialog the SDP of the 200OK response is used to generate a 183 response to the upstream
//Use the media server SDP to construct //the 183 response to the caller in the upstream Proxy callerProxy = (Proxy)mediaServerResponse.getSession(). getAttribute(CALLER_PROXY); SipServletResponse earlyMediaResponse = callerProxy.getOriginalRequest().createResponse(183);
earlyMediaResponse.setContent(mediaServerResponse.getContent(), mediaServerResponse.getContentType()); earlyMediaResponse.send();
After the upstream 183 response is sent proxying can be resumed
//Continue with proxying callerProxy.proxyTo(callerProxy.getOriginalRequest().getRequestURI());
Which will return control to the ProxyRingtoneServlet which will handle proxy responses and subsequent requests.