Search in sources :

Example 1 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader 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 SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader in project camel by apache.

the class SipSubscriptionListener method processNotify.

public synchronized void processNotify(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    LOG.debug("Notification received at Subscriber");
    SipProvider provider = (SipProvider) requestEvent.getSource();
    Request notify = requestEvent.getRequest();
    try {
        if (serverTransactionId == null) {
            LOG.info("ServerTransaction is null. Creating new Server transaction");
            serverTransactionId = provider.getNewServerTransaction(notify);
        }
        Dialog dialog = serverTransactionId.getDialog();
        if (dialog != subscriberDialog) {
            forkedDialog = dialog;
        }
        //Dispatch the response along the route
        dispatchExchange(notify.getContent());
        // Send back an success response
        Response response = sipSubscriber.getConfiguration().getMessageFactory().createResponse(200, notify);
        response.addHeader(sipSubscriber.getConfiguration().getContactHeader());
        serverTransactionId.sendResponse(response);
        SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) notify.getHeader(SubscriptionStateHeader.NAME);
        // Subscription is terminated?
        if (subscriptionState.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
            LOG.info("Subscription state is terminated. Deleting the current dialog");
            dialog.delete();
        }
    } catch (Exception e) {
        LOG.error("Exception thrown during Notify processing in the SipSubscriptionListener.", e);
    }
}
Also used : Response(javax.sip.message.Response) Dialog(javax.sip.Dialog) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) Request(javax.sip.message.Request) CamelException(org.apache.camel.CamelException) SipProvider(javax.sip.SipProvider)

Example 3 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader in project Openfire by igniterealtime.

the class SimpleSession method prepareNotifyRequest.

private Request prepareNotifyRequest(Dialog dialog) throws ParseException {
    if (dialog == null) {
        return null;
    }
    printDialog(dialog);
    String fromTag = dialog.getRemoteTag();
    Address fromAddress = dialog.getRemoteParty();
    SipURI destUri = (SipURI) fromAddress.getURI();
    dialog.incrementLocalSequenceNumber();
    long seqNum = dialog.getLocalSeqNumber();
    String callId = dialog.getCallId().getCallId();
    SipURI fromReqUri = null;
    Log.debug("Getting request URI from dialog");
    Address fromReqAddr = dialog.getRemoteTarget();
    if (fromReqAddr != null && fromReqAddr.getURI() != null && fromReqAddr.getURI() instanceof SipURI)
        fromReqUri = (SipURI) fromReqAddr.getURI();
    if (fromReqUri == null) {
        Log.debug("Getting request URI from destination URI");
        fromReqUri = destUri;
    }
    // Instantiate request packet
    Request notifyRequest = prepareRequest(RequestType.NOTIFY, destUri, fromTag, fromReqUri, callId, seqNum);
    //		Request notifyRequest = dialog.createRequest(Request.NOTIFY);
    ((FromHeader) notifyRequest.getHeader(FromHeader.NAME)).setTag(dialog.getLocalTag());
    // Set "subscription state" header
    SubscriptionStateHeader subscriptionStateHeader = headerFactory.createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE.toLowerCase());
    //		if (expires > 0) subscriptionStateHeader.setExpires(expires);
    notifyRequest.setHeader(subscriptionStateHeader);
    // Set "event" header
    notifyRequest.setHeader(headerFactory.createEventHeader("presence"));
    return notifyRequest;
}
Also used : InetAddress(java.net.InetAddress) Address(javax.sip.address.Address) FromHeader(javax.sip.header.FromHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) Request(javax.sip.message.Request) SipURI(javax.sip.address.SipURI)

Example 4 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader in project camel by apache.

the class SipPresenceAgentListener method sendNotification.

private void sendNotification(EventHeader eventHeader, boolean isInitial, Object body) throws SipException, ParseException {
    /*
         * 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.
         */
    Request notifyRequest = dialog.createRequest("NOTIFY");
    // Mark the contact header, to check that the remote contact is updated
    ((SipURI) sipPresenceAgent.getConfiguration().getContactHeader().getAddress().getURI()).setParameter(sipPresenceAgent.getConfiguration().getFromUser(), sipPresenceAgent.getConfiguration().getFromHost());
    SubscriptionStateHeader sstate;
    if (isInitial) {
        // Initial state is pending, second time we assume terminated (Expires==0)
        sstate = sipPresenceAgent.getConfiguration().getHeaderFactory().createSubscriptionStateHeader(isInitial ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
        // Need a reason for terminated
        if (sstate.getState().equalsIgnoreCase("terminated")) {
            sstate.setReasonCode("deactivated");
        }
    } else {
        sstate = sipPresenceAgent.getConfiguration().getHeaderFactory().createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
    }
    notifyRequest.addHeader(sstate);
    notifyRequest.setHeader(eventHeader);
    notifyRequest.setHeader(sipPresenceAgent.getConfiguration().getContactHeader());
    notifyRequest.setContent(body, sipPresenceAgent.getConfiguration().getContentTypeHeader());
    LOG.debug("Sending the following NOTIFY request to Subscriber: {}", notifyRequest);
    ClientTransaction clientTransactionId = sipPresenceAgent.getProvider().getNewClientTransaction(notifyRequest);
    dialog.sendRequest(clientTransactionId);
}
Also used : SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) ClientTransaction(javax.sip.ClientTransaction) Request(javax.sip.message.Request) SipURI(javax.sip.address.SipURI)

Example 5 with SubscriptionStateHeader

use of javax.sip.header.SubscriptionStateHeader 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)

Aggregations

SubscriptionStateHeader (javax.sip.header.SubscriptionStateHeader)7 Request (javax.sip.message.Request)7 SipURI (javax.sip.address.SipURI)5 Dialog (javax.sip.Dialog)4 SipProvider (javax.sip.SipProvider)4 Response (javax.sip.message.Response)4 ClientTransaction (javax.sip.ClientTransaction)3 ServerTransaction (javax.sip.ServerTransaction)3 Address (javax.sip.address.Address)3 ContactHeader (javax.sip.header.ContactHeader)3 EventHeader (javax.sip.header.EventHeader)3 ExpiresHeader (javax.sip.header.ExpiresHeader)3 FromHeader (javax.sip.header.FromHeader)3 ParseException (java.text.ParseException)2 InvalidArgumentException (javax.sip.InvalidArgumentException)2 SipException (javax.sip.SipException)2 TransactionDoesNotExistException (javax.sip.TransactionDoesNotExistException)2 TransactionUnavailableException (javax.sip.TransactionUnavailableException)2 ReferToHeader (javax.sip.header.ReferToHeader)2 ToHeader (javax.sip.header.ToHeader)2