Search in sources :

Example 1 with Node

use of org.jivesoftware.openfire.pubsub.Node in project Openfire by igniterealtime.

the class IQPEPHandler method getUserItems.

/**
     * Implements UserItemsProvider, adding PEP related items to a disco#items
     * result.
     */
@Override
public Iterator<Element> getUserItems(String name, JID senderJID) {
    ArrayList<Element> items = new ArrayList<>();
    String recipientJID = XMPPServer.getInstance().createJID(name, null, true).toBareJID();
    PEPService pepService = pepServiceManager.getPEPService(recipientJID);
    if (pepService != null) {
        CollectionNode rootNode = pepService.getRootCollectionNode();
        Element defaultItem = DocumentHelper.createElement("item");
        defaultItem.addAttribute("jid", recipientJID);
        for (Node node : pepService.getNodes()) {
            // Do not include the root node as an item element.
            if (node == rootNode) {
                continue;
            }
            AccessModel accessModel = node.getAccessModel();
            if (accessModel.canAccessItems(node, senderJID, new JID(recipientJID))) {
                Element item = defaultItem.createCopy();
                item.addAttribute("node", node.getNodeID());
                items.add(item);
            }
        }
    }
    return items.iterator();
}
Also used : JID(org.xmpp.packet.JID) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) LeafNode(org.jivesoftware.openfire.pubsub.LeafNode) ArrayList(java.util.ArrayList) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode)

Example 2 with Node

use of org.jivesoftware.openfire.pubsub.Node in project Openfire by igniterealtime.

the class PEPService method sendLastPublishedItems.

/**
     * Sends an event notification for the last published item of each leaf node under the
     * root collection node to the recipient JID. If the recipient has no subscription to
     * the root collection node, has not yet been authorized, or is pending to be
     * configured -- then no notifications are going to be sent.<p>
     *
     * Depending on the subscription configuration the event notifications may or may not have
     * a payload, may not be sent if a keyword (i.e. filter) was defined and it was not matched.
     *
     * @param recipientJID the recipient that is to receive the last published item notifications.
     */
public void sendLastPublishedItems(JID recipientJID) {
    // Ensure the recipient has a subscription to this service's root collection node.
    NodeSubscription subscription = rootCollectionNode.getSubscription(recipientJID);
    if (subscription == null) {
        subscription = rootCollectionNode.getSubscription(new JID(recipientJID.toBareJID()));
    }
    if (subscription == null) {
        return;
    }
    // Send the last published item of each leaf node to the recipient.
    for (Node leafNode : rootCollectionNode.getNodes()) {
        // Retrieve last published item for the leaf node.
        PublishedItem leafLastPublishedItem = null;
        leafLastPublishedItem = leafNode.getLastPublishedItem();
        if (leafLastPublishedItem == null) {
            continue;
        }
        // Check if the published item can be sent to the subscriber
        if (!subscription.canSendPublicationEvent(leafLastPublishedItem.getNode(), leafLastPublishedItem)) {
            return;
        }
        // Send event notification to the subscriber
        Message notification = new Message();
        Element event = notification.getElement().addElement("event", "http://jabber.org/protocol/pubsub#event");
        Element items = event.addElement("items");
        items.addAttribute("node", leafLastPublishedItem.getNodeID());
        Element item = items.addElement("item");
        if (leafLastPublishedItem.getNode().isItemRequired()) {
            item.addAttribute("id", leafLastPublishedItem.getID());
        }
        if (leafLastPublishedItem.getNode().isPayloadDelivered() && leafLastPublishedItem.getPayload() != null) {
            item.add(leafLastPublishedItem.getPayload().createCopy());
        }
        // Add a message body (if required)
        if (subscription.isIncludingBody()) {
            notification.setBody(LocaleUtils.getLocalizedString("pubsub.notification.message.body"));
        }
        // Include date when published item was created
        notification.getElement().addElement("delay", "urn:xmpp:delay").addAttribute("stamp", XMPPDateTimeFormat.format(leafLastPublishedItem.getCreationDate()));
        // Send the event notification to the subscriber
        this.sendNotification(subscription.getNode(), notification, subscription.getJID());
    }
}
Also used : JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) NodeSubscription(org.jivesoftware.openfire.pubsub.NodeSubscription) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) Element(org.dom4j.Element) PublishedItem(org.jivesoftware.openfire.pubsub.PublishedItem)

