Example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager getParams

List of usage examples for org.apache.commons.httpclient MultiThreadedHttpConnectionManager getParams

Introduction

In this page you can find the example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager getParams.

Prototype

public HttpConnectionManagerParams getParams() 

Source Link

Usage

From source file:com.twelve.capital.external.feed.util.HttpImpl.java

public HttpImpl() {

    // Override the default protocol socket factory because it uses
    // reflection for JDK 1.4 compatibility, which we do not need. It also
    // attemps to create a new socket in a different thread so that we
    // cannot track which class loader initiated the call.

    Protocol protocol = new Protocol("http", new FastProtocolSocketFactory(), 80);

    Protocol.registerProtocol("http", protocol);

    // Mimic behavior found in
    // http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

    if (Validator.isNotNull(_NON_PROXY_HOSTS)) {
        String nonProxyHostsRegEx = _NON_PROXY_HOSTS;

        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\.", "\\\\.");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\*", ".*?");
        nonProxyHostsRegEx = nonProxyHostsRegEx.replaceAll("\\|", ")|(");

        nonProxyHostsRegEx = "(" + nonProxyHostsRegEx + ")";

        _nonProxyHostsPattern = Pattern.compile(nonProxyHostsRegEx);
    }/*  ww w  .  j ava 2  s  .  co  m*/

    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams httpConnectionManagerParams = httpConnectionManager.getParams();

    httpConnectionManagerParams.setConnectionTimeout(_TIMEOUT);
    httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(new Integer(_MAX_CONNECTIONS_PER_HOST));
    httpConnectionManagerParams.setMaxTotalConnections(new Integer(_MAX_TOTAL_CONNECTIONS));
    httpConnectionManagerParams.setSoTimeout(_TIMEOUT);

    _httpClient.setHttpConnectionManager(httpConnectionManager);
    _proxyHttpClient.setHttpConnectionManager(httpConnectionManager);

    if (!hasProxyConfig() || Validator.isNull(_PROXY_USERNAME)) {
        return;
    }

    List<String> authPrefs = new ArrayList<String>();

    if (_PROXY_AUTH_TYPE.equals("username-password")) {
        _proxyCredentials = new UsernamePasswordCredentials(_PROXY_USERNAME, _PROXY_PASSWORD);

        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.NTLM);
    } else if (_PROXY_AUTH_TYPE.equals("ntlm")) {
        _proxyCredentials = new NTCredentials(_PROXY_USERNAME, _PROXY_PASSWORD, _PROXY_NTLM_HOST,
                _PROXY_NTLM_DOMAIN);

        authPrefs.add(AuthPolicy.NTLM);
        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.DIGEST);
    }

    HttpClientParams httpClientParams = _proxyHttpClient.getParams();

    httpClientParams.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
}

From source file:fr.cls.atoll.motu.library.misc.netcdf.NetCdfReader.java

