Package com.ericsson.ssa.sip

Source Code of com.ericsson.ssa.sip.AbstractProxyImpl

/*
* 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 com.ericsson.ssa.sip.PathNode.Type;

import java.io.IOException;
import java.io.Serializable;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.util.LogUtil;

import javax.servlet.Servlet;
import javax.servlet.ServletException;


/**
* Common superclass to ProxyInterface implementations that holds common state
* and common helper methods.
*
* Subclasses implements the dispatch()-methods.
*
*
* @author qmaghes
*
*/
public abstract class AbstractProxyImpl implements ProxyInterface, Serializable {
    static final Logger _log = LogUtil.SIP_LOGGER.getLogger();
    boolean _isAddToPath = false;
    boolean _isRecurse = true;
    boolean _isParallel = true;
    String _sipApplicationSessionName = null;
    boolean _isProxyStarted = false;
    private OutboundInterface oi = null;

    public AbstractProxyImpl(String applicationSessionName) {
        this._sipApplicationSessionName = applicationSessionName;
    }

    /**
     * For deserialization only
     *
     */
    public AbstractProxyImpl() {
    }

    /**
     * Get a confirmed version of this object.
     *
     */
    public final ConfirmedProxyImpl asConfirmed() {
        //      FIXME uncomment for performance reason once DialogFragmentManager does register 180 anymore.
        //       // If it already is confirmed, return itself.
        //      if(this instanceof ConfirmedProxyImpl){
        //        return (ConfirmedProxyImpl)this;
        //      }
        ConfirmedProxyImpl confirmedProxyImpl = new ConfirmedProxyImpl(_sipApplicationSessionName);
        confirmedProxyImpl._isAddToPath = this._isAddToPath;
        confirmedProxyImpl._isRecurse = this._isRecurse;
        confirmedProxyImpl._isParallel = this._isParallel;
        confirmedProxyImpl._dialogRecordRoute = dialogRecordRoute();

        return confirmedProxyImpl;
    }

    /**
     * Method telling whether the dialog established by this proxy should record route on
     * subsequent requests or not.
     *
     * Note that this is a product of whether the branch that established the dialog was a
     * record routing branch or not. It is not the same as getRecordRoute() which is just
     * the default setting of the proxy.
     *
     * @return
     */
    protected abstract boolean dialogRecordRoute() throws IllegalStateException;

  /*
     * Application session name attribute. Needed for servlet lookup.
     */
    public final String getApplicationSessionName() {
        return _sipApplicationSessionName;
    }

    public final boolean getRecurse() {
        return _isRecurse;
    }

    public final void setRecurse(boolean recurse) {
        _isRecurse = recurse;
    }

    public final boolean getAddToPath() {
        return _isAddToPath;
    }

    public final void setAddToPath(boolean addToPath) {
        _isAddToPath = addToPath;
    }

    protected void setProxyStarted() {
      _isProxyStarted = true;
  }

    public boolean isProxyStarted() {
    return _isProxyStarted;
  }

  public final boolean getParallel() {
        return _isParallel;
    }

    public final void setParallel(boolean parallel) {
        _isParallel = parallel;
    }

    public final boolean getStateful() {
        return true;
    }

    @Deprecated
    public void setStateful(boolean stateful) {
    }

    public void setOutboundInterface(OutboundInterface oi) {
        this.oi = oi;
    }

    public OutboundInterface getOutboundInterface() {
        return this.oi;
    }

    /**
     * Helper to fetch servlet instance.
     *
     * @param handler
     * @return
     */
    public Servlet getServlet(String handler) {
        return SipFactoryImpl.getInstance().getServiceHandler()
                             .getHandler(getApplicationSessionName(), handler);
    }

