Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

In this page you can find the example usage for org.dom4j Element addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.adspore.splat.xep0060.LeafNode.java

License:Open Source License

/**
 * Purges items that were published to the node. Only owners can request this operation.
 * This operation is only available for nodes configured to store items in the database. All
 * published items will be deleted with the exception of the last published item.
 */// w  w w.  j  ava2s.c o m
public void purge() {
    //PubSubPersistenceManager.purgeNode(this);
    // Broadcast purge notification to subscribers
    // Build packet to broadcast to subscribers
    Message message = new Message();
    Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
    Element items = event.addElement("purge");
    items.addAttribute("node", mNodeID);
    // Send notification that the node configuration has changed
    broadcastNodeEvent(message, false);
}

From source file:com.adspore.splat.xep0060.Node.java

License:Open Source License

/**
 * The node configuration has changed. If this is the first time the node is configured
 * after it was created (i.e. is not yet persistent) then do nothing. Otherwise, send
 * a notification to the node subscribers informing that the configuration has changed.
 *//*from  w ww. java  2 s.co m*/
private void nodeConfigurationChanged() {
    if (!isNotifiedOfConfigChanges() || !savedToDB) {
        // Do nothing if node was just created and configure or if notification
        // of config changes is disabled
        return;
    }

    // Build packet to broadcast to subscribers
    Message message = new Message();
    Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
    Element config = event.addElement("configuration");
    config.addAttribute("node", mNodeID);

    if (mDeliverPayloads) {
        config.add(getConfigurationChangeForm().getElement());
    }
    // Send notification that the node configuration has changed
    broadcastNodeEvent(message, false);
}

From source file:com.adspore.splat.xep0060.Node.java

License:Open Source License

/**
 * Deletes this node from memory and the database. Subscribers are going to be notified
 * that the node has been deleted after the node was successfully deleted.
 *
 * @return true if the node was successfully deleted.
 *//*from  ww  w  .ja  v a  2  s.  co  m*/
public boolean delete() {
    // Delete node from the database
    if (true) {
        // Remove this node from the parent node (if any)
        if (mParent != null) {
            mParent.removeChildNode(this);
        }
        deletingNode();
        // Broadcast delete notification to subscribers (if enabled)
        if (isNotifiedOfDelete()) {
            // Build packet to broadcast to subscribers
            Message message = new Message();
            Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
            Element items = event.addElement("delete");
            items.addAttribute("node", mNodeID);
            // Send notification that the node was deleted
            //   TODO:  This doesn't appear to be the same notification scheme as the publishing of events
            broadcastNodeEvent(message, true);
        }
        // Notify the parent (if any) that the node has been removed from the parent node
        if (mParent != null) {
            mParent.childNodeDeleted(this);
        }
        // Remove presence subscription when node was deleted.
        cancelPresenceSubscriptions();
        // Remove the node from memory
        mService.removeNode(getNodeID());

        // Clear collections in memory (clear them after broadcast was sent)
        affiliates.clear();
        subscriptionsByID.clear();
        subscriptionsByJID.clear();
        return true;
    }
    return false;
}

From source file:com.adspore.splat.xep0060.Node.java

License:Open Source License

/**
 * Sends the list of affiliations with the node to the owner that sent the IQ
 * request./* w  w  w .  j ava2 s  . c  o  m*/
 *
 * @param iqRequest IQ request sent by an owner of the node.
 */
void sendAffiliations(IQ iqRequest) {
    IQ reply = IQ.createResultIQ(iqRequest);
    Element childElement = iqRequest.getChildElement().createCopy();
    reply.setChildElement(childElement);
    Element affiliations = childElement.element("affiliations");

    for (NodeAffiliate affiliate : affiliates) {
        if (affiliate.getAffiliation() == NodeAffiliate.Affiliation.none) {
            continue;
        }
        Element entity = affiliations.addElement("affiliation");
        entity.addAttribute("jid", affiliate.getJID().toString());
        entity.addAttribute("affiliation", affiliate.getAffiliation().name());
    }
    // Send reply
    mService.send(reply);
}

From source file:com.adspore.splat.xep0060.Node.java

License:Open Source License

/**
 * Sends the list of subscriptions with the node to the owner that sent the IQ
 * request./*from  www.ja v  a  2  s.  c  om*/
 *
 * @param iqRequest IQ request sent by an owner of the node.
 */
