ct.addEventListener(this); return (ClientTransaction) ct; } catch (java.net.UnknownHostException ex) { lastException = ex; continue; } catch (java.text.ParseException ex) { InternalErrorHandler.handleException(ex); } } } if (LogWriter.needsLogging) { sipStack.logMessage("Error processing " + sipRequest); } if ( lastException == null ) { throw new TransactionUnavailableException( "Could not resolve next hop or listening point unavailable! "); } else { throw new TransactionUnavailableException ( "Could not resolve next hop or listening point unavailable! ", lastException); }
try {     String branchId = null; if (sipRequest.getTopmostVia().getBranch() == null || !sipRequest.getTopmostVia().getBranch().startsWith( SIPConstants.BRANCH_MAGIC_COOKIE)) { branchId = Utils.generateBranchId();
    public ClientTransaction getNewClientTransaction(Request request)            throws TransactionUnavailableException {        if (request == null)            throw new NullPointerException("null request");        SIPRequest sipRequest = (SIPRequest) request;        if (sipRequest.getTransaction() != null)            throw new TransactionUnavailableException(                    "Transaction already assigned to request");        // Prune illegal requests early.        if (sipRequest.getTopmostVia() != null) {            HostPort hp = sipRequest.getTopmostVia().getSentBy();            int port = hp.getPort() == -1 ? 5060 : hp.getPort();            Iterator it = sipStack.getListeningPoints();            boolean found = false;            // Note that lp.getPort() will in general return something different            // than the port used to create the LP when STUN is enabled.            while (it.hasNext()) {                ListeningPoint lp = (ListeningPoint) it.next();                if (lp.getPort() == port                        && lp.getTransport().equalsIgnoreCase(                                sipRequest.getTopmostVia().getTransport())) {                    found = true;                    break;                }            }            if (!found)                throw new TransactionUnavailableException(                        " No listening point for "                                + sipRequest.getTopmostVia().getTransport()                                + " at port " + port);        }        if (request.getMethod().equalsIgnoreCase(Request.CANCEL)) {            SIPClientTransaction ct = (SIPClientTransaction) sipStack                    .findCancelTransaction((SIPRequest) request, false);            if (ct != null) {                ClientTransaction retval = sipStack.createClientTransaction(ct                        .getMessageChannel());                ((SIPTransaction) retval)                        .setOriginalRequest((SIPRequest) request);                ((SIPTransaction) retval).addEventListener(this);                sipStack.addTransaction((SIPClientTransaction) retval);                ((SIPClientTransaction) retval).setDialog((SIPDialog) ct                        .getDialog());                return retval;            }        }        if (LogWriter.needsLogging)            sipStack.logMessage("could not find existing transaction for "                    + ((SIPRequest) request).getFirstLine());        // Could not find a dialog or the route is not set in dialog.        Iterator it = sipStack.getRouter().getNextHops(request);        String dialogId = sipRequest.getDialogId(false);        SIPDialog dialog = sipStack.getDialog(dialogId);        Exception lastException = null;        if (it == null || !it.hasNext()) {            // could not route the request as out of dialog.            // maybe the user has no router or the router cannot resolve            // the route.            // If this is part of a dialog then use the route from the dialog            if (dialog != null) {                try {                    HopImpl hop = dialog.getNextHop();                    if (hop != null) {                        SIPClientTransaction ct = (SIPClientTransaction) sipStack                                .createMessageChannel(this.listeningPoint.port, hop);                        String branchId = Utils.generateBranchId();                        if (sipRequest.getTopmostVia() != null) {                            sipRequest.getTopmostVia().setBranch(branchId);                        } else {                            // Find a message processor to assign this                            // transaction to.                            // MessageProcessor messageProcessor =                            // sipStack.getMessageProcessor(hop.getTransport());                            Via via = this.listeningPoint.messageProcessor                                    .getViaHeader();                            sipRequest.addHeader(via);                        }                        ct.setOriginalRequest(sipRequest);                        ct.setBranch(branchId);                        ct.setDialog(dialog);                        ct.addEventListener(this);                        return (ClientTransaction) ct;                    }                } catch (Exception ex) {                    throw new TransactionUnavailableException(ex.getMessage());                }            } else                throw new TransactionUnavailableException("no route!");        } else {            // An out of dialog route was found. Assign this to the            // client transaction.                    while (it.hasNext()) {                Hop hop = (Hop) it.next();                try {                    SIPClientTransaction ct = (SIPClientTransaction) sipStack                            .createMessageChannel(this.listeningPoint.port, hop);                    if (ct == null)                        continue;                    String branchId = Utils.generateBranchId();                    if (sipRequest.getTopmostVia() != null) {                        sipRequest.getTopmostVia().setBranch(branchId);                    } else {                        // Find a message processor to assign this                        // transaction to. MessageProcessor messageProcessor =                        // sipStack.getMessageProcessor(hop.getTransport());                        Via via = this.listeningPoint.messageProcessor                                .getViaHeader();                        sipRequest.addHeader(via);                    }                    ct.setOriginalRequest(sipRequest);                    ct.setBranch(branchId);                    // if the stack supports dialogs then                    if (sipStack.isDialogCreated(request.getMethod())) {                        // create a new dialog to contain this transaction                        // provided this is necessary.                        // This could be a re-invite                        // (but noticed by Brad Templeton)                        if (dialog != null)                            ct.setDialog(dialog);                        else if (sipStack.dialogSupport)                            sipStack.createDialog(ct);                    } else {                        ct.setDialog(dialog);                    }                    // The provider is the event listener for all transactions.                    ct.addEventListener(this);                    return (ClientTransaction) ct;                } catch (java.net.UnknownHostException ex) {                    lastException = ex;                    continue;                } catch (java.text.ParseException ex) {                    InternalErrorHandler.handleException(ex);                }            }        }        if (LogWriter.needsLogging) {            sipStack.logMessage("Error processing " + sipRequest);        }        if ( lastException == null ) {        throw new TransactionUnavailableException(                "Could not resolve next hop or listening point unavailable! ");        } else {            throw new TransactionUnavailableException            ( "Could not resolve next hop or listening point unavailable! ",                    lastException);        }    }