Search in sources :

Example 51 with DataForm

use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.

the class PubSubEngine method subscribeNode.

private void subscribeNode(PubSubService service, IQ iq, Element childElement, Element subscribeElement) {
    String nodeID = subscribeElement.attributeValue("node");
    Node node;
    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"));
            sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
            return;
        }
    } else {
        // Look for the specified node
        node = service.getNode(nodeID);
        if (node == null) {
            // Node does not exist. Return item-not-found error
            sendErrorPacket(iq, PacketError.Condition.item_not_found, null);
            return;
        }
    }
    // 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"));
        sendErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
        return;
    }
    // TODO Assumed that the owner of the subscription is the bare JID of the subscription JID. Waiting StPeter answer for explicit field.
    JID owner = subscriberJID.asBareJID();
    // Check if the node's access model allows the subscription to proceed
    AccessModel accessModel = node.getAccessModel();
    if (!accessModel.canSubscribe(node, owner, subscriberJID)) {
        sendErrorPacket(iq, accessModel.getSubsriptionError(), accessModel.getSubsriptionErrorDetail());
        return;
    }
    // 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
        sendErrorPacket(iq, PacketError.Condition.forbidden, null);
        return;
    }
    // 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
        sendErrorPacket(iq, PacketError.Condition.not_allowed, null);
        return;
    }
    // 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);
        }
    }
    // 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;
        }
    }
    // 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
                        sendErrorPacket(iq, PacketError.Condition.conflict, null);
                        return;
                    }
                } else if (!node.isMultipleSubscriptionsEnabled()) {
                    // 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;
                    }
                }
            }
        }
    }
    // Create a subscription and an affiliation if the subscriber doesn't have one
    node.createSubscription(iq, owner, subscriberJID, accessModel.isAuthorizationRequired(), optionsForm);
}
Also used : JID(org.xmpp.packet.JID) AccessModel(org.jivesoftware.openfire.pubsub.models.AccessModel) Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 52 with DataForm

use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.

the class PubSubEngine method process.

/**
     * Handles Message packets sent to the pubsub service. Messages may be of type error
     * when an event notification was sent to a susbcriber whose address is no longer available.<p>
     *
     * Answers to authorization requests sent to node owners to approve pending subscriptions
     * will also be processed by this method.
     *
     * @param service the PubSub service this action is to be performed for.
     * @param message the Message packet sent to the pubsub service.
     */
public void process(PubSubService service, Message message) {
    if (message.getType() == Message.Type.error) {
        // Process Messages of type error to identify possible subscribers that no longer exist
        if (message.getError().getType() == PacketError.Type.cancel) {
            // 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 = message.getFrom().asBareJID();
            // Terminate the subscription of the entity to all nodes hosted at the service
            cancelAllSubscriptions(service, owner);
        } else if (message.getError().getType() == PacketError.Type.auth) {
        // TODO Queue the message to be sent again later (will retry a few times and
        // will be discarded when the retry limit is reached)
        }
    } else if (message.getType() == Message.Type.normal) {
        // Check that this is an answer to an authorization request
        DataForm authForm = (DataForm) message.getExtension("x", "jabber:x:data");
        if (authForm != null && authForm.getType() == DataForm.Type.submit) {
            String formType = authForm.getField("FORM_TYPE").getValues().get(0);
            // Check that completed data form belongs to an authorization request
            if ("http://jabber.org/protocol/pubsub#subscribe_authorization".equals(formType)) {
                // Process the answer to the authorization request
                processAuthorizationAnswer(service, authForm, message);
            }
        }
    }
}
Also used : JID(org.xmpp.packet.JID) DataForm(org.xmpp.forms.DataForm)

Example 53 with DataForm

use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.

the class PubSubEngine method getSentConfigurationForm.

/**
     * Returns the data form included in the configure element sent by the node owner or
     * <tt>null</tt> if none was included or access model was defined. If the
     * owner just wants to set the access model to use for the node and optionally set the
     * list of roster groups (i.e. contacts present in the node owner roster in the
     * specified groups are allowed to access the node) allowed to access the node then
     * instead of including a data form the owner can just specify the "access" attribute
     * of the configure element and optionally include a list of group elements. In this case,
     * the method will create a data form including the specified data. This is a nice way
     * to accept both ways to configure a node but always returning a data form.
     *
     * @param configureElement the configure element sent by the owner.
     * @return the data form included in the configure element sent by the node owner or
     *         <tt>null</tt> if none was included or access model was defined.
     */
