Search in sources :

Example 26 with ServerTransaction

use of javax.sip.ServerTransaction in project load-balancer by RestComm.

the class TestSipListener method processPrack.

private void processPrack(Request request, ServerTransaction serverTransactionId) {
    try {
        prackReceived = true;
        prackRequestReceived = request;
        ServerTransaction st = serverTransactionId;
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        Response response = protocolObjects.messageFactory.createResponse(200, request);
        st.sendResponse(response);
        Thread.sleep(200);
        inviteServerTid.sendResponse(getFinalResponse());
    } catch (Exception e) {
        logger.error("Unexpected exception while trying to send the 200 to PRACK " + request, e);
    }
}
Also used : Response(javax.sip.message.Response) ServerTransaction(javax.sip.ServerTransaction) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) TransactionUnavailableException(javax.sip.TransactionUnavailableException)

Example 27 with ServerTransaction

use of javax.sip.ServerTransaction in project load-balancer by RestComm.

the class TestSipListener method processInvite.

/**
 * Process the invite request.
 */
public void processInvite(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    inviteReceived = true;
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    inviteRequest = request;
    logger.info("shootme: got an Invite " + request);
    try {
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        inviteServerTid = st;
        Dialog dialog = st.getDialog();
        if (request.getHeader(JoinHeader.NAME) != null) {
            setJoinRequestReceived(true);
            this.joinDialog = dialog;
        } else if (request.getHeader(ReplacesHeader.NAME) != null) {
            setReplacesRequestReceived(true);
            this.replacesDialog = dialog;
        } else {
            this.dialogCount++;
            this.dialog = dialog;
        }
        logger.info("Shootme: dialog = " + dialog);
        this.inviteRequest = request;
        boolean sendReliably = false;
        RequireHeader requireHeader = (RequireHeader) request.getHeader(RequireHeader.NAME);
        if (requireHeader != null && "100rel".equalsIgnoreCase(requireHeader.getOptionTag().trim())) {
            sendReliably = true;
        }
        if (respondWithError == null)
            for (int provisionalResponseToSend : provisionalResponsesToSend) {
                Thread.sleep(getTimeToWaitBetweenProvisionnalResponse());
                Response response = protocolObjects.messageFactory.createResponse(provisionalResponseToSend, request);
                if (provisionalResponseToSend >= Response.TRYING && provisionalResponseToSend < Response.OK) {
                    ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
                    if (provisionalResponseToSend != Response.TRYING && toHeader.getTag() == null) {
                        // Application is supposed to set.
                        toHeader.setTag(TO_TAG);
                    }
                    if (sendReliably && provisionalResponseToSend != Response.TRYING) {
                        requireHeader = protocolObjects.headerFactory.createRequireHeader("100rel");
                        response.addHeader(requireHeader);
                        Header rseqHeader = protocolObjects.headerFactory.createRSeqHeader(rseqNumber.getAndIncrement());
                        response.addHeader(rseqHeader);
                        dialog.sendReliableProvisionalResponse(response);
                    } else {
                        st.sendResponse(response);
                        eventListener.uasAfterResponse(response.getStatusCode(), appServer);
                    }
                }
            }
        if (respondWithError != null && !sendReliably) {
            Response response = protocolObjects.messageFactory.createResponse(respondWithError, request);
            response.setReasonPhrase("Unable to setup media services");
            st.sendResponse(response);
            eventListener.uasAfterResponse(response.getStatusCode(), appServer);
            return;
        }
        ContactHeader contactHeader = (ContactHeader) request.getHeader(ContactHeader.NAME);
        if (contactHeader != null && "0.0.0.0".equals(((SipURI) contactHeader.getAddress().getURI()).getHost())) {
            abortProcessing = true;
            throw new IllegalArgumentException("we received a contact header with 0.0.0.0 in an INVITE !");
        }
        if (!waitForCancel) {
            Address address = null;
            if (!isIpv6)
                address = protocolObjects.addressFactory.createAddress("Shootme <sip:127.0.0.1:" + myPort + ";transport=" + protocolObjects.transport + ">");
            else
                address = protocolObjects.addressFactory.createAddress("Shootme <sip:[::1]:" + myPort + ";transport=" + protocolObjects.transport + ">");
            contactHeader = protocolObjects.headerFactory.createContactHeader(address);
            setFinalResponse(protocolObjects.messageFactory.createResponse(finalResponseToSend, request));
            if (testAckViaParam) {
                ViaHeader viaHeader = (ViaHeader) getFinalResponse().getHeader(ViaHeader.NAME);
                viaHeader.setParameter("testAckViaParam", "true");
            }
            ToHeader toHeader = (ToHeader) getFinalResponse().getHeader(ToHeader.NAME);
            if (toHeader.getTag() == null) {
                // Application is supposed to set.
                toHeader.setTag(TO_TAG);
            }
            getFinalResponse().addHeader(contactHeader);
            if (!sendReliably) {
                Thread.sleep(2000);
                st.sendResponse(getFinalResponse());
                eventListener.uasAfterResponse(getFinalResponse().getStatusCode(), appServer);
            }
        } else {
            logger.info("Waiting for CANCEL, stopping the INVITE processing ");
            return;
        }
        if (("join").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
            sendJoinMessage = true;
        }
        if (("replaces").equalsIgnoreCase(((SipUri) request.getRequestURI()).getUser())) {
            sendReplacesMessage = true;
        }
    } catch (Exception ex) {
        logger.error("unexpected exception", ex);
        throw new RuntimeException(ex);
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) Address(javax.sip.address.Address) Request(javax.sip.message.Request) RequireHeader(javax.sip.header.RequireHeader) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) Response(javax.sip.message.Response) ReasonHeader(javax.sip.header.ReasonHeader) RequireHeader(javax.sip.header.RequireHeader) SIPETagHeader(javax.sip.header.SIPETagHeader) ContactHeader(javax.sip.header.ContactHeader) ReferToHeader(javax.sip.header.ReferToHeader) ViaHeader(javax.sip.header.ViaHeader) SIPIfMatchHeader(javax.sip.header.SIPIfMatchHeader) AuthorizationHeader(javax.sip.header.AuthorizationHeader) CallIdHeader(javax.sip.header.CallIdHeader) ContentLengthHeader(javax.sip.header.ContentLengthHeader) ExtensionHeader(javax.sip.header.ExtensionHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) Header(javax.sip.header.Header) ReplacesHeader(gov.nist.javax.sip.header.extensions.ReplacesHeader) CSeqHeader(javax.sip.header.CSeqHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) JoinHeader(gov.nist.javax.sip.header.extensions.JoinHeader) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) ProxyAuthorizationHeader(javax.sip.header.ProxyAuthorizationHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) EventHeader(javax.sip.header.EventHeader) FromHeader(javax.sip.header.FromHeader) ProxyAuthenticateHeader(javax.sip.header.ProxyAuthenticateHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) ViaHeader(javax.sip.header.ViaHeader) Dialog(javax.sip.Dialog) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) ServerTransaction(javax.sip.ServerTransaction) SipProvider(javax.sip.SipProvider)

