Example usage for org.apache.http.impl.client HttpClientBuilder build

List of usage examples for org.apache.http.impl.client HttpClientBuilder build

Introduction

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

Prototype

public CloseableHttpClient build() 

Source Link

Usage

From source file:ch.cyberduck.core.onedrive.OneDriveSession.java

@Override
protected OneDriveAPI connect(final HostKeyCallback key) throws BackgroundException {
    final HttpClientBuilder configuration = builder.build(this);
    configuration.addInterceptorLast(authorizationService);
    configuration.setServiceUnavailableRetryStrategy(retryHandler);
    final RequestExecutor executor = new OneDriveCommonsHttpRequestExecutor(configuration.build()) {
        @Override//from w  ww.  j a va  2 s. co m
        public void addAuthorizationHeader(final Set<RequestHeader> headers) {
            // Placeholder
            headers.add(new RequestHeader(HttpHeaders.AUTHORIZATION, "Bearer"));
        }
    };
    return new OneDriveAPI() {
        @Override
        public RequestExecutor getExecutor() {
            return executor;
        }

        @Override
        public boolean isBusinessConnection() {
            return false;
        }

        @Override
        public boolean isGraphConnection() {
            return StringUtils.equals("graph.microsoft.com", host.getHostname());
        }

        @Override
        public String getBaseURL() {
            return String.format("%s://%s%s", host.getProtocol().getScheme(), host.getHostname(),
                    host.getProtocol().getContext());
        }

        @Override
        public String getEmailURL() {
            return null;
        }
    };
}

From source file:org.sonatype.spice.zapper.client.hc4.Hc4ClientBuilder.java

public Hc4Client build() {
    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();

    final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    cm.setMaxTotal(200);/* w ww.j  av  a 2  s  . co  m*/
    cm.setDefaultMaxPerRoute(parameters.getMaximumTrackCount());

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(cm)
            .setUserAgent("Zapper/1.0-HC4");

    if (proxyServer != null) {
        httpClientBuilder.setProxy(proxyServer);
    }
    if (credentialsProvider != null) {
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return new Hc4Client(parameters, remoteUrl, httpClientBuilder.build(),
            preemptiveAuth ? credentialsProvider : null);
}

From source file:fr.treeptik.cloudunit.docker.JSONClient.java

public CloseableHttpClient build() throws IOException {

    if (withTLS) {
        org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom();
        HttpClientConnectionManager manager = getConnectionFactory(certsDirPath, 10);
        builder.setConnectionManager(manager);
        return builder.build();
    } else {/*from w  w  w.java 2s .  com*/
        return HttpClients.createDefault();
    }
}

From source file:org.eclipse.cft.server.core.internal.client.RestUtils.java

public static ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration,
        boolean trustSelfSignedCerts, boolean disableRedirectHandling) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();

    if (trustSelfSignedCerts) {
        httpClientBuilder.setSslcontext(buildSslContext());
        httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }/*from w  ww.j a  va 2 s  . c  o  m*/

    if (disableRedirectHandling) {
        httpClientBuilder.disableRedirectHandling();
    }

    if (httpProxyConfiguration != null) {
        HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(),
                httpProxyConfiguration.getProxyPort());
        httpClientBuilder.setProxy(proxy);

        if (httpProxyConfiguration.isAuthRequired()) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                    new AuthScope(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort()),
                    new UsernamePasswordCredentials(httpProxyConfiguration.getUsername(),
                            httpProxyConfiguration.getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }

    HttpClient httpClient = httpClientBuilder.build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClient);

    return requestFactory;
}

From source file:org.elasticsearch.xpack.security.authc.saml.SamlRealm.java

