Search in sources :

Example 1 with ServerTransaction

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

the class SimpleListener method processRequest.

public void processRequest(RequestEvent requestEvent) {
    ServerTransaction serverTransaction = requestEvent.getServerTransaction();
    Dialog dialog = null;
    if (serverTransaction != null) {
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Getting dialog");
        dialog = serverTransaction.getDialog();
    }
    int responseCode = 200;
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  Received a request event:  \n" + requestEvent.getRequest().toString());
    String fromAddr = "";
    Request request = requestEvent.getRequest();
    if (request.getHeader(FromHeader.NAME) != null) {
        FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
        Address fromAddress = fromHeader.getAddress();
        //			String displayName = fromAddress.getDisplayName();
        URI fromUri = fromAddress.getURI();
        if (fromUri != null) {
            if (fromUri.isSipURI()) {
                SipURI fromSipUri = (SipURI) fromUri;
                fromAddr = fromSipUri.getUser() + "@" + fromSipUri.getHost();
            } else {
                fromAddr = fromUri.toString();
            }
        }
    }
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  FromAddr = " + fromAddr);
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  Request method = '" + request.getMethod() + "'");
    if (request.getMethod().equals(Request.MESSAGE)) {
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Starting MESSAGE request handling process.");
        JID senderJid = getSession().getTransport().convertIDToJID(fromAddr);
        String msgContent = new String((byte[]) request.getContent());
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Forwarding MESSAGE request as XMPP message, setting from = " + senderJid + " and content = '" + msgContent + "'");
        getSession().getTransport().sendMessage(getSession().getJID(), senderJid, msgContent);
        getSession().sendResponse(responseCode, request, serverTransaction);
    } else if (request.getMethod().equals(Request.NOTIFY)) {
        SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) request.getHeader(SubscriptionStateHeader.NAME);
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY request handling process started.");
        if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
            Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY Active!");
            int expires = subscriptionStateHeader.getExpires();
            Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY Expiry = " + expires);
            try {
                if (expires > 0) {
                    String content = "";
                    if (request.getContent() != null)
                        content = new String((byte[]) request.getContent());
                    if (content.length() > 0) {
                        SimplePresence simplePresence = SimplePresence.parseSimplePresence(content);
                        try {
                            SimpleBuddy buddy = getSession().getBuddyManager().getBuddy(getSession().getTransport().convertIDToJID(fromAddr));
                            String verboseStatus = null;
                            if (simplePresence.getTupleStatus().isOpen()) {
                                switch(simplePresence.getRpid()) {
                                    case ON_THE_PHONE:
                                        // TODO: Translate this
                                        verboseStatus = "On Phone";
                                }
                            }
                            buddy.setPresenceAndStatus(((SimpleTransport) getSession().getTransport()).convertSIPStatusToXMPP(simplePresence), verboseStatus);
                        } catch (NotFoundException e) {
                            // Not in our contact list.  Ignore.
                            Log.debug("SIMPLE: Received presense notification for contact we don't care about: " + fromAddr);
                        }
                    }
                } else {
                    Presence p = new Presence();
                    p.setType(Presence.Type.unsubscribed);
                    p.setTo(getSession().getJID());
                    p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
                    getSession().getTransport().sendPacket(p);
                }
                Log.debug("SimpleListener(" + myUsername + ").processRequest:  Sending XMPP presence packet.");
            } catch (Exception ex) {
                Log.debug("SimpleListener(" + myUsername + ").processRequest:  Exception occured when processing NOTIFY packet...", ex);
            }
        } else if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
            Presence p = new Presence();
            p.setType(Presence.Type.unsubscribed);
            p.setTo(getSession().getJID());
            p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
            getSession().getTransport().sendPacket(p);
        }
        getSession().sendResponse(responseCode, request, serverTransaction);
    } else if (request.getMethod().equals(Request.SUBSCRIBE)) {
        Log.debug("SimpleListener for " + myUsername + ":  SUBSCRIBE request handling process.");
        ServerTransaction transaction = getSession().sendResponse(202, request, serverTransaction);
        Log.debug("SimpleListener for " + myUsername + ":  SUBSCRIBE should be followed by a NOTIFY");
        // Send NOTIFY packet.
        try {
            if (transaction != null)
                getSession().sendNotify(transaction.getDialog());
            else
                getSession().sendNotify(dialog);
        } catch (Exception e) {
            Log.debug("SimpleListener for " + myUsername + ":  Unable to prepare NOTIFY packet.", e);
        }
    }
}
Also used : Address(javax.sip.address.Address) JID(org.xmpp.packet.JID) FromHeader(javax.sip.header.FromHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) Request(javax.sip.message.Request) NotFoundException(org.jivesoftware.util.NotFoundException) SipURI(javax.sip.address.SipURI) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) NotFoundException(org.jivesoftware.util.NotFoundException) Dialog(javax.sip.Dialog) Presence(org.xmpp.packet.Presence) ServerTransaction(javax.sip.ServerTransaction)

Example 2 with ServerTransaction

