Search in sources :

Example 21 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project dianping-open-sdk by dianping.

the class ApiTool method requestApi.

public static String requestApi(String apiUrl, String appKey, String secret, Map<String, String> paramMap) {
    String queryString = getQueryString(appKey, secret, paramMap);
    StringBuffer response = new StringBuffer();
    HttpClientParams httpConnectionParams = new HttpClientParams();
    httpConnectionParams.setConnectionManagerTimeout(1000);
    HttpClient client = new HttpClient(httpConnectionParams);
    HttpMethod method = new GetMethod(apiUrl);
    try {
        if (StringUtils.isNotBlank(queryString)) {
            // Encode query string with UTF-8
            String encodeQuery = URIUtil.encodeQuery(queryString, "UTF-8");
            LOGGER.debug("Encoded Query:" + encodeQuery);
            method.setQueryString(encodeQuery);
        }
        client.executeMethod(method);
        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.append(line).append(System.getProperty("line.separator"));
        }
        reader.close();
    } catch (URIException e) {
        LOGGER.error("Can not encode query: " + queryString + " with charset UTF-8. ", e);
    } catch (IOException e) {
        LOGGER.error("Request URL: " + apiUrl + " failed. ", e);
    } finally {
        method.releaseConnection();
    }
    return response.toString();
}
Also used : URIException(org.apache.commons.httpclient.URIException) InputStreamReader(java.io.InputStreamReader) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) GetMethod(org.apache.commons.httpclient.methods.GetMethod) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 22 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project alfresco-repository by Alfresco.

the class ExplicitSolrStoreMappingWrapper method init.

public void init() {
    httpClientFactory = (HttpClientFactory) beanFactory.getBean(wrapped.getHttpClientFactory());
    random = new Random(123);
    if ((wrapped.getNodes() == null) || (wrapped.getNodes().length == 0)) {
        HttpClient httpClient = httpClientFactory.getHttpClient();
        HttpClientParams params = httpClient.getParams();
        // params.setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
        // httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("admin", "admin"));
        httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClient, wrapped.getBaseUrl()));
    } else {
        for (String node : wrapped.getNodes()) {
            String nodeHost = httpClientFactory.getHost();
            String nodePort = "" + httpClientFactory.getPort();
            String nodeBaseUrl = wrapped.getBaseUrl();
            if (node.length() > 0) {
                int colon = node.indexOf(':');
                int forward = (colon > -1) ? node.indexOf('/', colon) : node.indexOf('/');
                if (colon == -1) {
                    if (forward == -1) {
                        // single value
                        if (node.startsWith("/")) {
                            nodeBaseUrl = node;
                        }
                        try {
                            int port = Integer.parseInt(node);
                            nodePort = "" + port;
                        } catch (NumberFormatException nfe) {
                            nodeHost = node;
                        }
                    } else {
                        try {
                            String potentialPort = node.substring(0, forward);
                            if (potentialPort.length() > 0) {
                                int port = Integer.parseInt(potentialPort);
                                nodePort = "" + port;
                            }
                        } catch (NumberFormatException nfe) {
                            nodeHost = node.substring(0, forward);
                        }
                        nodeBaseUrl = node.substring(forward);
                    }
                } else {
                    if (forward == -1) {
                        if (colon > 0) {
                            nodeHost = node.substring(0, colon);
                        }
                        if (colon + 1 < node.length()) {
                            String port = node.substring(colon + 1);
                            if (port.length() > 0) {
                                nodePort = port;
                            }
                        }
                    } else {
                        if (colon > 0) {
                            nodeHost = node.substring(0, colon);
                        }
                        String port = node.substring(colon + 1, forward);
                        if (port.length() > 0) {
                            nodePort = port;
                        }
                        nodeBaseUrl = node.substring(forward);
                    }
                }
            }
            try {
                int realPort = Integer.parseInt(nodePort);
                httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClientFactory.getHttpClient(nodeHost, realPort), nodeBaseUrl));
            } catch (NumberFormatException nfe) {
                httpClientsAndBaseURLs.add(new HttpClientAndBaseUrl(httpClientFactory.getHttpClient(nodeHost, httpClientFactory.getPort()), nodeBaseUrl));
            }
        }
    }
    policy = new ExplicitShardingPolicy(wrapped.getNumShards(), wrapped.getReplicationFactor(), httpClientsAndBaseURLs.size());
}
Also used : Random(java.util.Random) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) ExplicitShardingPolicy(org.alfresco.util.shard.ExplicitShardingPolicy)

Aggregations

HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)22 HttpClient (org.apache.commons.httpclient.HttpClient)19 IOException (java.io.IOException)7 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)5 Map (java.util.Map)3 URIException (org.apache.commons.httpclient.URIException)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 PostMethod (org.apache.commons.httpclient.methods.PostMethod)3 HashMap (java.util.HashMap)2 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)2 AuthScope (org.apache.commons.httpclient.auth.AuthScope)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)1