Search in sources :

Example 6 with SIPServerTransaction

use of gov.nist.javax.sip.stack.SIPServerTransaction in project jain-sip.ha by RestComm.

the class ClusteredSipStackImpl method remoteServerTransactionRemoval.

/*
	 * (non-Javadoc)
	 * @see org.mobicents.ha.javax.sip.ClusteredSipStack#remoteServerTransactionRemoval(java.lang.String)
	 */
public void remoteServerTransactionRemoval(String transactionId) {
    final String txId = transactionId.toLowerCase();
    // assuming it's a confirmed dialog there is no chance it is on early dialogs too
    if (getStackLogger().isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
        getStackLogger().logDebug("sipStack " + this + " remote Server Transaction Removal of transaction Id : " + txId);
    }
    // the transaction id is set to lower case in the cache so it might not remove it correctly
    SIPServerTransaction sipServerTransaction = super.serverTransactionTable.remove(txId);
    if (sipServerTransaction != null) {
        super.removeFromMergeTable(sipServerTransaction);
        super.removePendingTransaction(sipServerTransaction);
        super.removeTransactionPendingAck(sipServerTransaction);
    }
}
Also used : SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction)

Example 7 with SIPServerTransaction

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

the class DialogFilter method processRequest.

/**
     * Process a request. Check for various conditions in the dialog that can result in the
     * message being dropped. Possibly return errors for these conditions.
     * 
     * @exception SIPServerException is thrown when there is an error processing the request.
     */
