Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

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.
 *//*from www . ja  va 2s.  co  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.
 *//*from  w  w  w . ja  v a 2  s.  com*/
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./* w  ww . j  a v a  2  s.  com*/
 *
 * @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.
 *//*from  www .  j a v a2  s . c  o  m*/
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);
}

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

License:Open Source License

private static IQ publishItemsToNode(PubSubService service, IQ iq, Element publishElement) {
    String nodeID = publishElement.attributeValue("node");

    if (nodeID == null) {
        // No node was specified. Return bad_request error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }/*from w w w.ja v a  2s.  c o m*/

    // Look for the specified node
    Node node = service.getNode(nodeID);
    if (node == null) {
        // Node does not exist. Return item-not-found error
        return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
    }

    JID from = iq.getFrom();
    // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet
    JID owner = new JID(from.getNode(), from.getDomain(), null, true);
    if (!node.getPublisherModel().canPublish(node, owner) && !service.isServiceAdmin(owner)) {
        // Entity does not have sufficient privileges to publish to node
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    if (node.isCollectionNode()) {
        // Node is a collection node. Return feature-not-implemented error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "publish");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }

    LeafNode leafNode = (LeafNode) node;
    Iterator itemElements = publishElement.elementIterator("item");

    // Check that an item was included if node persist items or includes payload
    if (!itemElements.hasNext() && leafNode.isItemRequired()) {
        Element pubsubError = DocumentHelper
                .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }

    // Check that no item was included if node doesn't persist items and doesn't
    // includes payload
    if (itemElements.hasNext() && !leafNode.isItemRequired()) {
        Element pubsubError = DocumentHelper
                .createElement(QName.get("item-forbidden", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }

    List<Element> items = new ArrayList<Element>();
    List entries;
    Element payload;
    while (itemElements.hasNext()) {
        Element item = (Element) itemElements.next();
        entries = item.elements();
        payload = entries.isEmpty() ? null : (Element) entries.get(0);
        // Check that a payload was included if node is configured to include payload
        // in notifications
        if (payload == null && leafNode.isPayloadDelivered()) {
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("payload-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        }

        // Check that the payload (if any) contains only one child element
        if (entries.size() > 1) {
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("invalid-payload", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);

        }
        items.add(item);
    }

    // Publish item and send event notifications to subscribers
    leafNode.publishItems(from, items);
    // Return success operation
    return createSuccessPacket(iq);
}

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

License:Open Source License

private static IQ deleteItems(PubSubService service, IQ iq, Element retractElement) {
    String nodeID = retractElement.attributeValue("node");

    if (nodeID == null) {
        // No node was specified. Return bad_request error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }//from w  w  w  .  j a v a 2s .  c  o m

    Node node;
    // Look for the specified node
    node = service.getNode(nodeID);
    if (node == null) {
        // Node does not exist. Return item-not-found error
        return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
    }

    // Get the items to delete
    Iterator itemElements = retractElement.elementIterator("item");
    if (!itemElements.hasNext()) {
        Element pubsubError = DocumentHelper
                .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }

    if (node.isCollectionNode()) {
        // Cannot delete items from a collection node. Return an error.
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "persistent-items");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }
    LeafNode leafNode = (LeafNode) node;

    if (!leafNode.isItemRequired()) {
        // Cannot delete items from a leaf node that doesn't handle itemIDs. Return an error.
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "persistent-items");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }

    List<PublishedItem> items = new ArrayList<PublishedItem>();
    while (itemElements.hasNext()) {
        Element itemElement = (Element) itemElements.next();
        String itemID = itemElement.attributeValue("id");
        if (itemID != null) {
            PublishedItem item = node.getPublishedItem(itemID);
            if (item == null) {
                // ItemID does not exist. Return item-not-found error
                return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
            } else {
                if (item.canDelete(iq.getFrom())) {
                    items.add(item);
                } else {
                    // Publisher does not have sufficient privileges to delete this item
                    createErrorPacket(iq, PacketError.Condition.forbidden, null);
                }
            }
        } else {
            // No item ID was specified so return a bad_request error
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("item-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        }
    }
    // Send reply with success
    //router.route(IQ.createResultIQ(iq));
    // Delete items and send subscribers a notification
    leafNode.deleteItems(items);
    return createSuccessPacket(iq);
}

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

License:Open Source License

private static IQ getSubscriptions(PubSubService service, IQ iq, Element childElement) {
    // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet
    JID owner = new JID(iq.getFrom().getNode(), iq.getFrom().getDomain(), null, true);
    Element subscriptionsElement = childElement.element("subscriptions");

    String nodeID = subscriptionsElement.attributeValue("node");
    Collection<NodeSubscription> subscriptions = new ArrayList<NodeSubscription>();

    if (nodeID == null) {
        // Collect subscriptions of owner for all nodes at the service
        for (Node node : service.getNodes()) {
            subscriptions.addAll(node.getSubscriptions(owner));
        }/*w  w w.ja v  a 2s  . co m*/
    } else {
        subscriptions.addAll(service.getNode(nodeID).getSubscriptions(owner));
    }

    // Create reply to send
    IQ reply = IQ.createResultIQ(iq);
    Element replyChildElement = childElement.createCopy();
    reply.setChildElement(replyChildElement);
    Element affiliationsElement = replyChildElement.element("subscriptions");
    // Add information about subscriptions including existing affiliations
    for (NodeSubscription subscription : subscriptions) {
        Element subElement = affiliationsElement.addElement("subscription");
        Node node = subscription.getNode();
        NodeAffiliate nodeAffiliate = subscription.getAffiliate();
        // Do not include the node id when node is the root collection node
        // or the results are for a specific node
        if (!node.isRootCollectionNode() && (nodeID == null)) {
            subElement.addAttribute("node", node.getNodeID());
        }
        subElement.addAttribute("jid", subscription.getJID().toString());
        subElement.addAttribute("subscription", subscription.getState().name());
        if (node.isMultipleSubscriptionsEnabled()) {
            subElement.addAttribute("subid", subscription.getID());
        }
    }
    return reply;
}

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

License:Open Source License

private static IQ getAffiliations(PubSubService service, IQ iq, Element childElement) {
    // TODO Assuming that owner is the bare JID (as defined in the JEP). This can be replaced with an explicit owner specified in the packet
    JID owner = new JID(iq.getFrom().getNode(), iq.getFrom().getDomain(), null, true);
    // Collect affiliations of owner for all nodes at the service
    Collection<NodeAffiliate> affiliations = new ArrayList<NodeAffiliate>();
    for (Node node : service.getNodes()) {
        NodeAffiliate nodeAffiliate = node.getAffiliate(owner);
        if (nodeAffiliate != null) {
            affiliations.add(nodeAffiliate);
        }/*from www .j  a  va  2  s .  co  m*/
    }
    // Create reply to send
    IQ reply = IQ.createResultIQ(iq);
    Element replyChildElement = childElement.createCopy();
    reply.setChildElement(replyChildElement);
    if (affiliations.isEmpty()) {
        // User does not have any affiliation or subscription with the pubsub service
        reply.setError(PacketError.Condition.item_not_found);
    } else {
        Element affiliationsElement = replyChildElement.element("affiliations");
        // Add information about affiliations without subscriptions
        for (NodeAffiliate affiliate : affiliations) {
            Element affiliateElement = affiliationsElement.addElement("affiliation");
            // Do not include the node id when node is the root collection node
            if (!affiliate.getNode().isRootCollectionNode()) {
                affiliateElement.addAttribute("node", affiliate.getNode().getNodeID());
            }
            affiliateElement.addAttribute("jid", affiliate.getJID().toString());
            affiliateElement.addAttribute("affiliation", affiliate.getAffiliation().name());
        }
    }
    return reply;
}

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

License:Open Source License

private static IQ getPublishedItems(PubSubService service, IQ iq, Element itemsElement) {
    String nodeID = itemsElement.attributeValue("node");
    String subID = itemsElement.attributeValue("subid");
    Node node;//from  w w  w.j a  v a2  s.  com
    if (nodeID == null) {
        // User must specify a leaf node ID so return a nodeid-required error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    } else {
        // Look for the specified node
        node = service.getNode(nodeID);
        if (node == null) {
            // Node does not exist. Return item-not-found error
            return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
        }
    }
    if (node.isCollectionNode()) {
        // Node is a collection node. Return feature-not-implemented error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "retrieve-items");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }
    // Check if sender and subscriber JIDs match or if a valid "trusted proxy" is being used
    JID subscriberJID = iq.getFrom();
    // TODO Assumed that the owner of the subscription is the bare JID of the subscription JID. Waiting StPeter answer for explicit field.
    JID owner = new JID(subscriberJID.getNode(), subscriberJID.getDomain(), null, true);
    // Check if the node's access model allows the subscription to proceed
    AccessModel accessModel = node.getAccessModel();
    if (!accessModel.canAccessItems(node, owner, subscriberJID)) {
        return createErrorPacket(iq, accessModel.getSubsriptionError(),
                accessModel.getSubsriptionErrorDetail());
    }
    // Check that the requester is not an outcast
    NodeAffiliate affiliate = node.getAffiliate(owner);
    if (affiliate != null && affiliate.getAffiliation() == NodeAffiliate.Affiliation.outcast) {
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    // Get the user's subscription
    NodeSubscription subscription = null;
    if (node.isMultipleSubscriptionsEnabled() && (node.getSubscriptions(owner).size() > 1)) {
        if (subID == null) {
            // No subid was specified and the node supports multiple subscriptions and the user
            // has multiple subscriptions
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("subid-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        } else {
            // Check if the specified subID belongs to an existing node subscription
            subscription = node.getSubscription(subID);
            if (subscription == null) {
                Element pubsubError = DocumentHelper
                        .createElement(QName.get("invalid-subid", "http://jabber.org/protocol/pubsub#errors"));
                return createErrorPacket(iq, PacketError.Condition.not_acceptable, pubsubError);
            }
        }
    }

    if (subscription != null && !subscription.isActive()) {
        Element pubsubError = DocumentHelper
                .createElement(QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.not_authorized, pubsubError);
    }

    LeafNode leafNode = (LeafNode) node;
    // Get list of items to send to the user
    boolean forceToIncludePayload = false;
    List<PublishedItem> items;
    String max_items = itemsElement.attributeValue("max_items");
    int recentItems = 0;
    if (max_items != null) {
        try {
            // Parse the recent number of items requested
            recentItems = Integer.parseInt(max_items);
        } catch (NumberFormatException e) {
            // There was an error parsing the number so assume that all items were requested
            Log.warn("Assuming that all items were requested", e);
            max_items = null;
        }
    }
    if (max_items != null) {
        // Get the N most recent published items
        items = new ArrayList<PublishedItem>(leafNode.getPublishedItems(recentItems));
    } else {
        List requestedItems = itemsElement.elements("item");
        if (requestedItems.isEmpty()) {
            // Get all the active items that were published to the node
            items = new ArrayList<PublishedItem>(leafNode.getPublishedItems());
        } else {
            items = new ArrayList<PublishedItem>();
            // Indicate that payload should be included (if exists) no matter
            // the node configuration
            forceToIncludePayload = true;
            // Get the items as requested by the user
            for (Iterator it = requestedItems.iterator(); it.hasNext();) {
                Element element = (Element) it.next();
                String itemID = element.attributeValue("id");
                PublishedItem item = leafNode.getPublishedItem(itemID);
                if (item != null) {
                    items.add(item);
                }
            }
        }
    }

    if (subscription != null && subscription.getKeyword() != null) {
        // Filter items that do not match the subscription keyword
        for (Iterator<PublishedItem> it = items.iterator(); it.hasNext();) {
            PublishedItem item = it.next();
            if (!subscription.isKeywordMatched(item)) {
                // Remove item that does not match keyword
                it.remove();
            }
        }
    }

    // Send items to the user
    leafNode.sendPublishedItems(iq, items, forceToIncludePayload);

    //   FIXME:  Temporary return value
    return createSuccessPacket(iq);
}

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

License:Open Source License

private static IQ createNode(PubSubService service, IQ iq, Element childElement, Element createElement) {
    // Get sender of the IQ packet
    JID from = iq.getFrom();/*from w  ww .  j a  va2 s .  c o m*/
    // Verify that sender has permissions to create nodes
    if (!service.canCreateNode(from)) {
        // The user is not allowed to create nodes so return an error
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }
    DataForm completedForm = null;
    CollectionNode parentNode = null;
    String nodeID = createElement.attributeValue("node");
    String newNodeID = nodeID;
    if (nodeID == null) {
        // User requested an instant node
        if (!service.isInstantNodeSupported()) {
            // Instant nodes creation is not allowed so return an error
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.not_acceptable, pubsubError);
        }
        do {
            // Create a new nodeID and make sure that the random generated string does not
            // match an existing node. Probability to match an existing node are very very low
            // but they exist :)
            newNodeID = StringUtils.randomString(15);
        } while (service.getNode(newNodeID) != null);
    }
    boolean collectionType = false;
    // Check if user requested to configure the node (using a data form)
    Element configureElement = childElement.element("configure");
    if (configureElement != null) {
        // Get the data form that contains the parent nodeID
        completedForm = getSentConfigurationForm(configureElement);
        if (completedForm != null) {
            // Calculate newNodeID when new node is affiliated with a Collection
            FormField field = completedForm.getField("pubsub#collection");
            if (field != null) {
                List<String> values = field.getValues();
                if (!values.isEmpty()) {
                    String parentNodeID = values.get(0);
                    Node tempNode = service.getNode(parentNodeID);
                    if (tempNode == null) {
                        // Requested parent node was not found so return an error
                        return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
                    } else if (!tempNode.isCollectionNode()) {
                        // Requested parent node is not a collection node so return an error
                        return createErrorPacket(iq, PacketError.Condition.not_acceptable, null);
                    }
                    parentNode = (CollectionNode) tempNode;
                }
            }
            field = completedForm.getField("pubsub#node_type");
            if (field != null) {
                // Check if user requested to create a new collection node
                List<String> values = field.getValues();
                if (!values.isEmpty()) {
                    collectionType = "collection".equals(values.get(0));
                }
            }
        }
    }
    // If no parent was defined then use the root collection node
    if (parentNode == null && service.isCollectionNodesSupported()) {
        parentNode = service.getRootCollectionNode();
    }
    // Check that the requested nodeID does not exist
    Node existingNode = service.getNode(newNodeID);
    if (existingNode != null) {
        // There is a conflict since a node with the same ID already exists
        return createErrorPacket(iq, PacketError.Condition.conflict, null);
    }

    if (collectionType && !service.isCollectionNodesSupported()) {
        // Cannot create a collection node since the service doesn't support it
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "collections");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }

    if (parentNode != null && !collectionType) {
        // Check if requester is allowed to add a new leaf child node to the parent node
        if (!parentNode.isAssociationAllowed(from)) {
            // User is not allowed to add child leaf node to parent node. Return an error.
            return createErrorPacket(iq, PacketError.Condition.forbidden, null);
        }
        // Check if number of child leaf nodes has not been exceeded
        if (parentNode.isMaxLeafNodeReached()) {
            // Max number of child leaf nodes has been reached. Return an error.
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("max-nodes-exceeded", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.conflict, pubsubError);
        }
    }

    // Create and configure the node
    boolean conflict = false;
    Node newNode = null;
    try {
        // TODO Assumed that the owner of the subscription is the bare JID of the subscription JID. Waiting StPeter answer for explicit field.
        JID owner = new JID(from.getNode(), from.getDomain(), null, true);
        synchronized (newNodeID.intern()) {
            if (service.getNode(newNodeID) == null) {
                // Create the node
                if (collectionType) {
                    newNode = new CollectionNode(service, parentNode, newNodeID, from);
                } else {
                    newNode = new LeafNode(service, parentNode, newNodeID, from);
                }
                // Add the creator as the node owner
                newNode.addOwner(owner);
                // Configure and save the node to the backend store
                if (completedForm != null) {
                    newNode.configure(completedForm);
                } else {
                    newNode.saveToDB();
                }
            } else {
                conflict = true;
            }
        }
        if (conflict) {
            // There is a conflict since a node with the same ID already exists
            return createErrorPacket(iq, PacketError.Condition.conflict, null);
        } else {
            // Return success to the node owner
            IQ reply = IQ.createResultIQ(iq);
            // Include new nodeID if it has changed from the original nodeID
            if (!newNode.getNodeID().equals(nodeID)) {
                Element elem = reply.setChildElement("pubsub", "http://jabber.org/protocol/pubsub");
                elem.addElement("create").addAttribute("node", newNode.getNodeID());
            }
            return reply;
        }
    } catch (NotAcceptableException e) {
        // Node should have at least one owner. Return not-acceptable error.
        return createErrorPacket(iq, PacketError.Condition.not_acceptable, null);
    }
}