Search in sources :

Example 86 with IQ

use of org.xmpp.packet.IQ 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 87 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class SearchPlugin method replyDataFormResult.

/**
	 * Constructs a query that is returned as an IQ packet that contains the search results.
	 * 
	 * @param users
	 *            set of users that will be used to construct the search results
	 * @param packet
	 *            the IQ packet sent by the client
	 * @return the iq packet that contains the search results
	 */
private IQ replyDataFormResult(Collection<User> users, IQ packet) {
    final DataForm searchResults = new DataForm(DataForm.Type.result);
    searchResults.addField("FORM_TYPE", null, FormField.Type.hidden);
    searchResults.addReportedField("jid", "JID", FormField.Type.jid_single);
    for (final String fieldName : getFilteredSearchFields()) {
        searchResults.addReportedField(fieldName, LocaleUtils.getLocalizedString("advance.user.search." + fieldName.toLowerCase(), "search"), FormField.Type.text_single);
    }
    for (final User user : users) {
        final String username = JID.unescapeNode(user.getUsername());
        final Map<String, Object> item = new HashMap<String, Object>();
        item.put("jid", username + "@" + serverName);
        item.put(LocaleUtils.getLocalizedString("advance.user.search.username", "search"), username);
        item.put(LocaleUtils.getLocalizedString("advance.user.search.name", "search"), (user.isNameVisible() ? removeNull(user.getName()) : ""));
        item.put(LocaleUtils.getLocalizedString("advance.user.search.email", "search"), (user.isEmailVisible() ? removeNull(user.getEmail()) : ""));
        searchResults.addItemFields(item);
    }
    IQ replyPacket = IQ.createResultIQ(packet);
    Element reply = replyPacket.setChildElement("query", NAMESPACE_JABBER_IQ_SEARCH);
    reply.add(searchResults.getElement());
    return replyPacket;
}
Also used : User(org.jivesoftware.openfire.user.User) HashMap(java.util.HashMap) Element(org.dom4j.Element) DataForm(org.xmpp.forms.DataForm) IQ(org.xmpp.packet.IQ)

Example 88 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class SearchPlugin method processSetPacket.

/**
	 * Processes an IQ stanza of type 'set', which in the context of 'Jabber Search' is a search request.
	 * 
	 * @param packet
	 *            An IQ stanza of type 'get'
	 * @return A result IQ stanza that contains the possbile search fields.
	 */
private IQ processSetPacket(IQ packet) {
    if (!packet.getType().equals(IQ.Type.set)) {
        throw new IllegalArgumentException("This method only accepts 'set' typed IQ stanzas as an argument.");
    }
    JID fromJID = packet.getFrom();
    final IQ resultIQ;
    // check if the request complies to the XEP-0055 standards
    if (!isValidSearchRequest(packet)) {
        resultIQ = IQ.createResultIQ(packet);
        resultIQ.setError(Condition.bad_request);
        return resultIQ;
    }
    final Element incomingForm = packet.getChildElement();
    final boolean isDataFormQuery = (incomingForm.element(QName.get("x", "jabber:x:data")) != null);
    final Element rsmElement = incomingForm.element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT));
    if (rsmElement != null) {
        final Element maxElement = rsmElement.element("max");
        final Element startIndexElement = rsmElement.element("index");
        int startIndex = 0;
        if (startIndexElement != null) {
            startIndex = Integer.parseInt(startIndexElement.getTextTrim());
        }
        int max = -1;
        if (maxElement != null) {
            max = Integer.parseInt(maxElement.getTextTrim());
        }
        final Set<User> searchResults = performSearch(incomingForm, startIndex, max);
        if (groupOnly) {
            Collection<Group> groups = GroupManager.getInstance().getGroups(fromJID);
            Set<User> allSearchResults = new HashSet<User>(searchResults);
            searchResults.clear();
            for (User user : allSearchResults) {
                for (Group group : groups) {
                    if (group.isUser(user.getUID())) {
                        searchResults.add(user);
                    }
                }
            }
        }
        // apply RSM
        final List<User> rsmResults;
        final ResultSet<User> rs = new ResultSetImpl<User>(searchResults);
        try {
            rsmResults = rs.applyRSMDirectives(rsmElement);
        } catch (NullPointerException e) {
            final IQ itemNotFound = IQ.createResultIQ(packet);
            itemNotFound.setError(Condition.item_not_found);
            return itemNotFound;
        }
        if (isDataFormQuery) {
            resultIQ = replyDataFormResult(rsmResults, packet);
        } else {
            resultIQ = replyNonDataFormResult(rsmResults, packet);
        }
        // add the additional 'set' element.
        final Element set = rs.generateSetElementFromResults(rsmResults);
        resultIQ.getChildElement().add(set);
    } else {
        final Set<User> searchResults = performSearch(incomingForm);
        if (groupOnly) {
            Collection<Group> groups = GroupManager.getInstance().getGroups(fromJID);
            Set<User> allSearchResults = new HashSet<User>(searchResults);
            searchResults.clear();
            for (User user : allSearchResults) {
                for (Group group : groups) {
                    if (group.isUser(user.getUID())) {
                        searchResults.add(user);
                    }
                }
            }
        }
        // don't apply RSM
        if (isDataFormQuery) {
            resultIQ = replyDataFormResult(searchResults, packet);
        } else {
            resultIQ = replyNonDataFormResult(searchResults, packet);
        }
    }
    return resultIQ;
}
Also used : Group(org.jivesoftware.openfire.group.Group) User(org.jivesoftware.openfire.user.User) ResultSetImpl(org.xmpp.resultsetmanagement.ResultSetImpl) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) HashSet(java.util.HashSet)