public void processRequest(SIPRequest sipRequest, MessageChannel incomingMessageChannel) {
    // Generate the wrapper JAIN-SIP object.
    if (sipStack.isLoggingEnabled())
        sipStack.getStackLogger().logDebug("PROCESSING INCOMING REQUEST " + sipRequest + " transactionChannel = " + transactionChannel + " listening point = " + listeningPoint.getIPAddress() + ":" + listeningPoint.getPort());
    if (listeningPoint == null) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Dropping message: No listening point registered!");
        return;
    }
    SipStackImpl sipStack = (SipStackImpl) transactionChannel.getSIPStack();
    SipProviderImpl sipProvider = listeningPoint.getProvider();
    if (sipProvider == null) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("No provider - dropping !!");
        return;
    }
    if (sipStack == null)
        InternalErrorHandler.handleException("Egads! no sip stack!");
    // Look for the registered SIPListener for the message channel.
    SIPServerTransaction transaction = (SIPServerTransaction) this.transactionChannel;
    if (transaction != null) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("transaction state = " + transaction.getState());
    }
    String dialogId = sipRequest.getDialogId(true);
    SIPDialog dialog = sipStack.getDialog(dialogId);
    /*
         * Check if we got this request on the contact address of the dialog If not the dialog
         * does not belong to this request. We check this condition if a contact address has been
         * assigned to the dialog. Forgive the sins of B2BUA's that like to record route ACK's
         */
    if (dialog != null && sipProvider != dialog.getSipProvider()) {
        Contact contact = dialog.getMyContactHeader();
        if (contact != null) {
            SipUri contactUri = (SipUri) (contact.getAddress().getURI());
            String ipAddress = contactUri.getHost();
            int contactPort = contactUri.getPort();
            String contactTransport = contactUri.getTransportParam();
            if (contactTransport == null)
                contactTransport = "udp";
            if (contactPort == -1) {
                if (contactTransport.equals("udp") || contactTransport.equals("tcp"))
                    contactPort = 5060;
                else
                    contactPort = 5061;
            }
            // dialog to the request.
            if (ipAddress != null && (!ipAddress.equals(listeningPoint.getIPAddress()) || contactPort != listeningPoint.getPort())) {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("nulling dialog -- listening point mismatch!  " + contactPort + "  lp port = " + listeningPoint.getPort());
                }
                dialog = null;
            }
        }
    }
    /*
         * RFC 3261 8.2.2.2 Merged requests: If the request has no tag in the To header field, the
         * UAS core MUST check the request against ongoing transactions. If the From tag, Call-ID,
         * and CSeq exactly match those associated with an ongoing transaction, but the request
         * does not match that transaction (based on the matching rules in Section 17.2.3), the
         * UAS core SHOULD generate a 482 (Loop Detected) response and pass it to the server
         * transaction. This support is only enabled when the stack has been instructed to
         * function with Automatic Dialog Support.
         */
    if (sipProvider.isAutomaticDialogSupportEnabled() && sipProvider.isDialogErrorsAutomaticallyHandled() && sipRequest.getToTag() == null) {
        SIPServerTransaction sipServerTransaction = sipStack.findMergedTransaction(sipRequest);
        if (sipServerTransaction != null) {
            this.sendLoopDetectedResponse(sipRequest, transaction);
            return;
        }
    }
    if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug("dialogId = " + dialogId);
        sipStack.getStackLogger().logDebug("dialog = " + dialog);
    }
    // endpoint, then the stack strips off the route header.
    if (sipRequest.getHeader(Route.NAME) != null && transaction.getDialog() != null) {
        RouteList routes = sipRequest.getRouteHeaders();
        Route route = (Route) routes.getFirst();
        SipUri uri = (SipUri) route.getAddress().getURI();
        int port;
        if (uri.getHostPort().hasPort()) {
            port = uri.getHostPort().getPort();
        } else {
            if (listeningPoint.getTransport().equalsIgnoreCase("TLS"))
                port = 5061;
            else
                port = 5060;
        }
        String host = uri.getHost();
        if ((host.equals(listeningPoint.getIPAddress()) || host.equalsIgnoreCase(listeningPoint.getSentBy())) && port == listeningPoint.getPort()) {
            if (routes.size() == 1)
                sipRequest.removeHeader(Route.NAME);
            else
                routes.removeFirst();
        }
    }
    if (sipRequest.getMethod().equals(Request.REFER) && dialog != null && sipProvider.isDialogErrorsAutomaticallyHandled()) {
        /*
             * An agent responding to a REFER method MUST return a 400 (Bad Request) if the
             * request contained zero or more than one Refer-To header field values.
             */
        ReferToHeader sipHeader = (ReferToHeader) sipRequest.getHeader(ReferTo.NAME);
        if (sipHeader == null) {
            this.sendBadRequestResponse(sipRequest, transaction, "Refer-To header is missing");
            return;
        }
        /*
             * A refer cannot be processed until we have either sent or received an ACK.
             */
        SIPTransaction lastTransaction = ((SIPDialog) dialog).getLastTransaction();
        if (lastTransaction != null && sipProvider.isDialogErrorsAutomaticallyHandled()) {
            SIPRequest lastRequest = (SIPRequest) lastTransaction.getRequest();
            if (lastTransaction instanceof SIPServerTransaction) {
                if (!((SIPDialog) dialog).isAckSeen() && lastRequest.getMethod().equals(Request.INVITE)) {
                    this.sendRequestPendingResponse(sipRequest, transaction);
                    return;
                }
            } else if (lastTransaction instanceof SIPClientTransaction) {
                long cseqno = lastRequest.getCSeqHeader().getSeqNumber();
                String method = lastRequest.getMethod();
                if (method.equals(Request.INVITE) && !dialog.isAckSent(cseqno)) {
                    this.sendRequestPendingResponse(sipRequest, transaction);
                    return;
                }
            }
        }
    } else if (sipRequest.getMethod().equals(Request.UPDATE)) {
        /*
             * Got an UPDATE method and the user dialog does not exist and the user wants to be a
             * User agent.
             * 
             */
        if (sipProvider.isAutomaticDialogSupportEnabled() && dialog == null) {
            this.sendCallOrTransactionDoesNotExistResponse(sipRequest, transaction);
            return;
        }
    } else if (sipRequest.getMethod().equals(Request.ACK)) {
        if (transaction != null && transaction.isInviteTransaction()) {
            // take care of it.
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Processing ACK for INVITE Tx ");
        } else {
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Processing ACK for dialog " + dialog);
            if (dialog == null) {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("Dialog does not exist " + sipRequest.getFirstLine() + " isServerTransaction = " + true);
                }
                SIPServerTransaction st = sipStack.getRetransmissionAlertTransaction(dialogId);
                if (st != null && st.isRetransmissionAlertEnabled()) {
                    st.disableRetransmissionAlerts();
                }
                /*
                     * JvB: must never drop ACKs that dont match a transaction! One cannot be sure
                     * if it isn't an ACK for a 2xx response
                     * 
                     */
                SIPServerTransaction ackTransaction = sipStack.findTransactionPendingAck(sipRequest);
                /*
                     * Found a transaction ( that we generated ) which is waiting for ACK. So ACK
                     * it and return.
                     */
                if (ackTransaction != null) {
                    if (sipStack.isLoggingEnabled())
                        sipStack.getStackLogger().logDebug("Found Tx pending ACK");
                    try {
                        ackTransaction.setAckSeen();
                        sipStack.removeTransaction(ackTransaction);
                        sipStack.removeTransactionPendingAck(ackTransaction);
                    } catch (Exception ex) {
                        if (sipStack.isLoggingEnabled()) {
                            sipStack.getStackLogger().logError("Problem terminating transaction", ex);
                        }
                    }
                    return;
                }
            } else {
                if (!dialog.handleAck(transaction)) {
                    if (!dialog.isSequnceNumberValidation()) {
                        if (sipStack.isLoggingEnabled()) {
                            sipStack.getStackLogger().logDebug("Dialog exists with loose dialog validation " + sipRequest.getFirstLine() + " isServerTransaction = " + true + " dialog = " + dialog.getDialogId());
                        }
                        SIPServerTransaction st = sipStack.getRetransmissionAlertTransaction(dialogId);
                        if (st != null && st.isRetransmissionAlertEnabled()) {
                            st.disableRetransmissionAlerts();
                        }
                    } else {
                        if (sipStack.isLoggingEnabled()) {
                            sipStack.getStackLogger().logDebug("Dropping ACK - cannot find a transaction or dialog");
                        }
                        SIPServerTransaction ackTransaction = sipStack.findTransactionPendingAck(sipRequest);
                        if (ackTransaction != null) {
                            if (sipStack.isLoggingEnabled())
                                sipStack.getStackLogger().logDebug("Found Tx pending ACK");
                            try {
                                ackTransaction.setAckSeen();
                                sipStack.removeTransaction(ackTransaction);
                                sipStack.removeTransactionPendingAck(ackTransaction);
                            } catch (Exception ex) {
                                if (sipStack.isLoggingEnabled()) {
                                    sipStack.getStackLogger().logError("Problem terminating transaction", ex);
                                }
                            }
                        }
                        return;
                    }
                } else {
                    transaction.passToListener();
                    dialog.addTransaction(transaction);
                    dialog.addRoute(sipRequest);
                    transaction.setDialog(dialog, dialogId);
                    if (sipRequest.getMethod().equals(Request.INVITE) && sipProvider.isDialogErrorsAutomaticallyHandled()) {
                        sipStack.putInMergeTable(transaction, sipRequest);
                    }
                    if (sipStack.deliverTerminatedEventForAck) {
                        try {
                            sipStack.addTransaction(transaction);
                            transaction.scheduleAckRemoval();
                        } catch (IOException ex) {
                        }
                    } else {
                        transaction.setMapped(true);
                    }
                }
            }
        }
    } else if (sipRequest.getMethod().equals(Request.PRACK)) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Processing PRACK for dialog " + dialog);
        if (dialog == null && sipProvider.isAutomaticDialogSupportEnabled()) {
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Dialog does not exist " + sipRequest.getFirstLine() + " isServerTransaction = " + true);
            }
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Sending 481 for PRACK - automatic dialog support is enabled -- cant find dialog!");
            }
            SIPResponse notExist = sipRequest.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
            try {
                sipProvider.sendResponse(notExist);
            } catch (SipException e) {
                sipStack.getStackLogger().logError("error sending response", e);
            }
            if (transaction != null) {
                sipStack.removeTransaction(transaction);
                transaction.releaseSem();
            }
            return;
        } else if (dialog != null) {
            if (!dialog.handlePrack(sipRequest)) {
                if (sipStack.isLoggingEnabled())
                    sipStack.getStackLogger().logDebug("Dropping out of sequence PRACK ");
                if (transaction != null) {
                    sipStack.removeTransaction(transaction);
                    transaction.releaseSem();
                }
                return;
            } else {
                try {
                    sipStack.addTransaction(transaction);
                    dialog.addTransaction(transaction);
                    dialog.addRoute(sipRequest);
                    transaction.setDialog(dialog, dialogId);
                } catch (Exception ex) {
                    InternalErrorHandler.handleException(ex);
                }
            }
        } else {
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Processing PRACK without a DIALOG -- this must be a proxy element");
        }
    } else if (sipRequest.getMethod().equals(Request.BYE)) {
        // Check for correct sequence numbering of the BYE
        if (dialog != null && !dialog.isRequestConsumable(sipRequest)) {
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Dropping out of sequence BYE " + dialog.getRemoteSeqNumber() + " " + sipRequest.getCSeq().getSeqNumber());
            if (dialog.getRemoteSeqNumber() >= sipRequest.getCSeq().getSeqNumber() && transaction.getState() == TransactionState.TRYING) {
                this.sendServerInternalErrorResponse(sipRequest, transaction);
            }
            // If the stack knows about the tx, then remove it.
            if (transaction != null)
                sipStack.removeTransaction(transaction);
            return;
        } else if (dialog == null && sipProvider.isAutomaticDialogSupportEnabled()) {
            // Drop bye's with 481 if dialog does not exist.
            // If dialog support is enabled then
            // there must be a dialog associated with the bye
            // No dialog could be found and requests on this
            // provider. Must act like a user agent -- so drop the request.
            // NOTE: if Automatic dialog support is not enabled,
            // then it is the application's responsibility to
            // take care of this error condition possibly.
            SIPResponse response = sipRequest.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
            response.setReasonPhrase("Dialog Not Found");
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("dropping request -- automatic dialog " + "support enabled and dialog does not exist!");
            try {
                transaction.sendResponse(response);
            } catch (SipException ex) {
                sipStack.getStackLogger().logError("Error in sending response", ex);
            }
            // If the stack knows about the tx, then remove it.
            if (transaction != null) {
                sipStack.removeTransaction(transaction);
                transaction.releaseSem();
                transaction = null;
            }
            return;
        }
        // to manage its own dialog layer.
        if (transaction != null && dialog != null) {
            try {
                if (sipProvider == dialog.getSipProvider()) {
                    sipStack.addTransaction(transaction);
                    dialog.addTransaction(transaction);
                    transaction.setDialog(dialog, dialogId);
                }
            } catch (IOException ex) {
                InternalErrorHandler.handleException(ex);
            }
        }
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("BYE Tx = " + transaction + " isMapped =" + transaction.isTransactionMapped());
        }
    } else if (sipRequest.getMethod().equals(Request.CANCEL)) {
        SIPServerTransaction st = (SIPServerTransaction) sipStack.findCancelTransaction(sipRequest, true);
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("Got a CANCEL, InviteServerTx = " + st + " cancel Server Tx ID = " + transaction + " isMapped = " + transaction.isTransactionMapped());
        }
        // Check if we can process the CANCEL request.
        if (sipRequest.getMethod().equals(Request.CANCEL)) {
            // default action and avoid bothering the listener.
            if (st != null && st.getState() == SIPTransaction.TERMINATED_STATE) {
                // just respond OK to the CANCEL and bail.
                if (sipStack.isLoggingEnabled())
                    sipStack.getStackLogger().logDebug("Too late to cancel Transaction");
                // send OK and just ignore the CANCEL.
                try {
                    transaction.sendResponse(sipRequest.createResponse(Response.OK));
                } catch (Exception ex) {
                    if (ex.getCause() != null && ex.getCause() instanceof IOException) {
                        st.raiseIOExceptionEvent();
                    }
                }
                return;
            }
            if (sipStack.isLoggingEnabled())
                sipStack.getStackLogger().logDebug("Cancel transaction = " + st);
        }
        if (transaction != null && st != null && st.getDialog() != null) {
            // Found an invite tx corresponding to the CANCEL.
            // Set up the client tx and pass up to listener.
            transaction.setDialog((SIPDialog) st.getDialog(), dialogId);
            dialog = (SIPDialog) st.getDialog();
        } else if (st == null && sipProvider.isAutomaticDialogSupportEnabled() && transaction != null) {
            // Could not find a invite tx corresponding to the CANCEL.
            // Automatic dialog support is enabled so I must behave like
            // an endpoint on this provider.
            // Send the error response for the cancel.
            SIPResponse response = sipRequest.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("dropping request -- automatic dialog support " + "enabled and INVITE ST does not exist!");
            }
            try {
                sipProvider.sendResponse(response);
            } catch (SipException ex) {
                InternalErrorHandler.handleException(ex);
            }
            if (transaction != null) {
                sipStack.removeTransaction(transaction);
                transaction.releaseSem();
            }
            return;
        }
        // statefully handled.
        if (st != null) {
            try {
                if (transaction != null) {
                    sipStack.addTransaction(transaction);
                    transaction.setPassToListener();
                    transaction.setInviteTransaction(st);
                    // Dont let the INVITE and CANCEL be concurrently
                    // processed.
                    st.acquireSem();
                }
            } catch (Exception ex) {
                InternalErrorHandler.handleException(ex);
            }
        }
    } else if (sipRequest.getMethod().equals(Request.INVITE)) {
        SIPTransaction lastTransaction = dialog == null ? null : dialog.getInviteTransaction();
        if (dialog != null && transaction != null && lastTransaction != null && sipRequest.getCSeq().getSeqNumber() > dialog.getRemoteSeqNumber() && lastTransaction instanceof SIPServerTransaction && sipProvider.isDialogErrorsAutomaticallyHandled() && dialog.isSequnceNumberValidation() && lastTransaction.isInviteTransaction() && lastTransaction.getState() != TransactionState.COMPLETED && lastTransaction.getState() != TransactionState.TERMINATED && lastTransaction.getState() != TransactionState.CONFIRMED) {
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Sending 500 response for out of sequence message");
            }
            this.sendServerInternalErrorResponse(sipRequest, transaction);
            return;
        }
        /*
             * Saw an interleaved invite before ACK was sent. RFC 3261 Chapter 14. A UAS that
             * receives an INVITE on a dialog while an INVITE it had sent on that dialog is in
             * progress MUST return a 491 (Request Pending) response to the received INVITE.
             */
        lastTransaction = (dialog == null ? null : dialog.getLastTransaction());
        if (dialog != null && sipProvider.isDialogErrorsAutomaticallyHandled() && lastTransaction != null && lastTransaction.isInviteTransaction() && lastTransaction instanceof ClientTransaction && lastTransaction.getLastResponse() != null && lastTransaction.getLastResponse().getStatusCode() == 200 && !dialog.isAckSent(lastTransaction.getLastResponse().getCSeq().getSeqNumber())) {
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Sending 491 response for client Dialog ACK not sent.");
            }
            this.sendRequestPendingResponse(sipRequest, transaction);
            return;
        }
        if (dialog != null && lastTransaction != null && sipProvider.isDialogErrorsAutomaticallyHandled() && lastTransaction.isInviteTransaction() && lastTransaction instanceof ServerTransaction && !dialog.isAckSeen()) {
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Sending 491 response for server Dialog ACK not seen.");
            }
            this.sendRequestPendingResponse(sipRequest, transaction);
            return;
        }
    }
    if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug("CHECK FOR OUT OF SEQ MESSAGE " + dialog + " transaction " + transaction);
    }
    if (dialog != null && transaction != null && !sipRequest.getMethod().equals(Request.BYE) && !sipRequest.getMethod().equals(Request.CANCEL) && !sipRequest.getMethod().equals(Request.ACK) && !sipRequest.getMethod().equals(Request.PRACK)) {
        if (!dialog.isRequestConsumable(sipRequest)) {
            // Drop the request
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("Dropping out of sequence message " + dialog.getRemoteSeqNumber() + " " + sipRequest.getCSeq());
            }
            if (dialog.getRemoteSeqNumber() >= sipRequest.getCSeq().getSeqNumber() && sipProvider.isDialogErrorsAutomaticallyHandled() && (transaction.getState() == TransactionState.TRYING || transaction.getState() == TransactionState.PROCEEDING)) {
                this.sendServerInternalErrorResponse(sipRequest, transaction);
            }
            return;
        }
        try {
            if (sipProvider == dialog.getSipProvider()) {
                sipStack.addTransaction(transaction);
                // This will set the remote sequence number.
                dialog.addTransaction(transaction);
                dialog.addRoute(sipRequest);
                transaction.setDialog(dialog, dialogId);
            }
        } catch (IOException ex) {
            transaction.raiseIOExceptionEvent();
            sipStack.removeTransaction(transaction);
            return;
        }
    }
    RequestEvent sipEvent;
    if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug(sipRequest.getMethod() + " transaction.isMapped = " + transaction.isTransactionMapped());
    }
    if (dialog == null && sipRequest.getMethod().equals(Request.NOTIFY)) {
        SIPClientTransaction pendingSubscribeClientTx = sipStack.findSubscribeTransaction(sipRequest, listeningPoint);
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("PROCESSING NOTIFY  DIALOG == null " + pendingSubscribeClientTx);
        }
        /*
             * RFC 3265: Upon receiving a NOTIFY request, the subscriber should check that it
             * matches at least one of its outstanding subscriptions; if not, it MUST return a
             * "481 Subscription does not exist" response unless another 400- or -class response
             * is more appropriate.
             */
        if (sipProvider.isAutomaticDialogSupportEnabled() && pendingSubscribeClientTx == null && !sipStack.deliverUnsolicitedNotify) {
            /*
                 * This is the case of the UAC receiving a Stray NOTIFY for which it has not
                 * previously sent out a SUBSCRIBE and for which it does not have an established
                 * dialog.
                 */
            try {
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("Could not find Subscription for Notify Tx.");
                }
                Response errorResponse = sipRequest.createResponse(Response.CALL_OR_TRANSACTION_DOES_NOT_EXIST);
                errorResponse.setReasonPhrase("Subscription does not exist");
                sipProvider.sendResponse(errorResponse);
                return;
            } catch (Exception ex) {
                sipStack.getStackLogger().logError("Exception while sending error response statelessly", ex);
                return;
            }
        }
        // notify to this dialog and pass it up.
        if (pendingSubscribeClientTx != null) {
            // The response to the pending subscribe tx can try to create
            // a dialog at the same time that the notify is trying to
            // create a dialog. Thus we cannot process both at the
            // same time.
            transaction.setPendingSubscribe(pendingSubscribeClientTx);
            // The transaction gets assigned to the dialog from the
            // outgoing subscribe. First see if anybody claimed the
            // default Dialog for the outgoing Subscribe request.
            SIPDialog subscriptionDialog = (SIPDialog) pendingSubscribeClientTx.getDefaultDialog();
            // TODO -- refactor this. Can probably be written far cleaner.
            if (subscriptionDialog == null || subscriptionDialog.getDialogId() == null || !subscriptionDialog.getDialogId().equals(dialogId)) {
                // the notify.
                if (subscriptionDialog != null && subscriptionDialog.getDialogId() == null) {
                    subscriptionDialog.setDialogId(dialogId);
                } else {
                    subscriptionDialog = pendingSubscribeClientTx.getDialog(dialogId);
                }
                if (sipStack.isLoggingEnabled()) {
                    sipStack.getStackLogger().logDebug("PROCESSING NOTIFY Subscribe DIALOG " + subscriptionDialog);
                }
                // the SUBSCRIBE on the subscribe tx.
                if (subscriptionDialog == null && (sipProvider.isAutomaticDialogSupportEnabled() || pendingSubscribeClientTx.getDefaultDialog() != null)) {
                    Event event = (Event) sipRequest.getHeader(EventHeader.NAME);
                    if (sipStack.isEventForked(event.getEventType())) {
                        subscriptionDialog = SIPDialog.createFromNOTIFY(pendingSubscribeClientTx, transaction);
                    }
                }
                if (subscriptionDialog != null) {
                    transaction.setDialog(subscriptionDialog, dialogId);
                    subscriptionDialog.setState(DialogState.CONFIRMED.getValue());
                    sipStack.putDialog(subscriptionDialog);
                    pendingSubscribeClientTx.setDialog(subscriptionDialog, dialogId);
                    if (!transaction.isTransactionMapped()) {
                        this.sipStack.mapTransaction(transaction);
                        // Let the listener see it if it just got
                        // created.
                        // otherwise, we have already processed the tx
                        // so
                        // we dont want the listener to see it.
                        transaction.setPassToListener();
                        try {
                            this.sipStack.addTransaction(transaction);
                        } catch (Exception ex) {
                        }
                    }
                }
            } else {
                // The subscription default dialog is our dialog.
                // Found a subscrbe dialog for the NOTIFY
                // So map the tx.
                transaction.setDialog(subscriptionDialog, dialogId);
                dialog = subscriptionDialog;
                if (!transaction.isTransactionMapped()) {
                    this.sipStack.mapTransaction(transaction);
                    // Let the listener see it if it just got created.
                    // otherwise, we have already processed the tx so
                    // we dont want the listener to see it.
                    transaction.setPassToListener();
                    try {
                        this.sipStack.addTransaction(transaction);
                    } catch (Exception ex) {
                    }
                }
                sipStack.putDialog(subscriptionDialog);
                if (pendingSubscribeClientTx != null) {
                    subscriptionDialog.addTransaction(pendingSubscribeClientTx);
                    pendingSubscribeClientTx.setDialog(subscriptionDialog, dialogId);
                }
            }
            if (transaction != null && ((SIPServerTransaction) transaction).isTransactionMapped()) {
                // Shadow transaction has been created and the stack
                // knows
                // about it.
                sipEvent = new RequestEvent((SipProvider) sipProvider, (ServerTransaction) transaction, subscriptionDialog, (Request) sipRequest);
            } else {
                // Shadow transaction has been created but the stack
                // does
                // not know
                // about it.
                sipEvent = new RequestEvent((SipProvider) sipProvider, null, subscriptionDialog, (Request) sipRequest);
            }
        } else {
            if (sipStack.isLoggingEnabled()) {
                sipStack.getStackLogger().logDebug("could not find subscribe tx");
            }
            // Got a notify out of the blue - just pass it up
            // for stateless handling by the application.
            sipEvent = new RequestEvent(sipProvider, null, null, (Request) sipRequest);
        }
    } else {
        // The listener can create the dialog if needed.
        if (transaction != null && (((SIPServerTransaction) transaction).isTransactionMapped())) {
            sipEvent = new RequestEvent(sipProvider, (ServerTransaction) transaction, dialog, (Request) sipRequest);
        } else {
            sipEvent = new RequestEvent(sipProvider, null, dialog, (Request) sipRequest);
        }
    }
    sipProvider.handleEvent(sipEvent, transaction);
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) RouteList(gov.nist.javax.sip.header.RouteList) SipUri(gov.nist.javax.sip.address.SipUri) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPRequest(gov.nist.javax.sip.message.SIPRequest) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) ReferToHeader(javax.sip.header.ReferToHeader) ServerTransaction(javax.sip.ServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) Route(gov.nist.javax.sip.header.Route) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) ClientTransaction(javax.sip.ClientTransaction) RequestEvent(javax.sip.RequestEvent) Request(javax.sip.message.Request) SIPRequest(gov.nist.javax.sip.message.SIPRequest) IOException(java.io.IOException) InvalidArgumentException(javax.sip.InvalidArgumentException) SipException(javax.sip.SipException) IOException(java.io.IOException) ObjectInUseException(javax.sip.ObjectInUseException) Contact(gov.nist.javax.sip.header.Contact) Response(javax.sip.message.Response) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) RequestEvent(javax.sip.RequestEvent) Event(gov.nist.javax.sip.header.Event) ResponseEvent(javax.sip.ResponseEvent) SipException(javax.sip.SipException) SipProvider(javax.sip.SipProvider)

