Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:com.adspore.splat.xep0060.models.PresenceAccess.java

License:Open Source License

@Override
public Element getSubsriptionErrorDetail() {
    return DocumentHelper.createElement(
            QName.get("presence-subscription-required", "http://jabber.org/protocol/pubsub#errors"));
}

From source file:com.adspore.splat.xep0060.models.RosterAccess.java

License:Open Source License

@Override
public Element getSubsriptionErrorDetail() {
    return DocumentHelper
            .createElement(QName.get("not-in-roster-group", "http://jabber.org/protocol/pubsub#errors"));
}

From source file:com.adspore.splat.xep0060.models.WhitelistAccess.java

License:Open Source License

@Override
public Element getSubsriptionErrorDetail() {
    return DocumentHelper.createElement(QName.get("closed-node", "http://jabber.org/protocol/pubsub#errors"));
}

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

License:Open Source License

/**
 * Handles IQ packets sent to the pubsub service. Requests of disco#info and disco#items
 * are not being handled by the engine. Instead the service itself should handle disco packets.
 *
 * @param service the PubSub service this action is to be performed for.
 * @param iq the IQ packet sent to the pubsub service.
 * @return true if the IQ packet was handled by the engine.
 *///from  www.  j ava2  s  . co m
public static IQ process(PubSubService service, IQ iq) {
    // Ignore IQs of type ERROR or RESULT
    if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
        return createErrorPacket(iq, Condition.bad_request, null);
    }

    Element childElement = iq.getChildElement();
    String namespace = null;

    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }

    //   PUBSUB
    if ("http://jabber.org/protocol/pubsub".equals(namespace)) {

        //   PUBLISH
        Element action = childElement.element("publish");
        if (action != null) {
            // Entity publishes an item
            return publishItemsToNode(service, iq, action);
        }

        //   SUBSCRIBE
        action = childElement.element("subscribe");
        if (action != null) {
            // Entity subscribes to a node
            return subscribeNode(service, iq, childElement, action);
        }

        //   OPTIONS
        action = childElement.element("options");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Subscriber requests subscription options form
                return getSubscriptionConfiguration(service, iq, childElement, action);
            } else {
                // Subscriber submits completed options form
                return configureSubscription(service, iq, action);
            }
        }

        //   CREATE
        action = childElement.element("create");
        if (action != null) {
            // Entity is requesting to create a new node
            return createNode(service, iq, childElement, action);
        }

        //   UNSUBSCRIBE
        action = childElement.element("unsubscribe");
        if (action != null) {
            // Entity unsubscribes from a node
            return unsubscribeNode(service, iq, action);
        }

        //   GET SUBSCRIPTIONS
        action = childElement.element("subscriptions");
        if (action != null) {
            // Entity requests all current subscriptions
            return getSubscriptions(service, iq, childElement);
        }

        //   GET AFFILIATIONS
        action = childElement.element("affiliations");
        if (action != null) {
            // Entity requests all current affiliations
            return getAffiliations(service, iq, childElement);
        }

        //   GET ITEMS
        action = childElement.element("items");
        if (action != null) {
            // Subscriber requests all active items
            return getPublishedItems(service, iq, action);
        }

        //   RETRACT
        action = childElement.element("retract");
        if (action != null) {
            // Entity deletes an item
            return deleteItems(service, iq, action);
        }

        // Unknown action requested
        //   TODO:  Add error packet
        return createErrorPacket(iq, PacketError.Condition.bad_request, null);
    }

    else if ("http://jabber.org/protocol/pubsub#owner".equals(namespace)) {
        //   CONFIGURE
        Element action = childElement.element("configure");
        if (action != null) {
            String nodeID = action.attributeValue("node");
            if (nodeID == null) {
                // if user is not sysadmin then return nodeid-required error
                if (!service.isServiceAdmin(iq.getFrom()) || !service.isCollectionNodesSupported()) {
                    // Configure elements must have a node attribute so answer an error
                    Element pubsubError = DocumentHelper.createElement(
                            QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
                    return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
                } else {
                    // Sysadmin is trying to configure root collection node
                    nodeID = service.getRootCollectionNode().getNodeID();
                }
            }
            if (IQ.Type.get == iq.getType()) {
                // Owner requests configuration form of a node
                return getNodeConfiguration(service, iq, childElement, nodeID);
            } else {
                // Owner submits or cancels node configuration form
                configureNode(service, iq, action, nodeID);
            }
        }

        //   DEFAULT
        action = childElement.element("default");
        if (action != null) {
            // Owner requests default configuration options for
            // leaf or collection nodes
            return getDefaultNodeConfiguration(service, iq, childElement, action);
        }

        //   DELETE
        action = childElement.element("delete");
        if (action != null) {
            // Owner deletes a node
            return deleteNode(service, iq, action);
        }

        //   SUBSCRIPTIONS
        action = childElement.element("subscriptions");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                return getNodeSubscriptions(service, iq, action);
            } else {
                return modifyNodeSubscriptions(service, iq, action);
            }
        }

        //   AFFILIATIONS
        action = childElement.element("affiliations");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                return getNodeAffiliations(service, iq, action);
            } else {
                return modifyNodeAffiliations(service, iq, action);
            }
        }

        //   PURGE
        action = childElement.element("purge");
        if (action != null) {
            // Owner purges items from a node
            return purgeNode(service, iq, action);

        }
    }

    //   TODO:  Temporarily blocking commands; this should be handled by the command service, not pubsub
    //        //   GET COMMANDS
    //        else if (SplatNamespaces.NAMESPACE_COMMANDS.equals(namespace)) {
    //            // Process ad-hoc command
    //            IQ reply = service.getManager().process(iq);
    //            router.route(reply);
    //            return true;
    //        }
    return createErrorPacket(iq, PacketError.Condition.bad_request, null);
}

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  ww  . j ava 2 s .  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  www. j a  v a 2  s.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 subscribeNode(PubSubService service, IQ iq, Element childElement, Element subscribeElement) {
    String nodeID = subscribeElement.attributeValue("node");
    Node node;/*from w  ww. ja v a  2  s . c o m*/
    if (nodeID == null) {
        if (service.isCollectionNodesSupported()) {
            // Entity subscribes to root collection node
            node = service.getRootCollectionNode();
        } else {
            // Service does not have a root collection node 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);
        }
    }
    // Check if sender and subscriber JIDs match or if a valid "trusted proxy" is being used
    JID from = iq.getFrom();
    JID subscriberJID = new JID(subscribeElement.attributeValue("jid"));
    if (!from.toBareJID().equals(subscriberJID.toBareJID()) && !service.isServiceAdmin(from)) {
        // JIDs do not match and requestor is not a service admin so return an error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("invalid-jid", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }
    // 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.canSubscribe(node, owner, subscriberJID)) {
        return createErrorPacket(iq, accessModel.getSubsriptionError(),
                accessModel.getSubsriptionErrorDetail());
    }
    //   TODO:  Not supporting anonymous...
    // Check if the subscriber is an anonymous user
    //        if (!UserManager.getInstance().isRegisteredUser(subscriberJID)) {
    //            // Anonymous users cannot subscribe to the node. Return forbidden error
    //            sendErrorPacket(iq, PacketError.Condition.forbidden, null);
    //            return;
    //        }
    // Check if the subscription owner is a user with outcast affiliation
    NodeAffiliate nodeAffiliate = node.getAffiliate(owner);
    if (nodeAffiliate != null && nodeAffiliate.getAffiliation() == NodeAffiliate.Affiliation.outcast) {
        // Subscriber is an outcast. Return forbidden error
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }
    // Check that subscriptions to the node are enabled
    if (!node.isSubscriptionEnabled() && !service.isServiceAdmin(from)) {
        // Sender is not a sysadmin and subscription is disabled so return an error
        return createErrorPacket(iq, PacketError.Condition.not_allowed, null);
    }

    // Get any configuration form included in the options element (if any)
    DataForm optionsForm = null;
    Element options = childElement.element("options");
    if (options != null) {
        Element formElement = options.element(QName.get("x", "jabber:x:data"));
        if (formElement != null) {
            optionsForm = new DataForm(formElement);
        }
    }

    // If leaf node does not support multiple subscriptions then check whether subscriber is
    // creating another subscription or not
    if (!node.isCollectionNode() && !node.isMultipleSubscriptionsEnabled()) {
        NodeSubscription existingSubscription = node.getSubscription(subscriberJID);
        if (existingSubscription != null) {
            // User is trying to create another subscription so
            // return current subscription state
            existingSubscription.sendSubscriptionState(iq);
            return createSuccessPacket(iq);
        }
    }

    // Check if subscribing twice to a collection node using same subscription type
    if (node.isCollectionNode()) {
        // By default assume that new subscription is of type node
        boolean isNodeType = true;
        if (optionsForm != null) {
            FormField field = optionsForm.getField("pubsub#subscription_type");
            if (field != null) {
                if ("items".equals(field.getValues().get(0))) {
                    isNodeType = false;
                }
            }
        }
        if (nodeAffiliate != null) {
            for (NodeSubscription subscription : nodeAffiliate.getSubscriptions()) {
                if (isNodeType) {
                    // User is requesting a subscription of type "nodes"
                    if (NodeSubscription.Type.nodes == subscription.getType()) {
                        // Cannot have 2 subscriptions of the same type. Return conflict error
                        return createErrorPacket(iq, PacketError.Condition.conflict, null);
                    }
                } else if (!node.isMultipleSubscriptionsEnabled()) {
                    // User is requesting a subscription of type "items" and
                    // multiple subscriptions is not allowed
                    if (NodeSubscription.Type.items == subscription.getType()) {
                        // User is trying to create another subscription so
                        // return current subscription state
                        subscription.sendSubscriptionState(iq);
                        return createSuccessPacket(iq);
                    }
                }
            }
        }
    }

    // Create a subscription and an affiliation if the subscriber doesn't have one
    return node.createSubscription(iq, owner, subscriberJID, accessModel.isAuthorizationRequired(),
            optionsForm);
}

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

