Search in sources :

Example 16 with ServerTransaction

use of javax.sip.ServerTransaction in project smscgateway by RestComm.

the class TxSipServerSbb method onMESSAGE.

// *********
// SIP Event Handlers
public void onMESSAGE(javax.sip.RequestEvent event, ActivityContextInterface aci) {
    if (this.logger.isFineEnabled()) {
        this.logger.fine("onMESSAGE " + event);
    }
    Sip sip = sipManagement.getSipByName(SipManagement.SIP_NAME);
    try {
        final Request request = event.getRequest();
        byte[] message = request.getRawContent();
        final ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
        final String toUser = ((SipUri) toHeader.getAddress().getURI()).getUser();
        final FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
        final String fromUser = ((SipUri) fromHeader.getAddress().getURI()).getUser();
        // Persist this message
        TargetAddress ta = this.createDestTargetAddress(toUser, sip.getNetworkId());
        byte[] udh = null;
        Header udhHeader = request.getHeader(SipXHeaders.XSmsUdh);
        if (udhHeader != null) {
            udh = this.hexStringToByteArray(((SIPHeader) udhHeader).getValue());
        }
        Header codingHeader = request.getHeader(SipXHeaders.XSmsCoding);
        DataCodingSchemeImpl codingSchme = dcsGsm7;
        if (codingHeader != null) {
            int dcs = Integer.parseInt(((SIPHeader) codingHeader).getValue());
            codingSchme = this.createDataCodingScheme(dcs);
        }
        Date validityPeriod = null;
        Header validityHeader = request.getHeader(SipXHeaders.XSmsValidty);
        if (validityHeader != null) {
            try {
                validityPeriod = MessageUtil.parseDate(((SIPHeader) validityHeader).getValue());
            } catch (ParseException e) {
                logger.severe("ParseException when parsing ValidityPeriod field: " + e.getMessage(), e);
                if (smscPropertiesManagement.isGenerateRejectionCdr()) {
                    generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e.getMessage(), true);
                }
                ServerTransaction serverTransaction = event.getServerTransaction();
                Response res;
                try {
                    res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
                    event.getServerTransaction().sendResponse(res);
                } catch (Exception e1) {
                    this.logger.severe("Exception while trying to send 500 response to sip", e1);
                }
                return;
            }
        }
        // Registered Delivery
        int regDeliveryInt = 0;
        Header regDeliveryHeader = request.getHeader(SipXHeaders.XRegDelivery);
        if (regDeliveryHeader != null) {
            regDeliveryInt = Integer.parseInt(((SIPHeader) regDeliveryHeader).getValue());
        }
        Sms sms = null;
        try {
            sms = this.createSmsEvent(fromUser, message, ta, persistence, udh, codingSchme, validityPeriod, regDeliveryInt, sip.getNetworkId());
            this.processSms(sms, persistence);
        } catch (SmscProcessingException e1) {
            if (!e1.isSkipErrorLogging()) {
                if (e1.isIsWarning()) {
                    this.logger.warning(e1.getMessage());
                } else {
                    this.logger.severe(e1.getMessage(), e1);
                }
                smscStatAggregator.updateMsgInFailedAll();
            }
            if (smscPropertiesManagement.isGenerateRejectionCdr() && !e1.isMessageRejectCdrCreated()) {
                if (sms != null) {
                    generateCDR(sms, CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), false, true);
                } else {
                    generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
                }
            }
            ServerTransaction serverTransaction = event.getServerTransaction();
            Response res;
            try {
                res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
                event.getServerTransaction().sendResponse(res);
            } catch (Exception e) {
                this.logger.severe("Exception while trying to send Ok response to sip", e);
            }
            return;
        } catch (Throwable e1) {
            this.logger.severe("Exception while processing a message from sip", e1);
            smscStatAggregator.updateMsgInFailedAll();
            if (smscPropertiesManagement.isGenerateRejectionCdr()) {
                generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
            }
            ServerTransaction serverTransaction = event.getServerTransaction();
            Response res;
            try {
                // TODO: we possibly need to response ERROR message to sip
                res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
                event.getServerTransaction().sendResponse(res);
            } catch (Exception e) {
                this.logger.severe("Exception while trying to send Ok response to sip", e);
            }
            return;
        }
        ServerTransaction serverTransaction = event.getServerTransaction();
        Response res;
        try {
            res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
            event.getServerTransaction().sendResponse(res);
        } catch (Exception e) {
            this.logger.severe("Exception while trying to send Ok response to sip", e);
        }
    } catch (Exception e) {
        this.logger.severe("Error while trying to process received the SMS " + event, e);
    }
}
Also used : FromHeader(javax.sip.header.FromHeader) SmscProcessingException(org.mobicents.smsc.library.SmscProcessingException) Request(javax.sip.message.Request) TargetAddress(org.mobicents.smsc.library.TargetAddress) SipUri(gov.nist.javax.sip.address.SipUri) DataCodingSchemeImpl(org.mobicents.protocols.ss7.map.smstpdu.DataCodingSchemeImpl) Date(java.util.Date) SmscProcessingException(org.mobicents.smsc.library.SmscProcessingException) ParseException(java.text.ParseException) Response(javax.sip.message.Response) ToHeader(javax.sip.header.ToHeader) FromHeader(javax.sip.header.FromHeader) SIPHeader(gov.nist.javax.sip.header.SIPHeader) Header(javax.sip.header.Header) SIPHeader(gov.nist.javax.sip.header.SIPHeader) ToHeader(javax.sip.header.ToHeader) Sms(org.mobicents.smsc.library.Sms) Sip(org.mobicents.smsc.domain.Sip) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction)