Example 8 with SIPServerTransaction

use of gov.nist.javax.sip.stack.SIPServerTransaction 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)

Example 9 with SIPServerTransaction

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

the class SipProviderImpl method transactionErrorEvent.

/**
     * Invoked when an error has ocurred with a transaction. Propagate up to the
     * listeners.
     *
     * @param transactionErrorEvent
     *            Error event.
     */
public void transactionErrorEvent(SIPTransactionErrorEvent transactionErrorEvent) {
    SIPTransaction transaction = (SIPTransaction) transactionErrorEvent.getSource();
    if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TRANSPORT_ERROR) {
        // There must be a way to inform the TU here!!
        if (sipStack.isLoggingEnabled()) {
            sipStack.getStackLogger().logDebug("TransportError occured on " + transaction);
        }
        // Treat this like a timeout event. (Suggestion from Christophe).
        Object errorObject = transactionErrorEvent.getSource();
        Timeout timeout = Timeout.TRANSACTION;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            SIPClientTransaction clientTx = (SIPClientTransaction) errorObject;
            Hop hop = clientTx.getNextHop();
            if (sipStack.getRouter() instanceof RouterExt) {
                ((RouterExt) sipStack.getRouter()).transactionTimeout(hop);
            }
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        // Handling transport error like timeout
        this.handleEvent(ev, (SIPTransaction) errorObject);
    } else if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TIMEOUT_ERROR) {
        // This is a timeout event.
        Object errorObject = transactionErrorEvent.getSource();
        Timeout timeout = Timeout.TRANSACTION;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            SIPClientTransaction clientTx = (SIPClientTransaction) errorObject;
            Hop hop = clientTx.getNextHop();
            if (sipStack.getRouter() instanceof RouterExt) {
                ((RouterExt) sipStack.getRouter()).transactionTimeout(hop);
            }
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        this.handleEvent(ev, (SIPTransaction) errorObject);
    } else if (transactionErrorEvent.getErrorID() == SIPTransactionErrorEvent.TIMEOUT_RETRANSMIT) {
        // This is a timeout retransmit event.
        // We should never get this if retransmit filter is
        // enabled (ie. in that case the stack should handle.
        // all retransmits.
        Object errorObject = transactionErrorEvent.getSource();
        Transaction tx = (Transaction) errorObject;
        if (tx.getDialog() != null)
            InternalErrorHandler.handleException("Unexpected event !", this.sipStack.getStackLogger());
        Timeout timeout = Timeout.RETRANSMIT;
        TimeoutEvent ev = null;
        if (errorObject instanceof SIPServerTransaction) {
            ev = new TimeoutEvent(this, (ServerTransaction) errorObject, timeout);
        } else {
            ev = new TimeoutEvent(this, (ClientTransaction) errorObject, timeout);
        }
        this.handleEvent(ev, (SIPTransaction) errorObject);
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) TimeoutEvent(javax.sip.TimeoutEvent) ServerTransaction(javax.sip.ServerTransaction) Transaction(javax.sip.Transaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) ClientTransaction(javax.sip.ClientTransaction) RouterExt(gov.nist.javax.sip.address.RouterExt) Timeout(javax.sip.Timeout) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) ClientTransaction(javax.sip.ClientTransaction) Hop(javax.sip.address.Hop) EventObject(java.util.EventObject) ServerTransaction(javax.sip.ServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction)

