Search in sources :

Example 46 with FormField

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

the class IQDiscoInfoHandlerTest method testGetFirstDataFormWithOneNotNull.

/**
 * Test for {@link IQDiscoInfoHandler#getFirstDataForm(dataForms)}. Verifies that an input argument that
 * a collection with exactly one item (that is not null) returns a not null Dataform.
 */
@Test
public void testGetFirstDataFormWithOneNotNull() throws Exception {
    // Setup fixture.
    final DataForm dataForm = new DataForm(DataForm.Type.result);
    final FormField fieldType = dataForm.addField();
    fieldType.setVariable("FORM_TYPE");
    fieldType.setType(FormField.Type.hidden);
    fieldType.addValue("http://jabber.org/network/serverinfo");
    final Set<DataForm> dataForms = new HashSet<>();
    dataForms.add(dataForm);
    // Execute system under test.
    final DataForm result = IQDiscoInfoHandler.getFirstDataForm(dataForms);
    // Verify results.
    Assert.assertNotNull(result);
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with FormField

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

the class IQOwnerHandler method init.

private void init() {
    Element element = DocumentHelper.createElement(QName.get("query", "http://jabber.org/protocol/muc#owner"));
    configurationForm = new DataForm(DataForm.Type.form);
    configurationForm.setTitle(LocaleUtils.getLocalizedString("muc.form.conf.title"));
    List<String> params = new ArrayList<>();
    params.add(room.getName());
    configurationForm.addInstruction(LocaleUtils.getLocalizedString("muc.form.conf.instruction", params));
    configurationForm.addField("FORM_TYPE", null, Type.hidden).addValue("http://jabber.org/protocol/muc#roomconfig");
    configurationForm.addField("muc#roomconfig_roomname", LocaleUtils.getLocalizedString("muc.form.conf.owner_roomname"), Type.text_single);
    configurationForm.addField("muc#roomconfig_roomdesc", LocaleUtils.getLocalizedString("muc.form.conf.owner_roomdesc"), Type.text_single);
    configurationForm.addField("muc#roomconfig_changesubject", LocaleUtils.getLocalizedString("muc.form.conf.owner_changesubject"), Type.boolean_type);
    final FormField maxUsers = configurationForm.addField("muc#roomconfig_maxusers", LocaleUtils.getLocalizedString("muc.form.conf.owner_maxusers"), Type.list_single);
    maxUsers.addOption("10", "10");
    maxUsers.addOption("20", "20");
    maxUsers.addOption("30", "30");
    maxUsers.addOption("40", "40");
    maxUsers.addOption("50", "50");
    maxUsers.addOption(LocaleUtils.getLocalizedString("muc.form.conf.none"), "0");
    final FormField broadcast = configurationForm.addField("muc#roomconfig_presencebroadcast", LocaleUtils.getLocalizedString("muc.form.conf.owner_presencebroadcast"), Type.list_multi);
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.moderator"), "moderator");
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.participant"), "participant");
    broadcast.addOption(LocaleUtils.getLocalizedString("muc.form.conf.visitor"), "visitor");
    configurationForm.addField("muc#roomconfig_publicroom", LocaleUtils.getLocalizedString("muc.form.conf.owner_publicroom"), Type.boolean_type);
    configurationForm.addField("muc#roomconfig_persistentroom", LocaleUtils.getLocalizedString("muc.form.conf.owner_persistentroom"), Type.boolean_type);
    configurationForm.addField("muc#roomconfig_moderatedroom", LocaleUtils.getLocalizedString("muc.form.conf.owner_moderatedroom"), Type.boolean_type);
    configurationForm.addField("muc#roomconfig_membersonly", LocaleUtils.getLocalizedString("muc.form.conf.owner_membersonly"), Type.boolean_type);
    configurationForm.addField(null, null, Type.fixed).addValue(LocaleUtils.getLocalizedString("muc.form.conf.allowinvitesfixed"));
    configurationForm.addField("muc#roomconfig_allowinvites", LocaleUtils.getLocalizedString("muc.form.conf.owner_allowinvites"), Type.boolean_type);
    configurationForm.addField("muc#roomconfig_passwordprotectedroom", LocaleUtils.getLocalizedString("muc.form.conf.owner_passwordprotectedroom"), Type.boolean_type);
    configurationForm.addField(null, null, Type.fixed).addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomsecretfixed"));
    configurationForm.addField("muc#roomconfig_roomsecret", LocaleUtils.getLocalizedString("muc.form.conf.owner_roomsecret"), Type.text_private);
    final FormField whois = configurationForm.addField("muc#roomconfig_whois", LocaleUtils.getLocalizedString("muc.form.conf.owner_whois"), Type.list_single);
    whois.addOption(LocaleUtils.getLocalizedString("muc.form.conf.moderator"), "moderators");
    whois.addOption(LocaleUtils.getLocalizedString("muc.form.conf.anyone"), "anyone");
    final FormField allowpm = configurationForm.addField("muc#roomconfig_allowpm", LocaleUtils.getLocalizedString("muc.form.conf.owner_allowpm"), Type.list_single);
    allowpm.addOption(LocaleUtils.getLocalizedString("muc.form.conf.anyone"), "anyone");
    allowpm.addOption(LocaleUtils.getLocalizedString("muc.form.conf.moderator"), "moderators");
    allowpm.addOption(LocaleUtils.getLocalizedString("muc.form.conf.participant"), "participants");
    allowpm.addOption(LocaleUtils.getLocalizedString("muc.form.conf.none"), "none");
    configurationForm.addField("muc#roomconfig_enablelogging", LocaleUtils.getLocalizedString("muc.form.conf.owner_enablelogging"), Type.boolean_type);
    configurationForm.addField("x-muc#roomconfig_reservednick", LocaleUtils.getLocalizedString("muc.form.conf.owner_reservednick"), Type.boolean_type);
    configurationForm.addField("x-muc#roomconfig_canchangenick", LocaleUtils.getLocalizedString("muc.form.conf.owner_canchangenick"), Type.boolean_type);
    configurationForm.addField(null, null, Type.fixed).addValue(LocaleUtils.getLocalizedString("muc.form.conf.owner_registration"));
    configurationForm.addField("x-muc#roomconfig_registration", LocaleUtils.getLocalizedString("muc.form.conf.owner_registration"), Type.boolean_type);
    configurationForm.addField(null, null, Type.fixed).addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomadminsfixed"));
    configurationForm.addField("muc#roomconfig_roomadmins", LocaleUtils.getLocalizedString("muc.form.conf.owner_roomadmins"), Type.jid_multi);
    configurationForm.addField(null, null, Type.fixed).addValue(LocaleUtils.getLocalizedString("muc.form.conf.roomownersfixed"));
    configurationForm.addField("muc#roomconfig_roomowners", LocaleUtils.getLocalizedString("muc.form.conf.owner_roomowners"), Type.jid_multi);
    // Create the probeResult and add the basic info together with the configuration form
    probeResult = element;
    probeResult.add(configurationForm.getElement());
}
Also used : Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 48 with FormField

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

the class IQOwnerHandler method refreshConfigurationFormValues.

private void refreshConfigurationFormValues() {
    synchronized (room) {
        FormField field = configurationForm.getField("muc#roomconfig_roomname");
        field.clearValues();
        field.addValue(room.getNaturalLanguageName());
        field = configurationForm.getField("muc#roomconfig_roomdesc");
        field.clearValues();
        field.addValue(room.getDescription());
        field = configurationForm.getField("muc#roomconfig_changesubject");
        field.clearValues();
        field.addValue((room.canOccupantsChangeSubject() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_maxusers");
        field.clearValues();
        field.addValue(Integer.toString(room.getMaxUsers()));
        field = configurationForm.getField("muc#roomconfig_presencebroadcast");
        field.clearValues();
        for (MUCRole.Role roleToBroadcast : room.getRolesToBroadcastPresence()) {
            field.addValue(roleToBroadcast.toString());
        }
        field = configurationForm.getField("muc#roomconfig_publicroom");
        field.clearValues();
        field.addValue((room.isPublicRoom() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_persistentroom");
        field.clearValues();
        field.addValue((room.isPersistent() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_moderatedroom");
        field.clearValues();
        field.addValue((room.isModerated() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_membersonly");
        field.clearValues();
        field.addValue((room.isMembersOnly() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_allowinvites");
        field.clearValues();
        field.addValue((room.canOccupantsInvite() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_passwordprotectedroom");
        field.clearValues();
        field.addValue((room.isPasswordProtected() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_roomsecret");
        field.clearValues();
        field.addValue(room.getPassword());
        field = configurationForm.getField("muc#roomconfig_whois");
        field.clearValues();
        field.addValue((room.canAnyoneDiscoverJID() ? "anyone" : "moderators"));
        field = configurationForm.getField("muc#roomconfig_allowpm");
        field.clearValues();
        field.addValue((room.canSendPrivateMessage()));
        field = configurationForm.getField("muc#roomconfig_enablelogging");
        field.clearValues();
        field.addValue((room.isLogEnabled() ? "1" : "0"));
        field = configurationForm.getField("x-muc#roomconfig_reservednick");
        field.clearValues();
        field.addValue((room.isLoginRestrictedToNickname() ? "1" : "0"));
        field = configurationForm.getField("x-muc#roomconfig_canchangenick");
        field.clearValues();
        field.addValue((room.canChangeNickname() ? "1" : "0"));
        field = configurationForm.getField("x-muc#roomconfig_registration");
        field.clearValues();
        field.addValue((room.isRegistrationEnabled() ? "1" : "0"));
        field = configurationForm.getField("muc#roomconfig_roomadmins");
        field.clearValues();
        for (JID jid : room.getAdmins()) {
            if (GroupJID.isGroup(jid)) {
                try {
                    // add each group member to the result (clients don't understand groups)
                    Group group = GroupManager.getInstance().getGroup(jid);
                    for (JID groupMember : group.getAll()) {
                        field.addValue(groupMember);
                    }
                } catch (GroupNotFoundException gnfe) {
                    Log.warn("Invalid group JID in the member list: " + jid);
                }
            } else {
                field.addValue(jid.toString());
            }
        }
        field = configurationForm.getField("muc#roomconfig_roomowners");
        field.clearValues();
        for (JID jid : room.getOwners()) {
            if (GroupJID.isGroup(jid)) {
                try {
                    // add each group member to the result (clients don't understand groups)
                    Group group = GroupManager.getInstance().getGroup(jid);
                    for (JID groupMember : group.getAll()) {
                        field.addValue(groupMember);
                    }
                } catch (GroupNotFoundException gnfe) {
                    Log.warn("Invalid group JID in the member list: " + jid);
                }
            } else {
                field.addValue(jid.toString());
            }
        }
        // Remove the old element
        probeResult.remove(probeResult.element(QName.get("x", "jabber:x:data")));
        // Add the new representation of configurationForm as an element
        probeResult.add(configurationForm.getElement());
    }
}
Also used : Group(org.jivesoftware.openfire.group.Group) GroupJID(org.jivesoftware.openfire.group.GroupJID) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) FormField(org.xmpp.forms.FormField)

Example 49 with FormField

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

the class SearchPlugin method processGetPacket.

/**
	 * Processes an IQ stanza of type 'get', which in the context of 'Jabber Search' is a request for available search fields.
	 * 
	 * @param packet
	 *            An IQ stanza of type 'get'
	 * @return A result IQ stanza that contains the possbile search fields.
	 */
private IQ processGetPacket(IQ packet) {
    if (!packet.getType().equals(IQ.Type.get)) {
        throw new IllegalArgumentException("This method only accepts 'get' typed IQ stanzas as an argument.");
    }
    IQ replyPacket = IQ.createResultIQ(packet);
    Element queryResult = DocumentHelper.createElement(QName.get("query", NAMESPACE_JABBER_IQ_SEARCH));
    String instructions = LocaleUtils.getLocalizedString("advance.user.search.details", "search");
    // non-data form
    queryResult.addElement("instructions").addText(instructions);
    queryResult.addElement("first");
    queryResult.addElement("last");
    queryResult.addElement("nick");
    queryResult.addElement("email");
    DataForm searchForm = new DataForm(DataForm.Type.form);
    searchForm.setTitle(LocaleUtils.getLocalizedString("advance.user.search.title", "search"));
    searchForm.addInstruction(instructions);
    searchForm.addField("FORM_TYPE", null, FormField.Type.hidden).addValue(NAMESPACE_JABBER_IQ_SEARCH);
    searchForm.addField("search", LocaleUtils.getLocalizedString("advance.user.search.search", "search"), FormField.Type.text_single).setRequired(true);
    for (String searchField : getFilteredSearchFields()) {
        final FormField field = searchForm.addField();
        field.setVariable(searchField);
        field.setType(FormField.Type.boolean_type);
        field.addValue("1");
        field.setLabel(LocaleUtils.getLocalizedString("advance.user.search." + searchField.toLowerCase(), "search"));
        field.setRequired(false);
    }
    queryResult.add(searchForm.getElement());
    replyPacket.setChildElement(queryResult);
    return replyPacket;
}
Also used : Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 50 with FormField

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

the class PubSubEngine method createNodeHelper.

/**
     * Checks if the following conditions are satisfied and creates a node
     * - Requester can create nodes
     * - Instant node creation is enabled
     * - Node does not already exist
     * - New node configuration is valid
     *
     * NOTE: This method should not reply to the client
     *
     * @param service
     * @param iq
     * @param childElement
     * @param createElement
     * @return {@link CreateNodeResponse}
     */
private CreateNodeResponse createNodeHelper(PubSubService service, IQ iq, Element childElement, Element createElement) {
    // Get sender of the IQ packet
    JID from = iq.getFrom();
    // Verify that sender has permissions to create nodes
    if (!service.canCreateNode(from) || (!UserManager.getInstance().isRegisteredUser(from) && !isComponent(from))) {
        // The user is not allowed to create nodes so return an error
        return new CreateNodeResponse(PacketError.Condition.forbidden, null, 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 new CreateNodeResponse(PacketError.Condition.not_acceptable, pubsubError, null);
        }
        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 new CreateNodeResponse(PacketError.Condition.item_not_found, null, null);
                    } else if (!tempNode.isCollectionNode()) {
                        // Requested parent node is not a collection node so return an error
                        return new CreateNodeResponse(PacketError.Condition.not_acceptable, null, 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 new CreateNodeResponse(PacketError.Condition.conflict, null, 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 new CreateNodeResponse(PacketError.Condition.feature_not_implemented, pubsubError, null);
    }
    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 new CreateNodeResponse(PacketError.Condition.forbidden, null, 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 new CreateNodeResponse(PacketError.Condition.conflict, pubsubError, null);
        }
    }
    // 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 = from.asBareJID();
        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();
                }
                CacheFactory.doClusterTask(new RefreshNodeTask(newNode));
            } else {
                conflict = true;
            }
        }
        if (conflict) {
            // There is a conflict since a node with the same ID already exists
            return new CreateNodeResponse(PacketError.Condition.conflict, null, null);
        } else {
            // Return success to the node owner
            return new CreateNodeResponse(null, null, newNode);
        }
    } catch (NotAcceptableException e) {
        // Node should have at least one owner. Return not-acceptable error.
        return new CreateNodeResponse(PacketError.Condition.not_acceptable, null, null);
    }
}
Also used : JID(org.xmpp.packet.JID) Element(org.dom4j.Element) RefreshNodeTask(org.jivesoftware.openfire.pubsub.cluster.RefreshNodeTask) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Aggregations

FormField (org.xmpp.forms.FormField)92 DataForm (org.xmpp.forms.DataForm)80 Element (org.dom4j.Element)21 JID (org.xmpp.packet.JID)13 IQ (org.xmpp.packet.IQ)7 HashSet (java.util.HashSet)6 ClientSession (org.jivesoftware.openfire.session.ClientSession)6 ArrayList (java.util.ArrayList)4 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)4 ParseException (java.text.ParseException)3 Date (java.util.Date)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)3 Group (org.jivesoftware.openfire.group.Group)3 Test (org.junit.Test)3 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 PacketException (org.jivesoftware.openfire.PacketException)2 GroupJID (org.jivesoftware.openfire.group.GroupJID)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2 MUCRoom (org.jivesoftware.openfire.muc.MUCRoom)2