Example usage for org.apache.http.config SocketConfig custom

List of usage examples for org.apache.http.config SocketConfig custom

Introduction

In this page you can find the example usage for org.apache.http.config SocketConfig custom.

Prototype

public static Builder custom() 

Source Link

Usage

From source file:com.hybris.datahub.outbound.utils.RestTemplateUtil.java

private RestTemplate initializationTemplate() {
    final ConnectionConfig connConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build();
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(100000).build();
    final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
            .<ConnectionSocketFactory>create();
    final ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
    registryBuilder.register("http", plainSF);

    registryBuilder.register("https", setUpSSL());

    final Registry<ConnectionSocketFactory> registry = registryBuilder.build();
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
    connManager.setDefaultConnectionConfig(connConfig);
    connManager.setDefaultSocketConfig(socketConfig);
    connManager.setMaxTotal(1000);//from w  w w  .ja v a2 s .  c om
    connManager.setDefaultMaxPerRoute(1000);
    final HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

    final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    clientHttpRequestFactory.setConnectTimeout(50000);
    clientHttpRequestFactory.setReadTimeout(30000);
    // clientHttpRequestFactory.setConnectionRequestTimeout(200);

    restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(clientHttpRequestFactory);
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

    return restTemplate;
}

From source file:de.hybris.platform.marketplaceintegrationbackoffice.utils.MarketplaceintegrationbackofficeRestTemplateUtil.java

private RestTemplate initializationTemplate() {
    final ConnectionConfig connConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build();
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(100000).build();
    final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
            .<ConnectionSocketFactory>create();
    final ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
    registryBuilder.register("http", plainSF);

    registryBuilder.register("https", setUpSSL());

    final Registry<ConnectionSocketFactory> registry = registryBuilder.build();
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
    connManager.setDefaultConnectionConfig(connConfig);
    connManager.setDefaultSocketConfig(socketConfig);
    connManager.setMaxTotal(1000);//from ww w.j  a  v  a  2  s. c  om
    connManager.setDefaultMaxPerRoute(1000);
    final HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

    final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    clientHttpRequestFactory.setConnectTimeout(5000);
    clientHttpRequestFactory.setReadTimeout(5000);
    clientHttpRequestFactory.setConnectionRequestTimeout(200);

    restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(clientHttpRequestFactory);
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

    return restTemplate;
}

From source file:com.meltmedia.cadmium.core.github.ApiClient.java

protected HttpClient createHttpClient() {
    HttpClient client = HttpClients.custom()
            .setDefaultSocketConfig(SocketConfig.custom().setSoReuseAddress(true).build()).build();
    return client;
}

From source file:org.jboss.additional.testsuite.jdkall.present.jaxrs.client.ApacheHttpClient432TestCase.java

@Test
@OperateOnDeployment(DEPLOYMENT)//from www  . j a  v a  2s.c om
public void apacheHttpClient4EngineServletTest(@ArquillianResource URL url) throws Exception {
    SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true)
            .setSoReuseAddress(true).build();

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

    connManager.setMaxTotal(100);

    connManager.setDefaultMaxPerRoute(100);

    connManager.setDefaultSocketConfig(socketConfig);

    RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(100)
            .setConnectionRequestTimeout(3000).setStaleConnectionCheckEnabled(true).build();

    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig)
            .setConnectionManager(connManager).build();

    final ClientHttpEngine executor;

    executor = new ApacheHttpClient43Engine(httpClient);

    ResteasyClient client = new ResteasyClientBuilder().httpEngine(executor).build();

    final ApacheHttpClient43Resource proxy = client
            .target("http://127.0.0.1:8080/" + ApacheHttpClient432TestCase.class.getSimpleName())
            .proxy(ApacheHttpClient43Resource.class);

    WebTarget target = client
            .target("http://127.0.0.1:8080/" + ApacheHttpClient432TestCase.class.getSimpleName() + "/test2");

    Response response = target.request().get();
    Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());

    try {
        Response s = proxy.get();

        assertEquals(200, s.getStatus());
    } catch (ProcessingException e) {
        logger.warn("Exception occured." + e);

    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:ee.ria.xroad.common.opmonitoring.OpMonitoringDaemonHttpClient.java

/**
 * Creates HTTP client./*from  w w w  . jav  a 2 s .c  o m*/
 * @param authKey the client's authentication key
 * @param clientMaxTotalConnections client max total connections
 * @param clientMaxConnectionsPerRoute client max connections per route
 * @param connectionTimeoutMilliseconds connection timeout in milliseconds
 * @param socketTimeoutMilliseconds socket timeout in milliseconds
 * @return HTTP client
 * @throws Exception if creating a HTTPS client and SSLContext
 * initialization fails
 */
public static CloseableHttpClient createHttpClient(InternalSSLKey authKey, int clientMaxTotalConnections,
        int clientMaxConnectionsPerRoute, int connectionTimeoutMilliseconds, int socketTimeoutMilliseconds)
        throws Exception {
    log.trace("createHttpClient()");

    RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create();

    if ("https".equalsIgnoreCase(OpMonitoringSystemProperties.getOpMonitorDaemonScheme())) {
        sfr.register("https", createSSLSocketFactory(authKey));
    } else {
        sfr.register("http", PlainConnectionSocketFactory.INSTANCE);
    }

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(sfr.build());
    cm.setMaxTotal(clientMaxTotalConnections);
    cm.setDefaultMaxPerRoute(clientMaxConnectionsPerRoute);
    cm.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());

    RequestConfig.Builder rb = RequestConfig.custom().setConnectTimeout(connectionTimeoutMilliseconds)
            .setConnectionRequestTimeout(connectionTimeoutMilliseconds)
            .setSocketTimeout(socketTimeoutMilliseconds);

    HttpClientBuilder cb = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(rb.build());

    // Disable request retry
    cb.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    return cb.build();
}

