Search in sources :

Example 1 with Request

use of javax.sip.message.Request 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 Request

use of javax.sip.message.Request in project Openfire by igniterealtime.

the class SimpleSession method prepareRequest.

/**
     * @param requestType Type of request
	 * @param destUri    The SipURI for the destination.  Leave <code>null</code> if a loopback request (e.g. REGISTER) is being made.
	 * @param toTag      The tag for to header.  Can leave null.
	 * @param requestUri The Request URI to set in the message.  Leave null if the default destination SipURI should be used.
     * @param callId     ID of call
     * @param seqNum     Sequence number
     * @return Prepared request
	 */
private Request prepareRequest(RequestType requestType, SipURI destUri, String toTag, SipURI requestUri, String callId, long seqNum) {
    Request request = null;
    String myXMPPUsername = this.jid.getNode();
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing request packet of type '" + requestType + "'");
    try {
        // Prepare request packet first
        request = messageFactory.createRequest(null);
        request.setMethod(requestType.toString());
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing request.", e);
    }
    // Prepare "From" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"From\" header...");
    String mySipUsername = registration.getUsername();
    try {
        SipURI fromUri = addressFactory.createSipURI(mySipUsername, sipHost);
        Address fromNameAddress = addressFactory.createAddress(fromUri);
        fromNameAddress.setDisplayName(mySipUsername);
        FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, getTag());
        // Use "set" because this header is mandatory.
        request.setHeader(fromHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing FromHeader.", e);
        return null;
    }
    // Prepare "To" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"To\" header...");
    try {
        if (destUri == null)
            destUri = addressFactory.createSipURI(mySipUsername, sipHost);
        Address toNameAddress = addressFactory.createAddress(destUri);
        ToHeader toHeader = headerFactory.createToHeader(toNameAddress, toTag);
        // Use "set" because this header is mandatory.
        request.setHeader(toHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing ToHeader.", e);
        return null;
    }
    // Prepare "Via" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"Via\" header...");
    try {
        ViaHeader viaHeader = headerFactory.createViaHeader(InetAddress.getLocalHost().getHostAddress(), sipPort, ListeningPoint.UDP, null);
        // Use "set" because this header is mandatory.
        request.setHeader(viaHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing ViaHeader.", e);
        return null;
    }
    // Prepare "CallId" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"CallId\" header...");
    CallIdHeader callIdHeader;
    try {
        if (callId != null)
            callIdHeader = headerFactory.createCallIdHeader(callId);
        else
            callIdHeader = udpSipProvider.getNewCallId();
        // Use "set" because this header is mandatory.
        request.setHeader(callIdHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing CallIdHeader.", e);
        return null;
    }
    // Prepare "CSeq" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"CSeq\" header...");
    try {
        CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(seqNum, requestType.toString());
        // Use "set" because this header is mandatory.
        request.setHeader(cSeqHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing CSeqHeader.", e);
        return null;
    }
    // Prepare "MaxForwards" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"MaxForwards\" header...");
    try {
        MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
        // Use "set" because this header is mandatory.
        request.setHeader(maxForwardsHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when preparing MaxForwardsHeader.", e);
        return null;
    }
    // Setting Request URI
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  setting request URI...");
    try {
        if (requestUri == null) {
            requestUri = (SipURI) destUri.clone();
            requestUri.setTransportParam(ListeningPoint.UDP);
        }
        request.setRequestURI(requestUri);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when setting request URI.", e);
        return null;
    }
    // Add "Contact" header
    Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Preparing \"Contact\" header...");
    try {
        SipURI contactURI = addressFactory.createSipURI(mySipUsername, InetAddress.getLocalHost().getHostAddress());
        contactURI.setPort(sipPort);
        Address contactAddress = addressFactory.createAddress(contactURI);
        contactAddress.setDisplayName(mySipUsername);
        ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
        request.setHeader(contactHeader);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest:  Exception occured when adding ContactHeader.", e);
        return null;
    }
    return request;
}
Also used : CSeqHeader(javax.sip.header.CSeqHeader) MaxForwardsHeader(javax.sip.header.MaxForwardsHeader) ContactHeader(javax.sip.header.ContactHeader) InetAddress(java.net.InetAddress) Address(javax.sip.address.Address) ViaHeader(javax.sip.header.ViaHeader) FromHeader(javax.sip.header.FromHeader) Request(javax.sip.message.Request) ToHeader(javax.sip.header.ToHeader) CallIdHeader(javax.sip.header.CallIdHeader) SipURI(javax.sip.address.SipURI) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 3 with Request

use of javax.sip.message.Request in project Openfire by igniterealtime.

the class SimpleSession method logIn.

// The following are SimpleSession specific methods
/**
     * @see net.sf.kraken.session.TransportSession#logIn(net.sf.kraken.type.PresenceType, String)
     */
@Override
public void logIn(PresenceType presenceType, String verboseStatus) {
    if (!this.isLoggedIn()) {
        this.setLoginStatus(TransportLoginStatus.LOGGING_IN);
        Log.debug("SimpleSession(" + jid.getNode() + ").login:  Start login as " + registration.getUsername() + ".");
        Request registerRequest = prepareRegisterRequest();
        if (registerRequest.getHeader(CallIdHeader.NAME) == null) {
            Log.debug("SimpleSession(" + getJID().getNode() + ").login:  Unable to create a SIP session ID!!");
            this.setLoginStatus(TransportLoginStatus.LOGGED_OUT);
            setFailureStatus(ConnectionFailureReason.UNKNOWN);
            sessionDisconnected("Unable to create SIP session ID!");
            return;
        } else {
            sessionId = ((CallIdHeader) registerRequest.getHeader(CallIdHeader.NAME)).getCallId();
        }
        try {
            registerRequest.addHeader(headerFactory.createExpiresHeader(365 * 24 * 60 * 60));
        } catch (Exception e) {
            Log.debug("SimpleSession(" + jid.getNode() + ").login:  " + "Unable to set the expiry interval, which is essential for a login.", e);
            this.setLoginStatus(TransportLoginStatus.LOGGED_OUT);
            return;
        }
        try {
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.ACK));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.BYE));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.CANCEL));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.INFO));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.INVITE));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.MESSAGE));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.NOTIFY));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.OPTIONS));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.REFER));
            registerRequest.addHeader(headerFactory.createAllowHeader(Request.SUBSCRIBE));
        } catch (Exception e) {
        // Ignore
        }
        try {
            sendRequest(registerRequest, ListeningPoint.UDP);
        } catch (Exception e) {
            Log.debug("SimpleSession(" + jid.getNode() + ").login:  Unable to send login packet.", e);
            this.setLoginStatus(TransportLoginStatus.LOGGED_OUT);
            setFailureStatus(ConnectionFailureReason.CAN_NOT_CONNECT);
            sessionDisconnected("Unable to send login packet!");
        }
    }
}
Also used : Request(javax.sip.message.Request) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 4 with Request

