Search in sources :

Example 61 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class NodeSubscription method sendSubscriptionState.

/**
 * Sends the current subscription status to the user that tried to create a subscription to
 * the node. The subscription status is sent to the subsciber after the subscription was
 * created or if the subscriber tries to subscribe many times and the node does not support
 * multpiple subscriptions.
 *
 * @param originalRequest the IQ packet sent by the subscriber to create the subscription.
 */
void sendSubscriptionState(IQ originalRequest) {
    IQ result = IQ.createResultIQ(originalRequest);
    Element child = result.setChildElement("pubsub", "http://jabber.org/protocol/pubsub");
    Element entity = child.addElement("subscription");
    if (!node.isRootCollectionNode()) {
        entity.addAttribute("node", node.getUniqueIdentifier().getNodeId());
    }
    entity.addAttribute("jid", getJID().toString());
    if (node.isMultipleSubscriptionsEnabled()) {
        entity.addAttribute("subid", getID());
    }
    entity.addAttribute("subscription", getState().name());
    Element subscribeOptions = entity.addElement("subscribe-options");
    if (node.isSubscriptionConfigurationRequired() && isConfigurationPending()) {
        subscribeOptions.addElement("required");
    }
    // Send the result
    node.getService().send(result);
}
Also used : Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ)

Example 62 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class SocketReader method process.

