/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.ericsson.ssa.sip;
import java.util.logging.Level;
// inserted by hockey (automatic)
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.util.LogUtil;
import com.ericsson.ssa.sip.PathNode.Type;
import javax.servlet.Servlet;
import javax.servlet.sip.SipServletMessage;
import javax.servlet.sip.UAMode;
/**
* This is a Singlton class handling all methods not handled by other FSM, which
* includes for example: INFO, MESSAGE, OPTIONS, PUBLISH, REGISTER
*
* @author ehsroha
*/
public class GeneralSession extends FSM {
private static final Logger m_Log = LogUtil.SIP_LOGGER.getLogger();
private static GeneralSession m_Instance = new GeneralSession();
private GeneralSession() {
}
/**
* Will return a GeneralSession if the request matches this FSM.
*
* @param m
* the incoming message is the key to decide which FSM to return
* @return a GeneralSession or null if no match occured
*/
public static FSM createFSM(SipServletMessage m) {
if (isStaticResponsible(m)) {
return m_Instance;
}
return null;
}
public Object clone() {
return m_Instance;
}
/**
* This FSM is responsible if the message is non of the methods: ACK, BYE,
* CANCEL, INVITE, PRACK, UPDATE, SUBSCRIBE, REFER or NOTIFY.
*
* @param m
* the incoming message is the key to decide whether this FSM will
* match or not.
*/
private static boolean isStaticResponsible(SipServletMessage m) {
String method = m.getMethod();
// all methods not handled by other FSM should be handled
// by this general FSM
return ((method.equals("ACK") || method.equals("BYE") ||
method.equals("CANCEL") || method.equals("INVITE") ||
method.equals("PRACK") || method.equals("UPDATE") ||
method.equals("SUBSCRIBE") || method.equals("REFER") ||
method.equals("NOTIFY")) == false);
}
public boolean isResponsible(SipServletMessage m) {
return isStaticResponsible(m);
}
/**
* Returns always false since it is a singleton class.
*/
public boolean isDeletable() {
return false;
}
public void send(SipServletRequestImpl req, UA ua)
throws IllegalStateException {
if (m_Log.isLoggable(Level.FINEST)) {
m_Log.log(Level.FINEST, req.toDebugString());
}
// the request need to be cloned before sending
SipServletRequestImpl clone = (SipServletRequestImpl) req.clone();
clone.setTransactionRequest(req);
// lets run in new thread...
super.send(clone, ua);
}
public void send(SipServletResponseImpl resp, UA ua)
throws IllegalStateException {
if (m_Log.isLoggable(Level.FINEST)) {
m_Log.log(Level.FINEST, resp.toDebugString());
}
if (!resp.hasToTag()) {
resp.createTag(Header.TO);
}
resp.getSessionImpl().updateSipSessionState(resp, ua.getType());
if (!resp.isCommitted() && resp.getSessionImpl().isB2buaHelper()) {
resp.getSessionImpl().addPendingMessage(resp, UAMode.UAS);
}
// the response need to be cloned before sending
final SipServletResponseImpl clone = (SipServletResponseImpl) resp.clone();
// if there is a transaction request, added it to the response...
SipServletRequestImpl req = resp.getRequestImpl().getTransactionRequest();
if (req != null) {
clone.setRequest(req);
clone.setSession(req.getSessionImpl());
}
// lets run in new thread...
super.send(clone, ua);
}
public void dispatch(SipServletRequestImpl req, UA ua) {
if (m_Log.isLoggable(Level.FINEST)) {
m_Log.log(Level.FINEST, req.toDebugString());
}
if (ua.getSipSession() != null) {
req.setSession(ua.getSipSession());
}
try {
Servlet s = ua.getServlet(req.getSessionImpl().getHandler());
if (s != null) {
req.getSessionImpl().updateSipSessionState(req, ua.getType());
if (!req.isCommitted() && req.getSessionImpl().isB2buaHelper()) {
req.getSessionImpl().addPendingMessage(req, UAMode.UAS);
}
s.service(req, null);
} else {
if (m_Log.isLoggable(Level.INFO)) {
m_Log.log(Level.INFO,
"Could not find servlet name: " +
req.getSessionImpl().getHandler() +
" in application: " +
req.getSessionImpl().getApplicationSessionImpl()
.getName());
}
}
} catch (Exception e) {
if (m_Log.isLoggable(Level.FINE)) {
m_Log.log(Level.FINE, "Problem in servlet ", e);
}
// problem in servlet...
// TR HH52078
SipServletResponseImpl resp = req.createTerminatingResponse(500);
if (resp == null) {
return;
}
resp.popDispatcher().dispatch(resp);
}
}
public void dispatch(SipServletResponseImpl resp, UA ua) {
if (m_Log.isLoggable(Level.FINEST)) {
m_Log.log(Level.FINEST, resp.toDebugString());
}
try {
Servlet s = ua.getServlet(resp.getSessionImpl().getHandler());
if (s != null) {
resp.getSessionImpl().updateSipSessionState(resp, ua.getType());
resp.setIncoming();
// first message will create the pending
// message lists for UAC and UAS..
if (resp.getRequestImpl().isB2buaHelper()) {
resp.getSessionImpl().createPendingMessageHelper();
}
// ...if pending lists exist, add pending uncommitted message
if (!resp.isCommitted() && resp.getSessionImpl().isB2buaHelper()) {
resp.getSessionImpl().addPendingMessage(resp, UAMode.UAC);
}
s.service(null, resp);
} else {
if (m_Log.isLoggable(Level.INFO)) {
m_Log.log(Level.INFO,
"Could not find servlet name: " +
resp.getSessionImpl().getHandler() +
" in application: " +
resp.getSessionImpl().getApplicationSessionImpl()
.getName());
}
}
} catch (Exception e) {
// problem in servlet, lets drop response
StringBuilder message = new StringBuilder("Problem in servlet ")
.append(", exception = ")
.append(e.getMessage());
if (m_Log.isLoggable(Level.FINE)) {
m_Log.log(Level.FINE, message.toString(), e);
} else if (m_Log.isLoggable(Level.INFO)) {
m_Log.log(Level.INFO, message.toString());
}
}
}
}