Search in sources :

Example 51 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project zm-mailbox by Zimbra.

the class ZMailbox method getRESTResource.

private InputStream getRESTResource(String relativePath, String startTimeArg, String endTimeArg, int msecTimeout, String alternateUrl) throws ServiceException {
    GetMethod get = null;
    URI uri = null;
    int statusCode;
    try {
        if (startTimeArg != null) {
            String encodedArg = URLEncoder.encode(startTimeArg, "UTF-8");
            if (!relativePath.contains("?")) {
                relativePath = relativePath + "?start=" + encodedArg;
            } else {
                relativePath = relativePath + "&start=" + encodedArg;
            }
        }
        if (endTimeArg != null) {
            String encodedArg = URLEncoder.encode(endTimeArg, "UTF-8");
            if (!relativePath.contains("?")) {
                relativePath = relativePath + "?end=" + encodedArg;
            } else {
                relativePath = relativePath + "&end=" + encodedArg;
            }
        }
        uri = getRestURI(relativePath, alternateUrl);
        HttpClient client = getHttpClient(uri);
        get = new GetMethod(uri.toString());
        if (msecTimeout > -1) {
            get.getParams().setSoTimeout(msecTimeout);
        }
        statusCode = HttpClientUtil.executeMethod(client, get);
        // parse the response
        if (statusCode == HttpServletResponse.SC_OK) {
            return new GetMethodInputStream(get);
        } else {
            String msg = String.format("GET from %s failed, status=%d.  %s", uri.toString(), statusCode, get.getStatusText());
            throw ServiceException.FAILURE(msg, null);
        }
    } catch (IOException e) {
        String fromUri = "";
        if (uri != null) {
            fromUri = " from " + uri.toString();
        }
        String msg = String.format("Unable to get REST resource%s: %s", fromUri, e.getMessage());
        throw ZClientException.IO_ERROR(msg, e);
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) URI(java.net.URI)

Example 52 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project zm-mailbox by Zimbra.

the class ZimbraHttpConnectionManager method main.

public static void main(String[] args) {
    // dump httpclient package defaults
    System.out.println(dumpParams("httpclient package defaults", new HttpConnectionManagerParams(), new HttpClientParams()));
    System.out.println(dumpParams("Internal ZimbraHttpConnectionManager", ZimbraHttpConnectionManager.getInternalHttpConnMgr().getParams().getConnMgrParams(), ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient().getParams()));
    System.out.println(dumpParams("External ZimbraHttpConnectionManager", ZimbraHttpConnectionManager.getExternalHttpConnMgr().getParams().getConnMgrParams(), ZimbraHttpConnectionManager.getExternalHttpConnMgr().getDefaultHttpClient().getParams()));
    HttpClient httpClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient();
    String connMgrName = httpClient.getHttpConnectionManager().getClass().getSimpleName();
    long connMgrTimeout = httpClient.getParams().getConnectionManagerTimeout();
    System.out.println("HttpConnectionManager for the HttpClient instance is: " + connMgrName);
    System.out.println("connection manager timeout for the HttpClient instance is: " + connMgrTimeout);
}
Also used : HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 53 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class ExchangeUtils method sendGetRequest.

public static String sendGetRequest(String urlAddress) throws Exception {
    HttpClient httpclient = new HttpClient();
    GetMethod getMethod = new GetMethod(urlAddress);
    TransportClientProperties tcp = TransportClientPropertiesFactory.create("http");
    if (tcp.getProxyHost().length() != 0) {
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(tcp.getProxyUser() != null ? tcp.getProxyUser() : "", tcp.getProxyPassword() != null ? tcp.getProxyUser() : "");
        httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
        HostConfiguration hcf = new HostConfiguration();
        hcf.setProxy(tcp.getProxyHost(), Integer.parseInt(tcp.getProxyPort()));
        httpclient.executeMethod(hcf, getMethod);
    } else {
        httpclient.executeMethod(getMethod);
    }
    String response = getMethod.getResponseBodyAsString();
    getMethod.releaseConnection();
    return response;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) TransportClientProperties(org.apache.axis.components.net.TransportClientProperties) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 54 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class ExchangeUtils method sendPostRequest.

public static String sendPostRequest(String urlAddress, Map<String, String> parameters) throws Exception {
    HttpClient httpclient = new HttpClient();
    PostMethod postMethod = new PostMethod(urlAddress);
    if (parameters != null) {
        NameValuePair[] postData = new NameValuePair[parameters.size()];
        int i = 0;
        for (String key : parameters.keySet()) {
            String value = parameters.get(key);
            postData[i++] = new NameValuePair(key, value);
        }
        postMethod.addParameters(postData);
    }
    httpclient.executeMethod(postMethod);
    String response = postMethod.getResponseBodyAsString();
    postMethod.releaseConnection();
    return response;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 55 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesAutoComplete method getResults.

private JSONArray getResults(String nsurl) throws Exception {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(nsurl);
    get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
    client.executeMethod(get);
    String rawJSON = get.getResponseBodyAsString();
    //parse
    JSONParser jp = new JSONParser();
    JSONObject jo = (JSONObject) jp.parse(rawJSON);
    //support search and auto bie webservices
    if (jo.containsKey("searchResults")) {
        return (JSONArray) ((JSONObject) jo.get("searchResults")).get("results");
    } else {
        return (JSONArray) jo.get("autoCompleteList");
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20