Example usage for org.apache.commons.httpclient HttpConnectionManager setParams

List of usage examples for org.apache.commons.httpclient HttpConnectionManager setParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnectionManager setParams.

Prototype

public abstract void setParams(HttpConnectionManagerParams paramHttpConnectionManagerParams);

Source Link

Usage

From source file:com.legstar.test.cixs.AbstractHttpClientTester.java

/**
 * Setup the static http connection pool.
 * //from w ww  . j  av  a  2  s  .  c  om
 * @return an http client instance
 */
protected static HttpClient createHttpClient() {

    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();

    connectionManagerParams.setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(DEFAULT_MAX_CONNECTIONS_PER_HOST);
    connectionManagerParams.setTcpNoDelay(TCP_NO_DELAY);
    connectionManagerParams.setSoTimeout(SOCKET_TIMEOUT);
    connectionManagerParams.setConnectionTimeout(CONNECT_TIMEOUT);

    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);
    connectionManager.closeIdleConnections(IDLE_CONNECTION_TIMEOUT);
    return new HttpClient(connectionManager);
}

From source file:com.moss.jaxwslite.ServiceFactory.java

public synchronized void setConnectionTimeout(int timeout) {

    HttpConnectionManager manager = client.getHttpConnectionManager();
    HttpConnectionManagerParams p = manager.getParams();

    p.setConnectionTimeout(timeout);/* ww  w  .  ja  v  a 2  s  .  c  o m*/

    manager.setParams(p);
}

From source file:com.trendmicro.hdfs.webdav.test.MiniClusterTestUtil.java

public HttpClient getClient() {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost("localhost");
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxConnectionsPerHost(hostConfig, 100);
    connectionManager.setParams(params);
    HttpClient client = new HttpClient(connectionManager);
    client.setHostConfiguration(hostConfig);
    return client;
}

From source file:com.silverpeas.openoffice.windows.webdav.WebdavManager.java

/**
 * Prepare HTTP connections to the WebDav server
 *
 * @param host the webdav server host name.
 * @param login the login for the user on the webdav server.
 * @param password the login for the user on the webdav server.
 *//*from ww w  .  ja  va  2  s .  c  o m*/
public WebdavManager(String host, String login, String password) {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(host);
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams();
    int maxHostConnections = 20;
    connectionParams.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
    connectionManager.setParams(connectionParams);
    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setParameter(HttpClientParams.CREDENTIAL_CHARSET, "UTF-8");
    clientParams.setParameter(HttpClientParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    client = new HttpClient(clientParams, connectionManager);
    Credentials creds = new UsernamePasswordCredentials(login, password);
    client.getState().setCredentials(AuthScope.ANY, creds);
    client.setHostConfiguration(hostConfig);
}

From source file:de.kp.ames.webdav.WebDAVClient.java

/**
 * Constructor/*from   ww  w .  java2s .c o  m*/
 * 
 * @param alias
 * @param keypass
 * @param uri
 */
public WebDAVClient(String alias, String keypass, String uri) {

    /*
     * Register request uri
     */
    this.uri = uri;

    /*
     * Setup HttpClient
     */
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(uri);

    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();

    int maxHostConnections = 200;
    params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);

    connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
    client.setHostConfiguration(hostConfig);

    Credentials creds = new UsernamePasswordCredentials(alias, keypass);
    client.getState().setCredentials(AuthScope.ANY, creds);

}

From source file:com.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

public StardogEndpoint(String url, String username, String passwd) {
    this.url = url;

    HttpConnectionManager manager = new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(20);
    params.setStaleCheckingEnabled(false);
    manager.setParams(params);

    HttpClientParams clientParams = new HttpClientParams();

    client = new HttpClient(clientParams, manager);

    setCredentials(username, passwd);/*  w w  w.  j  av a  2 s.  c  om*/
}

From source file:eionet.cr.harvest.PullHarvest.java

/**
 *
 * @return/*from  w  w  w  . ja v a 2s  .c o m*/
 */