Example 17 with ServerTransaction

use of javax.sip.ServerTransaction in project Spark by igniterealtime.

the class CallProcessing method sayInternalError.

// answer call
// ------------------ Internal Error
void sayInternalError(int callID) throws CommunicationsException {
    Call call = callDispatcher.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 transaction from the call's associated dialog!");
    }
    ServerTransaction serverTransaction = (ServerTransaction) transaction;
    Response internalError = null;
    try {
        internalError = sipManCallback.messageFactory.createResponse(Response.SERVER_INTERNAL_ERROR, dialog.getFirstTransaction().getRequest());
        sipManCallback.attachToTag(internalError, dialog);
    } catch (ParseException ex) {
        call.setState(Call.DISCONNECTED);
        throw new CommunicationsException("Failed to construct an OK response to an INVITE request", ex);
    }
    ContactHeader contactHeader = sipManCallback.getContactHeader();
    internalError.addHeader(contactHeader);
    try {
        serverTransaction.sendResponse(internalError);
    } 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);
    }
}
Also used : Response(javax.sip.message.Response) ContactHeader(javax.sip.header.ContactHeader) InvalidArgumentException(javax.sip.InvalidArgumentException) ServerTransaction(javax.sip.ServerTransaction) Transaction(javax.sip.Transaction) ClientTransaction(javax.sip.ClientTransaction) Dialog(javax.sip.Dialog) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 18 with ServerTransaction

use of javax.sip.ServerTransaction in project Spark by igniterealtime.

the class CallProcessing method processCancel.