public void initNetcdfHttpClient() throws MotuException {
    try {/*from  w w w . j ava2s .  co  m*/
        synchronized (this) {
            Field field = NetcdfDataset.class.getDeclaredField("httpClient");
            field.setAccessible(true);
            HttpClient httpClientNetcdfDataset = (HttpClient) field.get(null);
            HttpClientCAS httpClientCAS = null;

            field = DConnect2.class.getDeclaredField("_httpClient");
            field.setAccessible(true);
            HttpClient httpClientDConnect2 = (HttpClient) field.get(null);

            field = HTTPRandomAccessFile.class.getDeclaredField("_client");
            field.setAccessible(true);
            HttpClient httpClientHTTPRandomAccessFile = (HttpClient) field.get(null);

            if ((httpClientNetcdfDataset != null) && (httpClientDConnect2 != null)
                    && (httpClientHTTPRandomAccessFile != null)) {
                return;
            }

            if ((httpClientNetcdfDataset == null) && (httpClientDConnect2 == null)
                    && (httpClientHTTPRandomAccessFile == null)) {

                final int SO_TIMEOUT_VALUE = 180000; // in milliseconds 

                MultiThreadedHttpConnectionManager connectionManager = HttpUtil.createConnectionManager();
                // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out
                connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds 

                httpClientCAS = new HttpClientCAS(connectionManager);

                HttpClientParams httpClientParams = new HttpClientParams();
                httpClientParams.setParameter("http.protocol.allow-circular-redirects", true);
                httpClientCAS.setParams(httpClientParams);

                NetcdfDataset.setHttpClient(httpClientCAS);

                connectionManager = HttpUtil.createConnectionManager();
                // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out
                connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds 

                httpClientCAS = new HttpClientCAS(connectionManager);

                httpClientParams = new HttpClientParams();
                httpClientParams.setParameter("http.protocol.allow-circular-redirects", true);
                httpClientCAS.setParams(httpClientParams);

                DConnect2.setHttpClient(httpClientCAS);

                connectionManager = HttpUtil.createConnectionManager();
                // WARNING: because socket read can raise an infinite time out, set an arbitrary socket read time out
                connectionManager.getParams().setSoTimeout(SO_TIMEOUT_VALUE); // in milliseconds 

                httpClientCAS = new HttpClientCAS(connectionManager);

                httpClientParams = new HttpClientParams();
                httpClientParams.setParameter("http.protocol.allow-circular-redirects", true);
                httpClientCAS.setParams(httpClientParams);

                HTTPRandomAccessFile.setHttpClient(httpClientCAS);
            }

            if ((httpClientNetcdfDataset != null) && !(httpClientNetcdfDataset instanceof HttpClientCAS)) {
                throw new MotuException(String.format(
                        "Error in NetCdfReader acquireDataset - httpClientNetcdfDataset has been set but is no an HttpClientCAS object:'%s'",
                        httpClientNetcdfDataset.getClass().getName()));
            }

            if ((httpClientDConnect2 != null) && !(httpClientDConnect2 instanceof HttpClientCAS)) {
                throw new MotuException(String.format(
                        "Error in NetCdfReader acquireDataset - httpClientDConnect2 has been set but is no an HttpClientCAS object:'%s'",
                        httpClientDConnect2.getClass().getName()));
            }

            if ((httpClientHTTPRandomAccessFile != null)
                    && !(httpClientHTTPRandomAccessFile instanceof HttpClientCAS)) {
                throw new MotuException(String.format(
                        "Error in NetCdfReader acquireDataset - httpClientHTTPRandomAccessFile has been set but is no an HttpClientCAS object:'%s'",
                        httpClientHTTPRandomAccessFile.getClass().getName()));
            }

        }
    } catch (MotuException e) {
        throw e;
    } catch (Exception e) {
        throw new MotuException(
                "Error in NetCdfReader initNetcdfHttpClient - Unable to initialize httpClient object", e);
    }

}

From source file:org.activebpel.rt.axis.bpel.handlers.AeHTTPSender.java

