Search in sources :

Example 1 with CloseableHttpAsyncClient

use of org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient in project feign by OpenFeign.

the class AsyncApacheHttp5Client method createStartedClient.

private static CloseableHttpAsyncClient createStartedClient() {
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
    client.start();
    return client;
}
Also used : CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)

Example 2 with CloseableHttpAsyncClient

use of org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient in project cxf by apache.

the class AsyncHTTPConduitFactory method restartReactor.

private void restartReactor() {
    CloseableHttpAsyncClient client2 = client;
    resetVars();
    shutdown(client2);
}
Also used : CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)

Example 3 with CloseableHttpAsyncClient

use of org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient in project sslcontext-kickstart by Hakky54.

the class SSLFactoryIT method executeHttpsRequestWithMutualAuthenticationForAsyncClient.

@Test
@Tag("it-with-badssl.com")
void executeHttpsRequestWithMutualAuthenticationForAsyncClient() throws IOException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    LogCaptor logCaptor = LogCaptor.forName("nl.altindag.ssl");
    SSLFactory sslFactory = SSLFactory.builder().withIdentityMaterial("keystore/badssl-identity.p12", "badssl.com".toCharArray()).withTrustMaterial("keystore/badssl-truststore.p12", "badssl.com".toCharArray()).withTrustMaterial(// Adding additional trust material forces usage of CompositeX509ExtendedTrustManager and verbose logging
    KeyStoreUtils.createKeyStore()).build();
    PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(Apache5SslUtils.toTlsStrategy(sslFactory)).build();
    CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(connectionManager).build();
    httpAsyncClient.start();
    SimpleHttpResponse response = httpAsyncClient.execute(new BasicRequestProducer(Method.GET, new URI("https://client.badssl.com/")), SimpleResponseConsumer.create(), null, null, null).get(10, TimeUnit.SECONDS);
    logCaptor.close();
    int statusCode = response.getCode();
    if (statusCode == 400) {
        fail("Certificate may have expired and needs to be updated");
    } else {
        assertThat(statusCode).isEqualTo(200);
        assertThat(logCaptor.getLogs()).contains("Received the following server certificate: [CN=*.badssl.com, O=Lucas Garron Torres, L=Walnut Creek, ST=California, C=US]");
    }
}
Also used : BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) LogCaptor(nl.altindag.log.LogCaptor) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager) URI(java.net.URI) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 4 with CloseableHttpAsyncClient

use of org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient in project mercury by yellow013.

the class AsyncClientConnectionEviction method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).evictExpiredConnections().evictIdleConnections(TimeValue.ofSeconds(10)).build();
    client.start();
    final SimpleHttpRequest request = SimpleRequestBuilder.get().setHttpHost(new HttpHost("httpbin.org")).setPath("/").build();
    System.out.println("Executing request " + request);
    final Future<SimpleHttpResponse> future1 = client.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(final SimpleHttpResponse response) {
            System.out.println(request + "->" + new StatusLine(response));
            System.out.println(response.getBody());
        }

        @Override
        public void failed(final Exception ex) {
            System.out.println(request + "->" + ex);
        }

        @Override
        public void cancelled() {
            System.out.println(request + " cancelled");
        }
    });
    future1.get();
    Thread.sleep(TimeUnit.SECONDS.toMillis(30));
    // Previous connection should get evicted from the pool by now
    final Future<SimpleHttpResponse> future2 = client.execute(request, new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(final SimpleHttpResponse response) {
            System.out.println(request + "->" + new StatusLine(response));
            System.out.println(response.getBody());
        }

        @Override
        public void failed(final Exception ex) {
            System.out.println(request + "->" + ex);
        }

        @Override
        public void cancelled() {
            System.out.println(request + " cancelled");
        }
    });
    future2.get();
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) HttpHost(org.apache.hc.core5.http.HttpHost) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse)

Example 5 with CloseableHttpAsyncClient

use of org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient in project mercury by yellow013.

the class AsyncClientCustomSSL method main.

public static void main(final String[] args) throws Exception {
    // Trust standard CA and those trusted by our custom strategy
    final SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {

        @Override
        public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
            final X509Certificate cert = chain[0];
            return "CN=httpbin.org".equalsIgnoreCase(cert.getSubjectDN().getName());
        }
    }).build();
    final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().setSslContext(sslcontext).build();
    final PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(tlsStrategy).build();
    try (final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(cm).build()) {
        client.start();
        final HttpHost target = new HttpHost("https", "httpbin.org");
        final HttpClientContext clientContext = HttpClientContext.create();
        final SimpleHttpRequest request = SimpleRequestBuilder.get().setHttpHost(target).setPath("/").build();
        System.out.println("Executing request " + request);
        final Future<SimpleHttpResponse> future = client.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), clientContext, new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void completed(final SimpleHttpResponse response) {
                System.out.println(request + "->" + new StatusLine(response));
                final SSLSession sslSession = clientContext.getSSLSession();
                if (sslSession != null) {
                    System.out.println("SSL protocol " + sslSession.getProtocol());
                    System.out.println("SSL cipher suite " + sslSession.getCipherSuite());
                }
                System.out.println(response.getBody());
            }

            @Override
            public void failed(final Exception ex) {
                System.out.println(request + "->" + ex);
            }

            @Override
            public void cancelled() {
                System.out.println(request + " cancelled");
            }
        });
        future.get();
        System.out.println("Shutting down");
        client.close(CloseMode.GRACEFUL);
    }
}
Also used : TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) TrustStrategy(org.apache.hc.core5.ssl.TrustStrategy) SSLSession(javax.net.ssl.SSLSession) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) CertificateException(java.security.cert.CertificateException) StatusLine(org.apache.hc.core5.http.message.StatusLine) HttpHost(org.apache.hc.core5.http.HttpHost) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)

Aggregations

CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)16 StatusLine (org.apache.hc.core5.http.message.StatusLine)9 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)8 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)7 IOException (java.io.IOException)6 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)5 HttpException (org.apache.hc.core5.http.HttpException)4 HttpResponse (org.apache.hc.core5.http.HttpResponse)4 HttpRequest (org.apache.hc.core5.http.HttpRequest)3 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)3 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)3 ByteBuffer (java.nio.ByteBuffer)2 CharBuffer (java.nio.CharBuffer)2 X509Certificate (java.security.cert.X509Certificate)2 SSLContext (javax.net.ssl.SSLContext)2 SSLSession (javax.net.ssl.SSLSession)2 AsyncExecCallback (org.apache.hc.client5.http.async.AsyncExecCallback)2 AsyncExecChain (org.apache.hc.client5.http.async.AsyncExecChain)2