Search in sources :

Example 1 with TransactionAlreadyExistsException

use of javax.sip.TransactionAlreadyExistsException 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

SIPRequest (gov.nist.javax.sip.message.SIPRequest)1 MessageChannel (gov.nist.javax.sip.stack.MessageChannel)1 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)1 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)1 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 TransactionAlreadyExistsException (javax.sip.TransactionAlreadyExistsException)1 TransactionUnavailableException (javax.sip.TransactionUnavailableException)1