Example usage for org.apache.http.config RegistryBuilder register

List of usage examples for org.apache.http.config RegistryBuilder register

Introduction

In this page you can find the example usage for org.apache.http.config RegistryBuilder register.

Prototype

public RegistryBuilder<I> register(String str, I i) 

Source Link

Usage

From source file:org.ops4j.pax.web.itest.base.client.HttpComponentsWrapper.java

private CloseableHttpClient createHttpClient() throws KeyStoreException, IOException, NoSuchAlgorithmException,
        CertificateException, KeyManagementException {
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    SSLConnectionSocketFactory sslsf = null;
    try {// w  ww  .j  a v a2 s  .  c  o  m
        FileInputStream instream = new FileInputStream(new File(keyStore));
        try {
            trustStore.load(instream, "password".toCharArray());
        } finally {
            // CHECKSTYLE:OFF
            try {
                instream.close();
            } catch (Exception ignore) {
            }
            // CHECKSTYLE:ON
        }

        SSLContext sslContext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore).build();
        sslsf = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    } catch (FileNotFoundException e) {
        LOG.error("Error preparing SSL for testing. Https will not be available.", e);
    }

    PlainConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();

    RegistryBuilder<ConnectionSocketFactory> rb = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", plainsf);
    if (sslsf != null) {
        rb.register("https", sslsf);
    }

    Registry<ConnectionSocketFactory> registry = rb.build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

    return HttpClients.custom().setConnectionManager(cm).build();

}

From source file:ee.ria.xroad.proxy.clientproxy.ClientProxy.java

private HttpClientConnectionManager getClientConnectionManager() throws Exception {
    RegistryBuilder<ConnectionSocketFactory> sfr = RegistryBuilder.create();

    sfr.register("http", PlainConnectionSocketFactory.INSTANCE);

    if (SystemProperties.isSslEnabled()) {
        sfr.register("https", createSSLSocketFactory());
    }// ww  w.j  a va2  s. co  m

    SocketConfig.Builder sockBuilder = SocketConfig.custom().setTcpNoDelay(true);
    sockBuilder.setSoLinger(SystemProperties.getClientProxyHttpClientSoLinger());
    sockBuilder.setSoTimeout(SystemProperties.getClientProxyHttpClientTimeout());
    SocketConfig socketConfig = sockBuilder.build();

    PoolingHttpClientConnectionManager poolingManager = new PoolingHttpClientConnectionManager(sfr.build());
    poolingManager.setMaxTotal(SystemProperties.getClientProxyPoolTotalMaxConnections());
    poolingManager.setDefaultMaxPerRoute(SystemProperties.getClientProxyPoolDefaultMaxConnectionsPerRoute());
    poolingManager.setDefaultSocketConfig(socketConfig);
    poolingManager.setValidateAfterInactivity(
            SystemProperties.getClientProxyValidatePoolConnectionsAfterInactivityMs());

    return poolingManager;
}

