Search in sources :

Example 31 with User

use of org.jivesoftware.openfire.user.User 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 32 with User

use of org.jivesoftware.openfire.user.User in project Openfire by igniterealtime.

the class SearchPlugin method performSearch.

private Set<User> performSearch(Element incomingForm, int startIndex, int max) {
    Set<User> users = new HashSet<User>();
    Hashtable<String, String> searchList = extractSearchQuery(incomingForm);
    for (Entry<String, String> entry : searchList.entrySet()) {
        String field = entry.getKey();
        String query = entry.getValue();
        Collection<User> foundUsers = new ArrayList<User>();
        if (userManager != null && query.length() > 0 && !query.equals(NAMESPACE_JABBER_IQ_SEARCH)) {
            if (max >= 0) {
                foundUsers.addAll(userManager.findUsers(new HashSet<String>(Arrays.asList(field)), query, startIndex, max));
            } else {
                foundUsers.addAll(userManager.findUsers(new HashSet<String>(Arrays.asList(field)), query));
            }
        }
        // occasionally a null User is returned so filter them out
        for (User user : foundUsers) {
            if (user != null) {
                users.add(user);
            }
        }
    }
    return users;
}
Also used : User(org.jivesoftware.openfire.user.User) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 33 with User

use of org.jivesoftware.openfire.user.User in project Openfire by igniterealtime.

the class UserServiceController method addProperties.

/**
	 * Adds the properties.
	 *
	 * @param userEntity
	 *            the user entity
	 * @throws ServiceException
	 *             the service exception
	 */
private void addProperties(String username, List<UserProperty> properties) throws ServiceException {
    User user = getAndCheckUser(username);
    user.getProperties().clear();
    if (properties != null) {
        for (UserProperty property : properties) {
            user.getProperties().put(property.getKey(), property.getValue());
        }
    }
}
Also used : User(org.jivesoftware.openfire.user.User) UserProperty(org.jivesoftware.openfire.plugin.rest.entity.UserProperty)

Example 34 with User

use of org.jivesoftware.openfire.user.User in project Openfire by igniterealtime.

the class UserServiceController method deleteUser.

/**
	 * Delete user.
	 *
	 * @param username
	 *            the username
	 * @throws ServiceException
	 *             the service exception
	 */
public void deleteUser(String username) throws ServiceException {
    User user = getAndCheckUser(username);
    userManager.deleteUser(user);
    rosterManager.deleteRoster(server.createJID(username, null));
}
Also used : User(org.jivesoftware.openfire.user.User)

Example 35 with User

use of org.jivesoftware.openfire.user.User in project Openfire by igniterealtime.

the class UserServiceController method updateUser.

/**
	 * Update user.
	 *
	 * @param username
	 *            the username
	 * @param userEntity
	 *            the user entity
	 * @throws ServiceException
	 *             the service exception
	 */
public void updateUser(String username, UserEntity userEntity) throws ServiceException {
    if (userEntity != null && !username.isEmpty()) {
        // parameter
        if (userEntity.getUsername() != null) {
            if (!userEntity.getUsername().equals(username)) {
                JustMarriedController.changeName(username, userEntity.getUsername(), true, userEntity.getEmail(), userEntity.getName());
                addProperties(userEntity.getUsername(), userEntity.getProperties());
                return;
            }
        }
        User user = getAndCheckUser(username);
        if (userEntity.getPassword() != null) {
            user.setPassword(userEntity.getPassword());
        }
        if (userEntity.getName() != null) {
            user.setName(userEntity.getName());
        }
        if (userEntity.getEmail() != null) {
            user.setEmail(userEntity.getEmail());
        }
        addProperties(username, userEntity.getProperties());
    }
}
Also used : User(org.jivesoftware.openfire.user.User)

Aggregations

User (org.jivesoftware.openfire.user.User)58 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)26 Element (org.dom4j.Element)17 JID (org.xmpp.packet.JID)17 UserManager (org.jivesoftware.openfire.user.UserManager)10 ArrayList (java.util.ArrayList)9 Group (org.jivesoftware.openfire.group.Group)9 IQ (org.xmpp.packet.IQ)7 List (java.util.List)6 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)6 RosterItem (org.jivesoftware.openfire.roster.RosterItem)5 Date (java.util.Date)4 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)4 Message (org.xmpp.packet.Message)4 Presence (org.xmpp.packet.Presence)4 HashSet (java.util.HashSet)3 StringTokenizer (java.util.StringTokenizer)3 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2