Search in sources :

Example 1 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SipProviderImpl method getNewClientTransaction.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#getNewClientTransaction(javax.sip.message.Request)
     */
public ClientTransaction getNewClientTransaction(Request request) throws TransactionUnavailableException {
    if (request == null)
        throw new NullPointerException("null request");
    if (!sipStack.isAlive())
        throw new TransactionUnavailableException("Stack is stopped");
    SIPRequest sipRequest = (SIPRequest) request;
    if (sipRequest.getTransaction() != null)
        throw new TransactionUnavailableException("Transaction already assigned to request");
    if (sipRequest.getMethod().equals(Request.ACK)) {
        throw new TransactionUnavailableException("Cannot create client transaction for  " + Request.ACK);
    }
    // sloppy
    if (sipRequest.getTopmostVia() == null) {
        ListeningPointImpl lp = (ListeningPointImpl) this.getListeningPoint("udp");
        Via via = lp.getViaHeader();
        request.setHeader(via);
    }
    // Give the request a quick check to see if all headers are assigned.
    try {
        sipRequest.checkHeaders();
    } catch (ParseException ex) {
        throw new TransactionUnavailableException(ex.getMessage(), ex);
    }
    /*
         * User decided to give us his own via header branch. Lets see if it
         * results in a clash. If so reject the request.
         */
    if (sipRequest.getTopmostVia().getBranch() != null && sipRequest.getTopmostVia().getBranch().startsWith(SIPConstants.BRANCH_MAGIC_COOKIE) && sipStack.findTransaction((SIPRequest) request, false) != null) {
        throw new TransactionUnavailableException("Transaction already exists!");
    }
    if (request.getMethod().equalsIgnoreCase(Request.CANCEL)) {
        SIPClientTransaction ct = (SIPClientTransaction) sipStack.findCancelTransaction((SIPRequest) request, false);
        if (ct != null) {
            ClientTransaction retval = sipStack.createClientTransaction((SIPRequest) request, ct.getMessageChannel());
            ((SIPTransaction) retval).addEventListener(this);
            sipStack.addTransaction((SIPClientTransaction) retval);
            if (ct.getDialog() != null) {
                ((SIPClientTransaction) retval).setDialog((SIPDialog) ct.getDialog(), sipRequest.getDialogId(false));
            }
            return retval;
        }
    }
    if (sipStack.isLoggingEnabled())
        sipStack.getStackLogger().logDebug("could not find existing transaction for " + ((SIPRequest) request).getFirstLine() + " creating a new one ");
    // Could not find a dialog or the route is not set in dialog.
    Hop hop = null;
    try {
        hop = sipStack.getNextHop((SIPRequest) request);
        if (hop == null)
            throw new TransactionUnavailableException("Cannot resolve next hop -- transaction unavailable");
    } catch (SipException ex) {
        throw new TransactionUnavailableException("Cannot resolve next hop -- transaction unavailable", ex);
    }
    String transport = hop.getTransport();
    ListeningPointImpl listeningPoint = (ListeningPointImpl) this.getListeningPoint(transport);
    String dialogId = sipRequest.getDialogId(false);
    SIPDialog dialog = sipStack.getDialog(dialogId);
    if (dialog != null && dialog.getState() == DialogState.TERMINATED) {
        // throw new TransactionUnavailableException
        // ("Found a terminated dialog -- possible re-use of old tag
        // parameters");
        sipStack.removeDialog(dialog);
    }
    try {
        // Set the brannch id before you ask for a tx.
        // If the user has set his own branch Id and the
        // branch id starts with a valid prefix, then take it.
        // otherwise, generate one. If branch ID checking has 
        // been requested, set the branch ID.
        String branchId = null;
        if (sipRequest.getTopmostVia().getBranch() == null || !sipRequest.getTopmostVia().getBranch().startsWith(SIPConstants.BRANCH_MAGIC_COOKIE) || sipStack.checkBranchId()) {
            branchId = Utils.getInstance().generateBranchId();
            sipRequest.getTopmostVia().setBranch(branchId);
        }
        Via topmostVia = sipRequest.getTopmostVia();
        //set port and transport if user hasn't already done this.
        if (topmostVia.getTransport() == null)
            topmostVia.setTransport(transport);
        if (topmostVia.getPort() == -1)
            topmostVia.setPort(listeningPoint.getPort());
        branchId = sipRequest.getTopmostVia().getBranch();
        SIPClientTransaction ct = (SIPClientTransaction) sipStack.createMessageChannel(sipRequest, listeningPoint.getMessageProcessor(), hop);
        if (ct == null)
            throw new TransactionUnavailableException("Cound not create tx");
        ct.setNextHop(hop);
        ct.setOriginalRequest(sipRequest);
        ct.setBranch(branchId);
        // if the stack supports dialogs then
        if (sipStack.isDialogCreated(request.getMethod())) {
            // (but noticed by Brad Templeton)
            if (dialog != null)
                ct.setDialog(dialog, sipRequest.getDialogId(false));
            else if (this.isAutomaticDialogSupportEnabled()) {
                SIPDialog sipDialog = sipStack.createDialog(ct);
                ct.setDialog(sipDialog, sipRequest.getDialogId(false));
            }
        } else {
            if (dialog != null) {
                ct.setDialog(dialog, sipRequest.getDialogId(false));
            }
        }
        // The provider is the event listener for all transactions.
        ct.addEventListener(this);
        return (ClientTransaction) ct;
    } catch (IOException ex) {
        throw new TransactionUnavailableException("Could not resolve next hop or listening point unavailable! ", ex);
    } catch (java.text.ParseException ex) {
        InternalErrorHandler.handleException(ex);
        throw new TransactionUnavailableException("Unexpected Exception FIXME! ", ex);
    } catch (InvalidArgumentException ex) {
        InternalErrorHandler.handleException(ex);
        throw new TransactionUnavailableException("Unexpected Exception FIXME! ", ex);
    }
}
Also used : SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) ClientTransaction(javax.sip.ClientTransaction) Hop(javax.sip.address.Hop) IOException(java.io.IOException) SIPRequest(gov.nist.javax.sip.message.SIPRequest) Via(gov.nist.javax.sip.header.Via) InvalidArgumentException(javax.sip.InvalidArgumentException) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) ParseException(java.text.ParseException)

