Search in sources :

Example 21 with SSLContextBuilder

use of org.apache.http.conn.ssl.SSLContextBuilder in project jetty-bootstrap by teknux-org.

the class AbstractJettyBootstrapTest method get.

protected SimpleResponse get(String url) throws IllegalStateException, IOException, JettyBootstrapException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
    SimpleResponse simpleResponse = new SimpleResponse();
    CloseableHttpClient httpClient;
    HttpGet httpGet;
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT).build();
    if (ssl) {
        SSLContextBuilder sSLContextBuilder = new SSLContextBuilder();
        sSLContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sSLConnectionSocketFactory = new SSLConnectionSocketFactory(sSLContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        httpClient = HttpClients.custom().setSSLSocketFactory(sSLConnectionSocketFactory).build();
        httpGet = new HttpGet("https://" + HOST + ":" + getPort() + url);
    } else {
        httpClient = HttpClients.createDefault();
        httpGet = new HttpGet("http://" + HOST + ":" + getPort() + url);
    }
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(httpGet);
        simpleResponse.setStatusCode(response.getStatusLine().getStatusCode());
        simpleResponse.setContent(IOUtils.toString(response.getEntity().getContent()));
    } finally {
        if (response != null) {
            response.close();
        }
        httpClient.close();
    }
    return simpleResponse;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

SSLContextBuilder (org.apache.http.conn.ssl.SSLContextBuilder)21 SSLContext (javax.net.ssl.SSLContext)11 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)11 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)8 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)8 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)8 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)8 KeyManagementException (java.security.KeyManagementException)7 KeyStoreException (java.security.KeyStoreException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 TrustStrategy (org.apache.http.conn.ssl.TrustStrategy)7 RequestConfig (org.apache.http.client.config.RequestConfig)6 IOException (java.io.IOException)5 X509Certificate (java.security.cert.X509Certificate)5 CertificateException (java.security.cert.CertificateException)4 X509HostnameVerifier (org.apache.http.conn.ssl.X509HostnameVerifier)4 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)4 URI (java.net.URI)3 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)3