    /**
     * Helper to set session.
     *
     * @param req
     * @param pc
     * @throws IOException
     * @throws ServletException
     */
    protected void setDerivedOrOriginalSession(SipServletRequestImpl req,
        ProxyContext pc) throws IOException, ServletException {
        // dialog creational NOTIFY
        if (req.getMethod().equals("NOTIFY")) {
            // state Record Route in request to inform
            // that a RecordRoute header must be added
            req.indicateRecordRoute();

            // if the context has a session with same to tag as the dialog, use
            // it...
            if (!pc.getSipSession().hasNoToTag() &&
                    (req.getDialog().getToTag() != null) &&
                    req.getDialog().getToTag()
                           .equals(pc.getSipSession().getToTag())) {
                req.setSession(pc.getSipSession());
            } else {
                // ...otherwise fetch or create one
                DialogFragment df = req.getDialog();

                // lets update to-tag of dialog...
                SipSessionBase s = pc.getSipSession()
                                     .getOriginalOrDerivedSessionAndRegisterDialog(req,
                        df);

                if (s.isDerived()) {
                    // lets set the session cuz it is a derived session
                    req.setSession(s);
                    pc.setSipSession(s);
                }
            }
        } else {
            // the response should already have created
            // the dialog for all other methods...
            req.setSession(pc.getSipSession());
        }
    }

    /**
     * private helper
     *
     * @param resp
     */
    private void dispatchAndClone(SipServletResponseImpl resp) {
        // the request need to be cloned
        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());
        }

        clone.popDispatcher().dispatch(clone);
    }

    /**
     * private helper
     *
     * @param resp
     * @param pc
     * @throws IOException
     * @throws ServletException
     */
    protected void assignSession(SipServletResponseImpl resp, ProxyContext pc)
        throws IOException, ServletException {
        if (resp.getStatus() != 100) {
            // if the context has a session with same to tag as the dialog, use
            // it...
            if (!pc.getSipSession().hasNoToTag() &&
                    (resp.getDialog().getToTag() != null) &&
                    resp.getDialog().getToTag()
                            .equals(pc.getSipSession().getToTag())) {
                resp.setSession(pc.getSipSession());
            } else {
                // ...otherwise fetch or create one
                DialogFragment df = resp.getDialog();

                // lets update to-tag of dialog...
                SipSessionBase s = resp.getSessionImpl()
                                       .getOriginalOrDerivedSessionAndRegisterDialog(resp,
                        df);

                if (s.isDerived()) {
                    // lets set the session cuz it is a derived session
                    resp.setSession(s);
                }

                pc.setSipSession(s);
            }
        }
    }

    /**
     *
     * common helper
     *
     * @param resp
     * @param pc
     */
    public void invokeServletAndForward(SipServletResponseImpl resp,
        ProxyContext pc) {
        try {
            if (pc.getSipSession() != null && pc.getSipSession().isValid()) {
                assignSession(resp, pc);

                if (resp.getRequestImpl().getSupervised()) {
                    // if supervised is enabled invoke the servlet
                    invokeServlet(resp);
                }
            }
            dispatchAndClone(resp);
       } catch (Exception e) {
            // problem in servlet, lets drop response
            if (_log.isLoggable(Level.INFO)) {
                _log.log(Level.INFO, "Problem in servlet.", e);
            }
        }
    }

    /**
     * common helper
     *
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    protected void invokeServlet(SipServletResponseImpl resp)
        throws IOException, ServletException {
        if (resp.getStatus() != 100) {
            Servlet s = getServlet(resp.getSessionImpl().getHandler());

            if (s != null) {
                resp.getSessionImpl().updateSipSessionState(resp, Type.Proxy);
                s.service(null, resp);
            } else {
                if (_log.isLoggable(Level.INFO)) {
                    _log.log(Level.INFO,
                        "Could not find servlet name: " +
                        resp.getSessionImpl().getHandler() +
                        " in application: " +
                        resp.getSessionImpl().getApplicationSessionImpl()
                            .getName());
                }
            }
        }
    }
}
TOP

Related Classes of com.ericsson.ssa.sip.AbstractProxyImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.