Search in sources :

Example 1 with IOSession

use of org.apache.hc.core5.reactor.IOSession in project httpcomponents-core by apache.

the class LoggingIOSession method upgrade.

@Override
public void upgrade(final IOEventHandler handler) {
    Args.notNull(handler, "Protocol handler");
    if (this.log.isDebugEnabled()) {
        this.log.debug("{} protocol upgrade: {}", this.session, handler.getClass());
    }
    this.session.upgrade(new IOEventHandler() {

        @Override
        public void connected(final IOSession protocolSession) throws IOException {
            handler.connected(protocolSession);
        }

        @Override
        public void inputReady(final IOSession protocolSession, final ByteBuffer src) throws IOException {
            if (src != null && wireLog.isDebugEnabled()) {
                final ByteBuffer b = src.duplicate();
                logData(b, "<< ");
            }
            handler.inputReady(protocolSession, src);
        }

        @Override
        public void outputReady(final IOSession protocolSession) throws IOException {
            handler.outputReady(protocolSession);
        }

        @Override
        public void timeout(final IOSession protocolSession, final Timeout timeout) throws IOException {
            handler.timeout(protocolSession, timeout);
        }

        @Override
        public void exception(final IOSession protocolSession, final Exception cause) {
            handler.exception(protocolSession, cause);
        }

        @Override
        public void disconnected(final IOSession protocolSession) {
            handler.disconnected(protocolSession);
        }
    });
}
Also used : IOEventHandler(org.apache.hc.core5.reactor.IOEventHandler) Timeout(org.apache.hc.core5.util.Timeout) ProtocolIOSession(org.apache.hc.core5.reactor.ProtocolIOSession) IOSession(org.apache.hc.core5.reactor.IOSession) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 2 with IOSession

use of org.apache.hc.core5.reactor.IOSession 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 3 with IOSession

use of org.apache.hc.core5.reactor.IOSession in project httpcomponents-core by apache.

the class ServerH2UpgradeHandler method upgrade.

@Override
public void upgrade(final ProtocolIOSession ioSession, final FutureCallback<ProtocolIOSession> callback) {
    final HttpConnectionEventHandler protocolNegotiator = new ServerH2PrefaceHandler(ioSession, http2StreamHandlerFactory, callback);
    ioSession.upgrade(protocolNegotiator);
    try {
        protocolNegotiator.connected(ioSession);
    } catch (final IOException ex) {
        protocolNegotiator.exception(ioSession, ex);
    }
}
Also used : HttpConnectionEventHandler(org.apache.hc.core5.http.impl.nio.HttpConnectionEventHandler) IOException(java.io.IOException)

Example 4 with IOSession

use of org.apache.hc.core5.reactor.IOSession in project httpcomponents-core by apache.

the class ServerHttp1UpgradeHandler method upgrade.

@Override
public void upgrade(final ProtocolIOSession ioSession, final FutureCallback<ProtocolIOSession> callback) {
    final TlsDetails tlsDetails = ioSession.getTlsDetails();
    final ServerHttp1IOEventHandler eventHandler = new ServerHttp1IOEventHandler(http1StreamHandlerFactory.create(tlsDetails != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id, ioSession));
    ioSession.upgrade(eventHandler);
    ioSession.upgrade(eventHandler);
    try {
        eventHandler.connected(ioSession);
        if (callback != null) {
            callback.completed(ioSession);
        }
    } catch (final IOException ex) {
        eventHandler.exception(ioSession, ex);
    }
}
Also used : TlsDetails(org.apache.hc.core5.reactor.ssl.TlsDetails) IOException(java.io.IOException) ServerHttp1IOEventHandler(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandler)

Example 5 with IOSession

use of org.apache.hc.core5.reactor.IOSession in project httpcomponents-core by apache.

the class H2MultiplexingRequester method execute.

private void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final CancellableDependency cancellableDependency, final Timeout timeout, final HttpContext context) {
    Args.notNull(exchangeHandler, "Exchange handler");
    Args.notNull(timeout, "Timeout");
    Args.notNull(context, "Context");
    try {
        exchangeHandler.produceRequest((request, entityDetails, httpContext) -> {
            final String scheme = request.getScheme();
            final URIAuthority authority = request.getAuthority();
            if (authority == null) {
                throw new ProtocolException("Request authority not specified");
            }
            final HttpHost target = new HttpHost(scheme, authority);
            connPool.getSession(target, timeout, new FutureCallback<IOSession>() {

                @Override
                public void completed(final IOSession ioSession) {
                    ioSession.enqueue(new RequestExecutionCommand(new AsyncClientExchangeHandler() {

                        @Override
                        public void releaseResources() {
                            exchangeHandler.releaseResources();
                        }

                        @Override
                        public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException {
                            channel.sendRequest(request, entityDetails, httpContext);
                        }

                        @Override
                        public int available() {
                            return exchangeHandler.available();
                        }

                        @Override
                        public void produce(final DataStreamChannel channel) throws IOException {
                            exchangeHandler.produce(channel);
                        }

                        @Override
                        public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeInformation(response, httpContext);
                        }

                        @Override
                        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeResponse(response, entityDetails, httpContext);
                        }

                        @Override
                        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
                            exchangeHandler.updateCapacity(capacityChannel);
                        }

                        @Override
                        public void consume(final ByteBuffer src) throws IOException {
                            exchangeHandler.consume(src);
                        }

                        @Override
                        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                            exchangeHandler.streamEnd(trailers);
                        }

                        @Override
                        public void cancel() {
                            exchangeHandler.cancel();
                        }

                        @Override
                        public void failed(final Exception cause) {
                            exchangeHandler.failed(cause);
                        }
                    }, pushHandlerFactory, cancellableDependency, context), Command.Priority.NORMAL);
                    if (!ioSession.isOpen()) {
                        exchangeHandler.failed(new ConnectionClosedException());
                    }
                }

                @Override
                public void failed(final Exception ex) {
                    exchangeHandler.failed(ex);
                }

                @Override
                public void cancelled() {
                    exchangeHandler.cancel();
                }
            });
        }, context);
    } catch (final IOException | HttpException ex) {
        exchangeHandler.failed(ex);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) URIAuthority(org.apache.hc.core5.net.URIAuthority) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) IOException(java.io.IOException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) HttpHost(org.apache.hc.core5.http.HttpHost) EntityDetails(org.apache.hc.core5.http.EntityDetails) IOSession(org.apache.hc.core5.reactor.IOSession) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Aggregations

IOException (java.io.IOException)11 SSLIOSession (org.apache.hc.core5.reactor.ssl.SSLIOSession)10 IOSession (org.apache.hc.core5.reactor.IOSession)9 HttpHost (org.apache.hc.core5.http.HttpHost)7 CapacityWindow (org.apache.hc.core5.http.impl.nio.AbstractHttp1StreamDuplexer.CapacityWindow)6 Test (org.junit.jupiter.api.Test)6 ByteBuffer (java.nio.ByteBuffer)5 HttpRequest (org.apache.hc.core5.http.HttpRequest)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 InetSocketAddress (java.net.InetSocketAddress)4 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)4 Header (org.apache.hc.core5.http.Header)4 HttpException (org.apache.hc.core5.http.HttpException)4 Message (org.apache.hc.core5.http.Message)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)4 ProtocolIOSession (org.apache.hc.core5.reactor.ProtocolIOSession)4 Timeout (org.apache.hc.core5.util.Timeout)4 InterruptedIOException (java.io.InterruptedIOException)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3