Example 89 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class SearchPlugin method handleDiscoInfo.

/**
	 * Creates a response specific to the search plugin to Disco#Info requests.
	 * 
	 * @param iq
	 *            The IQ stanza that contains the request.
	 * @return An IQ stanza, formulated as an answer to the received request.
	 */
private static IQ handleDiscoInfo(IQ iq) {
    if (iq == null) {
        throw new IllegalArgumentException("Argument 'iq' cannot be null.");
    }
    if (!iq.getChildElement().getNamespaceURI().equals(IQDiscoInfoHandler.NAMESPACE_DISCO_INFO) || iq.getType() != Type.get) {
        throw new IllegalArgumentException("This is not a valid disco#info request.");
    }
    final IQ replyPacket = IQ.createResultIQ(iq);
    final Element responseElement = replyPacket.setChildElement("query", IQDiscoInfoHandler.NAMESPACE_DISCO_INFO);
    responseElement.addElement("identity").addAttribute("category", "directory").addAttribute("type", "user").addAttribute("name", "User Search");
    responseElement.addElement("feature").addAttribute("var", NAMESPACE_JABBER_IQ_SEARCH);
    responseElement.addElement("feature").addAttribute("var", IQDiscoInfoHandler.NAMESPACE_DISCO_INFO);
    responseElement.addElement("feature").addAttribute("var", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT);
    return replyPacket;
}
Also used : Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ)

Example 90 with IQ

use of org.xmpp.packet.IQ in project Openfire by igniterealtime.

the class LogComponent method processIQ.

private void processIQ(IQ iq) {
    IQ reply = IQ.createResultIQ(iq);
    Element childElement = iq.getChildElement();
    String namespace = childElement.getNamespaceURI();
    Element childElementCopy = iq.getChildElement().createCopy();
    reply.setChildElement(childElementCopy);
    if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
        if (iq.getTo().getNode() == null) {
            // Return service identity and features
            Element identity = childElementCopy.addElement("identity");
            identity.addAttribute("category", "component");
            identity.addAttribute("type", "generic");
            identity.addAttribute("name", "Remote Logger");
            childElementCopy.addElement("feature").addAttribute("var", "http://jabber.org/protocol/disco#info");
            childElementCopy.addElement("feature").addAttribute("var", NAMESPACE);
        }
    } else if (NAMESPACE.equals(namespace)) {
        if (iq.getTo().getNode() == null && iq.getFrom() != null) {
            reply = logListener.logReceived(reply);
        //reply.setTo(reply.getFrom());
        } else {
            reply.getChildElement().addAttribute("type", "unregistered");
        }
    }
    try {
        componentManager.sendPacket(this, reply);
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
}
Also used : Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ)

Aggregations

IQ (org.xmpp.packet.IQ)208 Element (org.dom4j.Element)141 JID (org.xmpp.packet.JID)49 PacketError (org.xmpp.packet.PacketError)35 Presence (org.xmpp.packet.Presence)19 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)18 Message (org.xmpp.packet.Message)17 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)16 ClientSession (org.jivesoftware.openfire.session.ClientSession)14 DataForm (org.xmpp.forms.DataForm)13 ArrayList (java.util.ArrayList)11 AgentNotFoundException (org.jivesoftware.xmpp.workgroup.AgentNotFoundException)10 Packet (org.xmpp.packet.Packet)10 PacketException (org.jivesoftware.openfire.PacketException)9 User (org.jivesoftware.openfire.user.User)8 List (java.util.List)7 PrivacyList (org.jivesoftware.openfire.privacy.PrivacyList)7 Iterator (java.util.Iterator)6 Test (org.junit.Test)6 FormField (org.xmpp.forms.FormField)6