Solved

doPrack() function implementation

Posted on 2007-11-23
5
605 Views
Last Modified: 2013-11-24
Hi,
I am trying to develope a B2bua servlet with the B2buaHelper interface using the Micromethod ACE and absolutly don´t know how to implement the doPrack() function.
The API doesn´t implement the SipServletRequest createPrack() function.
It is for a school project, can someone please help me?

Thanks for any answer or suggestion.
package com.oims.net.sip.services;
 

import java.io.IOException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.ListIterator;
 

import javax.servlet.ServletException;

import javax.servlet.sip.SipServlet;

import javax.servlet.sip.SipServletMessage;

import javax.servlet.sip.SipServletRequest;

import javax.servlet.sip.SipServletResponse;

import javax.servlet.sip.SipSession;

import javax.servlet.sip.SipURI;
 

import com.micromethod.sipmethod.server.module.sip.B2buaHelper;

import com.micromethod.sipmethod.server.module.sip.message.SipRequest;
 

/**

 * @author root

 * 

 */

public class B2bua extends SipServlet {
 

	private static final long serialVersionUID = 1L;

	private Integer Rx = 0;

	private Integer Tx = 0;

	private HashMap<String, String> MyMap;
 

	protected void doAck(SipServletRequest req) throws ServletException, IOException {

		logRx(null, req);

		// Retrieve the two call legs by B2buaHelper.

		B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

		SipSession s1 = req.getSession();

		SipSession s2 = b2buahelp.getLinkedSession(s1);

		// Retrieve the previous response from the callee to the B2BUA

		SipServletResponse resp = (SipServletResponse) s2.getAttribute("TERM_RESP");

		// Create the ACK to the callee

		SipServletRequest ack = resp.createAck();

		copyContent(req, ack);

		ack.send();

		logTx(null, ack);

		log("doAck");

		super.doAck(req);

	}
 

	protected void doBye(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doBye");

	}
 

	protected void doCancel(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doCancel");

	}
 

	public void init() throws ServletException {

		super.init();

		log("init");

	}
 

	public void destroy() {

		// TODO Auto-generated method stub

		log("destory");

	}
 

	protected void doInvite(SipServletRequest req) throws ServletException, IOException {

		// Cast SipServletRequest to SipRequest.

		SipRequest request = (SipRequest) req;

		// Get a reference to the B2buaHelper.

		B2buaHelper helper = request.getB2buaHelper();
 

		// Save reference to the original Session for further messages

		req.getSession().setAttribute("ORIG_REQ", req);
 

		// Create the request-to-callee in the B2BUA-to-callee call leg.

		// B2buaHelper automatically associate two SIP sessions with each

		// other and two SIP Requests with each other.

		SipServletRequest lreq = helper.createRequest(req, true, null);

		SipURI callee = (SipURI) req.getRequestURI();

		callee.setTransportParam("udp");

		lreq.setRequestURI(callee);

		copyContent(req, lreq);
 

		logRx(null, req);

		// Forward the request-to-callee to callee.

		lreq.send();

		logTx(null, lreq);

//		super.doInvite(req);

		log("doInvite");

		// super.doInvite(req);

	}
 

	protected void doOptions(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doOptions");

	}
 

	protected void doSubscribe(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doSubscribe");

	}
 

/*	protected void doRequest(SipServletRequest req) throws ServletException, IOException {

		log("doRequest");
 

		// retrieve the type of request and react accordingly

		String method = req.getMethod();

		if (req.isInitial()) {

				doInvite(req);

		} else {

			if (method.equalsIgnoreCase("Bye")) {

				doBye(req);

			} else if (method.equalsIgnoreCase("Cancel")) {

				doCancel(req);

			} else if (method.equalsIgnoreCase("Options")) {

				doOptions(req);

			} else if (method.equalsIgnoreCase("Prack")) {

				doPrack(req);

			} else if (method.equalsIgnoreCase("ack")) {

				doAck(req);

			} else if (method.equalsIgnoreCase("Subscribe")) {

				doSubscribe(req);

			} else if (method.equalsIgnoreCase("update")) {

				doUpdate(req);

			} else {

				// Retrieve the two call legs by B2buaHelper.

				B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

				SipSession s1 = req.getSession();

				SipSession s2 = b2buahelp.getLinkedSession(s1);
 

				// Create the request to be relayed and link sessions by B2buaHelper. .

				SipServletRequest lreq = b2buahelp.createRequest(s2, req, null);

				copyContent(req, lreq);
 

				lreq.send();

				logTx(null, lreq);

				super.doRequest(req);

			}

		}

		logRx(null, req);

	}

*/

