Ejemplo: proxy del servlet SIP sencillo

Este es un ejemplo de servlet de un proxy sencillo.

Proxy sencillo

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.sip.Proxy;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServlet;
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;


public class SimpleProxy extends SipServlet implements Servlet {


		final static  private	String 	SHUTDOWN_KEY = new String("shutdown");
		final static  private	String 	STATE_KEY = new String("state");
		final static  private	int	 	INVITE_RECEIVED = 1;

		/* (non-Java-doc)
	 	 * @see javax.servlet.sip.SipServlet#SipServlet()
	 */
		public SimpleProxy() {
				super();
	}

		/* (no Javadoc)
	 	 * @see javax.servlet.sip.SipServlet#doInvite(javax.servlet.sip.SipServletRequest)
	 */
		protected void doInvite(SipServletRequest request) throws ServletException,
			IOException {

				//log("SimpleProxy: doInvite: TOP");

		try {
						if (request.isInitial() == true)
			{
								//	Esto hace que se cree la sesión sip. Este ejemplo sólo utiliza la sesión cuando recibe
								//	un BYE pero Tivoli Performance Viewer se puede realizar el seguimiento de creación de llamadas
								//	viendo el recuento de sesiones activas.
								Integer state = new Integer(INVITE_RECEIVED);
								SipSession session = request.getSession();
								session.setAttribute(STATE_KEY, state);
            		log("SimpleProxy: doInvite: setting attribute");

								Proxy proxy = request.getProxy();

								SipFactory sipFactory = (SipFactory) getServletContext().getAttribute(SIP_FACTORY);
		        		        if (sipFactory == null) {
		            		            throw new ServletException("No SipFactory in context");
		        }

		        		        String callingNumber = request.getTo().toString();
		        		        if (callingNumber != null)
		        {
	            		            	String destStr = format_lookup(callingNumber);
	            		            	URI dest = sipFactory.createURI(destStr);

	            		            	//log("SimpleProxy: doInvite: Proxying to dest URI = " + dest.toString());

			        			        if (((SipURI)request.getRequestURI()).getTransportParam() != null)
			        				        	((SipURI)dest).setTransportParam(((SipURI)request.getRequestURI()).getTransportParam());

			        			        proxy.setRecordRoute(true);
										proxy.proxyTo(dest);
		        }
				else
				{
	            		            	//log("SimpleProxy: doInvite: La solicitud no es válida. No contenía un  campo Para:.");
										SipServletResponse sipresponse = request.createResponse(400);
										sipresponse.send();
				}
			}
			else
			{
		        		        //log("SimpleProxy: doInvite: target refresh, let container handle invite");
								super.doInvite(request);
			}
		}
		catch (Exception e) {
			e.printStackTrace();                                            
		}
	}

		/* (no Javadoc)
	 	 * @see javax.servlet.sip.SipServlet#doResponse(javax.servlet.sip.SipServletResponse)
	 */
		protected void doResponse(SipServletResponse response) throws ServletException,
			IOException {
 				super.doResponse(response);

 				//	Ejemplo de utilización del objeto de sesión para almacenar el estado de la sesión.
				SipSession session = response.getSession();
				if (session.getAttribute(SHUTDOWN_KEY) != null)
		{
						//log("SimpleProxy: doResponse: invalidating session");
						session.invalidate();
		}
	}

		/* (no Javadoc)
	 	 * @see javax.servlet.sip.SipServlet#doBye(javax.servlet.sip.SipServletRequest)
	 */
		protected void doBye(SipServletRequest request) throws ServletException,
			IOException {

						SipSession session = request.getSession();
				session.setAttribute(SHUTDOWN_KEY, new Boolean(true));

        //log("SimpleProxy: doBye: invalidar sesión cuando se reciban las respuestas.");
				super.doBye(request);
	}

		protected String format_lookup(String toFormat){
				int start_index = toFormat.indexOf('<') + 1;
				int end_index = toFormat.indexOf('>');

				if(start_index == 0){
						//no preocuparse al respecto
		}
				if(end_index == -1){
						end_index = toFormat.length();
		}

				return toFormat.substring(start_index, end_index);
	}
}

Icon that indicates the type of topic Reference topic



Timestamp icon Last updated: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rsip_servletsample1
File name: rsip_servletsample1.html