private DataForm getSentConfigurationForm(Element configureElement) {
    DataForm completedForm = null;
    FormField formField;
    Element formElement = configureElement.element(QName.get("x", "jabber:x:data"));
    if (formElement != null) {
        completedForm = new DataForm(formElement);
    }
    String accessModel = configureElement.attributeValue("access");
    if (accessModel != null) {
        if (completedForm == null) {
            // Create a form (i.e. simulate that the user sent a form with roster groups)
            completedForm = new DataForm(DataForm.Type.submit);
            // Add the hidden field indicating that this is a node config form
            formField = completedForm.addField();
            formField.setVariable("FORM_TYPE");
            formField.setType(FormField.Type.hidden);
            formField.addValue("http://jabber.org/protocol/pubsub#node_config");
        }
        if (completedForm.getField("pubsub#access_model") == null) {
            // Add the field that will specify the access model of the node
            formField = completedForm.addField();
            formField.setVariable("pubsub#access_model");
            formField.addValue(accessModel);
        } else {
            Log.debug("PubSubEngine: Owner sent access model in data form and as attribute: " + configureElement.asXML());
        }
        // Check if a list of groups was specified
        List groups = configureElement.elements("group");
        if (!groups.isEmpty()) {
            // Add the field that will contain the specified groups
            formField = completedForm.addField();
            formField.setVariable("pubsub#roster_groups_allowed");
            // Add each group as a value of the groups field
            for (Iterator it = groups.iterator(); it.hasNext(); ) {
                formField.addValue(((Element) it.next()).getTextTrim());
            }
        }
    }
    return completedForm;
}
Also used : Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) FormField(org.xmpp.forms.FormField)

Example 54 with DataForm

use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.

the class Node method getAuthRequestForm.

/**
     * Returns the data form to be included in the authorization request to be sent to
     * node owners when a new subscription needs to be approved.
     *
     * @param subscription the new subscription that needs to be approved.
     * @return the data form to be included in the authorization request.
     */
DataForm getAuthRequestForm(NodeSubscription subscription) {
    DataForm form = new DataForm(DataForm.Type.form);
    form.setTitle(LocaleUtils.getLocalizedString("pubsub.form.authorization.title"));
    form.addInstruction(LocaleUtils.getLocalizedString("pubsub.form.authorization.instruction"));
    FormField formField = form.addField();
    formField.setVariable("FORM_TYPE");
    formField.setType(FormField.Type.hidden);
    formField.addValue("http://jabber.org/protocol/pubsub#subscribe_authorization");
    formField = form.addField();
    formField.setVariable("pubsub#subid");
    formField.setType(FormField.Type.hidden);
    formField.addValue(subscription.getID());
    formField = form.addField();
    formField.setVariable("pubsub#node");
    formField.setType(FormField.Type.text_single);
    formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.node"));
    formField.addValue(getNodeID());
    formField = form.addField();
    formField.setVariable("pubsub#subscriber_jid");
    formField.setType(FormField.Type.jid_single);
    formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.subscriber"));
    formField.addValue(subscription.getJID().toString());
    formField = form.addField();
    formField.setVariable("pubsub#allow");
    formField.setType(FormField.Type.boolean_type);
    formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.allow"));
    formField.addValue(Boolean.FALSE);
    return form;
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 55 with DataForm

use of org.xmpp.forms.DataForm in project Openfire by igniterealtime.

the class CreateWorkgroup method addStageInformation.

@Override
protected void addStageInformation(SessionData data, Element command) {
    DataForm form = new DataForm(DataForm.Type.form);
    form.setTitle("Adding a new workgroup");
    form.addInstruction("Fill out this form to add a workgroup.");
    FormField field = form.addField();
    field.setType(FormField.Type.hidden);
    field.setVariable("FORM_TYPE");
    field.addValue("http://jabber.org/protocol/admin");
    field = form.addField();
    field.setType(FormField.Type.text_single);
    field.setLabel("The name of the workgroup to be added");
    field.setVariable("name");
    field.setRequired(true);
    field = form.addField();
    field.setType(FormField.Type.text_multi);
    field.setLabel("Username of the members");
    field.setVariable("members");
    field = form.addField();
    field.setType(FormField.Type.text_single);
    field.setLabel("Description");
    field.setVariable("description");
    // Add the form to the command
    command.add(form.getElement());
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Aggregations

DataForm (org.xmpp.forms.DataForm)81 FormField (org.xmpp.forms.FormField)67 Element (org.dom4j.Element)23 IQ (org.xmpp.packet.IQ)12 JID (org.xmpp.packet.JID)9 ArrayList (java.util.ArrayList)7 ClientSession (org.jivesoftware.openfire.session.ClientSession)6 HashMap (java.util.HashMap)4 List (java.util.List)4 Group (org.jivesoftware.openfire.group.Group)4 Date (java.util.Date)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)3 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)3 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)3 XStream (com.thoughtworks.xstream.XStream)2 ParseException (java.text.ParseException)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 PacketException (org.jivesoftware.openfire.PacketException)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2