/** @deprecated */
protected void initialize() {
    if (sConnectionManager == null) {
        synchronized (AeHTTPSender.class) {
            if (sConnectionManager == null) {
                // initialize the connection manager w/ its properties
                MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
                sClientProperties = CommonsHTTPClientPropertiesFactory.create();

                // max connections per host, defaults to constant above
                int maxConnectionsPerHost = getIntegerOption(
                        DefaultCommonsHTTPClientProperties.MAXIMUM_CONNECTIONS_PER_HOST_PROPERTY_KEY,
                        DEFAULT_MAX_CONNECTIONS_PER_HOST);

                cm.setMaxConnectionsPerHost(maxConnectionsPerHost);

                // max connections, defaults to constant above
                int maxConnections = getIntegerOption(
                        DefaultCommonsHTTPClientProperties.MAXIMUM_TOTAL_CONNECTIONS_PROPERTY_KEY,
                        DEFAULT_MAX_CONNECTIONS);
                // as per apache-commons problem report #36882 (max connections per host setting does not work)
                // cm.setMaxTotalConnections(maxConnections);
                cm.getParams().setMaxTotalConnections(maxConnections);

                // save the connection manager for future calls
                sConnectionManager = cm;

                sConnectionTimeoutThread = new IdleConnectionTimeoutThread();
                sConnectionTimeoutThread.addConnectionManager(sConnectionManager);

                int sweepInterval = getIntegerOption(KEY_IDLE_SWEEP_INTERVAL,
                        DEFAULT_IDLE_CONNECTION_SWEEP_INTERVAL);
                sConnectionTimeoutThread.setTimeoutInterval(sweepInterval);

                int idleTimeout = getIntegerOption(KEY_IDLE_TIMEOUT, DEFAULT_IDLE_CONNECTION_TIMEOUT);
                sConnectionTimeoutThread.setConnectionTimeout(idleTimeout);
                sConnectionTimeoutThread.start();
            }/*from   ww  w. j a  va  2s  .  c  om*/
        }
    }
    connectionManager = sConnectionManager;
    clientProperties = sClientProperties;
}

From source file:org.apache.airavata.client.stub.interpretor.WorkflowInterpretorStub.java

/**
 * Constructor that takes in a configContext and useseperate listner
 *///from  w  ww  .j a  v a 2  s . co m
public WorkflowInterpretorStub(org.apache.axis2.context.ConfigurationContext configurationContext,
        java.lang.String targetEndpoint, boolean useSeparateListener) throws org.apache.axis2.AxisFault {
    // To populate AxisService
    populateAxisService();
    populateFaults();
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.getParams().setMaxTotalConnections(10000);
    httpConnectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 100);
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(200);
    HttpClient httpClient = new HttpClient(httpConnectionManager);
    _serviceClient = new org.apache.axis2.client.ServiceClient(configurationContext, _service);

    configurationContext = _serviceClient.getServiceContext().getConfigurationContext();
    configurationContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
    configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
    configurationContext.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);
    _serviceClient.getOptions().setTo(new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
    _serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
}

From source file:org.apache.axis.transport.http.CommonsHTTPSender.java

protected void initialize() {
    MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    this.clientProperties = CommonsHTTPClientPropertiesFactory.create();
    cm.getParams().setDefaultMaxConnectionsPerHost(clientProperties.getMaximumConnectionsPerHost());
    cm.getParams().setMaxTotalConnections(clientProperties.getMaximumTotalConnections());
    // If defined, set the default timeouts
    // Can be overridden by the MessageContext
    if (this.clientProperties.getDefaultConnectionTimeout() > 0) {
        cm.getParams().setConnectionTimeout(this.clientProperties.getDefaultConnectionTimeout());
    }/*  ww w .  j  av  a2 s . co m*/
    if (this.clientProperties.getDefaultSoTimeout() > 0) {
        cm.getParams().setSoTimeout(this.clientProperties.getDefaultSoTimeout());
    }
    this.connectionManager = cm;
}

From source file:org.apache.hadoop.hbase.rest.client.Client.java

/**
 * Constructor//from w ww . jav a  2s .  c o  m
 * @param cluster the cluster definition
 */
public Client(Cluster cluster) {
    this.cluster = cluster;
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams managerParams = manager.getParams();
    managerParams.setConnectionTimeout(2000); // 2 s
    managerParams.setDefaultMaxConnectionsPerHost(10);
    managerParams.setMaxTotalConnections(100);
    extraHeaders = new ConcurrentHashMap<String, String>();
    this.httpClient = new HttpClient(manager);
    HttpClientParams clientParams = httpClient.getParams();
    clientParams.setVersion(HttpVersion.HTTP_1_1);
}

From source file:org.apache.hadoop.hbase.stargate.client.Client.java

/**
 * Constructor/*from  www  .  j a  v  a 2s.c  o  m*/
 * @param cluster the cluster definition
 */