From source file:com.adobe.acs.commons.mcp.impl.processes.asset.UrlAssetImport.java

@Override
public void init() throws RepositoryException {
    super.init();
    if (httpFactory != null) {
        HttpClientBuilder clientBuilder = httpFactory.newBuilder();
        clientBuilder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
        clientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(timeout).build());
        httpClient = clientBuilder.build();
    }/*from w  w w .j  av  a  2  s . c o m*/
}

From source file:com.hybris.integration.util.RestTemplateUtil.java

@PostConstruct
private RestTemplate initializationTemplate() {
    ConnectionConfig connConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build();
    SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(100000).build();
    RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
            .<ConnectionSocketFactory>create();
    ConnectionSocketFactory plainSF = new PlainConnectionSocketFactory();
    registryBuilder.register("http", plainSF);

    registryBuilder.register("https", setUpSSL());

    Registry<ConnectionSocketFactory> registry = registryBuilder.build();
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
    connManager.setDefaultConnectionConfig(connConfig);
    connManager.setDefaultSocketConfig(socketConfig);
    connManager.setMaxTotal(1000);//www.  j  av  a 2  s . c  o m
    connManager.setDefaultMaxPerRoute(1000);
    HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);
    clientHttpRequestFactory.setConnectTimeout(30000);
    clientHttpRequestFactory.setReadTimeout(30000);
    clientHttpRequestFactory.setConnectionRequestTimeout(30000);

    restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(clientHttpRequestFactory);
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

    return restTemplate;
}

From source file:org.ops4j.pax.url.mvn.internal.HttpClients.java