	protected void doUpdate(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doUpdate");

	}
 

	protected void doErrorResponse(SipServletResponse res) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doErrorResponse");

	}
 

	protected void doResponse(SipServletResponse res) throws ServletException, IOException {

		if (res.getStatus() > 100) {

			if (res.getStatus() == 401 || res.getStatus() == 407) {

				// Demonstrates how to send a authorization header in sipservlet.

				SipRequest req = (SipRequest) res.getSession().createRequest(res.getMethod());

				try {

					req.createAuthorizationHeader(res, "user", "pwd");

				} catch (Exception e) {

					e.printStackTrace();

				}

				copyContent(res.getRequest(), req);

				req.send();

			} else {

				String method = res.getMethod();
 

				if (method.equalsIgnoreCase("invite")) {

					// Keep the reference to the response so we can forward ACK properly later.

					res.getSession().setAttribute("TERM_RESP", res);

				} /*else if (method.equalsIgnoreCase("ack")) {

					;

				} else if (method.equalsIgnoreCase("refer")) {

					;

				}

				if (res.getStatus() < 200) {

					doProvisionalResponse(res);

				} else {

					B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

					SipServletRequest req = b2buahelp.getLinkedSipServletRequest((SipRequest) res.getRequest());

					if (req != null) {

						SipServletResponse resp = req.createResponse(res.getStatus(), res.getReasonPhrase());

						copyContent(res, resp);

						resp.send();

						logTx(resp, null);

					}

				}*/

			}

			super.doResponse(res);

		}

		log("doResponse");

	}
 

	protected void doProvisionalResponse(SipServletResponse res) throws ServletException, IOException {

		logRx(res, null);

		B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

		SipSession s1 = res.getSession();

		SipSession s2 = b2buahelp.getLinkedSession(s1);

		SipServletRequest req = (SipServletRequest) s2.getAttribute("ORIG_REQ");

		if (req != null) {

			SipServletResponse resp = req.createResponse(res.getStatus(), res.getReasonPhrase());

			copyContent(res, resp);

			resp.sendReliably();

			logTx(resp, null);

		}

		log("doProvisionalResponse");

	}
 

	protected void doPrack(SipServletRequest req) throws ServletException, IOException {

		logRx(null, req);

		// Retrieve the two call legs by B2buaHelper.

		B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(req.getSession());

		// Retrieve the previous response from the callee to the B2BUA

		SipServletRequest prck = s.createRequest(req.getMethod());

		

		copyContent(req, prck);
 

		prck.send();

		logTx(null, prck);

		log("doPrack");

	}
 

	/**

	 * Copies the contents of source message to target message.

	 */

	private void copyContent(SipServletMessage source, SipServletMessage target) throws IOException {

		target.setContent(source.getRawContent(), source.getContentType());

	}
 

	private void logRx(SipServletResponse res, SipServletRequest req) {

		Rx++;

		if (res != null) {

			B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(res.getSession());

			System.out.println("Session " + res.getSession().getId() + " <---linked---> " + s.getId()); 

			System.out.println( "-Rx Response " + Rx + ": " + res.getMethod() + " status " + res.getStatus());

			System.out.println(" to " + res.getTo() + " from " + res.getFrom() + "  @ " + res.getRemoteAddr() + ":"

					+ res.getRemotePort());

			System.out.println(res.toString());

		} else if (req != null) {

			B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(req.getSession());

			System.out.println("Session " + req.getSession().getId() + " <---link---> " + s.getId()); 

			System.out.println("-Rx Request " + Rx + ": Recieved " + req.getMethod());

			System.out.println(" for " + req.getRequestURI().toString() + " from " + req.getRemoteAddr() + ":"

					+ req.getRemotePort());

			System.out.println(req.toString());

		}

	}
 

	private void logTx(SipServletResponse res, SipServletRequest req) {

		Tx++;

		if (res != null) {

			B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(res.getSession());

			System.out.println("Session " + res.getSession().getId() + " <---linked---> " + s.getId());

			System.out.println("-Tx Response " + Tx + ": " + res.getMethod() + " status " + res.getStatus());

			System.out.println(" to " + res.getTo() + " from " + res.getFrom() + " VIA " + res.getRemoteAddr() + " : "

					+ res.getRemotePort());

			System.out.println(res.toString());

		} else if (req != null) {

			B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(req.getSession());

			System.out.println("Session " + req.getSession().getId() + " <---link---> " + s.getId());

			System.out.println("-Tx Request " + Tx + ": Sent " + req.getMethod());

			System.out.println(" to " + req.getRequestURI().toString() + " VIA " + req.getRemoteAddr()

					+ ":" + req.getRemotePort());

			System.out.println(req.toString());

		}

	}
 

}

