This is modified base on the example of EricssonSDS' B2BUA by Norz 2010-05-25 15:49:35 /** * Name of the p-asserted-identity header. */ private static final String ASSERTED_IDENTITY_HEADER = "p-asserted-identity"; /** * The INVITE is used to initiate a dialog between two users. The servlet * will intercept all subssequent MESSAGEs in the dialog and send them to * the other side. Since the servlet is acting as a B2BUA, the requests are * received and handled (200 OK) before being re-created in the * corresponding dialog. */ protected void doInvite(SipServletRequest sipServletRequest) throws ServletException, IOException { try { // Check if the invited side has been in call-sessionMap Address to = sipServletRequest.getTo(); String uri = to.getURI().toString(); Set set = sessionMap.entrySet(); Iterator it = set.iterator(); int n = -1; int m = -1; Map.Entry me = (Map.Entry)it; n = me.getKey().toString().indexOf(uri); m = me.getValue().toString().indexOf(uri); while(it.hasNext() && ( n == -1 || m == -1)) { me = (Map.Entry)it.next(); n = -1; m = -1; n = me.getKey().toString().indexOf(uri); m = me.getValue().toString().indexOf(uri); } if( n == -1 || m == -1){ //Callee is not in call session // Send back 200 OK to accept the session SipServletResponse response = sipServletRequest.createResponse(200); response.send(); } // Create the new INVITE for the destination Address from = sipServletRequest.getFrom(); //Address to = sipServletRequest.getTo(); SipApplicationSession applicationSession = sipFactory .createApplicationSession(); SipServletRequest request = sipFactory.createRequest( applicationSession, "INVITE", from, to); // The route is required because we want the request to go directory // to the s-cscf. addCscfRoute(request); // We also need to add the p-asserted-identity header for the s-cscf // to validate our request String assertedIdentity = sipServletRequest .getHeader(ASSERTED_IDENTITY_HEADER); addAssertedIdentity(request, assertedIdentity); // Set the default handler for the session. The default handler is // the name of the servlet // as described in the sip.xml request.getSession().setHandler("test002"); // Send the request request.send(); // Put the two entries in the map : one to match the callee // with the caller and vice-versa. These entries will then be used // to forward messages. // More code should handle the error cases (eg if the INVITE is // refused) // but this is not done here. SipSession calleeSession = request.getSession(); SipSession callerSession = sipServletRequest.getSession(); sessionMap.put(calleeSession, callerSession); sessionMap.put(callerSession, calleeSession); } catch (Exception e) { } } /** * Adds the p-asserted-identity header to <code>request</code>. The content * of the header is determiend by <code>assertedIdentity</code>. * * @param request * the request to which the p-asserted-identity is added. * @param assertedIdentity * the content of the header */ private void addAssertedIdentity(SipServletRequest request, String assertedIdentity) { request.setHeader(ASSERTED_IDENTITY_HEADER, assertedIdentity); } /** * Adds a route to the request. This will force the request to be routed to * the s-cscf which is located by default on port 5082 on the localhost. * This value needs to be changed if the s-cscf is located elsewhere. * * @param request * request to be updated with the new route. */ private void addCscfRoute(SipServletRequest request) { SipURI route = sipFactory.createSipURI("", "172.18.58.27:5082"); request.pushRoute(route); } /** * @inheritDoc */ protected void doResponse(SipServletResponse sipServletResponse) throws ServletException, IOException { if( sipServletResponse.getStatus() == 182 ) { sipServletResponse.setStatus(183); } super.doResponse(sipServletResponse); }