Search in sources :

Example 1 with HandlerFactory

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

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

the class Http1TestServer method start.

public InetSocketAddress start(final IOEventHandlerFactory handlerFactory) throws Exception {
    execute(handlerFactory);
    final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
    final ListenerEndpoint listener = future.get();
    return (InetSocketAddress) listener.getAddress();
}
Also used : ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with HandlerFactory

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

the class H2ServerBootstrap method create.

public HttpAsyncServer create() {
    final String actualCanonicalHostName = canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName();
    final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(actualCanonicalHostName, () -> lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN));
    for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry : handlerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
    if (!filters.isEmpty()) {
        final NamedElementChain<AsyncFilterHandler> filterChainDefinition = new NamedElementChain<>();
        filterChainDefinition.addLast(new TerminalAsyncServerFilter(new DefaultAsyncResponseExchangeHandlerFactory(registry)), StandardFilter.MAIN_HANDLER.name());
        filterChainDefinition.addFirst(new AsyncServerExpectationFilter(), StandardFilter.EXPECT_CONTINUE.name());
        for (final FilterEntry<AsyncFilterHandler> entry : filters) {
            switch(entry.position) {
                case AFTER:
                    filterChainDefinition.addAfter(entry.existing, entry.filterHandler, entry.name);
                    break;
                case BEFORE:
                    filterChainDefinition.addBefore(entry.existing, entry.filterHandler, entry.name);
                    break;
                case REPLACE:
                    filterChainDefinition.replace(entry.existing, entry.filterHandler);
                    break;
                case FIRST:
                    filterChainDefinition.addFirst(entry.filterHandler, entry.name);
                    break;
                case LAST:
                    // Don't add last, after TerminalAsyncServerFilter, as that does not delegate to the chain
                    // Instead, add the filter just before it, making it effectively the last filter
                    filterChainDefinition.addBefore(StandardFilter.MAIN_HANDLER.name(), entry.filterHandler, entry.name);
                    break;
            }
        }
        NamedElementChain<AsyncFilterHandler>.Node current = filterChainDefinition.getLast();
        AsyncServerFilterChainElement execChain = null;
        while (current != null) {
            execChain = new AsyncServerFilterChainElement(current.getValue(), execChain);
            current = current.getPrevious();
        }
        handlerFactory = new AsyncServerFilterChainExchangeHandlerFactory(execChain, exceptionCallback);
    } else {
        handlerFactory = new DefaultAsyncResponseExchangeHandlerFactory(registry, handler -> new BasicAsyncServerExpectationDecorator(handler, exceptionCallback));
    }
    final ServerH2StreamMultiplexerFactory http2StreamHandlerFactory = new ServerH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.server(), handlerFactory, h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, h2StreamListener);
    final TlsStrategy actualTlsStrategy = tlsStrategy != null ? tlsStrategy : new H2ServerTlsStrategy();
    final ServerHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ServerHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.server(), handlerFactory, http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, DefaultHttpRequestParserFactory.INSTANCE, DefaultHttpResponseWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, http1StreamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ServerHttpProtocolNegotiationStarter(http1StreamHandlerFactory, http2StreamHandlerFactory, versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, actualTlsStrategy, handshakeTimeout);
    return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, exceptionCallback, sessionListener, actualCanonicalHostName);
}
Also used : AsyncServerFilterChainElement(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) Args(org.apache.hc.core5.util.Args) H2Config(org.apache.hc.core5.http2.config.H2Config) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) CharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) AsyncServerFilterChainElement(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) IOSessionListener(org.apache.hc.core5.reactor.IOSessionListener) Supplier(org.apache.hc.core5.function.Supplier) ArrayList(java.util.ArrayList) HttpVersionPolicy(org.apache.hc.core5.http2.HttpVersionPolicy) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpProcessors(org.apache.hc.core5.http.impl.HttpProcessors) DefaultHttpResponseWriterFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory) LookupRegistry(org.apache.hc.core5.http.protocol.LookupRegistry) Http1Config(org.apache.hc.core5.http.config.Http1Config) NamedElementChain(org.apache.hc.core5.http.config.NamedElementChain) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) ServerHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiationStarter) BasicServerExchangeHandler(org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler) Decorator(org.apache.hc.core5.function.Decorator) HandlerFactory(org.apache.hc.core5.http.nio.HandlerFactory) Callback(org.apache.hc.core5.function.Callback) Timeout(org.apache.hc.core5.util.Timeout) H2Processors(org.apache.hc.core5.http2.impl.H2Processors) DefaultContentLengthStrategy(org.apache.hc.core5.http.impl.DefaultContentLengthStrategy) UriPatternType(org.apache.hc.core5.http.protocol.UriPatternType) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory) List(java.util.List) ServerH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory) H2StreamListener(org.apache.hc.core5.http2.impl.nio.H2StreamListener) DefaultHttpRequestParserFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory) IOSession(org.apache.hc.core5.reactor.IOSession) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) InetAddressUtils(org.apache.hc.core5.net.InetAddressUtils) ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) AsyncServerExpectationFilter(org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) StandardFilter(org.apache.hc.core5.http.impl.bootstrap.StandardFilter) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) AsyncServerExpectationFilter(org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory) Supplier(org.apache.hc.core5.function.Supplier) ServerH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ServerH2StreamMultiplexerFactory) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) NamedElementChain(org.apache.hc.core5.http.config.NamedElementChain) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) H2ServerTlsStrategy(org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy) ServerHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ServerHttpProtocolNegotiationStarter) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory)

Example 4 with HandlerFactory

use of org.apache.hc.core5.http.nio.HandlerFactory 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 5 with HandlerFactory

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

the class ClientH2StreamMultiplexer method createRemotelyInitiatedStream.

@Override
H2StreamHandler createRemotelyInitiatedStream(final H2StreamChannel channel, final HttpProcessor httpProcessor, final BasicHttpConnectionMetrics connMetrics, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory) throws IOException {
    final HttpCoreContext context = HttpCoreContext.create();
    context.setAttribute(HttpCoreContext.SSL_SESSION, getSSLSession());
    context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, getEndpointDetails());
    return new ClientPushH2StreamHandler(channel, httpProcessor, connMetrics, pushHandlerFactory != null ? pushHandlerFactory : this.pushHandlerFactory, context);
}
Also used : HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext)

Aggregations

AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)5 List (java.util.List)4 HandlerFactory (org.apache.hc.core5.http.nio.HandlerFactory)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 IOSession (org.apache.hc.core5.reactor.IOSession)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)3 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 HttpException (org.apache.hc.core5.http.HttpException)3 HttpHost (org.apache.hc.core5.http.HttpHost)3 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 CharCodingConfig (org.apache.hc.core5.http.config.CharCodingConfig)3 Http1Config (org.apache.hc.core5.http.config.Http1Config)3 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)3 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)3 HttpProcessors (org.apache.hc.core5.http.impl.HttpProcessors)3 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)3 AsyncServerRequestHandler (org.apache.hc.core5.http.nio.AsyncServerRequestHandler)3