Open in new window

0
Comment
Question by:arockbugs
  • 2
5 Comments
 
LVL 5

Expert Comment

by:deeppra
Comment Utility
Tell me u r problem in detail
0
 

Author Comment

by:arockbugs
Comment Utility
Hi Deepra,
Thanks for having a look.
It is not that easy to understand. so i will try my best to explain it.
If you are familar with SIP servlet and IMS, you will surely laugth while reading the following description.
I was hoping while posting the message to meet someone who as experience with IMS application programming.
The actual state of the servelt is below.

The code below is for a SIP Servlet implementing the B2buaHelper interface from JSR289.
I am developing using the Micromethod ACE plattform which unfortunately doesn´t implement the SipServletResponse.createPrack() method at this point.
Normaly the Caller instance first send a request (Invite) proceeded by the IMSCore network and sent to the B2bua Servlet. The B2bua Servlet creates a new invite for the Called instance.
Responses (provisional and final) are also handled identically in the reverse direction.
The prack request is in the case of the code above is not created properly (it misses the proper contact headers) because of session mismatch.
In the code below, i managed to implement the function(not nicely coded) but it is sill not performing as it should for IMS because of a system header within the request message that may not be programaticaly altered (The Rack header).
At this moment i don´t know if it is possible with the Micromethod ACE to get a working Prack message.



/**

 * 

 */

package com.oims.net.sip.services;
 

import java.io.IOException;
 

import javax.servlet.ServletException;

import javax.servlet.sip.SipServlet;

import javax.servlet.sip.SipServletMessage;

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;
 

import com.micromethod.sipmethod.server.module.sip.B2buaHelper;

import com.micromethod.sipmethod.server.module.sip.message.SipRequest;
 

/**

 * @author root

 * 

 */

public class B2bua extends SipServlet {
 

	private static final long serialVersionUID = 1L;

	private Integer Rx = 0;

	private Integer Tx = 0;

	private boolean GET_PRV = false;

	private SipSession ssin = null;

	private SipSession ssout = null;

	

	protected void doAck(SipServletRequest req) throws ServletException, IOException {

		// Retrieve the corresponding Session LEG and get the response from the callee

		B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(req.getSession());

		SipServletResponse res = (SipServletResponse) s.getAttribute("TER_RES");

		// Create the ACK to the callee

		SipServletRequest ack = res.createAck();

		copyContent(req, ack);

		ack.send();

		logTx(null, ack);

		log("doAck");

	}
 

	protected void doBye(SipServletRequest req) throws ServletException, IOException {

		// TODO 

		B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(req.getSession());

		SipServletRequest bye = b2buahelp.createRequest(s, req, null);

		bye.send();

		logTx(null, bye);

		log("doBye");

	}
 

	protected void doCancel(SipServletRequest req) throws ServletException, IOException {

		// TODO 

		B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(req.getSession());

		SipServletRequest cancel = b2buahelp.createRequest(s, req, null);

		cancel.send();

		logTx(null, cancel);

		log("doCancel");

	}
 

	public void init() throws ServletException {

		super.init();

		log("init");

	}
 

	public void destroy() {

		// TODO Auto-generated method stub

		log("destory");

	}
 