use of javax.sip.ServerTransaction in project XobotOS by xamarin.

the class SipProviderImpl method getNewDialog.

/*
     * (non-Javadoc)
     *
     * @see javax.sip.SipProvider#getNewDialog(javax.sip.Transaction)
     */
public Dialog getNewDialog(Transaction transaction) throws SipException {
    if (transaction == null)
        throw new NullPointerException("Null transaction!");
    if (!sipStack.isAlive())
        throw new SipException("Stack is stopped.");
    if (isAutomaticDialogSupportEnabled())
        throw new SipException(" Error - AUTOMATIC_DIALOG_SUPPORT is on");
    if (!sipStack.isDialogCreated(transaction.getRequest().getMethod()))
        throw new SipException("Dialog cannot be created for this method " + transaction.getRequest().getMethod());
    SIPDialog dialog = null;
    SIPTransaction sipTransaction = (SIPTransaction) transaction;
    if (transaction instanceof ServerTransaction) {
        SIPServerTransaction st = (SIPServerTransaction) transaction;
        Response response = st.getLastResponse();
        if (response != null) {
            if (response.getStatusCode() != 100)
                throw new SipException("Cannot set dialog after response has been sent");
        }
        SIPRequest sipRequest = (SIPRequest) transaction.getRequest();
        String dialogId = sipRequest.getDialogId(true);
        dialog = sipStack.getDialog(dialogId);
        if (dialog == null) {
            dialog = sipStack.createDialog((SIPTransaction) transaction);
            // create and register the dialog and add the inital route set.
            dialog.addTransaction(sipTransaction);
            dialog.addRoute(sipRequest);
            sipTransaction.setDialog(dialog, null);
        } else {
            sipTransaction.setDialog(dialog, sipRequest.getDialogId(true));
        }
        if (sipRequest.getMethod().equals(Request.INVITE) && this.isDialogErrorsAutomaticallyHandled()) {
            sipStack.putInMergeTable(st, sipRequest);
        }
    } else {
        SIPClientTransaction sipClientTx = (SIPClientTransaction) transaction;
        SIPResponse response = sipClientTx.getLastResponse();
        if (response == null) {
            // A response has not yet been received, then set this up as the
            // default dialog.
            SIPRequest request = (SIPRequest) sipClientTx.getRequest();
            String dialogId = request.getDialogId(false);
            dialog = sipStack.getDialog(dialogId);
            if (dialog != null) {
                throw new SipException("Dialog already exists!");
            } else {
                dialog = sipStack.createDialog(sipTransaction);
            }
            sipClientTx.setDialog(dialog, null);
        } else {
            throw new SipException("Cannot call this method after response is received!");
        }
    }
    dialog.addEventListener(this);
    return dialog;
}
Also used : Response(javax.sip.message.Response) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPClientTransaction(gov.nist.javax.sip.stack.SIPClientTransaction) SIPResponse(gov.nist.javax.sip.message.SIPResponse) SIPDialog(gov.nist.javax.sip.stack.SIPDialog) SIPTransaction(gov.nist.javax.sip.stack.SIPTransaction) SipException(javax.sip.SipException) ServerTransaction(javax.sip.ServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPServerTransaction(gov.nist.javax.sip.stack.SIPServerTransaction) SIPRequest(gov.nist.javax.sip.message.SIPRequest)

Example 3 with ServerTransaction

use of javax.sip.ServerTransaction in project XobotOS by xamarin.

the class SipHelper method sendRinging.

/**
     * @param event the INVITE request event
     */
public ServerTransaction sendRinging(RequestEvent event, String tag) throws SipException {
    try {
        Request request = event.getRequest();
        ServerTransaction transaction = getServerTransaction(event);
        Response response = mMessageFactory.createResponse(Response.RINGING, request);
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        toHeader.setTag(tag);
        response.addHeader(toHeader);
        if (DEBUG)
            Log.d(TAG, "send RINGING: " + response);
        transaction.sendResponse(response);
        return transaction;
    } catch (ParseException e) {
        throw new SipException("sendRinging()", e);
    }
}
Also used : Response(javax.sip.message.Response) Request(javax.sip.message.Request) ToHeader(javax.sip.header.ToHeader) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 4 with ServerTransaction

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

the class SipManager method processRequest.

// error occurred
// ============================= SIP LISTENER METHODS ==============================
public void processRequest(RequestEvent requestReceivedEvent) {
    Log.debug(requestReceivedEvent.getRequest().toString());
    ServerTransaction serverTransaction = requestReceivedEvent.getServerTransaction();
    Request request = requestReceivedEvent.getRequest();
    if (serverTransaction == null) {
        try {
            serverTransaction = sipProvider.getNewServerTransaction(request);
        } catch (TransactionAlreadyExistsException ex) {
            return;
        } catch (TransactionUnavailableException ex) {
            return;
        }
    }
    Dialog dialog = serverTransaction.getDialog();
    if (request.getMethod().equals(Request.NOTIFY)) {
        Response ok = null;
        try {
            ok = messageFactory.createResponse(Response.OK, request);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        ContactHeader contactHeader = null;
        try {
            contactHeader = getContactHeader();
            ok.addHeader(contactHeader);
            attachToTag(ok, dialog);
            System.err.println(ok.toString());
            serverTransaction.sendResponse(ok);
        } catch (CommunicationsException e) {
            e.printStackTrace();
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
        } catch (SipException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        fireMessageReceived(request);
        return;
    }
    // TODO REMOVE
    @SuppressWarnings("unused") Request requestClone = (Request) request.clone();
    // INVITE
    if (request.getMethod().equals(Request.INVITE)) {
        if (serverTransaction.getDialog().getState() == null) {
            callProcessing.processInvite(serverTransaction, request);
        } else {
            callProcessing.processReInvite(serverTransaction, request);
        }
    } else // ACK
    if (request.getMethod().equals(Request.ACK)) {
        if (serverTransaction != null && serverTransaction.getDialog().getFirstTransaction().getRequest().getMethod().equals(Request.INVITE)) {
            callProcessing.processAck(serverTransaction, request);
        } else {
        // just ignore
        }
    } else // BYE
    if (request.getMethod().equals(Request.BYE)) {
        if (dialog.getFirstTransaction().getRequest().getMethod().equals(Request.INVITE) || dialog.getFirstTransaction().getRequest().getMethod().equals(Request.REFER)) {
            callProcessing.processBye(serverTransaction, request);
        }
    } else // CANCEL
    if (request.getMethod().equals(Request.CANCEL)) {
        if (dialog.getFirstTransaction().getRequest().getMethod().equals(Request.INVITE)) {
            callProcessing.processCancel(serverTransaction, request);
        } else {
            sendNotImplemented(serverTransaction, request);
            fireUnknownMessageReceived(requestReceivedEvent.getRequest());
        }
    } else // REFER
    if (request.getMethod().equals(Request.REFER)) {
        transferProcessing.processRefer(serverTransaction, request);
    } else if (request.getMethod().equals(Request.INFO)) {
        /**
         * @todo add proper request handling
         */
        fireMessageReceived(request);
    } else if (request.getMethod().equals(Request.MESSAGE)) {
        messageProcessing.processMessageRequest(serverTransaction, request);
        fireMessageReceived(request);
    } else if (request.getMethod().equals(Request.OPTIONS)) {
        /**
         * @todo add proper request handling
         */
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    } else if (request.getMethod().equals(Request.PRACK)) {
        /**
         * @todo add proper request handling
         */
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    } else if (request.getMethod().equals(Request.REGISTER)) {
        /**
         * @todo add proper request handling
         */
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    } else if (request.getMethod().equals(Request.SUBSCRIBE)) {
    } else if (request.getMethod().equals(Request.UPDATE)) {
        /**
         * @todo add proper request handling
         */
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    } else if (request.getMethod().equals(Request.INFO)) {
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    } else {
        // We couldn't recognise the message
        sendNotImplemented(serverTransaction, request);
        fireUnknownMessageReceived(requestReceivedEvent.getRequest());
    }
}
Also used : ContactHeader(javax.sip.header.ContactHeader) Request(javax.sip.message.Request) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) TransportNotSupportedException(javax.sip.TransportNotSupportedException) ObjectInUseException(javax.sip.ObjectInUseException) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) PeerUnavailableException(javax.sip.PeerUnavailableException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) Response(javax.sip.message.Response) InvalidArgumentException(javax.sip.InvalidArgumentException) Dialog(javax.sip.Dialog) TransactionAlreadyExistsException(javax.sip.TransactionAlreadyExistsException) TransactionUnavailableException(javax.sip.TransactionUnavailableException) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

Example 5 with ServerTransaction

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

the class CallProcessing method sayBusyHere.

// cancel
// busy here
private void sayBusyHere(Dialog dialog) throws CommunicationsException {
    Request request = dialog.getFirstTransaction().getRequest();
    Response busyHere = null;
    try {
        busyHere = sipManCallback.messageFactory.createResponse(Response.BUSY_HERE, request);
        sipManCallback.attachToTag(busyHere, dialog);
    } catch (ParseException ex) {
        throw new CommunicationsException("Failed to create the BUSY_HERE response!", ex);
    }
    if (!dialog.isServer()) {
        throw new CommunicationsException("Cannot send BUSY_HERE in a client transaction");
    }
    ServerTransaction serverTransaction = (ServerTransaction) dialog.getFirstTransaction();
    try {
        serverTransaction.sendResponse(busyHere);
    } catch (SipException ex) {
        throw new CommunicationsException("Failed to send the BUSY_HERE response", ex);
    } catch (InvalidArgumentException e) {
        throw new CommunicationsException("Failed to send the BUSY_HERE response", e);
    }
}
Also used : Response(javax.sip.message.Response) InvalidArgumentException(javax.sip.InvalidArgumentException) Request(javax.sip.message.Request) ParseException(java.text.ParseException) ServerTransaction(javax.sip.ServerTransaction) SipException(javax.sip.SipException)

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