Search in sources :

Example 21 with ServerTransaction

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

the class TestSipListener method processMessage.

private void processMessage(Request request, ServerTransaction serverTransactionId) {
    if (request.toString().contains("408 received")) {
        txTimeoutReceived = true;
    }
    ServerTransaction serverTransaction = null;
    messageRequest = request;
    try {
        serverTransaction = (serverTransactionId == null ? sipProvider.getNewServerTransaction(request) : serverTransactionId);
    } catch (javax.sip.TransactionAlreadyExistsException ex) {
        ex.printStackTrace();
        return;
    } catch (javax.sip.TransactionUnavailableException ex1) {
        ex1.printStackTrace();
        return;
    }
    ContentTypeHeader contentTypeHeader = (ContentTypeHeader) request.getHeader(ContentTypeHeader.NAME);
    boolean sendInvitewithJoin = false;
    boolean sendInvitewithReplaces = false;
    if (contentTypeHeader != null) {
        if (TEXT_CONTENT_TYPE.equals(contentTypeHeader.getContentType())) {
            this.lastMessageContent = new String(request.getRawContent());
            allMessagesContent.add(new String(lastMessageContent));
            if (lastMessageContent.indexOf("Join : ") != -1) {
                this.lastMessageContent = lastMessageContent.substring("Join : ".length());
                sendInvitewithJoin = true;
            }
            if (lastMessageContent.indexOf("Replaces : ") != -1) {
                this.lastMessageContent = lastMessageContent.substring("Replaces : ".length());
                sendInvitewithReplaces = true;
            }
        }
    } else {
        if (request.getHeader("From").toString().contains("b2bua@sip-servlets"))
            b2buamessagereceived = true;
    }
    try {
        Response okResponse = protocolObjects.messageFactory.createResponse(Response.OK, request);
        ToHeader toHeader = (ToHeader) okResponse.getHeader(ToHeader.NAME);
        if (toHeader.getTag() == null) {
            toHeader.setTag(Integer.toString(new Random().nextInt(10000000)));
        }
        // okResponse.addHeader(contactHeader);
        serverTransaction.sendResponse(okResponse);
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.error("error sending OK response to message", ex);
    }
    if (sendInvitewithJoin) {
        try {
            String fromUser = "receiver";
            String fromHost = "sip-servlets.com";
            SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
            String toUser = "join-receiver";
            String toHost = "sip-servlets.com";
            SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
            String[] headerNames = new String[] { "Join" };
            String[] headerContents = new String[] { lastMessageContent };
            sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error("error sending INVITE with Join", ex);
        }
    }
    if (sendInvitewithReplaces) {
        try {
            String fromUser = "receiver";
            String fromHost = "sip-servlets.com";
            SipURI fromAddress = protocolObjects.addressFactory.createSipURI(fromUser, fromHost);
            String toUser = "replaces-receiver";
            String toHost = "sip-servlets.com";
            SipURI toAddress = protocolObjects.addressFactory.createSipURI(toUser, toHost);
            String[] headerNames = new String[] { "Replaces" };
            String[] headerContents = new String[] { lastMessageContent };
            sendSipRequest("INVITE", fromAddress, toAddress, null, null, false, headerNames, headerContents, true);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error("error sending INVITE with Join", ex);
        }
    }
}
Also used : ContentTypeHeader(javax.sip.header.ContentTypeHeader) 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) Random(java.util.Random) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ServerTransaction(javax.sip.ServerTransaction)

Example 22 with ServerTransaction

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

the class TestSipListener method processSubscribe.

