Search in sources :

Example 61 with FormField

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

the class IQMUCSearchHandler method getDataElement.

/**
 * Utility method that returns a 'jabber:iq:search' child element filled
 * with a blank dataform.
 *
 * @return Element, named 'query', escaped by the 'jabber:iq:search'
 *         namespace, filled with a blank dataform.
 */
private static Element getDataElement() {
    final DataForm searchForm = new DataForm(DataForm.Type.form);
    searchForm.setTitle("Chat Rooms Search");
    searchForm.addInstruction("Instructions");
    final FormField typeFF = searchForm.addField();
    typeFF.setVariable("FORM_TYPE");
    typeFF.setType(FormField.Type.hidden);
    typeFF.addValue(JABBER_IQ_SEARCH);
    final FormField nameFF = searchForm.addField();
    nameFF.setVariable("name");
    nameFF.setType(FormField.Type.text_single);
    nameFF.setLabel("Name");
    nameFF.setRequired(false);
    final FormField matchFF = searchForm.addField();
    matchFF.setVariable("name_is_exact_match");
    matchFF.setType(FormField.Type.boolean_type);
    matchFF.setLabel("Name must match exactly");
    matchFF.setRequired(false);
    final FormField subjectFF = searchForm.addField();
    subjectFF.setVariable(SUBJECT);
    subjectFF.setType(FormField.Type.text_single);
    subjectFF.setLabel("Subject");
    subjectFF.setRequired(false);
    final FormField userAmountFF = searchForm.addField();
    userAmountFF.setVariable(NUM_USERS);
    userAmountFF.setType(FormField.Type.text_single);
    userAmountFF.setLabel("Number of users");
    userAmountFF.setRequired(false);
    final FormField maxUsersFF = searchForm.addField();
    maxUsersFF.setVariable(NUM_MAX_USERS);
    maxUsersFF.setType(FormField.Type.text_single);
    maxUsersFF.setLabel("Max number allowed of users");
    maxUsersFF.setRequired(false);
    final FormField includePasswordProtectedFF = searchForm.addField();
    includePasswordProtectedFF.setVariable("include_password_protected");
    includePasswordProtectedFF.setType(FormField.Type.boolean_type);
    includePasswordProtectedFF.setLabel("Include password protected rooms");
    includePasswordProtectedFF.setRequired(false);
    final Element probeResult = DocumentHelper.createElement(QName.get("query", JABBER_IQ_SEARCH));
    probeResult.add(searchForm.getElement());
    return probeResult;
}
Also used : Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 62 with FormField

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

the class IQMUCSearchHandler method handleIQ.

/**
 * Constructs an answer on a IQ stanza that contains a search request. The
 * answer will be an IQ stanza of type 'result' or 'error'.
 *
 * @param iq
 *            The IQ stanza that is the search request.
 * @return An answer to the provided request.
 */