	protected void doInvite(SipServletRequest req) throws ServletException, IOException {
 

		// Get a B2buaHelper for the request

		// B2buaHelper automatically associate two SIP sessions with each other and two SIP Requests with each other.

		B2buaHelper helper = ((SipRequest) req).getB2buaHelper();

		// Send 100 Response to legA

		SipServletResponse res = req.createResponse(100, "Trying to connect you");

		res.send();
 

		// Save reference to the original Session for further messages

		req.getSession().setAttribute("ORI_REQ", req);

		req.getSession().setAttribute("GET_PRV", "FALSE");
 

		// Create a linked request and send it to the called.

		SipServletRequest called_req = helper.createRequest(req, true, null);

		SipURI callee = (SipURI) req.getRequestURI();

		callee.setTransportParam("udp");

		called_req.setRequestURI(callee);

		logRx(null, req);

		logTx(res, null);

		copyContent(req, called_req);

		called_req.send();

		logTx(null, called_req);

		log("doInvite");

	}
 

	protected void doOptions(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doOptions");

	}
 

	protected void doSubscribe(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doSubscribe");

	}
 

	protected void doRequest(SipServletRequest req) throws ServletException, IOException {

		log("doRequest");
 

		// retrieve the type of request and react accordingly

		String method = req.getMethod();

		if (req.isInitial()) {

			doInvite(req);

		} else {

			logRx(null, req);

			if (method.equalsIgnoreCase("Bye")) {

				doBye(req);

			} else if (method.equalsIgnoreCase("Cancel")) {

				doCancel(req);

			} else if (method.equalsIgnoreCase("Options")) {

				doOptions(req);

			} else if (method.equalsIgnoreCase("Prack")) {

				doPrack(req);

			} else if (method.equalsIgnoreCase("ack")) {

				doAck(req);

			} else if (method.equalsIgnoreCase("Subscribe")) {

				doSubscribe(req);

			} else if (method.equalsIgnoreCase("update")) {

				doUpdate(req);

			} else {

				// Retrieve the corresponding Session LEG and Create the request to the called

				B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

				SipSession s = b2buahelp.getLinkedSession(req.getSession());

				SipServletRequest called_req = b2buahelp.createRequest(s, req, null);

				copyContent(req, called_req);
 

				called_req.send();

				logTx(null, called_req);

				super.doRequest(req);

			}

		}

	}
 

	protected void doUpdate(SipServletRequest req) throws ServletException, IOException {

		// TODO Auto-generated method stub

		log("doUpdate");

	}
 

	protected void doErrorResponse(SipServletResponse res) throws ServletException, IOException {

		B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(res.getSession());

		SipServletRequest req = (SipServletRequest) s.getAttribute("ORI_REQ");

		if (req != null) {

			SipServletResponse resp = req.createResponse(res.getStatus(), res.getReasonPhrase());

			copyContent(res, resp);

			resp.send();

			logTx(resp, null);

		}

		log("doErrorResponse");

	}
 

	protected void doResponse(SipServletResponse res) throws ServletException, IOException {

		logRx(res, null);

		if (res.getStatus() > 100) {

			if (res.getStatus() == 401 || res.getStatus() == 407) {

				// Demonstrates how to send a authorization header in sipservlet.

				SipRequest req = (SipRequest) res.getSession().createRequest(res.getMethod());

				try {

					req.createAuthorizationHeader(res, "user", "pwd");

				} catch (Exception e) {

					e.printStackTrace();

				}

				copyContent(res.getRequest(), req);

				req.send();

			}

		}

		super.doResponse(res);

		log("doResponse");

	}
 

	protected void doProvisionalResponse(SipServletResponse res) throws ServletException, IOException {

		res.getSession().setAttribute("TER_RES", res);

		B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

		SipSession s = b2buahelp.getLinkedSession(res.getSession());

		SipServletRequest req = (SipServletRequest) s.getAttribute("ORI_REQ");

		if (! GET_PRV){		

			ssin= req.getSession();

			ssout = res.getSession();

			GET_PRV = true;

		}

		if (req != null) {

			SipServletResponse resp = req.createResponse(res.getStatus(), res.getReasonPhrase());

			copyContent(res, resp);

			resp.sendReliably();

			logTx(resp, null);

		}

		log("doProvisionalResponse");

	}
 

