Search in sources :

Example 51 with Response

use of javax.sip.message.Response in project Spark by igniterealtime.

the class TransferProcessing method sayOK.

public void sayOK(int callID) throws CommunicationsException {
    try {
        Call call = callProcessing.getCallDispatcher().getCall(callID);
        if (call == null) {
            throw new CommunicationsException("Failed to find call with id=" + callID);
        }
        Dialog dialog = call.getDialog();
        if (dialog == null) {
            call.setState(Call.DISCONNECTED);
            throw new CommunicationsException("Failed to extract call's associated dialog! Ending Call!");
        }
        Transaction transaction = dialog.getFirstTransaction();
        if (transaction == null || !dialog.isServer()) {
            call.setState(Call.DISCONNECTED);
            throw new CommunicationsException("Failed to extract a ServerTransaction " + "from the call's associated dialog!");
        }
        ServerTransaction serverTransaction = (ServerTransaction) transaction;
        Response ok = null;
        try {
            ok = sipManCallback.messageFactory.createResponse(Response.OK, dialog.getFirstTransaction().getRequest());
            sipManCallback.attachToTag(ok, dialog);
        } catch (ParseException ex) {
            call.setState(Call.DISCONNECTED);
            throw new CommunicationsException("Failed to construct an OK response to an INVITE request", ex);
        }
        // and should probably be removed
        if (((ToHeader) ok.getHeader(ToHeader.NAME)).getTag() == null) {
            try {
                ((ToHeader) ok.getHeader(ToHeader.NAME)).setTag(Integer.toString(dialog.hashCode()));
            } catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException("Unable to set to tag", ex);
            }
        }
        ContactHeader contactHeader = sipManCallback.getContactHeader();
        ok.addHeader(contactHeader);
        try {
            serverTransaction.sendResponse(ok);
        } catch (SipException ex) {
            call.setState(Call.DISCONNECTED);
            throw new CommunicationsException("Failed to send an OK response to an INVITE request", ex);
        } catch (InvalidArgumentException e) {
            call.setState(Call.DISCONNECTED);
            throw new CommunicationsException("Failed to send an OK response to an INVITE request", e);
        }
    } finally {
    }
}
Also used : Response(javax.sip.message.Response) ContactHeader(javax.sip.header.ContactHeader) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) ParseException(java.text.ParseException)

Example 52 with Response

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

the class TestSipListener method processRefer.

public void processRefer(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    try {
        SipProvider sipProvider = (SipProvider) requestEvent.getSource();
        Request request = requestEvent.getRequest();
        logger.info("shootist:  got a refer . ServerTxId = " + serverTransactionId);
        ServerTransaction st = requestEvent.getServerTransaction();
        if (st == null) {
            st = sipProvider.getNewServerTransaction(request);
        }
        inviteServerTid = st;
        Dialog dialog = st.getDialog();
        this.dialogCount++;
        this.dialog = dialog;
        logger.info("Shootme: dialog = " + dialog);
        Response response = protocolObjects.messageFactory.createResponse(referResponseToSend, request);
        sipETag = Integer.toString(new Random().nextInt(10000000));
        st.sendResponse(response);
        logger.info("shootist:  Sending " + referResponseToSend);
        List<Header> headers = new ArrayList<Header>();
        EventHeader eventHeader = (EventHeader) protocolObjects.headerFactory.createHeader(EventHeader.NAME, "Refer");
        headers.add(eventHeader);
        if (sendNotifyForRefer) {
            if (!referReceived) {
                referReceived = true;
                SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
                headers.add(subscriptionStateHeader);
                allMessagesContent.add("SIP/2.0 100 Trying");
                sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Trying", "message", "sipfrag;version=2.0", headers, null);
                Thread.sleep(1000);
                headers.remove(subscriptionStateHeader);
                subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "terminated;reason=noresource");
                headers.add(subscriptionStateHeader);
                if (inviteRequest == null) {
                    ExtensionHeader extensionHeader = (ExtensionHeader) protocolObjects.headerFactory.createHeader("Out-Of-Dialog", "true");
                    headers.add(extensionHeader);
                }
                allMessagesContent.add("SIP/2.0 200 OK");
                sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 200 OK", "message", "sipfrag;version=2.0", headers, null);
            } else {
                SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) protocolObjects.headerFactory.createHeader(SubscriptionStateHeader.NAME, "active;expires=3600");
                headers.add(subscriptionStateHeader);
                sendInDialogSipRequest(Request.NOTIFY, "SIP/2.0 100 Subsequent", "message", "sipfrag;version=2.0", headers, null);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : ExtensionHeader(javax.sip.header.ExtensionHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) Request(javax.sip.message.Request) ArrayList(java.util.ArrayList) EventHeader(javax.sip.header.EventHeader) 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) 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) Dialog(javax.sip.Dialog) ServerTransaction(javax.sip.ServerTransaction) SipProvider(javax.sip.SipProvider)

Example 53 with Response

use of javax.sip.message.Response 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 54 with Response

use of javax.sip.message.Response 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 55 with Response

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

the class TestSipListener method processUpdate.

public void processUpdate(Request request, ServerTransaction serverTransactionId) {
    try {
        logger.info("shootist:  got a update. ServerTxId = " + serverTransactionId);
        this.byeReceived = true;
        if (serverTransactionId == null) {
            logger.info("shootist:  null TID.");
            return;
        }
        Dialog dialog = serverTransactionId.getDialog();
        logger.info("Dialog State = " + dialog.getState());
        Response response = protocolObjects.messageFactory.createResponse(200, request);
        serverTransactionId.sendResponse(response);
        logger.info("shootist:  Sending OK.");
        logger.info("Dialog State = " + dialog.getState());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Response(javax.sip.message.Response) Dialog(javax.sip.Dialog) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) SipException(javax.sip.SipException) TransactionDoesNotExistException(javax.sip.TransactionDoesNotExistException) TransactionUnavailableException(javax.sip.TransactionUnavailableException)

Aggregations

Response (javax.sip.message.Response)77 ParseException (java.text.ParseException)42 SipException (javax.sip.SipException)41 InvalidArgumentException (javax.sip.InvalidArgumentException)32 Request (javax.sip.message.Request)28 ServerTransaction (javax.sip.ServerTransaction)22 TransactionUnavailableException (javax.sip.TransactionUnavailableException)20 Dialog (javax.sip.Dialog)18 ToHeader (javax.sip.header.ToHeader)17 Test (org.junit.Test)17 ListeningPoint (javax.sip.ListeningPoint)16 ContactHeader (javax.sip.header.ContactHeader)16 SipURI (javax.sip.address.SipURI)13 SIPResponse (gov.nist.javax.sip.message.SIPResponse)12 ClientTransaction (javax.sip.ClientTransaction)12 SipProvider (javax.sip.SipProvider)11 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)11 ViaHeader (javax.sip.header.ViaHeader)11 FromHeader (javax.sip.header.FromHeader)10 CSeqHeader (javax.sip.header.CSeqHeader)9