public IQ handleIQ(IQ iq) {
    final IQ reply = IQ.createResultIQ(iq);
    final Element formElement = iq.getChildElement().element(QName.get("x", "jabber:x:data"));
    if (formElement == null) {
        reply.setChildElement(getDataElement());
        return reply;
    }
    // parse params from request.
    final DataForm df = new DataForm(formElement);
    boolean nameIsExactMatch = false;
    String subject = null;
    int numUsers = -1;
    int numMaxUsers = -1;
    boolean includePasswordProtectedRooms = true;
    final Set<String> names = new HashSet<>();
    for (final FormField field : df.getFields()) {
        if (field.getVariable().equals("name")) {
            if (field.getFirstValue() != null && field.getFirstValue().trim().length() > 0) {
                names.add(field.getFirstValue());
            }
        }
    }
    final FormField matchFF = df.getField("name_is_exact_match");
    if (matchFF != null) {
        final String b = matchFF.getFirstValue();
        if (b != null) {
            nameIsExactMatch = b.equals("1") || b.equalsIgnoreCase("true") || b.equalsIgnoreCase("yes");
        }
    }
    final FormField subjectFF = df.getField(SUBJECT);
    if (subjectFF != null) {
        subject = subjectFF.getFirstValue();
    }
    try {
        final FormField userAmountFF = df.getField(NUM_USERS);
        if (userAmountFF != null) {
            String value = userAmountFF.getFirstValue();
            if (value != null && !"".equals(value)) {
                numUsers = Integer.parseInt(value);
            }
        }
        final FormField maxUsersFF = df.getField(NUM_MAX_USERS);
        if (maxUsersFF != null) {
            String value = maxUsersFF.getFirstValue();
            if (value != null && !"".equals(value)) {
                numMaxUsers = Integer.parseInt(value);
            }
        }
    } catch (NumberFormatException e) {
        reply.setError(PacketError.Condition.bad_request);
        return reply;
    }
    final FormField includePasswordProtectedRoomsFF = df.getField("include_password_protected");
    if (includePasswordProtectedRoomsFF != null) {
        final String b = includePasswordProtectedRoomsFF.getFirstValue();
        if (b != null) {
            try {
                includePasswordProtectedRooms = DataForm.parseBoolean(b);
            } catch (ParseException e) {
                reply.setError(PacketError.Condition.bad_request);
                return reply;
            }
        }
    }
    // search for chatrooms matching the request params.
    final List<MUCRoomSearchInfo> mucs = new ArrayList<>();
    for (MUCRoomSearchInfo room : mucService.getAllRoomSearchInfo()) {
        boolean find = false;
        if (!names.isEmpty()) {
            for (final String name : names) {
                if (nameIsExactMatch) {
                    if (name.equalsIgnoreCase(room.getNaturalLanguageName())) {
                        find = true;
                        break;
                    }
                } else {
                    if (room.getNaturalLanguageName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
                        find = true;
                        break;
                    }
                }
            }
        }
        if (subject != null && subject.trim().length() > 0 && room.getSubject().toLowerCase().indexOf(subject.toLowerCase()) != -1) {
            find = true;
        }
        if (numUsers > -1 && room.getParticipantCount() < numUsers) {
            find = false;
        }
        if (numMaxUsers > -1 && room.getMaxUsers() < numMaxUsers) {
            find = false;
        }
        if (!includePasswordProtectedRooms && room.isPasswordProtected()) {
            find = false;
        }
        if (find && canBeIncludedInResult(room)) {
            mucs.add(room);
        }
    }
    final ResultSet<MUCRoomSearchInfo> searchResults = new ResultSetImpl<>(sortByUserAmount(mucs));
    // See if the requesting entity would like to apply 'result set
    // management'
    final Element set = iq.getChildElement().element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT));
    final List<MUCRoomSearchInfo> mucrsm;
    // apply RSM only if the element exists, and the (total) results
    // set is not empty.
    final boolean applyRSM = set != null && !mucs.isEmpty();
    if (applyRSM) {
        if (!ResultSet.isValidRSMRequest(set)) {
            reply.setError(Condition.bad_request);
            return reply;
        }
        try {
            mucrsm = searchResults.applyRSMDirectives(set);
        } catch (NullPointerException e) {
            final IQ itemNotFound = IQ.createResultIQ(iq);
            itemNotFound.setError(Condition.item_not_found);
            return itemNotFound;
        }
    } else {
        // if no rsm, all found rooms are part of the result.
        mucrsm = new ArrayList<>(searchResults);
    }
    final Element res = DocumentHelper.createElement(QName.get("query", JABBER_IQ_SEARCH));
    final DataForm resultform = new DataForm(DataForm.Type.result);
    boolean atLeastoneResult = false;
    for (MUCRoomSearchInfo room : mucrsm) {
        final Map<String, Object> fields = new HashMap<>();
        fields.put("name", room.getNaturalLanguageName());
        fields.put(SUBJECT, room.getSubject());
        fields.put(NUM_USERS, room.getOccupantsCount());
        fields.put(NUM_MAX_USERS, determineMaxUsersDisplay(room.getMaxUsers()));
        fields.put("is_password_protected", room.isPasswordProtected());
        fields.put("is_member_only", room.isMembersOnly());
        fields.put("jid", room.getJID().toString());
        resultform.addItemFields(fields);
        atLeastoneResult = true;
    }
    if (atLeastoneResult) {
        resultform.addReportedField("name", "Name", FormField.Type.text_single);
        resultform.addReportedField(SUBJECT, "Subject", FormField.Type.text_single);
        resultform.addReportedField(NUM_USERS, "Number of users", FormField.Type.text_single);
        resultform.addReportedField(NUM_MAX_USERS, "Max number allowed of users", FormField.Type.text_single);
        resultform.addReportedField("is_password_protected", "Is a password protected room.", FormField.Type.boolean_type);
        resultform.addReportedField("is_member_only", "Is a member only room.", FormField.Type.boolean_type);
        resultform.addReportedField("jid", "JID", FormField.Type.jid_single);
    }
    res.add(resultform.getElement());
    if (applyRSM) {
        res.add(searchResults.generateSetElementFromResults(mucrsm));
    }
    reply.setChildElement(res);
    return reply;
}
Also used : ResultSetImpl(org.xmpp.resultsetmanagement.ResultSetImpl) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) DataForm(org.xmpp.forms.DataForm) ParseException(java.text.ParseException) FormField(org.xmpp.forms.FormField)

Example 63 with FormField

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

the class IQMuclumbusSearchHandler method getDataElement.

/**
 * Utility method that returns a 'search' child element filled
 * with a blank dataform.
 *
 * @return Element, named 'search', escaped by the 'https://xmlns.zombofant.net/muclumbus/search/1.0' namespace, filled with a blank dataform.
 */