void sendSubscriptions(IQ iqRequest) {
    IQ reply = IQ.createResultIQ(iqRequest);
    Element childElement = iqRequest.getChildElement().createCopy();
    reply.setChildElement(childElement);
    Element subscriptions = childElement.element("subscriptions");

    for (NodeAffiliate affiliate : affiliates) {
        for (NodeSubscription subscription : affiliate.getSubscriptions()) {
            if (subscription.isAuthorizationPending()) {
                continue;
            }
            Element entity = subscriptions.addElement("subscription");
            entity.addAttribute("jid", subscription.getJID().toString());
            //entity.addAttribute("affiliation", affiliate.getAffiliation().name());
            entity.addAttribute("subscription", subscription.getState().name());
            if (isMultipleSubscriptionsEnabled()) {
                entity.addAttribute("subid", subscription.getID());
            }
        }
    }
    // Send reply
    mService.send(reply);
}

From source file:com.adspore.splat.xep0060.Node.java

License:Open Source License

/**
 * Sends an event notification to the specified subscriber. The event notification may
 * include information about the affected subscriptions.
 *
 * @param subscriberJID the subscriber JID that will get the notification.
 * @param notification the message to send to the subscriber.
 * @param subIDs the list of affected subscription IDs or null when node does not
 *        allow multiple subscriptions.//from  w  w  w . ja v  a 2  s.c  o m
 */
protected void sendEventNotification(JID subscriberJID, Message notification, Collection<String> subIDs) {
    Element headers = null;
    if (subIDs != null) {
        // Notate the event notification with the ID of the affected subscriptions
        headers = notification.addChildElement("headers", "http://jabber.org/protocol/shim");
        for (String subID : subIDs) {
            Element header = headers.addElement("header");
            header.addAttribute("name", "pubsub#subid");
            header.setText(subID);
        }
    }

    // Verify that the subscriber JID is currently available to receive notification 
    // messages. This is required because the message router will deliver packets via 
    // the bare JID if a session for the full JID is not available. The "isActiveRoute"
    // condition below will prevent inadvertent delivery of multiple copies of each
    // event notification to the user, possibly multiple times (e.g. route.all-resources). 
    // (Refer to http://issues.igniterealtime.org/browse/OF-14 for more info.)
    //
    // This approach is informed by the following XEP-0060 implementation guidelines:
    //   12.2 "Intended Recipients for Notifications" - only deliver to subscriber JID
    //   12.4 "Not Routing Events to Offline Storage" - no offline storage for notifications
    //
    // Note however that this may be somewhat in conflict with the following:
    //   12.3 "Presence-Based Delivery of Events" - automatically detect user's presence
    //

    //   FIXME:  Verify this is correct solution...  Commented out.
    Map<String, String> allSubPresences = mService.mComponent.getBarePresences().get(subscriberJID.toBareJID());
    if (null != allSubPresences && allSubPresences.containsKey(subscriberJID.toString())) {
        String status = allSubPresences.get(subscriberJID.toString());
        if (status.equals("online")) {
            mService.sendNotification(this, notification, subscriberJID);
        }
    }

    if (headers != null) {
        // Remove the added child element that includes subscription IDs information
        notification.getElement().remove(headers);
    }
}

From source file:com.adspore.splat.xep0060.NodeAffiliate.java

License:Open Source License

/**
 * Sends an event notification for the published items to the affiliate. The event
 * notification may contain zero, one or many published items based on the items
 * included in the original publication. If the affiliate has many subscriptions and
 * many items were published then the affiliate will get a notification for each set
 * of items that affected the same subscriptions.
 *
 * @param notification the message to sent to the subscribers. The message will be completed
 *        with the items to include in each notification.
 * @param event the event Element included in the notification message. Passed as an
 *        optimization to avoid future look ups.
 * @param leafNode the leaf node where the items where published.
 * @param publishedItems the list of items that were published. Could be an empty list.
 */// ww  w.j a  v  a2 s .  c  o  m
