Example usage for org.apache.commons.httpclient.params HttpConnectionManagerParams HttpConnectionManagerParams

List of usage examples for org.apache.commons.httpclient.params HttpConnectionManagerParams HttpConnectionManagerParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpConnectionManagerParams HttpConnectionManagerParams.

Prototype

HttpConnectionManagerParams

Source Link

Usage

From source file:org.apache.wicket.threadtest.tester.Tester.java

/**
 * Runs the test./*from   www. j  a  v a  2s  . c om*/
 * 
 * @throws Exception
 */
public void run() throws Exception {

    activeThreads = 0;

    HttpConnectionManagerParams connManagerParams = new HttpConnectionManagerParams();
    connManagerParams.setDefaultMaxConnectionsPerHost(numberOfThreads * 2);
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    manager.setParams(connManagerParams);

    Server server = null;
    GetMethod getMethod = new GetMethod("http://localhost:" + port + "/");
    try {
        getMethod.setFollowRedirects(true);
        HttpClient httpClient = new HttpClient(params, manager);
        int code = httpClient.executeMethod(getMethod);
        if (code != 200) {
            server = startServer(port);
        }
    } catch (Exception e) {
        server = startServer(port);
    } finally {
        getMethod.releaseConnection();
    }

    try {

        ThreadGroup g = new ThreadGroup("runners");
        Thread[] threads = new Thread[numberOfThreads];
        HttpClient client = null;
        for (int i = 0; i < numberOfThreads; i++) {

            if (multipleSessions) {
                client = new HttpClient(params, manager);
                client.getHostConfiguration().setHost(host, port);
            } else {
                if (client == null) {
                    client = new HttpClient(params, manager);
                    client.getHostConfiguration().setHost(host, port);
                }
            }
            threads[i] = new Thread(g, new CommandRunner(commands, client, this));
        }

        long start = System.currentTimeMillis();

        for (int i = 0; i < numberOfThreads; i++) {
            activeThreads++;
            threads[i].start();
        }

        while (activeThreads > 0) {
            synchronized (this) {
                wait();
            }
        }

        long end = System.currentTimeMillis();
        long time = end - start;
        log.info("\n******** finished in " + Duration.milliseconds(time) + " (" + time + " milis)");

    } finally {
        MultiThreadedHttpConnectionManager.shutdownAll();
        if (server != null) {
            server.stop();
        }
    }
}

From source file:org.appverse.web.framework.backend.ws.helpers.StubHelper.java