private EndpointHttpClient prepareEndpointHttpClient() {

    HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams();
    managerParams.setDefaultMaxConnectionsPerHost(20);
    managerParams.setStaleCheckingEnabled(false);

    int httpTimeout = GeneralConfig.getIntProperty(GeneralConfig.HARVESTER_HTTP_TIMEOUT, getTimeout());
    managerParams.setConnectionTimeout(httpTimeout);
    managerParams.setSoTimeout(httpTimeout);

    HttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    manager.setParams(managerParams);

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setParameter("http.useragent", URLUtil.userAgentHeader());
    clientParams.setParameter("http.protocol.max-redirects", MAX_REDIRECTIONS);

    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Connection", "close");
    clientParams.setParameter("additionalHTTPHeaders", headers);

    return new EndpointHttpClient(clientParams, manager);
}

From source file:com.creditease.bdp.axis2.transport.http.HTTPSender.java

@Override
protected HttpClient getHttpClient(MessageContext msgContext) {
    ConfigurationContext configContext = msgContext.getConfigurationContext();

    HttpClient httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

    if (httpClient == null) {
        httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
    }/*from  w ww  .j a va  2s . co m*/

    if (httpClient != null) {
        return httpClient;
    }

    synchronized (this) {
        httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

        if (httpClient == null) {
            httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
        }

        if (httpClient != null) {
            return httpClient;
        }

        HttpConnectionManager connManager = (HttpConnectionManager) msgContext
                .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        if (connManager == null) {
            connManager = (HttpConnectionManager) msgContext
                    .getProperty(HTTPConstants.MUTTITHREAD_HTTP_CONNECTION_MANAGER);
        }
        if (connManager == null) {
            // reuse HttpConnectionManager
            synchronized (configContext) {
                connManager = (HttpConnectionManager) configContext
                        .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
                if (connManager == null) {
                    log.trace("Making new ConnectionManager");
                    connManager = new MultiThreadedHttpConnectionManager();
                    // NOTE: Added by CJ
                    Integer maxConnectionPerHostConf = (Integer) msgContext
                            .getProperty(Constants.MAX_CONNECTIONS_PER_HOST);
                    Integer maxConnectionTotalConf = (Integer) msgContext
                            .getProperty(Constants.MAX_CONNECTIONS_TOTAL);
                    if (maxConnectionPerHostConf != null || maxConnectionTotalConf != null) {
                        HttpConnectionManagerParams param = new HttpConnectionManagerParams();
                        if (maxConnectionPerHostConf != null) {
                            param.setDefaultMaxConnectionsPerHost(maxConnectionPerHostConf);
                        }
                        if (maxConnectionTotalConf != null) {
                            param.setMaxTotalConnections(maxConnectionTotalConf);
                        }
                        connManager.setParams(param);
                    }
                    configContext.setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, connManager);
                }
            }
        }
        /*
         * Create a new instance of HttpClient since the way
         * it is used here it's not fully thread-safe.
         */
        httpClient = new HttpClient(connManager);

        // Set the default timeout in case we have a connection pool starvation to 30sec
        httpClient.getParams().setConnectionManagerTimeout(30000);

        // Get the timeout values set in the runtime
        initializeTimeouts(msgContext, httpClient);

        return httpClient;
    }
}

From source file:oauth.OAuthTokenValidaterStubFactory.java

/**
 * This created httpclient pool that can be used to connect to external entity. This connection can be configured
 * via broker.xml by setting up the required http connection parameters.
 *
 * @return an instance of HttpClient that is configured with MultiThreadedHttpConnectionManager
 *//*from ww w  .ja va 2s  . c  om*/
private HttpClient createHttpClient() {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(Integer
            .parseInt(tokenValidationProperties.getProperty(UIConstants.MAXIMUM_HTTP_CONNECTION_PER_HOST)));
    params.setMaxTotalConnections(
            Integer.parseInt(tokenValidationProperties.getProperty(UIConstants.MAXIMUM_TOTAL_HTTP_CONNECTION)));
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(params);
    return new HttpClient(connectionManager);
}