From source file:com.arvato.thoroughly.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);/*from   w w w.java  2 s  .  co 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());

    reInitMessageConverter(restTemplate);

    return restTemplate;
}

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);// w ww .  j ava  2 s  . co m
    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:crawler.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false)
            .setCookieSpec(config.getCookiePolicy()).setRedirectsEnabled(false)
            .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout())
            .build();/*from ww w.  ja  v  a 2  s. c  o m*/

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    if (config.isIncludeHttpsPages()) {
        try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
              // By always trusting the ssl certificate
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(final X509Certificate[] chain, String authType) {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SniSSLConnectionSocketFactory(sslContext,
                    NoopHostnameVerifier.INSTANCE);
            connRegistryBuilder.register("https", sslsf);
        } catch (Exception e) {
            logger.warn("Exception thrown while trying to register https");
            logger.debug("Stacktrace", e);
        }
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new SniPoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent(config.getUserAgentString());
    clientBuilder.setDefaultHeaders(config.getDefaultHeaders());

    if (config.getProxyHost() != null) {
        if (config.getProxyUsername() != null) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        clientBuilder.setProxy(proxy);
        logger.debug("Working through Proxy: {}", proxy.getHostName());
    }

    httpClient = clientBuilder.build();
    if ((config.getAuthInfos() != null) && !config.getAuthInfos().isEmpty()) {
        doAuthetication(config.getAuthInfos());
    }

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

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);//w w  w.  j  a v  a2s  .c  o  m
    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:de.comlineag.snc.webcrawler.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false)
            .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).setRedirectsEnabled(false)
            .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout())
            .build();//from w ww .  ja  v a  2  s. c o m

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    if (config.isIncludeHttpsPages()) {
        try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
            // By always trusting the ssl certificate
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(final X509Certificate[] chain, String authType) {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            connRegistryBuilder.register("https", sslsf);
        } catch (Exception e) {
            logger.debug("Exception thrown while trying to register https:", e);
        }
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new PoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent(config.getUserAgentString());
    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        clientBuilder.setProxy(proxy);
    }
    clientBuilder.addInterceptorLast(new HttpResponseInterceptor() {
        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });

    httpClient = clientBuilder.build();

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:crawler.java.edu.uci.ics.crawler4j.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(false)
            .setCookieSpec(CookieSpecs.DEFAULT).setRedirectsEnabled(false)
            .setSocketTimeout(config.getSocketTimeout()).setConnectTimeout(config.getConnectionTimeout())
            .build();//from  w  w w  .j  a  va 2s .c om

    RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
    connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
    if (config.isIncludeHttpsPages()) {
        try { // Fixing: https://code.google.com/p/crawler4j/issues/detail?id=174
            // By always trusting the ssl certificate
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(final X509Certificate[] chain, String authType) {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            connRegistryBuilder.register("https", sslsf);
        } catch (Exception e) {
            logger.warn("Exception thrown while trying to register https");
            logger.debug("Stacktrace", e);
        }
    }

    Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
    connectionManager = new PoolingHttpClientConnectionManager(connRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());

    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    clientBuilder.setDefaultRequestConfig(requestConfig);
    clientBuilder.setConnectionManager(connectionManager);
    clientBuilder.setUserAgent(config.getUserAgentString());
    clientBuilder.setDefaultHeaders(config.getDefaultHeaders());

    if (config.getProxyHost() != null) {
        if (config.getProxyUsername() != null) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
            clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        clientBuilder.setProxy(proxy);
        logger.debug("Working through Proxy: {}", proxy.getHostName());
    }

    httpClient = clientBuilder.build();
    if ((config.getAuthInfos() != null) && !config.getAuthInfos().isEmpty()) {
        doAuthetication(config.getAuthInfos());
    }

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();
}

From source file:io.cloudslang.content.httpclient.build.auth.AuthSchemeProviderLookupBuilder.java

public Lookup<AuthSchemeProvider> buildAuthSchemeProviderLookup() {
    RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.create();

    for (String type : authTypes) {
        switch (type.trim()) {
        case "NTLM":
            registryBuilder.register(AuthSchemes.NTLM, new AuthSchemeProvider() {
                @Override//  w  w  w.  jav  a  2s.c om
                public AuthScheme create(HttpContext httpContext) {
                    return new NTLMScheme(new JCIFSEngine());
                }
            });
            break;
        case "BASIC":
            registryBuilder.register(AuthSchemes.BASIC,
                    new BasicSchemeFactory(Charset.forName(Utils.DEFAULT_CHARACTER_SET)));
            String value = username + ":" + password;
            byte[] encodedValue = Base64.encodeBase64(value.getBytes(StandardCharsets.UTF_8));
            headers.add(new BasicHeader("Authorization", "Basic " + new String(encodedValue)));
            break;
        case "DIGEST":
            registryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
            break;
        case "KERBEROS":
            if (kerberosConfigFile != null) {
                System.setProperty("java.security.krb5.conf", kerberosConfigFile);
            } else {
                File krb5Config;
                String domain = host.replaceAll(".*\\.(?=.*\\.)", "");
                try {
                    krb5Config = createKrb5Configuration(domain);
                } catch (IOException e) {
                    throw new RuntimeException("could not create the krb5 config file" + e.getMessage(), e);
                }
                System.setProperty("java.security.krb5.conf", krb5Config.toURI().toString());
            }

            if (kerberosLoginConfigFile != null) {
                System.setProperty("java.security.auth.login.config", kerberosLoginConfigFile);
            } else {
                File loginConfig;
                try {
                    loginConfig = createLoginConfig();
                } catch (IOException e) {
                    throw new RuntimeException(
                            "could not create the kerberos login config file" + e.getMessage(), e);
                }
                System.setProperty("java.security.auth.login.config", loginConfig.toURI().toString());
            }

            if (password != null) {
                System.setProperty(KrbHttpLoginModule.PAS, password);
            }
            if (username != null) {
                System.setProperty(KrbHttpLoginModule.USR, username);
            }

            System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

            boolean skipPort = Boolean.parseBoolean(skipPortAtKerberosDatabaseLookup);
            registryBuilder.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory(skipPort));
            registryBuilder.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(skipPort));
            break;
        case AuthTypes.ANONYMOUS:
            break;
        default:
            throw new IllegalStateException(
                    "Unsupported '" + HttpClientInputs.AUTH_TYPE + "'authentication scheme: " + type);
        }
    }
    return registryBuilder.build();
}

