Search in sources :

Example 1 with Group

use of org.jivesoftware.openfire.crowd.jaxb.Group in project Openfire by igniterealtime.

the class CrowdManager method getUserGroups.

/**
	 * Get all the groups of a given username
	 * @param username
	 * @return a List of groups name
	 * @throws RemoteException
	 */
public List<String> getUserGroups(String username) throws RemoteException {
    username = JID.unescapeNode(username);
    if (LOG.isDebugEnabled())
        LOG.debug("fetch all crowd groups for user:" + username);
    int maxResults = 100;
    int startIndex = 0;
    List<String> results = new ArrayList<>();
    StringBuilder request = new StringBuilder("user/group/nested?username=").append(urlEncode(username)).append("&max-results=").append(maxResults).append("&start-index=");
    try {
        while (true) {
            GetMethod get = createGetMethodXmlResponse(crowdServer.resolve(request.toString() + startIndex));
            Groups groups = null;
            try {
                int httpCode = client.executeMethod(get);
                if (httpCode != 200) {
                    handleHTTPError(get);
                }
                groups = JAXB.unmarshal(get.getResponseBodyAsStream(), Groups.class);
            } finally {
                get.releaseConnection();
            }
            if (groups != null && groups.group != null) {
                for (Group group : groups.group) {
                    results.add(group.name);
                }
                if (groups.group.size() != maxResults) {
                    break;
                } else {
                    startIndex += maxResults;
                }
            } else {
                break;
            }
        }
    } catch (IOException ioe) {
        handleError(ioe);
    }
    return results;
}
Also used : Group(org.jivesoftware.openfire.crowd.jaxb.Group) Groups(org.jivesoftware.openfire.crowd.jaxb.Groups) ArrayList(java.util.ArrayList) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 2 with Group

use of org.jivesoftware.openfire.crowd.jaxb.Group in project Openfire by igniterealtime.

the class CrowdManager method getGroup.

/**
	 * Get the description of a group from crowd
	 * @param groupName
	 * @return a Group object
	 * @throws RemoteException
	 */
public Group getGroup(String groupName) throws RemoteException {
    if (LOG.isDebugEnabled())
        LOG.debug("Get group:" + groupName + " from crowd");
    GetMethod get = createGetMethodXmlResponse(crowdServer.resolve("group?groupname=" + urlEncode(groupName)));
    Group group = null;
    try {
        int httpCode = client.executeMethod(get);
        if (httpCode != 200) {
            handleHTTPError(get);
        }
        group = JAXB.unmarshal(get.getResponseBodyAsStream(), Group.class);
    } catch (IOException ioe) {
        handleError(ioe);
    } finally {
        get.releaseConnection();
    }
    return group;
}
Also used : Group(org.jivesoftware.openfire.crowd.jaxb.Group) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 3 with Group

use of org.jivesoftware.openfire.crowd.jaxb.Group in project Openfire by igniterealtime.

the class CrowdManager method getAllGroupNames.

/**
	 * Get all the crowd groups
	 * @return a List of group names
	 * @throws RemoteException
	 */
public List<String> getAllGroupNames() throws RemoteException {
    if (LOG.isDebugEnabled())
        LOG.debug("fetch all crowd groups");
    int maxResults = 100;
    int startIndex = 0;
    List<String> results = new ArrayList<>();
    StringBuilder request = new StringBuilder("search?entity-type=group&restriction=active%3dtrue").append("&max-results=").append(maxResults).append("&start-index=");
    try {
        while (true) {
            GetMethod get = createGetMethodXmlResponse(crowdServer.resolve(request.toString() + startIndex));
            Groups groups = null;
            try {
                int httpCode = client.executeMethod(get);
                if (httpCode != 200) {
                    handleHTTPError(get);
                }
                groups = JAXB.unmarshal(get.getResponseBodyAsStream(), Groups.class);
            } finally {
                get.releaseConnection();
            }
            if (groups != null && groups.group != null) {
                for (Group group : groups.group) {
                    results.add(group.name);
                }
                if (groups.group.size() != maxResults) {
                    break;
                } else {
                    startIndex += maxResults;
                }
            } else {
                break;
            }
        }
    } catch (IOException ioe) {
        handleError(ioe);
    }
    return results;
}
Also used : Group(org.jivesoftware.openfire.crowd.jaxb.Group) Groups(org.jivesoftware.openfire.crowd.jaxb.Groups) ArrayList(java.util.ArrayList) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 Group (org.jivesoftware.openfire.crowd.jaxb.Group)3 ArrayList (java.util.ArrayList)2 Groups (org.jivesoftware.openfire.crowd.jaxb.Groups)2