Search in sources :

Example 1 with TerminalAsyncServerFilter

use of org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter 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 2 with TerminalAsyncServerFilter

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

the class AsyncServerBootstrap method create.

public HttpAsyncServer create() {
    final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(), () -> 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 ServerHttp1StreamDuplexerFactory streamHandlerFactory = new ServerHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.server(), handlerFactory, http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, connStrategy != null ? connStrategy : DefaultConnectionReuseStrategy.INSTANCE, DefaultHttpRequestParserFactory.INSTANCE, DefaultHttpResponseWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, streamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ServerHttp1IOEventHandlerFactory(streamHandlerFactory, tlsStrategy, handshakeTimeout);
    return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, exceptionCallback, sessionListener);
}
Also used : AsyncServerFilterChainElement(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainElement) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) Args(org.apache.hc.core5.util.Args) 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) ServerHttp1IOEventHandlerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory) 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) 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) ConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy) Timeout(org.apache.hc.core5.util.Timeout) DefaultContentLengthStrategy(org.apache.hc.core5.http.impl.DefaultContentLengthStrategy) UriPatternType(org.apache.hc.core5.http.protocol.UriPatternType) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory) List(java.util.List) 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) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) ServerHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) AsyncServerExpectationFilter(org.apache.hc.core5.http.nio.support.AsyncServerExpectationFilter) DefaultAsyncResponseExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory) NamedElementChain(org.apache.hc.core5.http.config.NamedElementChain) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) Supplier(org.apache.hc.core5.function.Supplier) AsyncServerFilterChainExchangeHandlerFactory(org.apache.hc.core5.http.nio.support.AsyncServerFilterChainExchangeHandlerFactory) ServerHttp1IOEventHandlerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory) ServerHttp1IOEventHandlerFactory(org.apache.hc.core5.http.impl.nio.ServerHttp1IOEventHandlerFactory) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Example 3 with TerminalAsyncServerFilter

use of org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter in project californium by eclipse.

the class HttpServer method registerProxy.

/**
 * Register proxy request handler.
 *
 * The proxy handler is used for all requests, which contains a
 * {@code absoluteURI}, which is not related to one of the used virtual
 * handlers.
 *
 * @param <T> request presentation
 * @param requestHandler request handler to register
 * @throws NullPointerException if request handler is {@code null}
 * @throws IllegalStateException if server was already started
 * @see #registerVirtual(String, String, AsyncServerRequestHandler)
 * @see <a href="https://tools.ietf.org/html/rfc2616#section-5.1.2" target=
 *      "_blank"> RFC2616, HTTP/1.1 - 5.1.2 Request-URI</a>
 * @since 3.0
 */
public <T> void registerProxy(final AsyncServerRequestHandler<T> requestHandler) {
    if (server != null) {
        throw new IllegalStateException("http server already started!");
    }
    Args.notNull(requestHandler, "Request handler");
    proxyServerFilter = new TerminalAsyncServerFilter(new HandlerFactory<AsyncServerExchangeHandler>() {

        @Override
        public AsyncServerExchangeHandler create(HttpRequest request, HttpContext context) throws HttpException {
            return new BasicServerExchangeHandler<>(requestHandler);
        }
    });
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HandlerFactory(org.apache.hc.core5.http.nio.HandlerFactory) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) BasicServerExchangeHandler(org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler)

Aggregations

HandlerFactory (org.apache.hc.core5.http.nio.HandlerFactory)3 BasicServerExchangeHandler (org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler)3 TerminalAsyncServerFilter (org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Callback (org.apache.hc.core5.function.Callback)2 Decorator (org.apache.hc.core5.function.Decorator)2 Supplier (org.apache.hc.core5.function.Supplier)2 CharCodingConfig (org.apache.hc.core5.http.config.CharCodingConfig)2 Http1Config (org.apache.hc.core5.http.config.Http1Config)2 NamedElementChain (org.apache.hc.core5.http.config.NamedElementChain)2 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)2 DefaultContentLengthStrategy (org.apache.hc.core5.http.impl.DefaultContentLengthStrategy)2 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)2 HttpProcessors (org.apache.hc.core5.http.impl.HttpProcessors)2 DefaultHttpRequestParserFactory (org.apache.hc.core5.http.impl.nio.DefaultHttpRequestParserFactory)2 DefaultHttpResponseWriterFactory (org.apache.hc.core5.http.impl.nio.DefaultHttpResponseWriterFactory)2 ServerHttp1StreamDuplexerFactory (org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexerFactory)2 AsyncFilterHandler (org.apache.hc.core5.http.nio.AsyncFilterHandler)2 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)2