private static Tuple<AbstractReloadingMetadataResolver, Supplier<EntityDescriptor>> parseHttpMetadata(
        String metadataUrl, RealmConfig config, SSLService sslService)
        throws ResolverException, ComponentInitializationException, PrivilegedActionException {
    final String entityId = require(config, IDP_ENTITY_ID);

    HttpClientBuilder builder = HttpClientBuilder.create();
    // ssl setup/*  w w w .jav  a 2  s . c  o  m*/
    Settings sslSettings = config.settings().getByPrefix(SamlRealmSettings.SSL_PREFIX);
    boolean isHostnameVerificationEnabled = sslService.getVerificationMode(sslSettings, Settings.EMPTY)
            .isHostnameVerificationEnabled();
    HostnameVerifier verifier = isHostnameVerificationEnabled ? new DefaultHostnameVerifier()
            : NoopHostnameVerifier.INSTANCE;
    SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(
            sslService.sslSocketFactory(sslSettings), verifier);
    builder.setSSLSocketFactory(factory);

    HTTPMetadataResolver resolver = new PrivilegedHTTPMetadataResolver(builder.build(), metadataUrl);
    TimeValue refresh = IDP_METADATA_HTTP_REFRESH.get(config.settings());
    resolver.setMinRefreshDelay(refresh.millis());
    resolver.setMaxRefreshDelay(refresh.millis());
    initialiseResolver(resolver, config);

    return new Tuple<>(resolver, () -> {
        // for some reason the resolver supports its own trust engine and custom socket factories.
        // we do not use these as we'd rather rely on the JDK versions for TLS security!
        SpecialPermission.check();
        try {
            return AccessController.doPrivileged(
                    (PrivilegedExceptionAction<EntityDescriptor>) () -> resolveEntityDescriptor(resolver,
                            entityId, metadataUrl));
        } catch (PrivilegedActionException e) {
            throw ExceptionsHelper.convertToRuntime((Exception) ExceptionsHelper.unwrapCause(e));
        }
    });
}

From source file:org.jboss.pnc.client.ApacheHttpClient43EngineWithRetry.java

@Override
protected HttpClient createDefaultHttpClient() {
    logger.info("Bootstrapping http engine with request retry handler...");
    final HttpClientBuilder builder = HttpClientBuilder.create();
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    if (defaultProxy != null) {
        requestBuilder.setProxy(defaultProxy);
    }//from w  w w  .j  a v a2 s .  com
    builder.disableContentCompression();
    builder.setDefaultRequestConfig(requestBuilder.build());

    HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
    builder.setRetryHandler(retryHandler);
    return builder.build();
}

From source file:org.metaeffekt.dcc.agent.DccAgentTest.java

private HttpClient newHttpClient() throws GeneralSecurityException, IOException {
    final char[] password = "changeit".toCharArray();

    final KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(DccAgentTest.class.getResourceAsStream("/client.keystore"), password);

    final KeyStore trustStore = KeyStore.getInstance("JKS");
    trustStore.load(DccAgentTest.class.getResourceAsStream("/client.truststore"), password);

    final SSLContextBuilder sslContextBuilder = SSLContexts.custom();
    sslContextBuilder.loadKeyMaterial(keyStore, password);
    sslContextBuilder.loadTrustMaterial(trustStore);

    final HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setSslcontext(sslContextBuilder.build());
    builder.setHostnameVerifier(new AllowAllHostnameVerifier());

    final HttpClient client = builder.build();
    return client;
}

From source file:edu.harvard.hms.dbmi.bd2k.irct.ri.exac.EXACResourceImplementation.java

private HttpClient createClient(SecureSession session) {
    HttpClientBuilder returns = HttpClientBuilder.create();
    return returns.build();
}

From source file:org.duracloud.common.web.RestHttpHelper.java

private CloseableHttpClient buildClient(HttpClientBuilder builder, Method method) {
    if (method.equals(Method.HEAD)) {
        builder.disableContentCompression();
    }/*from w w  w .  ja  v  a  2 s. c  o  m*/

    return builder.build();
}

From source file:com.hp.octane.integrations.services.rest.OctaneRestClientImpl.java

OctaneRestClientImpl(OctaneSDK.SDKServicesConfigurer configurer) {
    if (configurer == null) {
        throw new IllegalArgumentException("invalid configurer");
    }/*from w  w  w  .j a  v a2  s.  co  m*/

    this.configurer = configurer;

    SSLContext sslContext = SSLContexts.createSystemDefault();
    HostnameVerifier hostnameVerifier = new CustomHostnameVerifier();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);
    connectionManager.setDefaultMaxPerRoute(MAX_TOTAL_CONNECTIONS);

    HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connectionManager);

    httpClient = clientBuilder.build();
}