	protected void doPrack(SipServletRequest req) throws ServletException, IOException {

		// Retrieve the corresponding Session LEG and get the response from the callee

		SipServletResponse res = (SipServletResponse) ssout.getAttribute("TER_RES");

		B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

		SipSession s = res.getSession();
 

		SipServletRequest prck = b2buahelp.createRequest(s, req, null);

		URI callee = (res.getAddressHeader("Contact")).getURI();

		prck.setRequestURI(callee);

//		prck.setHeader("Rack", req.getHeader("Rack"));
 

		copyContent(req, prck);
 

		prck.send();

		logTx(null, prck);

		// super.doPrack(req);

		log("doPrack");

	}
 

	/**

	 * Copies the contents of source message to target message.

	 */

	private void copyContent(SipServletMessage source, SipServletMessage target) throws IOException {

		target.setContent(source.getRawContent(), source.getContentType());

	}
 

	private void logRx(SipServletResponse res, SipServletRequest req) {

		Rx++;

		if (res != null) {

			B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(res.getSession());

			System.out.println("Session " + res.getSession().getId() + " <---linked---> " + s.getId());

			System.out.println("-Rx Response " + Rx + ": " + res.getMethod() + " status " + res.getStatus());

			System.out.println(" to " + res.getTo() + " from " + res.getFrom() + "  @ " + res.getRemoteAddr() + ":"

					+ res.getRemotePort());

			System.out.println(res.toString());

		} else if (req != null) {

			B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(req.getSession());

			System.out.println("Session " + req.getSession().getId() + " <---link---> " + s.getId());

			System.out.println("-Rx Request " + Rx + ": Recieved " + req.getMethod());

			System.out.println(" for " + req.getRequestURI().toString() + " from " + req.getRemoteAddr() + ":"

					+ req.getRemotePort());

			System.out.println(req.toString());

		}

	}
 

	private void logTx(SipServletResponse res, SipServletRequest req) {

		Tx++;

		if (res != null) {

			B2buaHelper b2buahelp = ((SipRequest) res.getRequest()).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(res.getSession());

			System.out.println("Session " + res.getSession().getId() + " <---linked---> " + s.getId());

			System.out.println("-Tx Response " + Tx + ": " + res.getMethod() + " status " + res.getStatus());

			System.out.println(" to " + res.getTo() + " from " + res.getFrom() + " VIA " + res.getRemoteAddr() + " : "

					+ res.getRemotePort());

			System.out.println(res.toString());

		} else if (req != null) {

			B2buaHelper b2buahelp = ((SipRequest) req).getB2buaHelper();

			SipSession s = b2buahelp.getLinkedSession(req.getSession());

			System.out.println("Session " + req.getSession().getId() + " <---link---> " + s.getId());

			System.out.println("-Tx Request " + Tx + ": Sent " + req.getMethod());

			System.out.println(" to " + req.getRequestURI().toString() + " VIA " + req.getRemoteAddr() + ":"

					+ req.getRemotePort());

			System.out.println(req.toString());

		}

	}
 

}

Open in new window

0
 

Accepted Solution

by:
arockbugs earned 0 total points
Comment Utility
The solution to this problem is using the createAck() function.
The Micromethod AS handle all temporary requests with this function.

protected void doPrack(SipServletRequest req) throws ServletException, IOException {

		// Retrieve the corresponding Session LEG and get the response from the callee

		SipServletResponse res = (SipServletResponse) ssout.getAttribute("TER_RES");

 

		SipServletRequest prck = res.createAck();

		URI callee = (res.getAddressHeader("Contact")).getURI();

		prck.setRequestURI(callee);

 

		copyContent(req, prck);

 

		prck.send();

		logTx(null, prck);

		log("doPrack");

	}

Open in new window

0
 
LVL 1

Expert Comment

by:modus_operandi
Comment Utility
Closed, 250 points refunded.
modus_operandi
EE Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
factorial example challenge 10 60
stringclean challenge 26 54
Installed softare without GUID 6 53
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now