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:com.pingidentity.adapters.idp.mobileid.restservice.MssRequestHandlerRest.java

private MssRequestHandlerRest(Builder builder) {
    TlsConnection connection = new TlsConnection();
    sslContext = connection.createClientAuthenticatedConnection(builder.clientKey, builder.clientKeyPwd,
            builder.serverCertificate);/* w  w  w  .  j  a  v  a2 s . co m*/
    mssServiceUrl = builder.serverUrl;
    httpClient = HttpClients.custom().setSslcontext(sslContext).build();
}

From source file:fi.csc.kapaVirtaAS.VirtaClient.java

public VirtaClient(ASConfiguration conf) {
    cm = new PoolingHttpClientConnectionManager();
    Integer poolSize = new Integer(conf.getAdapterServiceConnectionPoolSize());
    cm.setMaxTotal(poolSize != null ? poolSize.intValue() : 10);
    client = HttpClients.custom().setConnectionManager(cm).build();
}

From source file:org.keycloak.adapters.springsecurity.client.KeycloakClientRequestFactory.java

public KeycloakClientRequestFactory() {
    super(HttpClients.custom().disableCookieManagement().build());
}

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);
    }//  w  ww .j  ava 2s  . c om

    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:com.jeecms.common.web.ClientCustomSSL.java

public static String getInSsl(String url, File pkcFile, String storeId, String params, String contentType)
        throws Exception {
    String text = "";
    // ???PKCS12// w  w w . j  av  a2 s .  c om
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    // ?PKCS12?
    FileInputStream instream = new FileInputStream(pkcFile);
    try {
        // PKCS12?(ID)
        keyStore.load(instream, storeId.toCharArray());
    } finally {
        instream.close();
    }

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, storeId.toCharArray()).build();
    // Allow TLSv1 protocol only
    // TLS 
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    // httpclientSSLSocketFactory
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {
        HttpPost post = new HttpPost(url);
        StringEntity s = new StringEntity(params, "utf-8");
        if (StringUtils.isBlank(contentType)) {
            s.setContentType("application/xml");
        }
        s.setContentType(contentType);
        post.setEntity(s);
        HttpResponse res = httpclient.execute(post);
        HttpEntity entity = res.getEntity();
        text = EntityUtils.toString(entity, "utf-8");
    } finally {
        httpclient.close();
    }
    return text;
}

From source file:estacionamento.util.HTTPUtil.java

/**
 *
 * @return/*from w w w . j av a 2  s  . c om*/
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws CertificateException {

        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };

    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, trustAllCerts, new SecureRandom());

    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

From source file:dk.dma.nogoservice.service.RemoteWeatherService.java

@Autowired
public RemoteWeatherService(PoolingHttpClientConnectionManager connectionManager) {
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
    template = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
    template.setErrorHandler(new RemoteErrorHandler());
}

From source file:eu.diacron.crawlservice.app.Util.java

public static String getCrawlid(URL urltoCrawl) {
    String crawlid = "";
    System.out.println("start crawling page");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*ww  w  .jav  a  2 s.co  m*/
        //HttpPost httppost = new HttpPost("http://diachron.hanzoarchives.com/crawl");
        HttpPost httppost = new HttpPost(Configuration.REMOTE_CRAWLER_URL_CRAWL_INIT);

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("name", UUID.randomUUID().toString()));
        urlParameters.add(new BasicNameValuePair("scope", "page"));
        urlParameters.add(new BasicNameValuePair("seed", urltoCrawl.toString()));

        httppost.setEntity(new UrlEncodedFormEntity(urlParameters));

        System.out.println("Executing request " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                crawlid = inputLine;
            }
            in.close();
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return crawlid;
}

From source file:com.nieyue.weixin.ssl.ClientCustomSSL.java

/**
 * ?/*from www.  j  av  a 2s .co  m*/
 * @return
 * @throws Exception
 */
public static CloseableHttpClient getCloseableHttpClient() throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(
            new File(ClientCustomSSL.class.getResource("").getPath() + "apiclient_cert.p12"));
    //?
    //FileInputStream instream = new FileInputStream("src/com/nieyue/weixin/ssl/apiclient_cert.p12");
    try {
        keyStore.load(instream, ThirdParty.GetValueByKey(ThirdParty.WEIXIN_YAYAO_MCH_ID).toCharArray());
    } finally {
        instream.close();
    }
    // Trust own CA and all self-signed certs
    @SuppressWarnings("deprecation")
    SSLContext sslcontext = SSLContexts.custom()
            .loadKeyMaterial(keyStore, ThirdParty.GetValueByKey(ThirdParty.WEIXIN_YAYAO_MCH_ID).toCharArray())
            .build();
    // Allow TLSv1 protocol only
    @SuppressWarnings("deprecation")
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    return httpclient;
}

From source file:com.github.yongchristophertang.engine.web.WebTemplate.java

/**
 * Access via {@link WebTemplateBuilder#build}
 */
WebTemplate(RequestConfig config) {
    httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
}