Class HttpServerBuilder


  • public abstract class HttpServerBuilder
    extends java.lang.Object
    A builder for building HTTP Servers.
    • Constructor Detail

      • HttpServerBuilder

        public HttpServerBuilder()
    • Method Detail

      • backlog

        public abstract HttpServerBuilder backlog​(int backlog)
        Sets the maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter. If a connection indication arrives when the queue is full, the connection may time out.
        Parameters:
        backlog - the backlog to use when accepting connections.
        Returns:
        this.
      • sslConfig

        public abstract HttpServerBuilder sslConfig​(ServerSslConfig config)
        Set the SSL/TLS configuration.
        Parameters:
        config - The configuration to use.
        Returns:
        this.
      • sslConfig

        public abstract HttpServerBuilder sslConfig​(ServerSslConfig defaultConfig,
                                                    java.util.Map<java.lang.String,​ServerSslConfig> sniMap)
        Set the SSL/TLS and SNI configuration.
        Parameters:
        defaultConfig - The configuration to use is the client certificate's SNI extension isn't present or the SNI hostname doesn't match any values in sniMap.
        sniMap - A map where the keys are matched against the client certificate's SNI extension value in order to provide the corresponding ServerSslConfig.
        Returns:
        this.
      • socketOption

        public abstract <T> HttpServerBuilder socketOption​(java.net.SocketOption<T> option,
                                                           T value)
        Adds a SocketOption that is applied.
        Type Parameters:
        T - the type of the value.
        Parameters:
        option - the option to apply.
        value - the value.
        Returns:
        this.
        See Also:
        StandardSocketOptions, ServiceTalkSocketOptions
      • enableWireLogging

        public abstract HttpServerBuilder enableWireLogging​(java.lang.String loggerName,
                                                            LogLevel logLevel,
                                                            java.util.function.BooleanSupplier logUserData)
        Enables wire-logging for this server.
        Parameters:
        loggerName - The name of the logger to log wire events.
        logLevel - The level to log at.
        logUserData - true to include user data (e.g. data, headers, etc.). false to exclude user data and log only network events.
        Returns:
        this.
      • disableDrainingRequestPayloadBody

        public final HttpServerBuilder disableDrainingRequestPayloadBody()
        Disables automatic consumption of request payload body when it is not consumed by the service.

        For persistent HTTP connections it is required to eventually consume the entire request payload to enable reading of the next request. This is required because requests are pipelined for HTTP/1.1, so if the previous request is not completely read, next request can not be read from the socket. For cases when there is a possibility that user may forget to consume request payload, ServiceTalk automatically consumes request payload body. This automatic consumption behavior may create some overhead and can be disabled using this method when it is guaranteed that all request paths consumes all request payloads eventually. An example of guaranteed consumption are non-streaming APIs.

        Returns:
        this.
      • allowDropRequestTrailers

        public abstract HttpServerBuilder allowDropRequestTrailers​(boolean allowDrop)
        Provide a hint if request trailers are allowed to be dropped. This hint maybe ignored if the transport can otherwise infer that the trailers should be preserved. For example, if the request headers contain Trailer then this hint maybe ignored.
        Parameters:
        allowDrop - true if request trailers are allowed to be dropped.
        Returns:
        this
      • appendConnectionAcceptorFilter

        public final HttpServerBuilder appendConnectionAcceptorFilter​(ConnectionAcceptorFactory factory)
        Appends the filter to the chain of filters used to decorate the ConnectionAcceptor used by this builder.

        The order of execution of these filters are in order of append. If 3 filters are added as follows:

             builder.appendConnectionAcceptorFilter(filter1).appendConnectionAcceptorFilter(filter2).
             appendConnectionAcceptorFilter(filter3)
         
        accepting a connection by a filter wrapped by this filter chain, the order of invocation of these filters will be:
             filter1 => filter2 => filter3
         
        Parameters:
        factory - ConnectionAcceptorFactory to append. Lifetime of this ConnectionAcceptorFactory is managed by this builder and the server started thereof.
        Returns:
        this
      • appendServiceFilter

        public final HttpServerBuilder appendServiceFilter​(java.util.function.Predicate<StreamingHttpRequest> predicate,
                                                           StreamingHttpServiceFilterFactory factory)
        Appends the filter to the chain of filters used to decorate the StreamingHttpService used by this builder, for every request that passes the provided Predicate.

        Note this method will be used to decorate the StreamingHttpService passed to listenStreaming(StreamingHttpService) before it is used by the server.

        The order of execution of these filters are in order of append. If 3 filters are added as follows:

             builder.append(filter1).append(filter2).append(filter3)
         
        accepting a request by a service wrapped by this filter chain, the order of invocation of these filters will be:
             filter1 => filter2 => filter3 => service
         
        Parameters:
        predicate - the Predicate to test if the filter must be applied.
        factory - StreamingHttpServiceFilterFactory to append.
        Returns:
        this
      • listenAndAwait

        public final ServerContext listenAndAwait​(HttpService service)
                                           throws java.lang.Exception
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A ServerContext by blocking the calling thread until the server is successfully started or throws an Exception if the server could not be started.
        Throws:
        java.lang.Exception - if the server could not be started.
      • listenStreamingAndAwait

        public final ServerContext listenStreamingAndAwait​(StreamingHttpService handler)
                                                    throws java.lang.Exception
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        handler - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A ServerContext by blocking the calling thread until the server is successfully started or throws an Exception if the server could not be started.
        Throws:
        java.lang.Exception - if the server could not be started.
      • listenBlockingAndAwait

        public final ServerContext listenBlockingAndAwait​(BlockingHttpService service)
                                                   throws java.lang.Exception
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A ServerContext by blocking the calling thread until the server is successfully started or throws an Exception if the server could not be started.
        Throws:
        java.lang.Exception - if the server could not be started.
      • listenBlockingStreamingAndAwait

        public final ServerContext listenBlockingStreamingAndAwait​(BlockingStreamingHttpService handler)
                                                            throws java.lang.Exception
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        handler - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A ServerContext by blocking the calling thread until the server is successfully started or throws an Exception if the server could not be started.
        Throws:
        java.lang.Exception - if the server could not be started.
      • listen

        public final Single<ServerContext> listen​(HttpService service)
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A Single that completes when the server is successfully started or terminates with an error if the server could not be started.
      • listenStreaming

        public final Single<ServerContext> listenStreaming​(StreamingHttpService service)
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A Single that completes when the server is successfully started or terminates with an error if the server could not be started.
      • listenBlocking

        public final Single<ServerContext> listenBlocking​(BlockingHttpService service)
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A Single that completes when the server is successfully started or terminates with an error if the server could not be started.
      • listenBlockingStreaming

        public final Single<ServerContext> listenBlockingStreaming​(BlockingStreamingHttpService service)
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this will result in a socket bind/listen on address.

        Parameters:
        service - Service invoked for every request received by this server. The returned ServerContext manages the lifecycle of the service, ensuring it is closed when the ServerContext is closed.
        Returns:
        A Single that completes when the server is successfully started or terminates with an error if the server could not be started.
      • doListen

        protected abstract Single<ServerContext> doListen​(@Nullable
                                                          ConnectionAcceptor connectionAcceptor,
                                                          StreamingHttpService service,
                                                          HttpExecutionStrategy strategy,
                                                          boolean drainRequestPayloadBody)
        Starts this server and returns the ServerContext after the server has been successfully started.

        If the underlying protocol (eg. TCP) supports it this should result in a socket bind/listen on address.

        Parameters:
        connectionAcceptor - ConnectionAcceptor to use for the server.
        service - StreamingHttpService to use for the server.
        strategy - the HttpExecutionStrategy to use for the service.
        drainRequestPayloadBody - if true the server implementation should automatically subscribe and ignore the payload body of incoming requests.
        Returns:
        A Single that completes when the server is successfully started or terminates with an error if the server could not be started.