Example 3 with Node

use of org.jivesoftware.openfire.pubsub.Node in project Openfire by igniterealtime.

the class AffiliationTask method run.

@Override
public void run() {
    log.debug("[TASK] New affiliation : {}", toString());
    Node node = getNode();
    NodeAffiliate affiliate = node.getAffiliate(jid);
    if (affiliate == null) {
        affiliate = new NodeAffiliate(node, jid);
        affiliate.setAffiliation(affiliation);
        node.addAffiliate(affiliate);
    } else {
        affiliate.setAffiliation(affiliation);
    }
}
Also used : Node(org.jivesoftware.openfire.pubsub.Node) NodeAffiliate(org.jivesoftware.openfire.pubsub.NodeAffiliate)

Example 4 with Node

use of org.jivesoftware.openfire.pubsub.Node in project Openfire by igniterealtime.

the class IQPEPHandler method subscribedToPresence.

@Override
public void subscribedToPresence(JID subscriberJID, JID authorizerJID) {
    final PEPService pepService = pepServiceManager.getPEPService(authorizerJID.toBareJID());
    if (pepService != null) {
        createSubscriptionToPEPService(pepService, subscriberJID, authorizerJID);
        // Delete any leaf node subscriptions the subscriber may have already
        // had (since a subscription to the PEP service, and thus its leaf PEP
        // nodes, would be duplicating publish notifications from previous leaf
        // node subscriptions).
        CollectionNode rootNode = pepService.getRootCollectionNode();
        for (Node node : pepService.getNodes()) {
            if (rootNode.isChildNode(node)) {
                for (NodeSubscription subscription : node.getSubscriptions(subscriberJID)) {
                    node.cancelSubscription(subscription);
                }
            }
        }
        pepService.sendLastPublishedItems(subscriberJID);
    }
}
Also used : NodeSubscription(org.jivesoftware.openfire.pubsub.NodeSubscription) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) LeafNode(org.jivesoftware.openfire.pubsub.LeafNode) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode)

Example 5 with Node

use of org.jivesoftware.openfire.pubsub.Node in project Openfire by igniterealtime.

the class PEPService method sendNotification.