From source file:org.apache.camel.component.http.HttpComponent.java

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
        throws Exception {
    String addressUri = "http://" + remaining;
    if (uri.startsWith("https:")) {
        addressUri = "https://" + remaining;
    }/*  ww  w. ja va  2s .com*/
    Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
    // must extract well known parameters before we create the endpoint
    // TODO cmueller: remove the "httpBindingRef" look up in Camel 3.0
    HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
    if (binding == null) {
        // try without ref
        binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
    }
    String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class);
    Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class);
    String authMethodPriority = getAndRemoveParameter(parameters, "authMethodPriority", String.class);
    HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters,
            "headerFilterStrategy", HeaderFilterStrategy.class);
    UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
    // http client can be configured from URI options
    HttpClientParams clientParams = new HttpClientParams();
    IntrospectionSupport.setProperties(clientParams, parameters, "httpClient.");
    // validate that we could resolve all httpClient. parameters as this component is lenient
    validateParameters(uri, parameters, "httpClient.");
    // http client can be configured from URI options
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    // setup the httpConnectionManagerParams
    IntrospectionSupport.setProperties(connectionManagerParams, parameters, "httpConnectionManager.");
    validateParameters(uri, parameters, "httpConnectionManager.");
    // make sure the component httpConnectionManager is take effect
    HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
    if (thisHttpConnectionManager == null) {
        // only set the params on the new created http connection manager
        thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
        thisHttpConnectionManager.setParams(connectionManagerParams);
    }
    // create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
    final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
    HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
    addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
    URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);

    // create the endpoint and connectionManagerParams already be set
    HttpEndpoint endpoint = new HttpEndpoint(endpointUri.toString(), this, clientParams,
            thisHttpConnectionManager, configurer);

    if (headerFilterStrategy != null) {
        endpoint.setHeaderFilterStrategy(headerFilterStrategy);
    } else {
        setEndpointHeaderFilterStrategy(endpoint);
    }
    if (urlRewrite != null) {
        // let CamelContext deal with the lifecycle of the url rewrite
        // this ensures its being shutdown when Camel shutdown etc.
        getCamelContext().addService(urlRewrite);
        endpoint.setUrlRewrite(urlRewrite);
    }

    // prefer to use endpoint configured over component configured
    if (binding == null) {
        // fallback to component configured
        binding = getHttpBinding();
    }
    if (binding != null) {
        endpoint.setBinding(binding);
    }
    if (proxyHost != null) {
        endpoint.setProxyHost(proxyHost);
        endpoint.setProxyPort(proxyPort);
    } else if (httpConfiguration != null) {
        endpoint.setProxyHost(httpConfiguration.getProxyHost());
        endpoint.setProxyPort(httpConfiguration.getProxyPort());
    }
    if (authMethodPriority != null) {
        endpoint.setAuthMethodPriority(authMethodPriority);
    } else if (httpConfiguration != null && httpConfiguration.getAuthMethodPriority() != null) {
        endpoint.setAuthMethodPriority(httpConfiguration.getAuthMethodPriority());
    } else {
        // no explicit auth method priority configured, so use convention over configuration
        // and set priority based on auth method
        if (!authMethods.isEmpty()) {
            authMethodPriority = CollectionHelper.collectionAsCommaDelimitedString(authMethods);
            endpoint.setAuthMethodPriority(authMethodPriority);
        }
    }
    setProperties(endpoint, parameters);
    // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
    URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);

    // validate http uri that end-user did not duplicate the http part that can be a common error
    String part = httpUri.getSchemeSpecificPart();
    if (part != null) {
        part = part.toLowerCase();
        if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://")
                || part.startsWith("//https://")) {
            throw new ResolveEndpointFailedException(uri,
                    "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
        }
    }
    endpoint.setHttpUri(httpUri);
    return endpoint;
}