private static Element getDataElement() {
    final DataForm searchForm = new DataForm(DataForm.Type.form);
    searchForm.setTitle("Chat Rooms Search");
    searchForm.addInstruction("Instructions");
    final FormField typeFF = searchForm.addField();
    typeFF.setVariable("FORM_TYPE");
    typeFF.setType(FormField.Type.hidden);
    typeFF.addValue(NAMESPACE + "#params");
    final FormField qFF = searchForm.addField();
    qFF.setVariable(VAR_Q);
    qFF.setType(FormField.Type.text_single);
    qFF.setLabel("Search for");
    qFF.setRequired(false);
    final FormField sinnameFF = searchForm.addField();
    sinnameFF.setVariable(VAR_SINNAME);
    sinnameFF.setType(FormField.Type.boolean_type);
    sinnameFF.setLabel("Search in name?");
    sinnameFF.setRequired(false);
    sinnameFF.addValue(true);
    final FormField sindescriptionFF = searchForm.addField();
    sindescriptionFF.setVariable(VAR_SINDESCRIPTION);
    sindescriptionFF.setType(FormField.Type.boolean_type);
    sindescriptionFF.setLabel("Search in description?");
    sindescriptionFF.setRequired(false);
    sindescriptionFF.addValue(true);
    final FormField sinaddr = searchForm.addField();
    sinaddr.setVariable(VAR_SINDADDR);
    sinaddr.setType(FormField.Type.boolean_type);
    sinaddr.setLabel("Search in address?");
    sinaddr.setRequired(false);
    sinaddr.addValue(true);
    final FormField minUsersFF = searchForm.addField();
    minUsersFF.setVariable(VAR_MIN_USERS);
    minUsersFF.setType(FormField.Type.text_single);
    minUsersFF.setLabel("Minimum number of users");
    minUsersFF.setRequired(false);
    minUsersFF.addValue(1);
    final FormField keyFF = searchForm.addField();
    keyFF.setVariable(VAR_KEY);
    keyFF.setType(FormField.Type.list_single);
    keyFF.setLabel("Sort results by");
    keyFF.setRequired(false);
    keyFF.addOption("Number of online users", Key.nusers.name());
    keyFF.addOption("Address", Key.address.name());
    keyFF.addValue(Key.address.name());
    final Element result = DocumentHelper.createElement(QName.get(REQUEST_ELEMENT_NAME, NAMESPACE));
    result.add(searchForm.getElement());
    return result;
}
Also used : Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 64 with FormField

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

the class IQMuclumbusSearchHandler method parseSearchParameters.

static SearchParameters parseSearchParameters(final Element formElement) throws ParseException, IllegalArgumentException {
    Log.debug("Parsing search parameters from request.");
    final SearchParameters result = new SearchParameters();
    final DataForm df = new DataForm(formElement);
    final FormField qFF = df.getField(VAR_Q);
    if (qFF != null) {
        result.setQ(qFF.getFirstValue());
    }
    final FormField sinnameFF = df.getField(VAR_SINNAME);
    if (sinnameFF != null) {
        final String b = sinnameFF.getFirstValue();
        if (b != null) {
            result.setSinname(DataForm.parseBoolean(b));
        }
    }
    final FormField sinddescriptionff = df.getField(VAR_SINDESCRIPTION);
    if (sinddescriptionff != null) {
        final String b = sinddescriptionff.getFirstValue();
        if (b != null) {
            result.setSindescription(DataForm.parseBoolean(b));
        }
    }
    final FormField sinaddrFF = df.getField(VAR_SINDADDR);
    if (sinaddrFF != null) {
        final String b = sinaddrFF.getFirstValue();
        if (b != null) {
            result.setSinaddr(DataForm.parseBoolean(b));
        }
    }
    final FormField minUsersFF = df.getField(VAR_MIN_USERS);
    if (minUsersFF != null) {
        final String i = minUsersFF.getFirstValue();
        if (i != null) {
            result.setMinUsers(Integer.parseInt(i));
        }
    }
    final FormField keyFF = df.getField(VAR_KEY);
    if (keyFF != null) {
        final String k = keyFF.getFirstValue();
        if (k != null) {
            result.setKey(Key.valueOf(k));
        }
    }
    Log.debug("Parsed: " + result);
    return result;
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField)

Example 65 with FormField

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

the class IQOfflineMessagesHandler method getExtendedInfos.

@Override
public Set<DataForm> getExtendedInfos(String name, String node, JID senderJID) {
    // Mark that offline messages shouldn't be sent when the user becomes available
    stopOfflineFlooding(senderJID);
    final DataForm dataForm = new DataForm(DataForm.Type.result);
    final FormField field1 = dataForm.addField();
    field1.setVariable("FORM_TYPE");
    field1.setType(FormField.Type.hidden);
    field1.addValue(NAMESPACE);
    final FormField field2 = dataForm.addField();
    field2.setVariable("number_of_messages");
    field2.addValue(String.valueOf(messageStore.getCount(senderJID.getNode())));
    final Set<DataForm> dataForms = new HashSet<>();
    dataForms.add(dataForm);
    return dataForms;
}
Also used : DataForm(org.xmpp.forms.DataForm) FormField(org.xmpp.forms.FormField) HashSet(java.util.HashSet)

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