Search in sources :

Example 1 with H2ClientTlsStrategy

use of org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy in project httpcomponents-core by apache.

the class H2RequesterBootstrap method create.

public H2AsyncRequester create() {
    final ManagedConnPool<HttpHost, IOSession> connPool;
    switch(poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
        case LAX:
            connPool = new LaxConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
        case STRICT:
        default:
            connPool = new StrictConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, maxTotal > 0 ? maxTotal : 50, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
    }
    final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
    for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
    final TlsStrategy actualTlsStrategy = tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy();
    final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, new DefaultHttpResponseParserFactory(http1Config), DefaultHttpRequestWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, http1StreamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttpProtocolNegotiationStarter(http1StreamHandlerFactory, http2StreamHandlerFactory, versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, actualTlsStrategy, handshakeTimeout);
    return new H2AsyncRequester(versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, ioReactorConfig, ioEventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool, actualTlsStrategy, handshakeTimeout);
}
Also used : H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) ClientHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ClientHttpProtocolNegotiationStarter) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) DefaultAsyncPushConsumerFactory(org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory) DefaultHttpResponseParserFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory) ClientHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory) HttpHost(org.apache.hc.core5.http.HttpHost) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) IOSession(org.apache.hc.core5.reactor.IOSession) Supplier(org.apache.hc.core5.function.Supplier) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory)

Example 2 with H2ClientTlsStrategy

use of org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy in project httpcomponents-core by apache.

the class H2MultiplexingRequesterBootstrap method create.

public H2MultiplexingRequester create() {
    final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
    for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
    return new H2MultiplexingRequester(ioReactorConfig, (ioSession, attachment) -> new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, strictALPNHandshake), ioSessionDecorator, exceptionCallback, sessionListener, DefaultAddressResolver.INSTANCE, tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy());
}
Also used : RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) DefaultAsyncPushConsumerFactory(org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory) ClientH2PrefaceHandler(org.apache.hc.core5.http2.impl.nio.ClientH2PrefaceHandler) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) Supplier(org.apache.hc.core5.function.Supplier)

Example 3 with H2ClientTlsStrategy

use of org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy in project httpcomponents-core by apache.

the class H2TlsAlpnRequestExecutionExample method main.

public static void main(final String[] args) throws Exception {
    // Create and start requester
    final H2Config h2Config = H2Config.custom().setPushEnabled(false).build();
    final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap().setH2Config(h2Config).setTlsStrategy(new H2ClientTlsStrategy(SSLContexts.createSystemDefault(), (endpoint, sslEngine) -> {
        // ====
        return null;
    })).setStreamListener(new H2StreamListener() {

        @Override
        public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
            for (int i = 0; i < headers.size(); i++) {
                System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
            }
        }

        @Override
        public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
            for (int i = 0; i < headers.size(); i++) {
                System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
            }
        }

        @Override
        public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) {
        }

        @Override
        public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) {
        }

        @Override
        public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
        }

        @Override
        public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) {
        }
    }).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.out.println("HTTP requester shutting down");
        requester.close(CloseMode.GRACEFUL);
    }));
    requester.start();
    final HttpHost target = new HttpHost("https", "nghttp2.org", 443);
    final String[] requestUris = new String[] { "/httpbin/ip", "/httpbin/user-agent", "/httpbin/headers" };
    final CountDownLatch latch = new CountDownLatch(requestUris.length);
    for (final String requestUri : requestUris) {
        final Future<AsyncClientEndpoint> future = requester.connect(target, Timeout.ofSeconds(5));
        final AsyncClientEndpoint clientEndpoint = future.get();
        clientEndpoint.execute(AsyncRequestBuilder.get().setHttpHost(target).setPath(requestUri).build(), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {

            @Override
            public void completed(final Message<HttpResponse, String> message) {
                clientEndpoint.releaseAndReuse();
                final HttpResponse response = message.getHead();
                final String body = message.getBody();
                System.out.println(requestUri + "->" + response.getCode() + " " + response.getVersion());
                System.out.println(body);
                latch.countDown();
            }

            @Override
            public void failed(final Exception ex) {
                clientEndpoint.releaseAndDiscard();
                System.out.println(requestUri + "->" + ex);
                latch.countDown();
            }

            @Override
            public void cancelled() {
                clientEndpoint.releaseAndDiscard();
                System.out.println(requestUri + " cancelled");
                latch.countDown();
            }
        });
    }
    latch.await();
    System.out.println("Shutting down I/O reactor");
    requester.initiateShutdown();
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame) Message(org.apache.hc.core5.http.Message) SSLContexts(org.apache.hc.core5.ssl.SSLContexts) H2Config(org.apache.hc.core5.http2.config.H2Config) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) H2RequesterBootstrap(org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap) Header(org.apache.hc.core5.http.Header) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Timeout(org.apache.hc.core5.util.Timeout) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Future(java.util.concurrent.Future) HttpHost(org.apache.hc.core5.http.HttpHost) HttpConnection(org.apache.hc.core5.http.HttpConnection) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) CloseMode(org.apache.hc.core5.io.CloseMode) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) HttpResponse(org.apache.hc.core5.http.HttpResponse) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) Message(org.apache.hc.core5.http.Message) HttpConnection(org.apache.hc.core5.http.HttpConnection) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) HttpHost(org.apache.hc.core5.http.HttpHost) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) List(java.util.List) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) HttpResponse(org.apache.hc.core5.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) Header(org.apache.hc.core5.http.Header) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) H2Config(org.apache.hc.core5.http2.config.H2Config)

Aggregations

H2ClientTlsStrategy (org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy)3 Supplier (org.apache.hc.core5.function.Supplier)2 HttpHost (org.apache.hc.core5.http.HttpHost)2 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)2 ClientH2StreamMultiplexerFactory (org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory)2 DefaultAsyncPushConsumerFactory (org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory)2 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1 Header (org.apache.hc.core5.http.Header)1 HttpConnection (org.apache.hc.core5.http.HttpConnection)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 Message (org.apache.hc.core5.http.Message)1 HttpAsyncRequester (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)1 ClientHttp1StreamDuplexerFactory (org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory)1 DefaultHttpResponseParserFactory (org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory)1 AsyncClientEndpoint (org.apache.hc.core5.http.nio.AsyncClientEndpoint)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1