Example 28 with ServerTransaction

use of javax.sip.ServerTransaction in project jain-sip.ha by RestComm.

the class SimpleB2BUAHandler method forwardResponse.

/**
 * @param responseEvent
 * @throws InvalidArgumentException
 */
@SuppressWarnings("unchecked")
private void forwardResponse(Response receivedResponse) throws SipException, InvalidArgumentException {
    if (receivedResponse.getStatusCode() == 202) {
        return;
    }
    System.out.println("Forwarding " + receivedResponse);
    final ServerTransaction origServerTransaction = this.getServerTransaction();
    Response forgedResponse = null;
    try {
        forgedResponse = messageFactory.createResponse(receivedResponse.getStatusCode(), origServerTransaction.getRequest());
    } catch (ParseException e) {
        throw new SipException("Failed to forge message", e);
    }
    // if ((getIncomingDialog() == null || getIncomingDialog().getState() == DialogState.EARLY) && localTag != null && getIncomingDialog() != null && getIncomingDialog().isServer()) {
    // // no tag set in the response, since the dialog creating transaction didn't had it
    // try {
    // ((ToHeader)forgedResponse.getHeader(ToHeader.NAME)).setTag(localTag);
    // } catch (ParseException e) {
    // throw new SipException("Failed to set local tag", e);
    // }
    // }
    // copy headers
    ListIterator<String> lit = receivedResponse.getHeaderNames();
    String headerName = null;
    ListIterator<Header> headersIterator = null;
    while (lit.hasNext()) {
        headerName = lit.next();
        if (SimpleB2BUAHandler.getHeadersToOmmitOnResponseCopy().contains(headerName)) {
            continue;
        } else {
            forgedResponse.removeHeader(headerName);
            headersIterator = receivedResponse.getHeaders(headerName);
            while (headersIterator.hasNext()) {
                forgedResponse.addLast((Header) headersIterator.next().clone());
            }
        }
    }
    // Copy content
    final byte[] rawOriginal = receivedResponse.getRawContent();
    if (rawOriginal != null && rawOriginal.length != 0) {
        final byte[] copy = new byte[rawOriginal.length];
        System.arraycopy(rawOriginal, 0, copy, 0, copy.length);
        try {
            forgedResponse.setContent(copy, (ContentTypeHeader) forgedResponse.getHeader(ContentTypeHeader.NAME));
        } catch (ParseException e) {
            throw new SipException("Failed to copy content.", e);
        }
    }
    // set contact if the received response had it
    if (receivedResponse.getHeader(ContactHeader.NAME) != null) {
        final String transport = ((ViaHeader) forgedResponse.getHeader(ViaHeader.NAME)).getTransport();
        forgedResponse.setHeader(((ListeningPointImpl) sipProvider.getListeningPoint(transport)).createContactHeader());
    }
    // set the to tag mandatory so that the dialog gets replicated
    if (((MessageExt) forgedResponse).getToHeader().getTag() == null) {
        try {
            ((MessageExt) forgedResponse).getToHeader().setTag("696");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    origServerTransaction.sendResponse(forgedResponse);
}
Also used : Response(javax.sip.message.Response) MessageExt(gov.nist.javax.sip.message.MessageExt) ContactHeader(javax.sip.header.ContactHeader) ViaHeader(javax.sip.header.ViaHeader) CallIdHeader(javax.sip.header.CallIdHeader) ContentLengthHeader(javax.sip.header.ContentLengthHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) Header(javax.sip.header.Header) CSeqHeader(javax.sip.header.CSeqHeader) ContentTypeHeader(javax.sip.header.ContentTypeHeader) ToHeader(javax.sip.header.ToHeader) RouteHeader(javax.sip.header.RouteHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) EventHeader(javax.sip.header.EventHeader) FromHeader(javax.sip.header.FromHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) ViaHeader(javax.sip.header.ViaHeader) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 29 with ServerTransaction

use of javax.sip.ServerTransaction in project jain-sip.ha by RestComm.

the class SimpleStatefulProxy method forwardResponse.

/**
 * @param responseEvent
 * @throws InvalidArgumentException
 */
@SuppressWarnings("unchecked")
private void forwardResponse(Response receivedResponse) throws SipException, InvalidArgumentException {
    System.out.println("Forwarding " + receivedResponse);
    final ServerTransaction origServerTransaction = this.getServerTransaction();
    Response forgedResponse = (Response) receivedResponse.clone();
    forgedResponse.removeFirst(ViaHeader.NAME);
    origServerTransaction.sendResponse(forgedResponse);
}
Also used : Response(javax.sip.message.Response) ServerTransaction(javax.sip.ServerTransaction)

Aggregations

ServerTransaction (javax.sip.ServerTransaction)29 Response (javax.sip.message.Response)21 SipException (javax.sip.SipException)20 Request (javax.sip.message.Request)20 ParseException (java.text.ParseException)19 InvalidArgumentException (javax.sip.InvalidArgumentException)17 TransactionUnavailableException (javax.sip.TransactionUnavailableException)10 ToHeader (javax.sip.header.ToHeader)10 Dialog (javax.sip.Dialog)9 ContactHeader (javax.sip.header.ContactHeader)9 ClientTransaction (javax.sip.ClientTransaction)7 SipProvider (javax.sip.SipProvider)7 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)7 FromHeader (javax.sip.header.FromHeader)6 ReferToHeader (javax.sip.header.ReferToHeader)6 ViaHeader (javax.sip.header.ViaHeader)6 Random (java.util.Random)5 SipURI (javax.sip.address.SipURI)5 ContentTypeHeader (javax.sip.header.ContentTypeHeader)5 EventHeader (javax.sip.header.EventHeader)5