Package org.cafesip.jiplet

Source Code of org.cafesip.jiplet.JipletTransaction

/*
* Created on Jan 30, 2005
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet;

import java.util.Date;

import javax.sip.address.URI;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.ToHeader;
import javax.sip.message.Message;

/**
* This class encapsulates a SIP transaction. The jiplet container supports
* transaction scope variables. A transaction is created when a jiplet sends a
* request and registers for a response by calling the
* Jiplet.registerForResponse() method. The transaction remains active until a
* terminating response is received, a time-out event is received or if the
* jiplet application cancels the response by calling the
* Jiplet.cancelResponse() method. A variable defined in the transaction-scope
* remains visible to the jiplets during the duration of a transaction. A jiplet
* can retrieve this object by calling the Jiplet.getTransaction() method.
* Thereafter, the methods offered by the base class ScopedVariables may be used
* to create, retrieve and remove transaction-scope variables.
*
* @author Amit Chatterjee
*/
public class JipletTransaction extends ScopedVariables
{
    private URI fromUri;

    private URI toUri;

    private String callId;

    private CSeqHeader cseq;

    private String fromTag;

    private Jiplet jiplet;

    private long timeout = -1L;

    private Date expiryTime;

    private String toString;

    private int hashCode;

    /**
     * A constructor for this class.
     *
     * @param message
     * @param jiplet
     * @param timeout
     */
    protected JipletTransaction(Message message, Jiplet jiplet, long timeout)
            throws JipletException
    {
        super();

        FromHeader from = (FromHeader) message.getHeader(FromHeader.NAME);
        if (from == null)
        {
            throw new JipletException(FromHeader.NAME + " header is missing");
        }

        fromUri = from.getAddress().getURI();
        fromTag = from.getTag();

        ToHeader to = (ToHeader) message.getHeader(ToHeader.NAME);
        if (to == null)
        {
            throw new JipletException(ToHeader.NAME + " header is missing");
        }
        toUri = to.getAddress().getURI();

        CallIdHeader call = (CallIdHeader) message.getHeader(CallIdHeader.NAME);
        if (call == null)
        {
            throw new JipletException(CallIdHeader.NAME + " header is missing");
        }
        callId = call.getCallId();

        cseq = (CSeqHeader) message.getHeader(CSeqHeader.NAME);
        if (cseq == null)
        {
            throw new JipletException(CSeqHeader.NAME + " header is missing");
        }
       
        this.jiplet = jiplet;
        this.timeout = timeout;
        setExpiryTime();

        toString = "From: " + nullProof(fromUri) + " tag=" + nullProof(fromTag)
                + "\n" + "To: " + nullProof(toUri) + "\n" + "CallId: "
                + nullProof(callId) + "\n" + "CSeq: " + nullProof(cseq);

        hashCode = toString.hashCode();
    }

    /**
     * A constructor for this class.
     *
     * @param message
     * @throws JipletException
     */
    protected JipletTransaction(Message message) throws JipletException
    {
        this(message, null, -1L);
    }

    /**
     * @return Returns the jiplet.
     */
    protected Jiplet getJiplet()
    {
        return jiplet;
    }

    /**
     * @return Returns the toUri.
     */
    protected URI getToUri()
    {
        return toUri;
    }

    protected String getFromTag()
    {
        return fromTag;
    }

    /**
     * @return Returns the timeout.
     */
    protected long getTimeout()
    {
        return timeout;
    }

    /**
     * Returs true if the transaction provided by the paramater is the same.
     *
     * @param transaction
     * @return
     */
    protected boolean equals(JipletTransaction transaction)
    {
        if ((fromUri.equals(transaction.getFromUri()) == true)
                && (toUri.equals(transaction.getToUri()) == true)
                && (callId.equals(transaction.getCallId()) == true)
                && (cseq.equals(transaction.getCseq()) == true)
                && (fromTag.equals(transaction.getFromTag()) == true))
        {
            return true;
        }

        return false;
    }

    /**
     * @return Returns the fromUri.
     */
    protected URI getFromUri()
    {
        return fromUri;
    }

    /**
     * @return Returns the callId.
     */
    protected String getCallId()
    {
        return callId;
    }

    /**
     * @return Returns the cseq.
     */
    protected CSeqHeader getCseq()
    {
        return cseq;
    }

    /**
     * @return Returns the expiryTime.
     */
    protected Date getExpiryTime()
    {
        return expiryTime;
    }

    /*
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        return toString;
    }

    private String nullProof(Object obj)
    {
        return obj == null ? "NULL" : obj.toString();
    }

    /*
     * @see java.lang.Object#hashCode()
     */
    public int hashCode()
    {
        return toString.hashCode();
    }

    protected void resetExpiryTime()
    {
        setExpiryTime();
    }

    private void setExpiryTime()
    {
        if (timeout > 0)
        {
            Date cur = new Date();
            expiryTime = new Date(cur.getTime() + timeout);
        }
    }
}
TOP

Related Classes of org.cafesip.jiplet.JipletTransaction

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.