Search in sources :

Example 21 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project zeppelin by apache.

the class ZeppelinRestApiTest method getNoteContent.

private String getNoteContent(String id) throws IOException {
    GetMethod get = httpGet("/notebook/export/" + id);
    assertThat(get, isAllowed());
    get.addRequestHeader("Origin", "http://localhost");
    Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    assertEquals(200, get.getStatusCode());
    String body = resp.get("body").toString();
    // System.out.println("Body is " + body);
    get.releaseConnection();
    return body;
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 22 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class CrowdManager method getAllUsers.

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

Example 23 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod 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 24 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class CrowdManager method createGetMethodXmlResponse.

private GetMethod createGetMethodXmlResponse(URI uri) {
    GetMethod get = new GetMethod(uri.toString());
    get.addRequestHeader(HEADER_ACCEPT_APPLICATION_XML);
    get.addRequestHeader(HEADER_ACCEPT_CHARSET_UTF8);
    return get;
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 25 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project Openfire by igniterealtime.

the class CrowdManager method getGroupMembers.

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

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17