@Override
public void sendNotification(Node node, Message message, JID recipientJID) {
    message.setTo(recipientJID);
    message.setFrom(getAddress());
    message.setID(StringUtils.randomString(8));
    // If the recipient subscribed with a bare JID and this PEPService can retrieve
    // presence information for the recipient, collect all of their full JIDs and
    // send the notification to each below.
    Set<JID> recipientFullJIDs = new HashSet<>();
    if (XMPPServer.getInstance().isLocal(recipientJID)) {
        if (recipientJID.getResource() == null) {
            for (ClientSession clientSession : SessionManager.getInstance().getSessions(recipientJID.getNode())) {
                recipientFullJIDs.add(clientSession.getAddress());
            }
        }
    } else {
        // Since recipientJID is not local, try to get presence info from cached known remote
        // presences.
        // TODO: OF-605 the old code depends on a cache that would contain presence state on all (?!) JIDS on all (?!)
        // remote domains. As we cannot depend on this information to be correct (even if we could ensure that this
        // potentially unlimited amount of data would indeed be manageable in the first place), this code was removed.
        recipientFullJIDs.add(recipientJID);
    }
    if (recipientFullJIDs.isEmpty()) {
        router.route(message);
        return;
    }
    for (JID recipientFullJID : recipientFullJIDs) {
        // to the service owner.
        try {
            JID publisher = null;
            // Get the ID of the node that had an item published to or retracted from.
            Element itemsElement = message.getElement().element("event").element("items");
            String nodeID = itemsElement.attributeValue("node");
            // Get the ID of the item that was published or retracted.
            String itemID = null;
            Element itemElement = itemsElement.element("item");
            if (itemElement == null) {
                Element retractElement = itemsElement.element("retract");
                if (retractElement != null) {
                    itemID = retractElement.attributeValue("id");
                }
            } else {
                itemID = itemElement.attributeValue("id");
            }
            // Check if the recipientFullJID is interested in notifications for this node.
            // If the recipient has not yet requested any notification filtering, continue and send
            // the notification.
            EntityCapabilities entityCaps = entityCapsManager.getEntityCapabilities(recipientFullJID);
            if (entityCaps != null) {
                if (!entityCaps.containsFeature(nodeID + "+notify")) {
                    return;
                }
            }
            // This full JID will be used as the "replyto" address in the addressing extension.
            if (node.isCollectionNode()) {
                for (Node leafNode : node.getNodes()) {
                    if (leafNode.getNodeID().equals(nodeID)) {
                        publisher = leafNode.getPublishedItem(itemID).getPublisher();
                        // Ensure the recipientJID has access to receive notifications for items published to the leaf node.
                        AccessModel accessModel = leafNode.getAccessModel();
                        if (!accessModel.canAccessItems(leafNode, recipientFullJID, publisher)) {
                            return;
                        }
                        break;
                    }
                }
            } else {
                publisher = node.getPublishedItem(itemID).getPublisher();
            }
            // Ensure the recipient is subscribed to the service owner's (publisher's) presence.
            if (canProbePresence(publisher, recipientFullJID)) {
                Element addresses = DocumentHelper.createElement(QName.get("addresses", "http://jabber.org/protocol/address"));
                Element address = addresses.addElement("address");
                address.addAttribute("type", "replyto");
                address.addAttribute("jid", publisher.toString());
                Message extendedMessage = message.createCopy();
                extendedMessage.addExtension(new PacketExtension(addresses));
                extendedMessage.setTo(recipientFullJID);
                router.route(extendedMessage);
            }
        } catch (IndexOutOfBoundsException e) {
        // Do not add addressing extension to message.
        } catch (UserNotFoundException e) {
            // Do not add addressing extension to message.
            router.route(message);
        } catch (NullPointerException e) {
            try {
                if (canProbePresence(getAddress(), recipientFullJID)) {
                    message.setTo(recipientFullJID);
                }
            } catch (UserNotFoundException e1) {
            // Do nothing
            }
            router.route(message);
        }
    }
}
Also used : PacketExtension(org.xmpp.packet.PacketExtension) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) CollectionNode(org.jivesoftware.openfire.pubsub.CollectionNode) Node(org.jivesoftware.openfire.pubsub.Node) EntityCapabilities(org.jivesoftware.openfire.entitycaps.EntityCapabilities) ClientSession(org.jivesoftware.openfire.session.ClientSession) HashSet(java.util.HashSet)

Aggregations

Node (org.jivesoftware.openfire.pubsub.Node)8 CollectionNode (org.jivesoftware.openfire.pubsub.CollectionNode)5 Element (org.dom4j.Element)3 JID (org.xmpp.packet.JID)3 LeafNode (org.jivesoftware.openfire.pubsub.LeafNode)2 NodeAffiliate (org.jivesoftware.openfire.pubsub.NodeAffiliate)2 NodeSubscription (org.jivesoftware.openfire.pubsub.NodeSubscription)2 AccessModel (org.jivesoftware.openfire.pubsub.models.AccessModel)2 Message (org.xmpp.packet.Message)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Lock (java.util.concurrent.locks.Lock)1 EntityCapabilities (org.jivesoftware.openfire.entitycaps.EntityCapabilities)1 PublishedItem (org.jivesoftware.openfire.pubsub.PublishedItem)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)1 PacketExtension (org.xmpp.packet.PacketExtension)1