void sendPublishedNotifications(Message notification, Element event, LeafNode leafNode,
        List<PublishedItem> publishedItems) {

    if (!publishedItems.isEmpty()) {
        Map<List<NodeSubscription>, List<PublishedItem>> itemsBySubs = getItemsBySubscriptions(leafNode,
                publishedItems);

        // Send one notification for published items that affect the same subscriptions
        for (List<NodeSubscription> nodeSubscriptions : itemsBySubs.keySet()) {
            // Add items information
            Element items = event.addElement("items");
            items.addAttribute("node", getNode().getNodeID());
            for (PublishedItem publishedItem : itemsBySubs.get(nodeSubscriptions)) {
                // FIXME: This was added for compatibility with PEP supporting clients.
                //        Alternate solution needed when XEP-0163 version > 1.0 is released.
                //
                // If the node ID looks like a JID, replace it with the published item's node ID.
                if (getNode().getNodeID().indexOf("@") >= 0) {
                    items.addAttribute("node", publishedItem.getNodeID());
                }

                // Add item information to the event notification
                Element item = items.addElement("item");
                if (leafNode.isItemRequired()) {
                    item.addAttribute("id", publishedItem.getID());
                }
                if (leafNode.isPayloadDelivered()) {
                    item.add(publishedItem.getPayload().createCopy());
                }
                // Add leaf leafNode information if affiliated leafNode and node
                // where the item was published are different
                if (leafNode != getNode()) {
                    item.addAttribute("node", leafNode.getNodeID());
                }
            }
            // Send the event notification
            sendEventNotification(notification, nodeSubscriptions);
            // Remove the added items information
            event.remove(items);
        }
    } else {
        // Filter affiliate subscriptions and only use approved and configured ones
        List<NodeSubscription> affectedSubscriptions = new ArrayList<NodeSubscription>();
        for (NodeSubscription subscription : getSubscriptions()) {
            if (subscription.canSendPublicationEvent(leafNode, null)) {
                affectedSubscriptions.add(subscription);
            }
        }
        // Add item information to the event notification
        Element items = event.addElement("items");
        items.addAttribute("node", leafNode.getNodeID());
        // Send the event notification
        sendEventNotification(notification, affectedSubscriptions);
        // Remove the added items information
        event.remove(items);
    }
}

From source file:com.adspore.splat.xep0060.NodeAffiliate.java

License:Open Source License

/**
 * Sends an event notification to the affiliate for the deleted items. The event
 * notification may contain one or many published items based on the items included
 * in the original publication. If the affiliate has many subscriptions and many
 * items were deleted then the affiliate will get a notification for each set
 * of items that affected the same subscriptions.
 *
 * @param notification the message to sent to the subscribers. The message will be completed
 *        with the items to include in each notification.
 * @param event the event Element included in the notification message. Passed as an
 *        optimization to avoid future look ups.
 * @param leafNode the leaf node where the items where deleted from.
 * @param publishedItems the list of items that were deleted.
 */// w  w  w. j  a  va 2 s .c  om
void sendDeletionNotifications(Message notification, Element event, LeafNode leafNode,
        List<PublishedItem> publishedItems) {

    if (!publishedItems.isEmpty()) {
        Map<List<NodeSubscription>, List<PublishedItem>> itemsBySubs = getItemsBySubscriptions(leafNode,
                publishedItems);

        // Send one notification for published items that affect the same subscriptions
        for (List<NodeSubscription> nodeSubscriptions : itemsBySubs.keySet()) {
            // Add items information
            Element items = event.addElement("items");
            items.addAttribute("node", leafNode.getNodeID());
            for (PublishedItem publishedItem : itemsBySubs.get(nodeSubscriptions)) {
                // Add retract information to the event notification
                Element item = items.addElement("retract");
                if (leafNode.isItemRequired()) {
                    item.addAttribute("id", publishedItem.getID());
                }
            }
            // Send the event notification
            sendEventNotification(notification, nodeSubscriptions);
            // Remove the added items information
            event.remove(items);
        }
    }
}

From source file:com.adspore.splat.xep0060.NodeSubscription.java

License:Open Source License

/**
 * 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.//from w w  w.  j av a  2  s .  co  m
 *
 * @param originalRequest the IQ packet sent by the subscriber to create the subscription.
 */
IQ 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.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
    return result;
}

From source file:com.adspore.splat.xep0060.NodeSubscription.java

License:Open Source License

/**
 * Sends an event notification for the last published item to the subscriber. If
 * the subscription has not yet been authorized or is pending to be configured then
 * no notification is going to be sent.<p>
 *
 * Depending on the subscription configuration the event notification 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 publishedItem the last item that was published to the node.
 */// w w w .ja v a  2  s .  com
void sendLastPublishedItem(PublishedItem publishedItem) {
    // Check if the published item can be sent to the subscriber
    if (!canSendPublicationEvent(publishedItem.getNode(), publishedItem)) {
        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", node.getNodeID());
    Element item = items.addElement("item");
    if (((LeafNode) node).isItemRequired()) {
        item.addAttribute("id", publishedItem.getID());
    }
    if (node.isPayloadDelivered() && publishedItem.getPayload() != null) {
        item.add(publishedItem.getPayload().createCopy());
    }
    // Add a message body (if required)
    if (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(publishedItem.getCreationDate()));
    // Send the event notification to the subscriber
    node.getService().sendNotification(node, notification, jid);
}