use of javax.sip.message.Request in project Openfire by igniterealtime.

the class SimpleSession method sendMessage.

/**
     * @see net.sf.kraken.session.TransportSession#sendMessage(org.xmpp.packet.JID, String)
     */
@Override
public void sendMessage(JID jid, String message) {
    Log.debug("SimpleSession(" + jid.getNode() + "):  Starting message sending process.");
    ContentTypeHeader contentTypeHeader;
    try {
        contentTypeHeader = headerFactory.createContentTypeHeader("text", "plain");
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").sendMessage:  Unable to initiate ContentType header.", e);
        return;
    }
    Log.debug("SimpleSession(" + jid.getNode() + "):  Finished adding ContentType header.");
    MessageContent content = new MessageContent(contentTypeHeader, message);
    try {
        Request request = prepareMessageRequest(content, getTransport().convertJIDToID(jid));
        sendRequest(request, ListeningPoint.UDP);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").sendMessage:  Unable to send message.", e);
    }
//		if (!prepareRequest(RequestType.MESSAGE, ((SimpleTransport) transport).convertJIDToID(jid), null, null, 1L, 70, null, content)) {
//			Log.debug("SimpleSession(" + this.jid.getNode() + ").sendMessage:  Unable to send message!");
//		}
}
Also used : ContentTypeHeader(javax.sip.header.ContentTypeHeader) Request(javax.sip.message.Request) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 5 with Request