From source file:org.openscore.content.httpclient.build.auth.AuthSchemeProviderLookupBuilder.java

public Lookup<AuthSchemeProvider> buildAuthSchemeProviderLookup() {
    RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.create();

    for (String type : authTypes) {
        switch (type.trim()) {
        case "NTLM":
            registryBuilder.register(AuthSchemes.NTLM, new AuthSchemeProvider() {
                @Override//from   ww  w. ja  v  a2 s  .  co m
                public AuthScheme create(HttpContext httpContext) {
                    return new NTLMScheme(new JCIFSEngine());
                }
            });
            break;
        case "BASIC":
            registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory(Charset.forName("UTF-8")));
            String value = username + ":" + password;
            byte[] encodedValue = Base64.encodeBase64(value.getBytes(StandardCharsets.UTF_8));
            headers.add(new BasicHeader("Authorization", "Basic " + new String(encodedValue)));
            break;
        case "DIGEST":
            registryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
            break;
        case "KERBEROS":
            if (getSettingsKey().equals(System.getProperty("oohttpclient.krb.last.settings"))) {
                break;
            }
            if (kerberosConfigFile != null) {
                System.setProperty("java.security.krb5.conf", kerberosConfigFile);
            } else {
                File krb5Config;
                String domain = host.replaceAll(".*\\.(?=.*\\.)", "");
                try {
                    krb5Config = createKrb5Configuration(domain);
                } catch (IOException e) {
                    throw new RuntimeException("could not create the krb5 config file" + e.getMessage(), e);
                }
                System.setProperty("java.security.krb5.conf", krb5Config.toURI().toString());
            }

            if (kerberosLoginConfigFile != null) {
                System.setProperty("java.security.auth.login.config", kerberosLoginConfigFile);
            } else {
                File loginConfig;
                try {
                    loginConfig = createLoginConfig();
                } catch (IOException e) {
                    throw new RuntimeException(
                            "could not create the kerberos login config file" + e.getMessage(), e);
                }
                System.setProperty("java.security.auth.login.config", loginConfig.toURI().toString());
            }

            //todo fix security issue
            if (password != null) {
                System.setProperty(KrbHttpLoginModule.PAS, password);
            }
            if (username != null) {
                System.setProperty(KrbHttpLoginModule.USR, username);
            }

            System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

            boolean skipPort = Boolean.parseBoolean(skipPortAtKerberosDatabaseLookup);
            registryBuilder.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory(skipPort));
            registryBuilder.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(skipPort));
            System.setProperty("oohttpclient.krb.last.settings", getSettingsKey());
            break;
        default:
            throw new IllegalStateException(
                    "Unsupported '" + HttpClientInputs.AUTH_TYPE + "'authentication scheme: " + type);
        }
    }
    return registryBuilder.build();
}