private static PoolingHttpClientConnectionManager createConnManager(PropertyResolver resolver, String pid) {
    boolean SSL_INSECURE = getBoolean(resolver, "maven.wagon.http.ssl.insecure",
            !getBoolean(resolver, pid + "certificateCheck", false));
    boolean IGNORE_SSL_VALIDITY_DATES = getBoolean(resolver, "maven.wagon.http.ssl.ignore.validity.dates",
            false);//from   w  ww  .  j a  va  2s .c om
    boolean SSL_ALLOW_ALL = getBoolean(resolver, "maven.wagon.http.ssl.allowall",
            !getBoolean(resolver, pid + "certificateCheck", false));
    boolean PERSISTENT_POOL = getBoolean(resolver, "maven.wagon.http.pool", true);
    int MAX_CONN_PER_ROUTE = getInteger(resolver, "maven.wagon.httpconnectionManager.maxPerRoute", 20);
    int MAX_CONN_TOTAL = getInteger(resolver, "maven.wagon.httpconnectionManager.maxTotal", 40);

    String sslProtocolsStr = getProperty(resolver, "https.protocols", null);
    String cipherSuitesStr = getProperty(resolver, "https.cipherSuites", null);
    String[] sslProtocols = sslProtocolsStr != null ? sslProtocolsStr.split(" *, *") : null;
    String[] cipherSuites = cipherSuitesStr != null ? cipherSuitesStr.split(" *, *") : null;

    SSLConnectionSocketFactory sslConnectionSocketFactory;
    if (SSL_INSECURE) {
        try {
            SSLContext sslContext = new SSLContextBuilder().useSSL()
                    .loadTrustMaterial(null, new RelaxedTrustStrategy(IGNORE_SSL_VALIDITY_DATES)).build();
            sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, sslProtocols, cipherSuites,
                    SSL_ALLOW_ALL ? SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
                            : SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        } catch (Exception ex) {
            throw new SSLInitializationException(ex.getMessage(), ex);
        }
    } else {
        sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                HttpsURLConnection.getDefaultSSLSocketFactory(), sslProtocols, cipherSuites,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }

    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslConnectionSocketFactory).build();

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
    if (PERSISTENT_POOL) {
        connManager.setDefaultMaxPerRoute(MAX_CONN_PER_ROUTE);
        connManager.setMaxTotal(MAX_CONN_TOTAL);
    } else {
        connManager.setMaxTotal(1);
    }

    boolean soKeepAlive = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_KEEPALIVE, false);
    int soLinger = getInteger(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_LINGER, -1);
    boolean soReuseAddress = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_REUSEADDRESS,
            false);
    boolean soTcpNoDelay = getBoolean(resolver, pid + ServiceConstants.PROPERTY_SOCKET_TCP_NODELAY, true);
    //        int soTimeout = getInteger( resolver, pid + ServiceConstants.PROPERTY_SOCKET_SO_TIMEOUT, 0 );
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(soKeepAlive) // default false
            .setSoLinger(soLinger) // default -1
            .setSoReuseAddress(soReuseAddress) // default false
            .setTcpNoDelay(soTcpNoDelay) // default true
            .setSoTimeout(0) // default 0, but set in org.apache.http.impl.conn.CPoolProxy.setSocketTimeout()
            // this value is not used
            .build();
    connManager.setDefaultSocketConfig(socketConfig);

    int bufferSize = getInteger(resolver, pid + ServiceConstants.PROPERTY_CONNECTION_BUFFER_SIZE, 8192);
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(bufferSize) // default 8192
            .setFragmentSizeHint(bufferSize) // default 'buffer size'
            .build();
    connManager.setDefaultConnectionConfig(connectionConfig);

    return connManager;
}

From source file:com.continuuity.loom.scheduler.callback.HttpPostClusterCallback.java

public void initialize(Configuration conf, ClusterStoreService clusterStoreService) {
    this.clusterStoreService = clusterStoreService;
    this.onStartUrl = conf.get(Constants.HttpCallback.START_URL);
    this.onSuccessUrl = conf.get(Constants.HttpCallback.SUCCESS_URL);
    this.onFailureUrl = conf.get(Constants.HttpCallback.FAILURE_URL);
    this.startTriggerActions = parseActionsString(
            conf.get(Constants.HttpCallback.START_TRIGGERS, Constants.HttpCallback.DEFAULT_START_TRIGGERS));
    this.successTriggerActions = parseActionsString(
            conf.get(Constants.HttpCallback.SUCCESS_TRIGGERS, Constants.HttpCallback.DEFAULT_SUCCESS_TRIGGERS));
    this.failureTriggerActions = parseActionsString(
            conf.get(Constants.HttpCallback.FAILURE_TRIGGERS, Constants.HttpCallback.DEFAULT_FAILURE_TRIGGERS));
    if (onStartUrl != null) {
        LOG.debug("before hook will be triggered on actions {}", Joiner.on(',').join(startTriggerActions));
    }/*  w  ww  .j  a v a 2s .c  o m*/
    if (onSuccessUrl != null) {
        LOG.debug("after hook will be triggered on actions {}", Joiner.on(',').join(successTriggerActions));
    }
    if (onFailureUrl != null) {
        LOG.debug("after hook will be triggered on actions {}", Joiner.on(',').join(failureTriggerActions));
    }

    int maxConnections = conf.getInt(Constants.HttpCallback.MAX_CONNECTIONS,
            Constants.HttpCallback.DEFAULT_MAX_CONNECTIONS);
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setDefaultMaxPerRoute(maxConnections);
    connectionManager.setMaxTotal(maxConnections);

    SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(
            conf.getInt(Constants.HttpCallback.SOCKET_TIMEOUT, Constants.HttpCallback.DEFAULT_SOCKET_TIMEOUT))
            .build();
    connectionManager.setDefaultSocketConfig(socketConfig);
    this.httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
}

From source file:com.hp.autonomy.frontend.find.idol.beanconfiguration.IdolConfiguration.java

@Bean
public HttpClient httpClient() {
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(HTTP_SOCKET_TIMEOUT).build();

    return HttpClientBuilder.create().setMaxConnPerRoute(HTTP_MAX_CONNECTIONS_PER_ROUTE)
            .setMaxConnTotal(HTTP_MAX_CONNECTIONS_TOTAL).setDefaultSocketConfig(socketConfig).build();
}