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:ch.sourcepond.maven.plugin.jenkins.it.utils.HttpsServerStartupBarrier.java

@Override
protected CloseableHttpClient createClient() throws KeyManagementException, NoSuchAlgorithmException,
        KeyStoreException, CertificateException, IOException, URISyntaxException {
    final URL url = getClass().getResource(KEYSTORE_NAME);
    // Trust own CA and all self-signed certs
    final SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File(url.toURI()),
            TEST_PASSWORD.toCharArray(), new TrustSelfSignedStrategy()).build();
    // Allow TLSv1 protocol only
    final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    final CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

    return httpclient;
}

From source file:org.elasticsearch.http.netty.NettyHttpCompressionIT.java

public void testCompressesResponseIfRequested() throws Exception {
    ensureGreen();/*  w  w w  .j a va 2  s  .  c om*/

    // we need to intercept early, otherwise internal logic in HttpClient will just remove the header and we cannot verify it
    ContentEncodingHeaderExtractor headerExtractor = new ContentEncodingHeaderExtractor();
    CloseableHttpClient internalClient = HttpClients.custom().addInterceptorFirst(headerExtractor).build();

    HttpResponse response = httpClient(internalClient).path("/")
            .addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING).execute();
    assertEquals(200, response.getStatusCode());
    assertTrue(headerExtractor.hasContentEncodingHeader());
    assertEquals(GZIP_ENCODING, headerExtractor.getContentEncodingHeader().getValue());
}

From source file:io.logspace.agent.hq.HqClient.java

public HqClient(String baseUrl, String agentControllerId, String spaceToken) {
    super();//from   w ww  .j a  v a  2 s .  c o  m

    this.baseUrl = baseUrl;
    if (this.baseUrl == null || this.baseUrl.trim().length() == 0) {
        throw new AgentControllerInitializationException("The base URL must not be empty!");
    }

    this.agentControllerId = agentControllerId;
    this.spaceToken = spaceToken;

    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT)
            .setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT).build();

    Set<? extends Header> defaultHeaders = Collections
            .singleton(new BasicHeader("Accept", APPLICATION_JSON.getMimeType()));

    this.httpClient = HttpClients.custom().disableAutomaticRetries().setDefaultRequestConfig(requestConfig)
            .setDefaultHeaders(defaultHeaders).build();
}

From source file:com.ibm.idreambooks.DreamBooks.java

public BookReviewList getReviewList() {
    BookReviewList bookReviewList = new BookReviewList();

    try {/* w  w  w .j a va 2s  .  c o m*/
        if (bookName != null && url != null && apiKey != null) {
            RequestConfig config = RequestConfig.custom().setSocketTimeout(10 * 1000)
                    .setConnectTimeout(10 * 1000).build();
            CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).build();
            URIBuilder builder = new URIBuilder();
            builder.setScheme("http").setHost(url).setPath("/api/books/reviews.json")
                    .setParameter("key", apiKey).setParameter("q", bookName);
            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);

            httpGet.setHeader("Content-Type", "text/plain");
            HttpResponse httpResponse = httpclient.execute(httpGet);

            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));

                // Read all the books from the best seller list
                ObjectMapper mapper = new ObjectMapper();
                bookReviewList = mapper.readValue(rd, BookReviewList.class);
                logger.debug("iDreamBooks reviews {}", bookReviewList.toString());
            } else {
                logger.error("could not get reviews from iDreamBooks http code {}",
                        httpResponse.getStatusLine().getStatusCode());
            }
        }

    } catch (Exception e) {
        logger.error("could not get reviews from iDreamBooks {}", e.getMessage());
    }

    return bookReviewList;
}

From source file:com.linkedin.multitenant.db.RocksdbDatabase.java

public RocksdbDatabase() {
    RequestConfig rq = RequestConfig.custom().setStaleConnectionCheckEnabled(false).build();

    m_client = HttpClients.custom().setDefaultRequestConfig(rq).setMaxConnTotal(2).build();
    m_handler = new MyResponseHandler();
}

From source file:org.elasticsearch.plugin.SitePluginTests.java

public HttpRequestBuilder httpClient() {
    RequestConfig.Builder builder = RequestConfig.custom().setRedirectsEnabled(false);
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(builder.build()).build();
    return new HttpRequestBuilder(httpClient)
            .httpTransport(internalCluster().getDataNodeInstance(HttpServerTransport.class));
}

From source file:cycronix.ctlib.CThttp.java

private void enableSelfSigned() {
    try {//from   w w  w. j  a v a  2 s  .  c om
        httpclient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(
                        SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()))
                .build();
    } catch (Exception e) {
        System.err.println("Exception on TrustSelfSigned");
    }
}

From source file:io.crate.integrationtests.BlobSslEnabledITest.java

@Test
public void testRedirectContainsHttpsScheme() throws Exception {
    String blobUri = blobUri(uploadSmallBlob());
    CloseableHttpClient client = HttpClients.custom().disableRedirectHandling().build();
    List<String> redirectLocations = getRedirectLocations(client, blobUri, address);
    if (redirectLocations.isEmpty()) {
        redirectLocations = getRedirectLocations(client, blobUri, address2);
    }/* ww  w. j a va  2 s.  c  om*/
    String uri = redirectLocations.iterator().next();
    assertThat(uri, startsWith("https"));
}

From source file:com.kappaware.logtrawler.output.httpclient.HttpClient.java

public HttpClient(SslHandling sslHandling, On404 on404) {
    this.on404 = on404;
    if (sslHandling == SslHandling.NONE) {
        httpclient = HttpClients.custom().build();
    } else {/*from   w ww  .  j a  v a  2 s  .  c om*/
        try {
            SSLConnectionSocketFactory sslConnectionSocketFactory;
            SSLContext sslContext = SSLContext.getInstance("TLS");
            if (sslHandling == SslHandling.STRICT_SSL) {
                sslContext.init(null, null, null);
                sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                        new BrowserCompatHostnameVerifier());
            } else {
                sslContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, null);
                sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                        new AllowAllHostnameVerifier());
            }
            httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
        } catch (Exception e) {
            throw new RuntimeException("Exception in SSL configuration", e);
        }
    }
}

From source file:it.polimi.tower4clouds.common.net.DefaultRestClient.java

public DefaultRestClient() {
    connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(maxThreads);
    connManager.setMaxTotal(maxThreads);
    client = HttpClients.custom().setConnectionManager(connManager).disableContentCompression().build();
}