/**
 * Process the invite request.
 */
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    try {
        logger.info("notifier: got an Subscribe sending OK");
        logger.info("notifier:  " + request);
        logger.info("notifier : dialog = " + requestEvent.getDialog());
        EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
        // this.gotSubscribeRequest = true;
        // Always create a ServerTransaction, best as early as possible in the code
        Response response = null;
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        // Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
        boolean isInitial = requestEvent.getDialog() == null;
        if (isInitial) {
            // JvB: need random tags to test forking
            String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
            response = protocolObjects.messageFactory.createResponse(202, request);
            ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
            // Sanity check: to header should not ahve a tag. Else the dialog
            // should have matched
            // Application is supposed to set.
            toHeader.setTag(toTag);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        } else {
            response = protocolObjects.messageFactory.createResponse(202, request);
            this.dialog = st.getDialog();
            // subscribe dialogs do not terminate on bye.
            this.dialog.terminateOnBye(false);
        }
        // Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
        Address address = null;
        if (!isIpv6)
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
        else
            address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
        ((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
        ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
        response.addHeader(contactHeader);
        // Expires header is mandatory in 2xx responses to SUBSCRIBE
        ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
        if (expires == null) {
            // rather short
            expires = protocolObjects.headerFactory.createExpiresHeader(30);
        }
        response.addHeader(expires);
        /*
			 * JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2: 
			 * "[...] a NOTIFY message is always sent immediately after any 200-
			 * class response to a SUBSCRIBE request"
			 * 
			 *  Do this before creating the NOTIFY request below
			 */
        st.sendResponse(response);
        /*
			 * NOTIFY requests MUST contain a "Subscription-State" header with a
			 * value of "active", "pending", or "terminated". The "active" value
			 * indicates that the subscription has been accepted and has been
			 * authorized (in most cases; see section 5.2.). The "pending" value
			 * indicates that the subscription has been received, but that
			 * policy information is insufficient to accept or deny the
			 * subscription at this time. The "terminated" value indicates that
			 * the subscription is not active.
			 */
        if (sendNotify) {
            Request notifyRequest = dialog.createRequest("NOTIFY");
            // Mark the contact header, to check that the remote contact is updated
            // ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
            // Initial state is pending, second time we assume terminated (Expires==0)
            SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
            allSubscriptionStates.add(sstate.getState().toLowerCase());
            // Need a reason for terminated
            if (sstate.getState().equalsIgnoreCase("terminated")) {
                sstate.setReasonCode("deactivated");
            }
            notifyRequest.addHeader(sstate);
            notifyRequest.setHeader(eventHeader);
            notifyRequest.setHeader(contactHeader);
            // notifyRequest.setHeader(routeHeader);
            ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
            if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
                Thread.sleep(timeToWaitBetweenSubsNotify);
            }
            // Let the other side know that the tx is pending acceptance
            // 
            dialog.sendRequest(ct);
            logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
            logger.info("Dialog " + dialog);
            logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
            if (expires.getExpires() != 0) {
                Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
                myEventSource.start();
            }
        }
    } catch (Throwable ex) {
        logger.info(ex.getMessage(), ex);
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) Address(javax.sip.address.Address) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) EventHeader(javax.sip.header.EventHeader) SipURI(javax.sip.address.SipURI) Response(javax.sip.message.Response) ViaHeader(javax.sip.header.ViaHeader) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) ExpiresHeader(javax.sip.header.ExpiresHeader) ServerTransaction(javax.sip.ServerTransaction) SipProvider(javax.sip.SipProvider)

Example 23 with ServerTransaction

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

the class BackToBackUserAgent method forwardResponse.

private void forwardResponse(ResponseEvent receivedResponse) throws SipException {
    try {
        ServerTransaction serverTransaction = (ServerTransaction) receivedResponse.getClientTransaction().getApplicationData();
        Request stRequest = serverTransaction.getRequest();
        Response newResponse = this.messageFactory.createResponse(receivedResponse.getResponse().getStatusCode(), stRequest);
        ListeningPoint peerListeningPoint = sp.getListeningPoint(transport);
        ContactHeader peerContactHeader = ((ListeningPointExt) peerListeningPoint).createContactHeader();
        newResponse.setHeader(peerContactHeader);
        serverTransaction.sendResponse(newResponse);
    } catch (InvalidArgumentException e) {
        throw new SipException("invalid response", e);
    } catch (ParseException e) {
        throw new SipException("invalid response", e);
    }
}
Also used : Response(javax.sip.message.Response) ContactHeader(javax.sip.header.ContactHeader) ListeningPointExt(gov.nist.javax.sip.ListeningPointExt) InvalidArgumentException(javax.sip.InvalidArgumentException) Request(javax.sip.message.Request) ListeningPoint(javax.sip.ListeningPoint) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 24 with ServerTransaction

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

the class BackToBackUserAgent method processRequest.

