Search in sources :

Example 1 with PublishedItem

use of org.jivesoftware.openfire.pubsub.PublishedItem 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)

Aggregations

Element (org.dom4j.Element)1 CollectionNode (org.jivesoftware.openfire.pubsub.CollectionNode)1 Node (org.jivesoftware.openfire.pubsub.Node)1 NodeSubscription (org.jivesoftware.openfire.pubsub.NodeSubscription)1 PublishedItem (org.jivesoftware.openfire.pubsub.PublishedItem)1 JID (org.xmpp.packet.JID)1 Message (org.xmpp.packet.Message)1