/*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.mobicents.servlet.sip.testsuite.dispatcher;
import java.io.IOException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipURI;
import org.apache.log4j.Logger;
/**
*
* @author <A HREF="mailto:jean.deruelle@gmail.com">Jean Deruelle</A>
*
*/
public class DispatcherSipServlet
extends SipServlet {
private static final long serialVersionUID = 1L;
private static transient Logger logger = Logger.getLogger(DispatcherSipServlet.class);
private SipFactory sipFactory;
private static String TEST_BAD_HANDLER_USERNAME = "badHandler";
/** Creates a new instance of MainHandlerSipServlet */
public DispatcherSipServlet() {
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
logger.info("the main handler test sip servlet has been started");
try {
// Getting the Sip factory from the JNDI Context
Properties jndiProps = new Properties();
Context initCtx = new InitialContext(jndiProps);
Context envCtx = (Context) initCtx.lookup("java:comp/env");
sipFactory = (SipFactory) envCtx.lookup("sip/org.mobicents.servlet.sip.testsuite.dispatcher.DispatcherApplication/SipFactory");
logger.info("Sip Factory ref from JNDI : " + sipFactory);
} catch (NamingException e) {
throw new ServletException("Uh oh -- JNDI problem !", e);
}
}
/**
* {@inheritDoc}
*/
protected void doInvite(SipServletRequest request) throws ServletException,
IOException {
logger.info("Got request: "
+ request.getMethod());
RequestDispatcher requestDispatcher;
if(TEST_BAD_HANDLER_USERNAME.equalsIgnoreCase(((SipURI)request.getFrom().getURI()).getUser())) {
requestDispatcher = request.getRequestDispatcher(TEST_BAD_HANDLER_USERNAME);
} else {
requestDispatcher = request.getRequestDispatcher("SecondaryHandlerSipServlet");
}
request.setAttribute("testAttributePassing", "ok");
requestDispatcher.forward(request, null);
}
}