public void processRequest(RequestEvent requestEvent) {
    try {
        Request request = requestEvent.getRequest();
        if (request.getMethod().equals(Request.INVITE)) {
            ServerTransaction st = sp.getNewServerTransaction(requestEvent.getRequest());
            incomingDialog = st.getDialog();
            try {
                replyToRequestEvent(requestEvent.getRequest(), st, Response.TRYING);
                Request newRequest = (Request) request.clone();
                newRequest.removeHeader(RouteHeader.NAME);
                newRequest.removeHeader(RecordRouteHeader.NAME);
                FromHeader fromHeader = (FromHeader) newRequest.getHeader(FromHeader.NAME);
                fromHeader.setTag(Long.toString(Math.abs(new Random().nextLong())));
                ViaHeader viaHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createViaHeader();
                newRequest.setHeader(viaHeader);
                ContactHeader contactHeader = ((ListeningPointExt) sp.getListeningPoint(transport)).createContactHeader();
                newRequest.setHeader(contactHeader);
                SipURI route = this.protocolObjects.addressFactory.createSipURI("lbint", lbAddress + ":" + lbPort);
                route.setParameter("node_host", "127.0.0.1");
                route.setParameter("node_port", "" + port);
                route.setLrParam();
                RouteHeader routeHeader = (RouteHeader) this.protocolObjects.headerFactory.createRouteHeader(this.protocolObjects.addressFactory.createAddress(route));
                newRequest.setHeader(routeHeader);
                clientTransaction = sp.getNewClientTransaction(newRequest);
                outgoingDialog = clientTransaction.getDialog();
                clientTransaction.setApplicationData(st);
                clientTransaction.sendRequest();
            } catch (Throwable e) {
                e.printStackTrace();
                replyToRequestEvent(request, st, Response.SERVICE_UNAVAILABLE);
            }
        } else if (request.getMethod().equals(Request.BYE)) {
            ServerTransaction st = requestEvent.getServerTransaction();
            replyToRequestEvent(requestEvent.getRequest(), st, Response.OK);
            Dialog peerDialog = getPeerDialog(requestEvent.getDialog());
            Request outgoingRequest = peerDialog.createRequest(requestEvent.getRequest().getMethod());
            final ClientTransaction ct = sp.getNewClientTransaction(outgoingRequest);
            peerDialog.sendRequest(ct);
        } else if (request.getMethod().equals(Request.CANCEL)) {
            try {
                final Dialog peerDialog = outgoingDialog;
                final DialogState peerDialogState = peerDialog.getState();
                if (peerDialogState == null || peerDialogState == DialogState.EARLY) {
                    Request cancelRequest = clientTransaction.createCancel();
                    sp.sendRequest(cancelRequest);
                } else {
                    clientTransaction = sp.getNewClientTransaction(peerDialog.createRequest(Request.BYE));
                    clientTransaction.sendRequest();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) RouteHeader(javax.sip.header.RouteHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) DialogState(javax.sip.DialogState) FromHeader(javax.sip.header.FromHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) ListeningPointExt(gov.nist.javax.sip.ListeningPointExt) Random(java.util.Random) ViaHeader(javax.sip.header.ViaHeader) Dialog(javax.sip.Dialog) ServerTransaction(javax.sip.ServerTransaction)

Example 25 with ServerTransaction

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

the class TestSipListener method processCancel.

private void processCancel(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    try {
        cancelReceived = true;
        SipProvider sipProvider = (SipProvider) requestEvent.getSource();
        Request request = requestEvent.getRequest();
        Response response = protocolObjects.messageFactory.createResponse(Response.OK, request);
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        Dialog dialog = st.getDialog();
        logger.info("Shootme: dialog = " + dialog);
        st.sendResponse(response);
        response = protocolObjects.messageFactory.createResponse(Response.REQUEST_TERMINATED, inviteRequest);
        inviteServerTid.sendResponse(response);
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.error("error sending CANCEL responses", ex);
    }
}
Also used : Response(javax.sip.message.Response) Dialog(javax.sip.Dialog) Request(javax.sip.message.Request) ServerTransaction(javax.sip.ServerTransaction) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) SipProvider(javax.sip.SipProvider)

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