public static void configureEndpoint(String endpointPropertiesFile, String timeoutPropertyName,
        ServiceClient _serviceClient) {//from   w  w  w. j  ava 2  s. c o  m
    Properties endpointsProperties = new Properties();
    InputStream endPointsInputStream = StubHelper.class.getResourceAsStream(endpointPropertiesFile);
    try {
        endpointsProperties.load(endPointsInputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String accountTimeoutString = (String) endpointsProperties.get(timeoutPropertyName);
    try {
        long accountTimeout = new Long(accountTimeoutString) * 1000;
        _serviceClient.getOptions().setTimeOutInMilliSeconds(accountTimeout);
    } catch (NumberFormatException e) {
        logger.equals("Error login axis account service timeout");
    }
    String endpointProxyEnabled = (String) endpointsProperties.get("endpoint.proxy.enabled");
    if (endpointProxyEnabled != null && endpointProxyEnabled.equals("true")) {
        HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
        String endpointProxyHost = endpointsProperties.getProperty("endpoint.proxy.host");
        proxyProperties.setProxyName(endpointProxyHost);
        int endpointProxyPort = new Integer(endpointsProperties.getProperty("endpoint.proxy.port"));
        proxyProperties.setProxyPort(endpointProxyPort);
        _serviceClient.getOptions().setProperty(HTTPConstants.PROXY, proxyProperties);
    }
    if (endpointsProperties.getProperty("endpoint.ignore_SSL_errors") != null
            && endpointsProperties.getProperty("endpoint.ignore_SSL_errors").equals("true")) {
        // Create a trust manager that does not validate certificate
        // chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }
    }

    ConfigurationContext configurationContext = _serviceClient.getServiceContext().getConfigurationContext();
    MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(50);
    multiThreadedHttpConnectionManager.setParams(params);
    HttpClient httpClient = new HttpClient(multiThreadedHttpConnectionManager);
    configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);

}

From source file:org.attribyte.api.http.impl.commons.Commons3Client.java

private HttpConnectionManagerParams fromOptions(ClientOptions options) {
    HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams();
    if (options != ClientOptions.IMPLEMENTATION_DEFAULT) {
        connectionParams.setConnectionTimeout(options.connectionTimeoutMillis);
        connectionParams.setSoTimeout(options.socketTimeoutMillis);
        connectionParams.setDefaultMaxConnectionsPerHost(options.maxConnectionsPerDestination);
        connectionParams.setMaxTotalConnections(options.maxConnectionsTotal);
        connectionParams.setSendBufferSize(options.requestBufferSize);
        connectionParams.setReceiveBufferSize(options.responseBufferSize);
        //connectionParams.setLinger();
        //connectionParams.setStaleCheckingEnabled();
        //connectionParams.setTcpNoDelay();
    }//from www  . j a  v a2 s  .  c  o  m
    return connectionParams;
}

From source file:org.bonitasoft.connectors.webdav.exo.common.ExoConnector.java

/**
 * initiate client/*from  ww w . j a v  a2s  .com*/
 * 
 * @param host
 * @param port
 * @param username
 * @param password
 */
public void initClient(final String host, final int port, final String username, final String password) {

    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("WebDAVConnector {host=" + host + ", port=" + port + ", username=" + username
                + ", password=******}");
    }

    final HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(host, port);

    final HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    final HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    final int maxHostConnections = 20;
    params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
    connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
    final Credentials creds = new UsernamePasswordCredentials(username, password);
    client.getState().setCredentials(AuthScope.ANY, creds);
    client.setHostConfiguration(hostConfig);
}

From source file:org.collectionspace.chain.csp.persistence.services.connection.ServicesConnection.java

private void initClient() {
    if (manager != null)
        return;// w ww  .  jav a2s .  co  m
    synchronized (getClass()) {
        if (manager != null)
            return;

        // We're only connecting to one host, so set the max connections per host to be
        // the same as the max total connections.

        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        params.setMaxTotalConnections(MAX_SERVICES_CONNECTIONS);
        params.setDefaultMaxConnectionsPerHost(MAX_SERVICES_CONNECTIONS);

        manager = new MultiThreadedHttpConnectionManager();
        manager.setParams(params);
    }
}

From source file:org.eurekastreams.server.service.utility.http.HttpDocumentFetcherImpl.java

/**
 * Retrieves an XML document from a given URL.
 *
 * @param url/*from ww w .j a  va2s  . c o  m*/
 *            The URL from which to get the document.
 * @param httpHeaders
 *            HTTP headers to add to the request.
 * @param proxyHost
 *            host name to use (if desired) for proxying http requests.
 * @param proxyPort
 *            port for http proxy server.
 * @param timeout
 *            the timeout period to wait for the request to return (in ms).
 * @param domFactory
 *            Factory for creating document builders.
 * @return The document.
 * @throws IOException
 *             On error.
 * @throws ParserConfigurationException
 *             On error.
 * @throws SAXException
 *             On error.
 */
public Document fetchDocument(final String url, final Map<String, String> httpHeaders, final String proxyHost,
        final String proxyPort, final int timeout, final DocumentBuilderFactory domFactory)
        throws IOException, ParserConfigurationException, SAXException {
    HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams();
    managerParams.setSoTimeout(timeout);
    managerParams.setConnectionTimeout(timeout);

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

    HttpClientParams params = new HttpClientParams();
    params.setConnectionManagerTimeout(timeout);
    params.setSoTimeout(timeout);

    HttpClient client = new HttpClient(params, manager);
    HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(1, true);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);

    if (!proxyHost.isEmpty()) {
        client.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
    }

    GetMethod get = new GetMethod(url);

    if (httpHeaders != null) {
        for (Map.Entry<String, String> header : httpHeaders.entrySet()) {
            get.setRequestHeader(header.getKey(), header.getValue());
        }
    }

    try {
        client.executeMethod(get);

        DocumentBuilder builder = domFactory.newDocumentBuilder();

        return builder.parse(get.getResponseBodyAsStream());
    } finally {
        get.releaseConnection();
    }
}

