Search in sources :

Example 1 with SIPETagHeader

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

the class TestSipListener method processResponse.

public void processResponse(ResponseEvent responseReceivedEvent) {
    if (abortProcessing) {
        logger.error("Processing aborted");
        return;
    }
    Response response = (Response) responseReceivedEvent.getResponse();
    System.out.println("Process response : " + response);
    eventListener.uacAfterResponse(response.getStatusCode(), appServer);
    if (response.getStatusCode() == 491)
        numberOf491s++;
    RecordRouteHeader recordRouteHeader = (RecordRouteHeader) response.getHeader(RecordRouteHeader.NAME);
    ContactHeader contactHeader = (ContactHeader) response.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 a response !");
    }
    if (response.getStatusCode() >= 400 && response.getStatusCode() < 999) {
        this.serverErrorReceived = true;
    }
    if (response.getStatusCode() == 503) {
        this.serviceUnavailableReceived = true;
    }
    if (response.toString().toLowerCase().contains("info")) {
        lastInfoResponseTime = System.currentTimeMillis();
    }
    ClientTransaction tid = responseReceivedEvent.getClientTransaction();
    CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
    logger.info("Response received : Status Code = " + response.getStatusCode() + " " + cseq);
    // not dropping in PRACK case on REINVITE the ClientTx can be null it seems
    if (tid == null && !prackSent) {
        if (countRetrans) {
            nbRetrans++;
        }
        logger.info("Stray response -- dropping ");
        return;
    }
    if (tid != null) {
        logger.info("transaction state is " + tid.getState());
        logger.info("Dialog = " + tid.getDialog());
        if (tid.getDialog() != null) {
            logger.info("Dialog State is " + tid.getDialog().getState());
        }
    }
    try {
        if (response.getStatusCode() > 100 && response.getStatusCode() < 200) {
            informationalResponse = response;
        }
        if (response.getStatusCode() >= 200 && response.getStatusCode() < 700) {
            logger.info("final response received : status code " + response.getStatusCode());
            finalResponseReceived = true;
            setFinalResponseStatus(response.getStatusCode());
            setFinalResponse(response);
        }
        if (response.getStatusCode() == Response.OK) {
            if (cseq.getMethod().equals(Request.INVITE) && sendAck) {
                inviteOkResponse = response;
                @SuppressWarnings("deprecation") Request ackRequest = responseReceivedEvent.getClientTransaction().createAck();
                if (useToURIasRequestUri) {
                    ackRequest.setRequestURI(requestURI);
                }
                if (timeToWaitBeforeAck > 0) {
                    Thread.sleep(timeToWaitBeforeAck);
                }
                if (!sendSubsequentRequestsThroughSipProvider && tid.getDialog() != null) {
                    tid.getDialog().sendAck(ackRequest);
                } else {
                    sipProvider.sendRequest(ackRequest);
                }
                ackSent = true;
                // If the caller is supposed to send the bye
                if (sendReinvite && !reinviteSent) {
                    List<Header> headers = new ArrayList<Header>();
                    Header reinviteHeader = protocolObjects.headerFactory.createHeader("ReInvite", "true");
                    headers.add(reinviteHeader);
                    if (prackSent) {
                        headers.add(protocolObjects.headerFactory.createHeader(RequireHeader.NAME, "100rel"));
                    }
                    sendInDialogSipRequest("INVITE", null, null, null, headers, null);
                    reinviteSent = true;
                    return;
                }
                if (sendBye) {
                    // Thread.sleep(30000);
                    sendBye();
                }
                if (sendByeAfterTerminatingNotify) {
                    tid.getDialog().terminateOnBye(false);
                }
            } else if (cseq.getMethod().equals(Request.BYE)) {
                okToByeReceived = true;
            } else if (cseq.getMethod().equals(Request.CANCEL)) {
                this.cancelOkReceived = true;
            // if (tid.getDialog().getState() == DialogState.CONFIRMED) {
            // // oops cancel went in too late. Need to hang up the
            // // dialog.
            // logger.info("Sending BYE -- cancel went in too late !!");
            // Request byeRequest = dialog.createRequest(Request.BYE);
            // ClientTransaction ct = sipProvider
            // .getNewClientTransaction(byeRequest);
            // tid.getDialog().sendRequest(ct);
            // }
            } else if (cseq.getMethod().equals(Request.PUBLISH)) {
                SIPETagHeader sipTagHeader = (SIPETagHeader) response.getHeader(SIPETag.NAME);
                sipETag = sipTagHeader.getETag();
            } else if (cseq.getMethod().equals(Request.PRACK)) {
                okToPrackReceived = true;
            }
        } else if (response.getStatusCode() == Response.MOVED_TEMPORARILY) {
            // Dialog dies as soon as you get an error response.
            if (cseq.getMethod().equals(Request.INVITE)) {
                // lookup the contact header
                ContactHeader contHdr = (ContactHeader) response.getHeader(ContactHeader.NAME);
                // we can re-use the from header
                FromHeader from = ((FromHeader) response.getHeader(FromHeader.NAME));
                // we use the to-address, but without the tag
                ToHeader to = (ToHeader) (response.getHeader(ToHeader.NAME)).clone();
                to.removeParameter("tag");
                // the call-id can be re-used
                CallIdHeader callID = ((CallIdHeader) response.getHeader(CallIdHeader.NAME));
                // we take the next cseq
                long seqNo = (((CSeqHeader) response.getHeader(CSeqHeader.NAME)).getSeqNumber());
                logger.info("seqNo = " + seqNo);
                CSeqHeader cseqNew = protocolObjects.headerFactory.createCSeqHeader(++seqNo, "INVITE");
                // Create ViaHeaders (either use tcp or udp)
                ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
                ViaHeader viaHeader = null;
                if (!isIpv6)
                    viaHeader = protocolObjects.headerFactory.createViaHeader("127.0.0.1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), protocolObjects.transport, null);
                else
                    viaHeader = protocolObjects.headerFactory.createViaHeader("::1", sipProvider.getListeningPoint(protocolObjects.transport).getPort(), protocolObjects.transport, null);
                // add via headers
                viaHeaders.add(viaHeader);
                // create max forwards
                MaxForwardsHeader maxForwardsHeader = protocolObjects.headerFactory.createMaxForwardsHeader(10);
                // create invite Request
                SipURI newUri = (SipURI) this.requestURI.clone();
                newUri.setParameter("redirection", "true");
                requestURI = newUri;
                Request invRequest = protocolObjects.messageFactory.createRequest(newUri, "INVITE", callID, cseqNew, from, to, viaHeaders, maxForwardsHeader);
                // we set the Request URI to the address given
                SipURI contactURI = protocolObjects.addressFactory.createSipURI(null, this.listeningPoint.getIPAddress());
                contactURI.setPort(this.listeningPoint.getPort());
                contactURI.setTransportParam(protocolObjects.transport);
                Address address = protocolObjects.addressFactory.createAddress(contactURI);
                ContactHeader contact = protocolObjects.headerFactory.createContactHeader(address);
                invRequest.addHeader(contact);
                // the contacat header in the response contains where to redirect
                // the request to -- which in this case happens to be back to the
                // same location.
                ContactHeader chdr = (ContactHeader) response.getHeader(ContactHeader.NAME);
                SipURI sipUri = (SipURI) chdr.getAddress().getURI();
                // sipUri.setLrParam();
                RouteHeader routeHeader = protocolObjects.headerFactory.createRouteHeader(chdr.getAddress());
                invRequest.addHeader(routeHeader);
                invRequest.setRequestURI(sipUri);
                logger.info("Sending INVITE to " + contHdr.getAddress().getURI().toString());
                inviteClientTid = sipProvider.getNewClientTransaction(invRequest);
                logger.info("New TID = " + inviteClientTid);
                inviteClientTid.sendRequest();
                logger.info("sendReqeust succeeded " + inviteClientTid);
                Dialog dialog = inviteClientTid.getDialog();
                this.dialogCount++;
                this.dialog = dialog;
            }
        } else if (response.getStatusCode() == Response.REQUEST_TERMINATED) {
            if (cseq.getMethod().equals(Request.INVITE)) {
                this.requestTerminatedReceived = true;
            }
        } else if (response.getStatusCode() == Response.RINGING && sendUpdateOn180) {
            Request updateRequest = dialog.createRequest(Request.UPDATE);
            ClientTransaction ct = sipProvider.getNewClientTransaction(updateRequest);
            dialog.sendRequest(ct);
        } else if (response.getStatusCode() > Response.TRYING && response.getStatusCode() < Response.OK) {
            RequireHeader requireHeader = (RequireHeader) response.getHeader(RequireHeader.NAME);
            if (requireHeader != null && "100rel".equalsIgnoreCase(requireHeader.getOptionTag().trim())) {
                Request prack = dialog.createPrack(response);
                ClientTransaction ct = sipProvider.getNewClientTransaction(prack);
                dialog.sendRequest(ct);
                prackSent = true;
            }
        }
    /**
     * end of modified code
     */
    } catch (Exception ex) {
        logger.error("An unexpected exception occured while processing the response", ex);
    }
}
Also used : MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ContactHeader(javax.sip.header.ContactHeader) RouteHeader(javax.sip.header.RouteHeader) RecordRouteHeader(javax.sip.header.RecordRouteHeader) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ArrayList(java.util.ArrayList) RecordRouteHeader(javax.sip.header.RecordRouteHeader) 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) CSeqHeader(javax.sip.header.CSeqHeader) 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) SIPETagHeader(javax.sip.header.SIPETagHeader) ViaHeader(javax.sip.header.ViaHeader) Dialog(javax.sip.Dialog) ReferToHeader(javax.sip.header.ReferToHeader) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader)

