Search in sources :

Example 1 with RequestExecutionCommand

use of org.apache.hc.core5.http.nio.command.RequestExecutionCommand in project httpcomponents-core by apache.

the class ClientSessionEndpoint method execute.

public void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final HttpContext context) {
    Asserts.check(!closed.get(), "Connection is already closed");
    final Command executionCommand = new RequestExecutionCommand(exchangeHandler, pushHandlerFactory, null, context);
    ioSession.enqueue(executionCommand, Command.Priority.NORMAL);
    if (!ioSession.isOpen()) {
        exchangeHandler.failed(new ConnectionClosedException());
    }
}
Also used : ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) Command(org.apache.hc.core5.reactor.Command) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand)

Example 2 with RequestExecutionCommand

use of org.apache.hc.core5.http.nio.command.RequestExecutionCommand in project httpcomponents-core by apache.

the class ClientH2StreamMultiplexer method createLocallyInitiatedStream.

@Override
H2StreamHandler createLocallyInitiatedStream(final ExecutableCommand command, final H2StreamChannel channel, final HttpProcessor httpProcessor, final BasicHttpConnectionMetrics connMetrics) throws IOException {
    if (command instanceof RequestExecutionCommand) {
        final RequestExecutionCommand executionCommand = (RequestExecutionCommand) command;
        final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
        final HandlerFactory<AsyncPushConsumer> pushHandlerFactory = executionCommand.getPushHandlerFactory();
        final HttpCoreContext context = HttpCoreContext.adapt(executionCommand.getContext());
        context.setAttribute(HttpCoreContext.SSL_SESSION, getSSLSession());
        context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, getEndpointDetails());
        return new ClientH2StreamHandler(channel, httpProcessor, connMetrics, exchangeHandler, pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory, context);
    }
    throw new H2ConnectionException(H2Error.INTERNAL_ERROR, "Unexpected executable command");
}
Also used : AsyncPushConsumer(org.apache.hc.core5.http.nio.AsyncPushConsumer) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) H2ConnectionException(org.apache.hc.core5.http2.H2ConnectionException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand)

Example 3 with RequestExecutionCommand

use of org.apache.hc.core5.http.nio.command.RequestExecutionCommand 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)

Example 4 with RequestExecutionCommand

use of org.apache.hc.core5.http.nio.command.RequestExecutionCommand in project httpcomponents-core by apache.

the class ClientHttp1StreamDuplexer method execute.

@Override
void execute(final RequestExecutionCommand executionCommand) throws HttpException, IOException {
    final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler();
    final HttpCoreContext context = HttpCoreContext.adapt(executionCommand.getContext());
    context.setAttribute(HttpCoreContext.SSL_SESSION, getSSLSession());
    context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, getEndpointDetails());
    final ClientHttp1StreamHandler handler = new ClientHttp1StreamHandler(outputChannel, httpProcessor, http1Config, connectionReuseStrategy, exchangeHandler, context);
    pipeline.add(handler);
    outgoing = handler;
    if (handler.isOutputReady()) {
        handler.produceOutput();
    }
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext)

Example 5 with RequestExecutionCommand

use of org.apache.hc.core5.http.nio.command.RequestExecutionCommand in project httpcomponents-core by apache.

the class AbstractHttp1StreamDuplexer method processCommands.

private void processCommands() throws HttpException, IOException {
    for (; ; ) {
        final Command command = ioSession.poll();
        if (command == null) {
            return;
        }
        if (command instanceof ShutdownCommand) {
            final ShutdownCommand shutdownCommand = (ShutdownCommand) command;
            requestShutdown(shutdownCommand.getType());
        } else if (command instanceof RequestExecutionCommand) {
            if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0) {
                command.cancel();
            } else {
                execute((RequestExecutionCommand) command);
                return;
            }
        } else {
            throw new HttpException("Unexpected command: " + command.getClass());
        }
    }
}
Also used : Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) HttpException(org.apache.hc.core5.http.HttpException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand)

Aggregations

RequestExecutionCommand (org.apache.hc.core5.http.nio.command.RequestExecutionCommand)5 AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)3 Command (org.apache.hc.core5.reactor.Command)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 HttpException (org.apache.hc.core5.http.HttpException)2 ShutdownCommand (org.apache.hc.core5.http.nio.command.ShutdownCommand)2 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)2 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)2 IOException (java.io.IOException)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 EntityDetails (org.apache.hc.core5.http.EntityDetails)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)1 AsyncPushConsumer (org.apache.hc.core5.http.nio.AsyncPushConsumer)1 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)1 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)1 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)1