Example 2 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SipProviderImpl method getNewDialog.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#getNewDialog(javax.sip.Transaction)
     */
public Dialog getNewDialog(Transaction transaction) throws SipException {
    if (transaction == null)
        throw new NullPointerException("Null transaction!");
    if (!sipStack.isAlive())
        throw new SipException("Stack is stopped.");
    if (isAutomaticDialogSupportEnabled())
        throw new SipException(" Error - AUTOMATIC_DIALOG_SUPPORT is on");
    if (!sipStack.isDialogCreated(transaction.getRequest().getMethod()))
        throw new SipException("Dialog cannot be created for this method " + transaction.getRequest().getMethod());
    SIPDialog dialog = null;
    SIPTransaction sipTransaction = (SIPTransaction) transaction;
    if (transaction instanceof ServerTransaction) {
        SIPServerTransaction st = (SIPServerTransaction) transaction;
        Response response = st.getLastResponse();
        if (response != null) {
            if (response.getStatusCode() != 100)
                throw new SipException("Cannot set dialog after response has been sent");
        }
        SIPRequest sipRequest = (SIPRequest) transaction.getRequest();
        String dialogId = sipRequest.getDialogId(true);
        dialog = sipStack.getDialog(dialogId);
        if (dialog == null) {
            dialog = sipStack.createDialog((SIPTransaction) transaction);
            // create and register the dialog and add the inital route set.
            dialog.addTransaction(sipTransaction);
            dialog.addRoute(sipRequest);
            sipTransaction.setDialog(dialog, null);
        } else {
            sipTransaction.setDialog(dialog, sipRequest.getDialogId(true));
        }
        if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
            sipStack.putInMergeTable(st, sipRequest);
        }
    } else {
        SIPClientTransaction sipClientTx = (SIPClientTransaction) transaction;
        SIPResponse response = sipClientTx.getLastResponse();
        if (response == null) {
            // A response has not yet been received, then set this up as the
            // default dialog.
            SIPRequest request = (SIPRequest) sipClientTx.getRequest();
            String dialogId = request.getDialogId(false);
            dialog = sipStack.getDialog(dialogId);
            if (dialog != null) {
                throw new SipException("Dialog already exists!");
            } else {
                dialog = sipStack.createDialog(sipTransaction);
            }
            sipClientTx.setDialog(dialog, null);
        } else {
            throw new SipException("Cannot call this method after response is received!");
        }
    }
    dialog.addEventListener(this);
    return dialog;
}
Also used : Response(javax.sip.message.Response) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) SipException(javax.sip.SipException) ServerTransaction(javax.sip.ServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 3 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class AuthenticationHelperImpl method setAuthenticationHeaders.

/*
     * (non-Javadoc)
     *
     * @see gov.nist.javax.sip.clientauthutils.AuthenticationHelper#attachAuthenticationHeaders(javax.sip.message.Request)
     */
public void setAuthenticationHeaders(Request request) {
    SIPRequest sipRequest = (SIPRequest) request;
    String callId = sipRequest.getCallId().getCallId();
    request.removeHeader(AuthorizationHeader.NAME);
    Collection<AuthorizationHeader> authHeaders = this.cachedCredentials.getCachedAuthorizationHeaders(callId);
    if (authHeaders == null) {
        if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Could not find authentication headers for " + callId);
        return;
    }
    for (AuthorizationHeader authHeader : authHeaders) {
        request.addHeader(authHeader);
    }
}
Also used : ProxyAuthorizationHeader(javax.sip.header.ProxyAuthorizationHeader) AuthorizationHeader(javax.sip.header.AuthorizationHeader) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 4 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class ListeningPointImpl method sendHeartbeat.

public void sendHeartbeat(String ipAddress, int port) throws IOException {
    HostPort targetHostPort = new HostPort();
    targetHostPort.setHost(new Host(ipAddress));
    targetHostPort.setPort(port);
    MessageChannel messageChannel = this.messageProcessor.createMessageChannel(targetHostPort);
    SIPRequest siprequest = new SIPRequest();
    siprequest.setNullRequest();
    messageChannel.sendMessage(siprequest);
}
Also used : HostPort(gov.nist.core.HostPort) Host(gov.nist.core.Host) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 5 with SIPRequest

use of gov.nist.javax.sip.message.SIPRequest in project XobotOS by xamarin.

the class SIPTransactionStack method auditTransactions.

/**
     * Audits SIP transactions for leaks
     *
     * @return Audit report, null if no transaction leaks were found
     */
private String auditTransactions(ConcurrentHashMap transactionsMap, long a_nLeakedTransactionTimer) {
    String auditReport = "  Leaked transactions:\n";
    int leakedTransactions = 0;
    long currentTime = System.currentTimeMillis();
    // Make a shallow copy of the transaction list.
    // This copy will remain intact as leaked transactions are removed by
    // the stack.
    LinkedList transactionsList = new LinkedList(transactionsMap.values());
    // Iterate through our copy
    Iterator it = transactionsList.iterator();
    while (it.hasNext()) {
        SIPTransaction sipTransaction = (SIPTransaction) it.next();
        if (sipTransaction != null) {
            if (sipTransaction.auditTag == 0) {
                // First time we see this transaction. Mark it as audited.
                sipTransaction.auditTag = currentTime;
            } else {
                // up.
                if (currentTime - sipTransaction.auditTag >= a_nLeakedTransactionTimer) {
                    // Leaked transaction found
                    leakedTransactions++;
                    // Generate some report
                    TransactionState transactionState = sipTransaction.getState();
                    SIPRequest origRequest = sipTransaction.getOriginalRequest();
                    String origRequestMethod = (origRequest != null ? origRequest.getMethod() : null);
                    String transactionReport = sipTransaction.getClass().getName() + ", state: " + (transactionState != null ? transactionState.toString() : "null") + ", OR: " + (origRequestMethod != null ? origRequestMethod : "null");
                    auditReport += "    " + transactionReport + "\n";
                    // Kill it
                    removeTransaction(sipTransaction);
                    if (isLoggingEnabled())
                        stackLogger.logDebug("auditTransactions: leaked " + transactionReport);
                }
            }
        }
    }
    // Return final report
    if (leakedTransactions > 0) {
        auditReport += "    Total: " + Integer.toString(leakedTransactions) + " leaked transactions detected and removed.\n";
    } else {
        auditReport = null;
    }
    return auditReport;
}
Also used : TransactionState(javax.sip.TransactionState) Iterator(java.util.Iterator) SIPRequest(gov.nist.javax.sip.message.SIPRequest) LinkedList(java.util.LinkedList)

Aggregations

SIPRequest (gov.nist.javax.sip.message.SIPRequest)32 IOException (java.io.IOException)17 ParseException (java.text.ParseException)17 SipException (javax.sip.SipException)16 InvalidArgumentException (javax.sip.InvalidArgumentException)10 SIPResponse (gov.nist.javax.sip.message.SIPResponse)9 Via (gov.nist.javax.sip.header.Via)8 ObjectInUseException (javax.sip.ObjectInUseException)8 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)6 SIPDialog (gov.nist.javax.sip.stack.SIPDialog)6 DialogDoesNotExistException (javax.sip.DialogDoesNotExistException)6 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)6 Hop (javax.sip.address.Hop)6 SipURI (javax.sip.address.SipURI)4 ListeningPointImpl (gov.nist.javax.sip.ListeningPointImpl)3 From (gov.nist.javax.sip.header.From)3 Route (gov.nist.javax.sip.header.Route)3 To (gov.nist.javax.sip.header.To)3 SIPServerTransaction (gov.nist.javax.sip.stack.SIPServerTransaction)3 SIPTransaction (gov.nist.javax.sip.stack.SIPTransaction)3