License:Open Source License

private static IQ unsubscribeNode(PubSubService service, IQ iq, Element unsubscribeElement) {
    String nodeID = unsubscribeElement.attributeValue("node");
    String subID = unsubscribeElement.attributeValue("subid");
    String jidAttribute = unsubscribeElement.attributeValue("jid");

    // Check if the specified JID has a subscription with the node
    if (jidAttribute == null) {
        // No JID was specified so return an error indicating that jid is required
        Element pubsubError = DocumentHelper
                .createElement(QName.get("jid-required", "http://jabber.org/protocol/pubsub#errors"));
        return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
    }/* www  .  ja  v  a  2 s  .c  om*/
    Node node;

    if (nodeID == null) {
        if (service.isCollectionNodesSupported()) {
            // Entity unsubscribes from root collection node
            node = service.getRootCollectionNode();
        } else {
            // Service does not have a root collection node 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);
        }
    }
    NodeSubscription subscription = null;
    JID owner = new JID(jidAttribute);

    if (node.isMultipleSubscriptionsEnabled() && node.getSubscriptions(owner).size() > 1) {
        if (subID == null) {
            // No subid was specified and the node supports multiple subscriptions
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("subid-required", "http://jabber.org/protocol/pubsub#errors"));
            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);
            }
        }
    } else {
        JID subscriberJID = new JID(jidAttribute);
        subscription = node.getSubscription(subscriberJID);
        if (subscription == null) {
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.unexpected_request, pubsubError);
        }
    }
    JID from = iq.getFrom();
    // Check that unsubscriptions to the node are enabled
    if (!node.isSubscriptionEnabled() && !service.isServiceAdmin(from)) {
        // Sender is not a sysadmin and unsubscription is disabled so return an error
        return createErrorPacket(iq, PacketError.Condition.not_allowed, null);
    }

    // A subscription was found so check if the user is allowed to cancel the subscription
    if (!subscription.canModify(from) && !subscription.canModify(owner)) {
        // Requestor is prohibited from unsubscribing entity
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    // Cancel subscription
    node.cancelSubscription(subscription);
    // Send reply with success
    //router.route(IQ.createResultIQ(iq));
    return createSuccessPacket(iq);
}

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