public Client(Cluster cluster) {
    this.cluster = cluster;
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams managerParams = manager.getParams();
    managerParams.setConnectionTimeout(2000); // 2 s
    managerParams.setDefaultMaxConnectionsPerHost(10);
    managerParams.setMaxTotalConnections(100);
    this.httpClient = new HttpClient(manager);
    HttpClientParams clientParams = httpClient.getParams();
    clientParams.setVersion(HttpVersion.HTTP_1_1);
}

From source file:org.cryptomator.frontend.webdav.WebDavServerTest.java

@Test
public void testMultipleGetWithRangeAsync() throws IOException, URISyntaxException, InterruptedException {
    final String testResourceUrl = servletRoot + "/foo.txt";

    // prepare 8MiB test data:
    final byte[] plaintextData = new byte[2097152 * Integer.BYTES];
    final ByteBuffer plaintextDataByteBuffer = ByteBuffer.wrap(plaintextData);
    for (int i = 0; i < 2097152; i++) {
        plaintextDataByteBuffer.putInt(i);
    }//from w  w  w. j a v a 2  s. com
    try (WritableFile w = fs.file("foo.txt").openWritable()) {
        plaintextDataByteBuffer.flip();
        w.write(plaintextDataByteBuffer);
    }

    final MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    cm.getParams().setDefaultMaxConnectionsPerHost(50);
    final HttpClient client = new HttpClient(cm);

    // multiple async range requests:
    final List<ForkJoinTask<?>> tasks = new ArrayList<>();
    final Random generator = new Random(System.currentTimeMillis());

    final AtomicBoolean success = new AtomicBoolean(true);

    // 10 full interrupted requests:
    for (int i = 0; i < 10; i++) {
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 200) {
                    LOG.error("Invalid status code for interrupted full request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 crappy interrupted range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length);
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final int statusCode = client.executeMethod(getMethod);
                if (statusCode != 206) {
                    LOG.error("Invalid status code for interrupted range request");
                    success.set(false);
                }
                getMethod.getResponseBodyAsStream().read();
                getMethod.getResponseBodyAsStream().close();
                getMethod.releaseConnection();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 50 normal open range requests:
    for (int i = 0; i < 50; i++) {
        final int lower = generator.nextInt(plaintextData.length - 512);
        final int upper = plaintextData.length - 1;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-");
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 10];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for open range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for open range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for open range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    // 200 normal closed range requests:
    for (int i = 0; i < 200; i++) {
        final int pos1 = generator.nextInt(plaintextData.length - 512);
        final int pos2 = pos1 + 512;
        final ForkJoinTask<?> task = ForkJoinTask.adapt(() -> {
            try {
                final int lower = Math.min(pos1, pos2);
                final int upper = Math.max(pos1, pos2);
                final HttpMethod getMethod = new GetMethod(testResourceUrl);
                getMethod.addRequestHeader("Range", "bytes=" + lower + "-" + upper);
                final byte[] expected = Arrays.copyOfRange(plaintextData, lower, upper + 1);
                final int statusCode = client.executeMethod(getMethod);
                final byte[] responseBody = new byte[upper - lower + 1];
                final int bytesRead = IOUtils.read(getMethod.getResponseBodyAsStream(), responseBody);
                getMethod.releaseConnection();
                if (statusCode != 206) {
                    LOG.error("Invalid status code for closed range request");
                    success.set(false);
                } else if (upper - lower + 1 != bytesRead) {
                    LOG.error("Invalid response length for closed range request");
                    success.set(false);
                } else if (!Arrays.equals(expected, Arrays.copyOfRange(responseBody, 0, bytesRead))) {
                    LOG.error("Invalid response body for closed range request");
                    success.set(false);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        tasks.add(task);
    }

    Collections.shuffle(tasks, generator);

    final ForkJoinPool pool = new ForkJoinPool(4);
    for (ForkJoinTask<?> task : tasks) {
        pool.execute(task);
    }
    for (ForkJoinTask<?> task : tasks) {
        task.join();
    }
    pool.shutdown();
    cm.shutdown();

    Assert.assertTrue(success.get());
}

From source file:org.eclipse.orion.server.cf.CFActivator.java

/**
 * Returns an HTTPClient instance that is configured to support multiple connections
 * in different threads. Callers must explicitly release any connections made using this
 * client./*w ww  .  j a  va 2  s  .  c om*/
 */
public synchronized HttpClient getHttpClient() {
    //see http://hc.apache.org/httpclient-3.x/threading.html
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams connectionManagerParams = connectionManager.getParams();
    connectionManagerParams.setConnectionTimeout(30000);
    connectionManager.setParams(connectionManagerParams);

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setConnectionManagerTimeout(300000); // 5 minutes

    return new HttpClient(clientParams, connectionManager);
}

From source file:org.iavante.sling.gad.transcoder.impl.TranscoderServiceImpl.java

public int sendConversionTask3(String conversionType, String sourceLocation, String targetLocation,
        String notificationUrl, String externalStorageServer, String externalStorageUrl, String params,
        String ds_custom_props) {

    Map<String, String> envs = System.getenv();
    Set<String> keys = envs.keySet();
    Iterator<String> it = keys.iterator();

    List authPrefs = new ArrayList(2);
    Credentials defaultcreds;/*w w w. ja v  a 2s .c om*/

    authPrefs.add(AuthPolicy.DIGEST);
    authPrefs.add(AuthPolicy.BASIC);

    defaultcreds = new UsernamePasswordCredentials(transcodingServerUser, transcodingServerPassword);

    // Set client connection params
    MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams connParams = connManager.getParams();
    connParams.setConnectionTimeout(800);
    HttpClient client = new HttpClient(connManager);

    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    if (log.isInfoEnabled())
        log.info("Sending conversion task");
    Node convTypeNode = null;
    try {
        convTypeNode = rootNode.getNode(baseRepoDir + "/" + COMPONENTS_FOLDER + "/" + TRANSCODER_FOLDER)
                .getNode(conversionType);
    } catch (PathNotFoundException e) {
        e.printStackTrace();
    } catch (RepositoryException e) {
        e.printStackTrace();
    }

    String executable = null;

    try {
        executable = convTypeNode.getProperty("command").getValue().getString();
        if (params == null) {
            params = convTypeNode.getProperty("params").getValue().getString();
        }
        if (ds_custom_props == null) {
            ds_custom_props = "";
        }
    } catch (ValueFormatException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (PathNotFoundException e) {
        e.printStackTrace();
    } catch (RepositoryException e) {
        e.printStackTrace();
    }

    PostMethod post = new PostMethod(sendConversionUrl);
    //post.getParams().setParameter("http.socket.timeout", new Integer(50));
    post.setDoAuthentication(true);

    NameValuePair[] data = { new NameValuePair("source_location", sourceLocation),
            new NameValuePair("target_location", targetLocation), new NameValuePair("executable", executable),
            new NameValuePair("params", params), new NameValuePair("ds_custom_props", ds_custom_props),
            new NameValuePair("notification_url", notificationUrl),
            new NameValuePair("extern_storage_server", externalStorageServer),
            new NameValuePair("sling:resourceType", "gad/job"),
            new NameValuePair("extern_storage_url", externalStorageUrl) };

    post.setRequestBody(data);

    post.setDoAuthentication(true);

    int status = 0;
    int intentos = 0;

    while ((status != 201) && (intentos < 2)) {
        try {
            client.executeMethod(post);
            status = post.getStatusCode();
            log.info("Conversion sent. Status code: " + status);
        } catch (HttpException e1) {
            log.error("Excepcion: HttpException");
            e1.printStackTrace();
            post.releaseConnection();
            return status;
        } catch (IOException e1) {
            log.error("Excepcion: IOexception");
            e1.printStackTrace();
            post.releaseConnection();
            return status;
        } finally {
            intentos++;
        }
    }
    post.releaseConnection();
    return status;

}