void processCancel(ServerTransaction serverTransaction, Request cancelRequest) {
    if (!serverTransaction.getDialog().getFirstTransaction().getRequest().getMethod().equals(Request.INVITE)) {
        return;
    }
    // find the call
    Call call = callDispatcher.findCall(serverTransaction.getDialog());
    if (call == null) {
        sipManCallback.fireUnknownMessageReceived(cancelRequest);
        return;
    }
    // change status
    call.setState(Call.DISCONNECTED);
    // (report and fix by Ranga)
    try {
        Response ok = sipManCallback.messageFactory.createResponse(Response.OK, cancelRequest);
        sipManCallback.attachToTag(ok, call.getDialog());
        serverTransaction.sendResponse(ok);
    } catch (ParseException ex) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to create an OK Response to an CANCEL request.", ex));
    } catch (SipException ex) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send an OK Response to an CANCEL request.", ex));
    } catch (InvalidArgumentException e) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a NOT_FOUND response to an INVITE request!", e));
    }
    try {
        // stop the invite transaction as well
        Transaction tran = call.getDialog().getFirstTransaction();
        // filtered by the stack but it doesn't hurt checking anyway
        if (!(tran instanceof ServerTransaction)) {
            sipManCallback.fireCommunicationsError(new CommunicationsException("Received a misplaced CANCEL request!"));
            return;
        }
        ServerTransaction inviteTran = (ServerTransaction) tran;
        Request invite = call.getDialog().getFirstTransaction().getRequest();
        Response requestTerminated = sipManCallback.messageFactory.createResponse(Response.REQUEST_TERMINATED, invite);
        sipManCallback.attachToTag(requestTerminated, call.getDialog());
        inviteTran.sendResponse(requestTerminated);
    } catch (ParseException ex) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to create a REQUEST_TERMINATED Response to an INVITE request.", ex));
    } catch (SipException ex) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send an REQUEST_TERMINATED Response to an INVITE request.", ex));
    } catch (InvalidArgumentException e) {
        sipManCallback.fireCommunicationsError(new CommunicationsException("Failed to send a NOT_FOUND response to an INVITE request!", e));
    }
}
Also used : Response(javax.sip.message.Response) InvalidArgumentException(javax.sip.InvalidArgumentException) ServerTransaction(javax.sip.ServerTransaction) Transaction(javax.sip.Transaction) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) ParseException(java.text.ParseException) SipException(javax.sip.SipException) ServerTransaction(javax.sip.ServerTransaction)

Example 19 with ServerTransaction

use of javax.sip.ServerTransaction 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 20 with ServerTransaction

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

the class TestSipListener method processRequest.

public void processRequest(RequestEvent requestReceivedEvent) {
    if (abortProcessing) {
        logger.error("Processing aborted");
        return;
    }
    Request request = requestReceivedEvent.getRequest();
    eventListener.uasAfterRequestReceived(request.getMethod(), appServer);
    if (firstRequest == null)
        firstRequest = request;
    ServerTransaction serverTransactionId = requestReceivedEvent.getServerTransaction();
    logger.info("\n\nRequest " + request.getMethod() + " received at " + protocolObjects.sipStack.getStackName() + " with server transaction id " + serverTransactionId + " dialog " + requestReceivedEvent.getDialog());
    if (request.getMethod().equals(Request.INVITE)) {
        processInvite(requestReceivedEvent, serverTransactionId);
    }
    if (request.getMethod().equals(Request.BYE)) {
        processBye(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.ACK)) {
        processAck(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.PRACK)) {
        processPrack(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.CANCEL)) {
        processCancel(requestReceivedEvent, serverTransactionId);
    }
    if (request.getMethod().equals(Request.MESSAGE)) {
        processMessage(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.REGISTER)) {
        processRegister(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.NOTIFY)) {
        processNotify(requestReceivedEvent, serverTransactionId);
    }
    if (request.getMethod().equals(Request.SUBSCRIBE)) {
        processSubscribe(requestReceivedEvent, serverTransactionId);
    }
    if (request.getMethod().equals(Request.UPDATE)) {
        processUpdate(request, serverTransactionId);
    }
    if (request.getMethod().equals(Request.PUBLISH)) {
        processPublish(requestReceivedEvent, serverTransactionId);
    }
    if (request.getMethod().equals(Request.REFER)) {
        processRefer(requestReceivedEvent, serverTransactionId);
    }
}
Also used : Request(javax.sip.message.Request) 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