License:Open Source License

private static IQ getSubscriptionConfiguration(PubSubService service, IQ iq, Element childElement,
        Element optionsElement) {
    String nodeID = optionsElement.attributeValue("node");
    String subID = optionsElement.attributeValue("subid");
    Node node;/*from w w  w. ja v  a2  s  .c om*/
    if (nodeID == null) {
        if (service.isCollectionNodesSupported()) {
            // Entity requests subscription options of root collection node
            node = service.getRootCollectionNode();
        } else {
            // Service does not have a root collection node 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);
        }
    }
    NodeSubscription subscription;
    if (node.isMultipleSubscriptionsEnabled()) {
        if (subID == null) {
            // No subid was specified and the node supports 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);
            }
        }
    } else {
        // Check if the specified JID has a subscription with the node
        String jidAttribute = optionsElement.attributeValue("jid");
        if (jidAttribute == null) {
            // No JID was specified so return an error indicating that jid is required
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("jid-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        }
        JID subscriberJID = new JID(jidAttribute);
        subscription = node.getSubscription(subscriberJID);
        if (subscription == null) {
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.unexpected_request, pubsubError);
        }
    }

    // A subscription was found so check if the user is allowed to get the subscription options
    if (!subscription.canModify(iq.getFrom())) {
        // Requestor is prohibited from getting the subscription options
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    // Return data form containing subscription configuration to the subscriber
    IQ reply = IQ.createResultIQ(iq);
    Element replyChildElement = childElement.createCopy();
    reply.setChildElement(replyChildElement);
    replyChildElement.element("options").add(subscription.getConfigurationForm().getElement());
    return reply;
}

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

License:Open Source License

private static IQ configureSubscription(PubSubService service, IQ iq, Element optionsElement) {
    String nodeID = optionsElement.attributeValue("node");
    String subID = optionsElement.attributeValue("subid");
    Node node;//  w  ww.j  av  a2 s.c  om
    if (nodeID == null) {
        if (service.isCollectionNodesSupported()) {
            // Entity submits new subscription options of root collection node
            node = service.getRootCollectionNode();
        } else {
            // Service does not have a root collection node 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);
        }
    }
    NodeSubscription subscription;
    if (node.isMultipleSubscriptionsEnabled()) {
        if (subID == null) {
            // No subid was specified and the node supports 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);
            }
        }
    } else {
        // Check if the specified JID has a subscription with the node
        String jidAttribute = optionsElement.attributeValue("jid");
        if (jidAttribute == null) {
            // No JID was specified so return an error indicating that jid is required
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("jid-required", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        }
        JID subscriberJID = new JID(jidAttribute);
        subscription = node.getSubscription(subscriberJID);
        if (subscription == null) {
            Element pubsubError = DocumentHelper
                    .createElement(QName.get("not-subscribed", "http://jabber.org/protocol/pubsub#errors"));
            return createErrorPacket(iq, PacketError.Condition.unexpected_request, pubsubError);
        }
    }

    // A subscription was found so check if the user is allowed to submits
    // new subscription options
    if (!subscription.canModify(iq.getFrom())) {
        // Requestor is prohibited from setting new subscription options
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    Element formElement = optionsElement.element(QName.get("x", "jabber:x:data"));
    if (formElement != null) {
        // Change the subscription configuration based on the completed form
        subscription.configure(iq, new DataForm(formElement));
    } else {
        // No data form was included so return bad request error
        return createErrorPacket(iq, PacketError.Condition.bad_request, null);
    }
    return createSuccessPacket(iq);
}