Search in sources :

Example 6 with SIPDialog

use of gov.nist.javax.sip.stack.SIPDialog in project XobotOS by xamarin.

the class SipProviderImpl method getNewServerTransaction.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#getNewServerTransaction(javax.sip.message.Request)
     */
public ServerTransaction getNewServerTransaction(Request request) throws TransactionAlreadyExistsException, TransactionUnavailableException {
    if (!sipStack.isAlive())
        throw new TransactionUnavailableException("Stack is stopped");
    SIPServerTransaction transaction = null;
    SIPRequest sipRequest = (SIPRequest) request;
    try {
        sipRequest.checkHeaders();
    } catch (ParseException ex) {
        throw new TransactionUnavailableException(ex.getMessage(), ex);
    }
    if (request.getMethod().equals(Request.ACK)) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logError("Creating server transaction for ACK -- makes no sense!");
        throw new TransactionUnavailableException("Cannot create Server transaction for ACK ");
    }
    /*
         * Got a notify.
         */
    if (sipRequest.getMethod().equals(Request.NOTIFY) && sipRequest.getFromTag() != null && sipRequest.getToTag() == null) {
        SIPClientTransaction ct = sipStack.findSubscribeTransaction(sipRequest, (ListeningPointImpl) this.getListeningPoint());
        /* Issue 104 */
        if (ct == null && !sipStack.deliverUnsolicitedNotify) {
            throw new TransactionUnavailableException("Cannot find matching Subscription (and gov.nist.javax.sip.DELIVER_UNSOLICITED_NOTIFY not set)");
        }
    }
    if (!sipStack.acquireSem()) {
        throw new TransactionUnavailableException("Transaction not available -- could not acquire stack lock");
    }
    try {
        if (sipStack.isDialogCreated(sipRequest.getMethod())) {
            if (sipStack.findTransaction((SIPRequest) request, true) != null)
                throw new TransactionAlreadyExistsException("server transaction already exists!");
            transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
            if (transaction == null)
                throw new TransactionUnavailableException("Transaction not available");
            if (transaction.getOriginalRequest() == null)
                transaction.setOriginalRequest(sipRequest);
            try {
                sipStack.addTransaction(transaction);
            } catch (IOException ex) {
                throw new TransactionUnavailableException("Error sending provisional response");
            }
            // So I can handle timeouts.
            transaction.addEventListener(this);
            if (isAutomaticDialogSupportEnabled()) {
                // If automatic dialog support is enabled then
                // this tx gets his own dialog.
                String dialogId = sipRequest.getDialogId(true);
                SIPDialog dialog = sipStack.getDialog(dialogId);
                if (dialog == null) {
                    dialog = sipStack.createDialog(transaction);
                }
                transaction.setDialog(dialog, sipRequest.getDialogId(true));
                if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
                    sipStack.putInMergeTable(transaction, sipRequest);
                }
                dialog.addRoute(sipRequest);
                if (dialog.getRemoteTag() != null && dialog.getLocalTag() != null) {
                    this.sipStack.putDialog(dialog);
                }
            }
        } else {
            if (isAutomaticDialogSupportEnabled()) {
                /*
                     * Under automatic dialog support, dialog is tied into a transaction. You cannot
                     * create a server tx except for dialog creating transactions. After that, all
                     * subsequent transactions are created for you by the stack.
                     */
                transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
                if (transaction != null)
                    throw new TransactionAlreadyExistsException("Transaction exists! ");
                transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
                if (transaction == null)
                    throw new TransactionUnavailableException("Transaction not available!");
                if (transaction.getOriginalRequest() == null)
                    transaction.setOriginalRequest(sipRequest);
                // Map the transaction.
                try {
                    sipStack.addTransaction(transaction);
                } catch (IOException ex) {
                    throw new TransactionUnavailableException("Could not send back provisional response!");
                }
                // If there is a dialog already assigned then just update the
                // dialog state.
                String dialogId = sipRequest.getDialogId(true);
                SIPDialog dialog = sipStack.getDialog(dialogId);
                if (dialog != null) {
                    dialog.addTransaction(transaction);
                    dialog.addRoute(sipRequest);
                    transaction.setDialog(dialog, sipRequest.getDialogId(true));
                }
            } else {
                transaction = (SIPServerTransaction) sipStack.findTransaction((SIPRequest) request, true);
                if (transaction != null)
                    throw new TransactionAlreadyExistsException("Transaction exists! ");
                transaction = (SIPServerTransaction) ((SIPRequest) request).getTransaction();
                if (transaction != null) {
                    if (transaction.getOriginalRequest() == null)
                        transaction.setOriginalRequest(sipRequest);
                    // Map the transaction.
                    sipStack.mapTransaction(transaction);
                    // If there is a dialog already assigned then just
                    // assign the dialog to the transaction.
                    String dialogId = sipRequest.getDialogId(true);
                    SIPDialog dialog = sipStack.getDialog(dialogId);
                    if (dialog != null) {
                        dialog.addTransaction(transaction);
                        dialog.addRoute(sipRequest);
                        transaction.setDialog(dialog, sipRequest.getDialogId(true));
                    }
                    return transaction;
                } else {
                    // tx does not exist so create the tx.
                    MessageChannel mc = (MessageChannel) sipRequest.getMessageChannel();
                    transaction = sipStack.createServerTransaction(mc);
                    if (transaction == null)
                        throw new TransactionUnavailableException("Transaction unavailable -- too many servrer transactions");
                    transaction.setOriginalRequest(sipRequest);
                    sipStack.mapTransaction(transaction);
                    // If there is a dialog already assigned then just
                    // assign the dialog to the transaction.
                    String dialogId = sipRequest.getDialogId(true);
                    SIPDialog dialog = sipStack.getDialog(dialogId);
                    if (dialog != null) {
                        dialog.addTransaction(transaction);
                        dialog.addRoute(sipRequest);
                        transaction.setDialog(dialog, sipRequest.getDialogId(true));
                    }
                    return transaction;
                }
            }
        }
        return transaction;
    } finally {
        sipStack.releaseSem();
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) MessageChannel(gov.nist.javax.sip.stack.MessageChannel) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) IOException(java.io.IOException) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Aggregations

SIPDialog (gov.nist.javax.sip.stack.SIPDialog)6 SIPRequest (gov.nist.javax.sip.message.SIPRequest)5 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)5 IOException (java.io.IOException)4 SipException (javax.sip.SipException)4 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)3 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)3 InvalidArgumentException (javax.sip.InvalidArgumentException)3 SIPResponse (gov.nist.javax.sip.message.SIPResponse)2 ParseException (java.text.ParseException)2 ClientTransaction (javax.sip.ClientTransaction)2 ObjectInUseException (javax.sip.ObjectInUseException)2 ServerTransaction (javax.sip.ServerTransaction)2 TransactionUnavailableException (javax.sip.TransactionUnavailableException)2 Request (javax.sip.message.Request)2 Response (javax.sip.message.Response)2 Reason (gov.nist.javax.sip.DialogTimeoutEvent.Reason)1 SipUri (gov.nist.javax.sip.address.SipUri)1 Contact (gov.nist.javax.sip.header.Contact)1 Event (gov.nist.javax.sip.header.Event)1