Example 2 with SIPETagHeader

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

the class TestSipListener method processPublish.

private void processPublish(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    try {
        SipProvider sipProvider = (SipProvider) requestEvent.getSource();
        Request request = requestEvent.getRequest();
        logger.info("shootist:  got a publish . 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);
        if (request.getRawContent() != null) {
            this.lastMessageContent = new String(request.getRawContent());
            allMessagesContent.add(new String(lastMessageContent));
        }
        SIPIfMatchHeader sipIfMatchHeader = (SIPIfMatchHeader) request.getHeader(SIPIfMatchHeader.NAME);
        boolean sipIfMatchFound = true;
        if (sipIfMatchHeader != null && sipIfMatchHeader.getETag() != null && !sipIfMatchHeader.getETag().equals(sipETag)) {
            sipIfMatchFound = false;
        }
        if (sipIfMatchFound) {
            Response response = protocolObjects.messageFactory.createResponse(200, request);
            sipETag = Integer.toString(new Random().nextInt(10000000));
            SIPETagHeader sipTagHeader = protocolObjects.headerFactory.createSIPETagHeader(sipETag);
            response.addHeader(sipTagHeader);
            response.addHeader(request.getHeader(ExpiresHeader.NAME));
            st.sendResponse(response);
            logger.info("shootist:  Sending OK.");
        } else {
            Response response = protocolObjects.messageFactory.createResponse(500, request);
            serverTransactionId.sendResponse(response);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Response(javax.sip.message.Response) Random(java.util.Random) SIPETagHeader(javax.sip.header.SIPETagHeader) Dialog(javax.sip.Dialog) Request(javax.sip.message.Request) ServerTransaction(javax.sip.ServerTransaction) SIPIfMatchHeader(javax.sip.header.SIPIfMatchHeader) 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

ParseException (java.text.ParseException)2 Dialog (javax.sip.Dialog)2 InvalidArgumentException (javax.sip.InvalidArgumentException)2 SipException (javax.sip.SipException)2 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)2 TransactionUnavailableException (javax.sip.TransactionUnavailableException)2 SIPETagHeader (javax.sip.header.SIPETagHeader)2 SIPIfMatchHeader (javax.sip.header.SIPIfMatchHeader)2 Request (javax.sip.message.Request)2 Response (javax.sip.message.Response)2 JoinHeader (gov.nist.javax.sip.header.extensions.JoinHeader)1 ReplacesHeader (gov.nist.javax.sip.header.extensions.ReplacesHeader)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ClientTransaction (javax.sip.ClientTransaction)1 ServerTransaction (javax.sip.ServerTransaction)1 SipProvider (javax.sip.SipProvider)1 Address (javax.sip.address.Address)1 SipURI (javax.sip.address.SipURI)1 AuthorizationHeader (javax.sip.header.AuthorizationHeader)1