From source file:org.exoplatform.bonitaextension.connectors.webdav.common.WebDAVClient.java

/**
 * Create a new webdav Client with a user name and a password to connect to eXo server
 *
 * @param host//from   ww w . j  a  v a 2  s .  c  o m
 * @param port
 * @param username
 * @param password
 */
public WebDAVClient(String host, Long port, String username, String password) {

    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("WebDAVConnector {host=" + host + ", port=" + port + ", username=" + username
                + ", password=******}");
    }

    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(host, port.intValue());

    HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    int maxHostConnections = 20;
    params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
    connectionManager.setParams(params);

    client = new HttpClient(connectionManager);
    Credentials creds = new UsernamePasswordCredentials(username, password);
    client.getState().setCredentials(AuthScope.ANY, creds);
    client.setHostConfiguration(hostConfig);

}

From source file:org.exoplatform.services.common.HttpClientImpl.java

private void setHost(String protocol, String host, int port) throws Exception {
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams para = new HttpConnectionManagerParams();
    para.setConnectionTimeout(HTTP_TIMEOUT);
    para.setDefaultMaxConnectionsPerHost(10);
    para.setMaxTotalConnections(20);//from w w w. j a v  a  2 s.co m
    para.setStaleCheckingEnabled(true);
    manager.setParams(para);
    http = new HttpClient(manager);
    http.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    http.getParams().setParameter("http.socket.timeout", new Integer(HTTP_TIMEOUT));
    http.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    http.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    http.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    if (port < 0)
        port = 80;
    HostConfiguration hostConfig = http.getHostConfiguration();
    hostConfig.setHost(host, port, protocol);

    String proxyHost = System.getProperty("httpclient.proxy.host");
    if (proxyHost == null || proxyHost.trim().length() < 1)
        return;
    String proxyPort = System.getProperty("httpclient.proxy.port");
    hostConfig.setProxy(proxyHost, Integer.parseInt(proxyPort));

    String username = System.getProperty("httpclient.proxy.username");
    String password = System.getProperty("httpclient.proxy.password");
    String ntlmHost = System.getProperty("httpclient.proxy.ntlm.host");
    String ntlmDomain = System.getProperty("httpclient.proxy.ntlm.domain");

    Credentials ntCredentials;
    if (ntlmHost == null || ntlmDomain == null) {
        ntCredentials = new UsernamePasswordCredentials(username, password);
    } else {
        ntCredentials = new NTCredentials(username, password, ntlmHost, ntlmDomain);
    }
    http.getState().setProxyCredentials(AuthScope.ANY, ntCredentials);
}

From source file:org.geosdi.geoplatform.services.httpclient.GeoSDIHttpClient.java

public GeoSDIHttpClient() {

    System.setProperty("jsse.enableSNIExtension", "false");
    System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setSoTimeout(30000);/* ww w. j a  v a 2  s  .  com*/
    params.setConnectionTimeout(30000);
    params.setMaxTotalConnections(6);
    params.setDefaultMaxConnectionsPerHost(6);
    this.connectionManager.setParams(params);

    this.client = this.createHttpClient();

    //this.applySystemProxySettings();
}

From source file:org.geoserver.gss.HTTPGSSClientFactory.java

HttpClient getClient() {
    if (client == null) {
        client = new HttpClient();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        // setting timeouts (one minute hard coded, TODO: make this configurable)
        params.setSoTimeout(60 * 1000);//  w ww  . j av a2s.  co m
        params.setConnectionTimeout(60 * 1000);
        params.setDefaultMaxConnectionsPerHost(1);
        MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
        manager.setParams(params);
        client.setHttpConnectionManager(manager);
    }
    return client;
}