protected void process(Element doc) throws Exception {
    if (doc == null) {
        return;
    }
    String tag = doc.getName();
    if ("message".equals(tag)) {
        Message packet;
        try {
            packet = new Message(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer with an error.
            Message reply = new Message();
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            reply.getElement().addAttribute("from", doc.attributeValue("to"));
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        processMessage(packet);
    } else if ("presence".equals(tag)) {
        Presence packet;
        try {
            packet = new Presence(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer an error
            Presence reply = new Presence();
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            reply.getElement().addAttribute("from", doc.attributeValue("to"));
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        // Check that the presence type is valid. If not then assume available type
        try {
            packet.getType();
        } catch (IllegalArgumentException e) {
            Log.debug("Invalid presence (type): " + packet);
            // The presence packet contains an invalid presence type so replace it with
            // an available presence type
            packet.setType(null);
        }
        // Check that the presence show is valid. If not then assume available show value
        try {
            packet.getShow();
        } catch (IllegalArgumentException e) {
            Log.debug("Invalid presence (show): " + packet);
            // The presence packet contains an invalid presence show so replace it with
            // an available presence show
            packet.setShow(null);
        }
        if (session.getStatus() == Session.STATUS_CLOSED && packet.isAvailable()) {
            // Ignore available presence packets sent from a closed session. A closed
            // session may have buffered data pending to be processes so we want to ignore
            // just Presences of type available
            Log.warn("Ignoring available presence packet of closed session: " + packet);
            return;
        }
        processPresence(packet);
    } else if ("iq".equals(tag)) {
        IQ packet;
        try {
            packet = getIQ(doc);
        } catch (IllegalArgumentException e) {
            Log.debug("SocketReader: Rejecting packet. JID malformed", e);
            // The original packet contains a malformed JID so answer an error
            IQ reply = new IQ();
            if (!doc.elements().isEmpty()) {
                reply.setChildElement(((Element) doc.elements().get(0)).createCopy());
            }
            reply.setID(doc.attributeValue("id"));
            reply.setTo(session.getAddress());
            if (doc.attributeValue("to") != null) {
                reply.getElement().addAttribute("from", doc.attributeValue("to"));
            }
            reply.setError(PacketError.Condition.jid_malformed);
            session.process(reply);
            return;
        }
        processIQ(packet);
    } else {
        if (!processUnknowPacket(doc)) {
            Log.warn(LocaleUtils.getLocalizedString("admin.error.packet.tag") + doc.asXML());
            open = false;
        }
    }
}
Also used : Message(org.xmpp.packet.Message) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Presence(org.xmpp.packet.Presence)

Example 63 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class MulticastRouter method receivedAnswer.

@Override
public void receivedAnswer(IQ packet) {
    // Look for the root node being discovered
    String domain = packet.getFrom().toString();
    boolean isRoot = true;
    if (!nodes.containsKey(domain)) {
        domain = roots.get(domain);
        isRoot = false;
    }
    // Check if this is a disco#info response
    if ("http://jabber.org/protocol/disco#info".equals(packet.getChildElement().getNamespaceURI())) {
        // Check if the node supports JEP-33
        boolean supports = false;
        for (Iterator it = packet.getChildElement().elementIterator("feature"); it.hasNext(); ) {
            if (NAMESPACE.equals(((Element) it.next()).attributeValue("var"))) {
                supports = true;
                break;
            }
        }
        if (supports) {
            // JEP-33 is supported by the entity
            Collection<String> items = nodes.remove(domain);
            for (String item : items) {
                roots.remove(item);
            }
            String multicastService = packet.getFrom().toString();
            cache.put(domain, multicastService);
            sendToRemoteServer(domain, multicastService);
        } else {
            if (isRoot && IQ.Type.error != packet.getType()) {
                // Discover node items with the hope that a sub-item supports JEP-33
                IQ iq = new IQ(IQ.Type.get);
                iq.setFrom(server.getServerInfo().getXMPPDomain());
                iq.setTo(packet.getFrom());
                iq.setChildElement("query", "http://jabber.org/protocol/disco#items");
                // Send the disco#items request to the remote server or component. The reply will be
                // processed by the IQResultListener (interface that this class implements)
                iqRouter.addIQResultListener(iq.getID(), this);
                iqRouter.route(iq);
            } else if (!isRoot) {
                // Process the disco#info response of an item that does not support JEP-33
                roots.remove(packet.getFrom().toString());
                Collection<String> items = nodes.get(domain);
                if (items != null) {
                    items.remove(packet.getFrom().toString());
                    if (items.isEmpty()) {
                        nodes.remove(domain);
                        cache.put(domain, "");
                        sendToRemoteServer(domain, "");
                    }
                }
            } else {
                // Root domain does not support disco#info
                nodes.remove(domain);
                cache.put(domain, "");
                sendToRemoteServer(domain, "");
            }
        }
    } else {
        // This is a disco#items response
        Collection<Element> items = packet.getChildElement().elements("item");
        if (IQ.Type.error == packet.getType() || items.isEmpty()) {
            // Root domain does not support disco#items
            nodes.remove(domain);
            cache.put(domain, "");
            sendToRemoteServer(domain, "");
        } else {
            // Keep the list of items found in the requested domain
            List<String> jids = new ArrayList<>();
            for (Element item : items) {
                String jid = item.attributeValue("jid");
                jids.add(jid);
                // Add that this item was found for the following domain
                roots.put(jid, domain);
            }
            nodes.put(domain, new CopyOnWriteArrayList<>(jids));
            // Send disco#info to each discovered item
            for (Element item : items) {
                // Discover if remote server supports JEP-33 (Extended Stanza Addressing)
                IQ iq = new IQ(IQ.Type.get);
                iq.setFrom(server.getServerInfo().getXMPPDomain());
                iq.setTo(item.attributeValue("jid"));
                Element child = iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
                if (item.attributeValue("node") != null) {
                    child.addAttribute("node", item.attributeValue("node"));
                }
                // Send the disco#info request to the discovered item. The reply will be
                // processed by the IQResultListener (interface that this class implements)
                iqRouter.addIQResultListener(iq.getID(), this);
                iqRouter.route(iq);
            }
        }
    }
}
Also used : Element(org.dom4j.Element) Iterator(java.util.Iterator) IQ(org.xmpp.packet.IQ) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Collection(java.util.Collection)

Example 64 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class MulticastRouter method sendToRemoteEntity.

/**
 * Sends pending packets of the requested domain but first try to discover if remote server
 * supports multicast service. If we already have cached information about the requested
 * domain then just deliver the packet.
 *
 * @param domain the domain that has pending packets to be sent.
 */
private void sendToRemoteEntity(String domain) {
    // Check if there is cached information about the requested domain
    String multicastService = cache.get(domain);
    if (multicastService != null) {
        sendToRemoteServer(domain, multicastService);
    } else {
        // No cached information was found so discover if remote server
        // supports JEP-33 (Extended Stanza Addressing). The reply to the disco
        // request is going to be process in #receivedAnswer(IQ packet)
        IQ iq = new IQ(IQ.Type.get);
        iq.setFrom(server.getServerInfo().getXMPPDomain());
        iq.setTo(domain);
        iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
        // Indicate that we are searching for info of the specified domain
        nodes.put(domain, new CopyOnWriteArrayList<String>());
        // Send the disco#info request to the remote server or component. The reply will be
        // processed by the IQResultListener (interface that this class implements)
        iqRouter.addIQResultListener(iq.getID(), this);
        iqRouter.route(iq);
    }
}
Also used : IQ(org.xmpp.packet.IQ)

Example 65 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class IQvCardHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
    IQ result = IQ.createResultIQ(packet);
    IQ.Type type = packet.getType();
    if (type.equals(IQ.Type.set)) {
        try {
            User user = userManager.getUser(packet.getFrom().getNode());
            Element vcard = packet.getChildElement();
            if (vcard != null) {
                try {
                    VCardManager.getInstance().setVCard(user.getUsername(), vcard);
                } catch (UnsupportedOperationException e) {
                    Log.debug("Entity '{}' tried to set VCard, but the configured VCard provider is read-only. An IQ error will be returned to sender.", packet.getFrom());
                    // VCards can include binary data. Let's not echo that back in the error.
                    // result.setChildElement( packet.getChildElement().createCopy() );
                    result.setError(PacketError.Condition.not_allowed);
                    // default to server locale.
                    Locale locale = JiveGlobals.getLocale();
                    final Session session = SessionManager.getInstance().getSession(result.getTo());
                    if (session != null && session.getLanguage() != null) {
                        // use client locale if one is available.
                        locale = session.getLanguage();
                    }
                    result.getError().setText(LocaleUtils.getLocalizedString("vcard.read_only", locale), locale.getLanguage());
                }
            }
        } catch (UserNotFoundException e) {
            // VCards can include binary data. Let's not echo that back in the error.
            // result.setChildElement( packet.getChildElement().createCopy() );
            result.setError(PacketError.Condition.item_not_found);
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
            result.setError(PacketError.Condition.internal_server_error);
        }
    } else if (type.equals(IQ.Type.get)) {
        JID recipient = packet.getTo();
        // If no TO was specified then get the vCard of the sender of the packet
        if (recipient == null) {
            recipient = packet.getFrom();
        }
        // By default return an empty vCard
        result.setChildElement("vCard", "vcard-temp");
        // Only try to get the vCard values of non-anonymous users
        if (recipient != null) {
            if (recipient.getNode() != null && server.isLocal(recipient)) {
                VCardManager vManager = VCardManager.getInstance();
                Element userVCard = vManager.getVCard(recipient.getNode());
                if (userVCard != null) {
                    // Check if the requester wants to ignore some vCard's fields
                    Element filter = packet.getChildElement().element(QName.get("filter", "vcard-temp-filter"));
                    if (filter != null) {
                        // Create a copy so we don't modify the original vCard
                        userVCard = userVCard.createCopy();
                        // Ignore fields requested by the user
                        for (Iterator toFilter = filter.elementIterator(); toFilter.hasNext(); ) {
                            Element field = (Element) toFilter.next();
                            Element fieldToRemove = userVCard.element(field.getName());
                            if (fieldToRemove != null) {
                                fieldToRemove.detach();
                            }
                        }
                    }
                    result.setChildElement(userVCard);
                }
            } else {
                result = IQ.createResultIQ(packet);
                result.setChildElement(packet.getChildElement().createCopy());
                result.setError(PacketError.Condition.item_not_found);
            }
        } else {
            result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.item_not_found);
        }
    } else {
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_acceptable);
    }
    return result;
}
Also used : Locale(java.util.Locale) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Iterator(java.util.Iterator) VCardManager(org.jivesoftware.openfire.vcard.VCardManager) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Session(org.jivesoftware.openfire.session.Session)

Aggregations

IQ (org.xmpp.packet.IQ)208 Element (org.dom4j.Element)141 JID (org.xmpp.packet.JID)49 PacketError (org.xmpp.packet.PacketError)35 Presence (org.xmpp.packet.Presence)19 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)18 Message (org.xmpp.packet.Message)17 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)16 ClientSession (org.jivesoftware.openfire.session.ClientSession)14 DataForm (org.xmpp.forms.DataForm)13 ArrayList (java.util.ArrayList)11 AgentNotFoundException (org.jivesoftware.xmpp.workgroup.AgentNotFoundException)10 Packet (org.xmpp.packet.Packet)10 PacketException (org.jivesoftware.openfire.PacketException)9 User (org.jivesoftware.openfire.user.User)8 List (java.util.List)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 Iterator (java.util.Iterator)6 Test (org.junit.Test)6 FormField (org.xmpp.forms.FormField)6