Example usage for org.apache.http.impl.client HttpClients custom

List of usage examples for org.apache.http.impl.client HttpClients custom

Introduction

In this page you can find the example usage for org.apache.http.impl.client HttpClients custom.

Prototype

public static HttpClientBuilder custom() 

Source Link

Document

Creates builder object for construction of custom CloseableHttpClient instances.

Usage

From source file:org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTUtil.java

/**
 * Return a http client instance/*from  w  w w.ja va  2  s  . co  m*/
 *
 * @param protocol- service endpoint protocol http/https
 * @return
 */
public static HttpClient getHttpClient(String protocol)
        throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpClient httpclient;
    if (HTTPS_PROTOCOL.equals(protocol)) {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } else {
        httpclient = HttpClients.createDefault();
    }
    return httpclient;
}

From source file:org.workin.http.httpclient.v4.factory.CloseableHttpClientFactoryBean.java

public CloseableHttpClientFactoryBean() {
    this.builder = HttpClients.custom();
}

From source file:com.yaauie.unfurl.UrlExpander.java

public UrlExpander(final HttpClientConnectionManager connectionManager) {
    this.httpClient = HttpClients.custom().setConnectionManager(connectionManager)
            .setRedirectStrategy(new DefaultRedirectStrategy()).build();
}

From source file:org.andstatus.app.net.http.MyHttpClientFactory.java

public static HttpClient getHttpClient(SslModeEnum sslMode) {
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", TlsSniSocketFactory.getInstance(sslMode)).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    // max.  3 connections in total
    connectionManager.setMaxTotal(3);/*from  ww w  .j a v a 2  s .co m*/
    // max.  2 connections per host
    connectionManager.setDefaultMaxPerRoute(2);

    // use request defaults from AndroidHttpClient
    RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
            .setConnectTimeout(MyPreferences.getConnectionTimeoutMs())
            .setSocketTimeout(2 * MyPreferences.getConnectionTimeoutMs()).setStaleConnectionCheckEnabled(false)
            .build();

    HttpClientBuilder builder = HttpClients.custom().useSystemProperties()
            .setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig)
            /* TODO maybe:  
            .setRetryHandler(DavHttpRequestRetryHandler.INSTANCE)
            .setRedirectStrategy(DavRedirectStrategy.INSTANCE)  
            */
            .disableRedirectHandling().setUserAgent(HttpConnection.USER_AGENT).disableCookieManagement();

    return builder.build();
}

From source file:org.wso2.carbon.device.mgt.iot.util.IoTUtil.java

/**
 * Return a http client instance/*  ww  w .j  a  v  a  2 s.com*/
 * @param protocol- service endpoint protocol http/https
 * @return
 */
public static HttpClient getHttpClient(int port, String protocol)
        throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpClient httpclient;
    if (HTTPS_PROTOCOL.equals(protocol)) {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } else {
        httpclient = HttpClients.createDefault();
    }
    return httpclient;
}

From source file:com.anrisoftware.simplerest.core.DefaultHttpClientProvider.java

DefaultHttpClientProvider() {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    this.httpclient = HttpClients.custom().setConnectionManager(cm).build();
}

From source file:photosharing.api.ExecutorUtil.java

/**
 * helper method that returns an HTTPClient executor with credentials
 * available./*from  w  ww  .jav a 2  s. co  m*/
 * 
 * Also enables the test case to connect to ANY SSL Certificate
 * valid/invalid
 * 
 * @return {Executor} or Null if there is an issue
 */
public static Executor getExecutor() {
    Executor executor = null;

    /*
     * if using one of the environments without a trusted CA chain or
     * you are using Fiddler, you want to set TRUST=TRUE in appconfig.properties
     */
    Configuration config = Configuration.getInstance(null);
    String sTrust = config.getValue(Configuration.TRUST);
    boolean trusted = Boolean.parseBoolean(sTrust);
    if (trusted) {
        try {
            HttpClientBuilder builder = HttpClients.custom();

            // Setup the SSL Context to Trust Any SSL Certificate
            SSLContextBuilder sslBuilder = new SSLContextBuilder();
            sslBuilder.loadTrustMaterial(null, new TrustStrategy() {
                /**
                 * override for fiddler proxy
                 */
                public boolean isTrusted(X509Certificate[] certs, String host) throws CertificateException {
                    return true;
                }
            });
            SSLContext sslContext = sslBuilder.build();
            builder.setHostnameVerifier(new AllowAllHostnameVerifier());
            builder.setSslcontext(sslContext);

            CloseableHttpClient httpClient = builder.build();
            executor = Executor.newInstance(httpClient);
        } catch (NoSuchAlgorithmException e) {
            logger.log(Level.SEVERE, "Issue with No Algorithm " + e.toString());
        } catch (KeyStoreException e) {
            logger.log(Level.SEVERE, "Issue with KeyStore " + e.toString());
        } catch (KeyManagementException e) {
            logger.log(Level.SEVERE, "Issue with KeyManagement  " + e.toString());
        }
    }

    return executor;
}

From source file:org.restcomm.connect.commons.common.http.CustomHttpClientBuilder.java

public static HttpClient build(MainConfigurationSet config, int timeout) {
    SslMode mode = config.getSslMode();//  ww  w .j  a va  2 s . com
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
            .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setCookieSpec(CookieSpecs.STANDARD)
            .build();
    if (mode == SslMode.strict) {
        return HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
    } else {
        return buildAllowallClient(requestConfig);
    }
}

From source file:org.cloudfoundry.identity.uaa.util.UaaHttpRequestUtils.java

protected static HttpClientBuilder getClientBuilder(boolean skipSslValidation) {
    HttpClientBuilder builder = HttpClients.custom().useSystemProperties()
            .setRedirectStrategy(new DefaultRedirectStrategy());
    if (skipSslValidation) {
        builder.setSslcontext(getNonValidatingSslContext());
    }/*ww w  .jav a 2s .  c o  m*/
    builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
    return builder;
}

From source file:org.openehealth.ipf.tutorials.ref.Client.java

public Client() {
    mgr = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setConnectionManager(mgr).build();
    contentType = ContentType.TEXT_PLAIN;
}