Example 10 with SIPServerTransaction

use of gov.nist.javax.sip.stack.SIPServerTransaction in project jain-sip.ha by RestComm.

the class ServerTransactionCacheData method getServerTransaction.

public SIPServerTransaction getServerTransaction(String txId) throws SipCacheException {
    SIPServerTransaction haSipServerTransaction = null;
    final Cache jbossCache = getMobicentsCache().getJBossCache();
    Configuration config = jbossCache.getConfiguration();
    final boolean isBuddyReplicationEnabled = config.getBuddyReplicationConfig() != null && config.getBuddyReplicationConfig().isEnabled();
    TransactionManager transactionManager = config.getRuntimeConfig().getTransactionManager();
    boolean doTx = false;
    try {
        if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
            clusteredlogger.logDebug("transaction manager :" + transactionManager);
        }
        if (transactionManager != null && transactionManager.getTransaction() == null) {
            if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
                clusteredlogger.logDebug("transaction manager begin transaction");
            }
            transactionManager.begin();
            doTx = true;
        }
        // Adding code to handle Buddy replication to force data gravitation
        if (isBuddyReplicationEnabled) {
            if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
                clusteredlogger.logDebug("forcing data gravitation since buddy replication is enabled");
            }
            jbossCache.getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
        }
        final Node<String, Object> childNode = getNode().getChild(txId);
        if (childNode != null) {
            try {
                final Map<String, Object> transactionMetaData = childNode.getData();
                final Object dialogAppData = childNode.get(APPDATA);
                haSipServerTransaction = createServerTransaction(txId, transactionMetaData, dialogAppData);
            } catch (CacheException e) {
                throw new SipCacheException("A problem occured while retrieving the following server transaction " + txId + " from the Cache", e);
            }
        } else {
            if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
                clusteredlogger.logDebug("no child node found for transactionId " + txId);
            }
        }
    } catch (Exception ex) {
        try {
            if (transactionManager != null) {
                // Let's set it no matter what.
                transactionManager.setRollbackOnly();
            }
        } catch (Exception exn) {
            clusteredlogger.logError("Problem rolling back session mgmt transaction", exn);
        }
    } finally {
        if (doTx) {
            try {
                if (transactionManager.getTransaction().getStatus() != Status.STATUS_MARKED_ROLLBACK) {
                    if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
                        clusteredlogger.logDebug("transaction manager committing transaction");
                    }
                    transactionManager.commit();
                } else {
                    if (clusteredlogger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
                        clusteredlogger.logDebug("endBatch(): rolling back batch");
                    }
                    transactionManager.rollback();
                }
            } catch (RollbackException re) {
                // Do nothing here since cache may rollback automatically.
                clusteredlogger.logWarning("endBatch(): rolling back transaction with exception: " + re);
            } catch (RuntimeException re) {
                throw re;
            } catch (Exception e) {
                throw new RuntimeException("endTransaction(): Caught Exception ending batch: ", e);
            }
        }
    }
    return haSipServerTransaction;
}
Also used : Configuration(org.jboss.cache.config.Configuration) CacheException(org.jboss.cache.CacheException) RollbackException(javax.transaction.RollbackException) MobicentsHASIPServerTransaction(gov.nist.javax.sip.stack.MobicentsHASIPServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) IOException(java.io.IOException) CacheException(org.jboss.cache.CacheException) RollbackException(javax.transaction.RollbackException) ParseException(java.text.ParseException) PeerUnavailableException(javax.sip.PeerUnavailableException) TransactionManager(javax.transaction.TransactionManager) MobicentsCache(org.mobicents.cache.MobicentsCache) Cache(org.jboss.cache.Cache)

Aggregations

SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)10 IOException (java.io.IOException)6 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)5 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)4 ParseException (java.text.ParseException)4 SIPRequest (gov.nist.javax.sip.message.SIPRequest)3 SIPResponse (gov.nist.javax.sip.message.SIPResponse)3 MobicentsHASIPServerTransaction (gov.nist.javax.sip.stack.MobicentsHASIPServerTransaction)3 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)3 PeerUnavailableException (javax.sip.PeerUnavailableException)3 ServerTransaction (javax.sip.ServerTransaction)3 SipException (javax.sip.SipException)3 SipCacheException (org.mobicents.ha.javax.sip.cache.SipCacheException)3 MessageChannel (gov.nist.javax.sip.stack.MessageChannel)2 Map (java.util.Map)2 ClientTransaction (javax.sip.ClientTransaction)2 Hop (javax.sip.address.Hop)2 Response (javax.sip.message.Response)2 IMap (com.hazelcast.core.IMap)1 RouterExt (gov.nist.javax.sip.address.RouterExt)1