use of javax.sip.message.Request in project Openfire by igniterealtime.

the class SimpleSession method addContact.

/**
     * @see net.sf.kraken.session.TransportSession#addContact(org.xmpp.packet.JID, String, java.util.ArrayList)
     */
@Override
public void addContact(JID jid, String nickname, ArrayList<String> groups) {
    Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Roster of " + jid.toString() + " locked!");
    String destId = getTransport().convertJIDToID(jid);
    PseudoRosterItem rosterItem;
    if (pseudoRoster.hasItem(destId)) {
        rosterItem = pseudoRoster.getItem(destId);
        rosterItem.setNickname(nickname);
    } else {
        rosterItem = pseudoRoster.createItem(destId, nickname, null);
    }
    getBuddyManager().storeBuddy(new SimpleBuddy(getBuddyManager(), destId, rosterItem));
    Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Starting addContact function for " + destId);
    Request subscribeRequest;
    try {
        subscribeRequest = prepareSubscribeRequest(destId);
        subscribeRequest.addHeader(headerFactory.createExpiresHeader(365 * 24 * 60 * 60));
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Unable to prepare SUBSCRIBE request.", e);
        Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Roster of " + jid.toString() + " unlocked!");
        return;
    }
    try {
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.ACK));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.BYE));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.CANCEL));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.INFO));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.INVITE));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.MESSAGE));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.NOTIFY));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.OPTIONS));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.REFER));
        subscribeRequest.addHeader(headerFactory.createAllowHeader(Request.SUBSCRIBE));
        subscribeRequest.addHeader(headerFactory.createEventHeader("presence"));
        subscribeRequest.addHeader(headerFactory.createAcceptHeader("application", "pidf+xml"));
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Unable to add a header", e);
    }
    try {
        sendRequest(subscribeRequest, ListeningPoint.UDP);
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Unable to send request.", e);
        Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Roster of " + jid.toString() + " unlocked!");
        return;
    }
    Log.debug("SimpleSession(" + jid.getNode() + ").addContact:  Roster of " + jid.toString() + " unlocked!");
}
Also used : Request(javax.sip.message.Request) PseudoRosterItem(net.sf.kraken.pseudoroster.PseudoRosterItem) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Aggregations

Request (javax.sip.message.Request)38 ParseException (java.text.ParseException)21 SipException (javax.sip.SipException)21 InvalidArgumentException (javax.sip.InvalidArgumentException)13 ClientTransaction (javax.sip.ClientTransaction)9 SipURI (javax.sip.address.SipURI)9 Response (javax.sip.message.Response)9 NotFoundException (org.jivesoftware.util.NotFoundException)7 TooManyListenersException (java.util.TooManyListenersException)6 ServerTransaction (javax.sip.ServerTransaction)6 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)6 Address (javax.sip.address.Address)5 CSeqHeader (javax.sip.header.CSeqHeader)5 ViaHeader (javax.sip.header.ViaHeader)5 SIPRequest (gov.nist.javax.sip.message.SIPRequest)4 FromHeader (javax.sip.header.FromHeader)4 SubscriptionStateHeader (javax.sip.header.SubscriptionStateHeader)4 ToHeader (javax.sip.header.ToHeader)4 SIPClientTransaction (gov.nist.javax.sip.stack.SIPClientTransaction)3 IOException (java.io.IOException)3