Search in sources :

Example 16 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project ovirt-engine by oVirt.

the class HttpUtils method getConnection.

/**
 * There are places in the code where use http to communicate with vdsm or external providers
 *
 * @param connectionTimeOut
 *            - the instance type of the interface for this connection
 * @param clientRetries
 *            - Number of retries if timeout occurd
 * @param maxConnectionsPerHost
 *            - maximum number of connections allowed for a given host
 * @param maxTotalConnections
 *            - The maximum number of connections allowed
 * @return {@link HttpClient}.
 */
public static HttpClient getConnection(int connectionTimeOut, int clientRetries, int maxConnectionsPerHost, int maxTotalConnections) {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(connectionTimeOut);
    params.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
    params.setMaxTotalConnections(maxTotalConnections);
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.setParams(params);
    // Create the client:
    HttpClient client = new HttpClient(httpConnectionManager);
    // Configure the HTTP client so it will retry the execution of
    // methods when there are IO errors:
    int retries = Config.getValue(ConfigValues.vdsRetries);
    HttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(retries, false);
    HttpClientParams parameters = client.getParams();
    parameters.setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    // Done:
    return client;
}
Also used : HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 17 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project openmeetings by apache.

the class AppointmentManager method createHttpClient.

/**
 * Returns a new HttpClient with the inbuilt connection manager in this.
 *
 * @return HttpClient object that was created.
 */
public HttpClient createHttpClient() {
    if (connmanager == null) {
        connmanager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        params.setDefaultMaxConnectionsPerHost(MAX_HOST_CONNECTIONS);
        params.setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
        connmanager.setParams(params);
    }
    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setConnectionManagerTimeout(CONNECTION_MANAGER_TIMEOUT);
    return new HttpClient(connmanager);
}
Also used : HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 18 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project cosmic by MissionCriticalCloud.

the class ClusterServiceServletImpl method getHttpClient.

private HttpClient getHttpClient() {
    if (s_client == null) {
        final MultiThreadedHttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
        mgr.getParams().setDefaultMaxConnectionsPerHost(4);
        // TODO make it configurable
        mgr.getParams().setMaxTotalConnections(1000);
        s_client = new HttpClient(mgr);
        final HttpClientParams clientParams = new HttpClientParams();
        clientParams.setSoTimeout(ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000);
        s_client.setParams(clientParams);
    }
    return s_client;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams)

Example 19 with HttpClientParams

use of org.apache.commons.httpclient.params.HttpClientParams in project maven-plugins by apache.

the class ClassicJiraDownloader method doExecute.

/**
 * Execute the query on the JIRA server.
 *
 * @throws Exception on error
 */
public void doExecute() throws Exception {
    try {
        HttpClient client = new HttpClient();
        // MCHANGES-89 Allow circular redirects
        HttpClientParams clientParams = client.getParams();
        clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        // MCHANGES-237
        clientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpState state = new HttpState();
        HostConfiguration hc = new HostConfiguration();
        client.setHostConfiguration(hc);
        client.setState(state);
        String baseUrl = JiraHelper.getBaseUrl(project.getIssueManagement().getUrl());
        getLog().debug("JIRA lives at: " + baseUrl);
        // Here we only need the host part of the URL
        determineProxy(baseUrl, client);
        prepareBasicAuthentication(client);
        boolean jiraAuthenticationSuccessful = false;
        if (isJiraAuthenticationConfigured()) {
            // Here we only need the parts up to and including the host part of the URL
            jiraAuthenticationSuccessful = doJiraAuthentication(client, baseUrl);
        }
        if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful) || !isJiraAuthenticationConfigured()) {
            String fullUrl;
            if (useJql) {
                fullUrl = getJqlQueryURL();
            } else {
                fullUrl = getParameterBasedQueryURL(client);
            }
            if (log.isDebugEnabled()) {
                log.debug("download jira issues from url " + fullUrl);
            }
            // execute the GET
            download(client, fullUrl);
        }
    } catch (Exception e) {
        if (project.getIssueManagement() != null) {
            getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e);
        } else {
            getLog().error("Error accessing mock project issues", e);
        }
    }
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpState(org.apache.commons.httpclient.HttpState) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 20 with HttpClientParams

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

the class DemoApiTool 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)

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