Class HttpServerBuilder
- java.lang.Object
-
- io.servicetalk.http.api.HttpServerBuilder
-
public abstract class HttpServerBuilder extends java.lang.ObjectA builder for building HTTP Servers.
-
-
Constructor Summary
Constructors Constructor Description HttpServerBuilder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract HttpServerBuilderallowDropRequestTrailers(boolean allowDrop)Provide a hint if request trailers are allowed to be dropped.HttpServerBuilderappendConnectionAcceptorFilter(ConnectionAcceptorFactory factory)Appends the filter to the chain of filters used to decorate theConnectionAcceptorused by this builder.HttpServerBuilderappendServiceFilter(StreamingHttpServiceFilterFactory factory)Appends the filter to the chain of filters used to decorate theStreamingHttpServiceused by this builder.HttpServerBuilderappendServiceFilter(java.util.function.Predicate<StreamingHttpRequest> predicate, StreamingHttpServiceFilterFactory factory)Appends the filter to the chain of filters used to decorate theStreamingHttpServiceused by this builder, for every request that passes the providedPredicate.abstract HttpServerBuilderbacklog(int backlog)Sets the maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter.abstract HttpServerBuilderbufferAllocator(BufferAllocator allocator)Sets theBufferAllocatorto be used by this server.HttpServerBuilderdisableDrainingRequestPayloadBody()Disables automatic consumption of requestpayload bodywhen it is not consumed by the service.protected abstract Single<ServerContext>doListen(ConnectionAcceptor connectionAcceptor, StreamingHttpService service, HttpExecutionStrategy strategy, boolean drainRequestPayloadBody)Starts this server and returns theServerContextafter the server has been successfully started.abstract HttpServerBuilderenableWireLogging(java.lang.String loggerName)Deprecated.UseenableWireLogging(String, LogLevel, BooleanSupplier)instead.abstract HttpServerBuilderenableWireLogging(java.lang.String loggerName, LogLevel logLevel, java.util.function.BooleanSupplier logUserData)Enables wire-logging for this server.HttpServerBuilderexecutionStrategy(HttpExecutionStrategy strategy)Sets theHttpExecutionStrategyto be used by this server.abstract HttpServerBuilderioExecutor(IoExecutor ioExecutor)Sets theIoExecutorto be used by this server.Single<ServerContext>listen(HttpService service)Starts this server and returns theServerContextafter the server has been successfully started.ServerContextlistenAndAwait(HttpService service)Starts this server and returns theServerContextafter the server has been successfully started.Single<ServerContext>listenBlocking(BlockingHttpService service)Starts this server and returns theServerContextafter the server has been successfully started.ServerContextlistenBlockingAndAwait(BlockingHttpService service)Starts this server and returns theServerContextafter the server has been successfully started.Single<ServerContext>listenBlockingStreaming(BlockingStreamingHttpService service)Starts this server and returns theServerContextafter the server has been successfully started.ServerContextlistenBlockingStreamingAndAwait(BlockingStreamingHttpService handler)Starts this server and returns theServerContextafter the server has been successfully started.Single<ServerContext>listenStreaming(StreamingHttpService service)Starts this server and returns theServerContextafter the server has been successfully started.ServerContextlistenStreamingAndAwait(StreamingHttpService handler)Starts this server and returns theServerContextafter the server has been successfully started.abstract HttpServerBuilderprotocols(HttpProtocolConfig... protocols)Configurations of various HTTP protocol versions.abstract HttpServerSecurityConfiguratorsecure()Initiates security configuration for this server.abstract HttpServerSecurityConfiguratorsecure(java.lang.String... sniHostnames)Initiates security configuration for this server for the passedsniHostnames.abstract <T> HttpServerBuildersocketOption(java.net.SocketOption<T> option, T value)Adds aSocketOptionthat is applied.abstract HttpServerBuildertransportObserver(TransportObserver transportObserver)Sets aTransportObserverthat provides visibility into transport events.
-
-
-
Method Detail
-
protocols
public abstract HttpServerBuilder protocols(HttpProtocolConfig... protocols)
Configurations of various HTTP protocol versions.Note: the order of specified protocols will reflect on priorities for ALPN in case the connections are
secured.- Parameters:
protocols-HttpProtocolConfigfor each protocol that should be supported.- Returns:
this.
-
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.
-
secure
public abstract HttpServerSecurityConfigurator secure()
Initiates security configuration for this server. Calling anycommitmethod on the returnedHttpServerSecurityConfiguratorwill commit the configuration.Additionally use
secure(String...)to define configurations for specific SNI hostnames. If such configuration is additionally defined then configuration using this method is used as default if the hostname does not match any of the specified hostnames.- Returns:
HttpServerSecurityConfiguratorto configure security for this server. It is mandatory to call any one of thecommitmethods after all configuration is done.
-
secure
public abstract HttpServerSecurityConfigurator secure(java.lang.String... sniHostnames)
Initiates security configuration for this server for the passedsniHostnames. Calling anycommitmethod on the returnedHttpServerSecurityConfiguratorwill commit the configuration.When using this method, it is mandatory to also define the default configuration using
secure()which is used when the hostname does not match any of the specifiedsniHostnames.- Parameters:
sniHostnames- SNI hostnames for which this config is being defined.- Returns:
HttpServerSecurityConfiguratorto configure security for this server. It is mandatory to call any one of thecommitmethods after all configuration is done.
-
socketOption
public abstract <T> HttpServerBuilder socketOption(java.net.SocketOption<T> option, T value)
Adds aSocketOptionthat 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
@Deprecated public abstract HttpServerBuilder enableWireLogging(java.lang.String loggerName)
Deprecated.UseenableWireLogging(String, LogLevel, BooleanSupplier)instead.Enables wire-logging for this server.- Parameters:
loggerName- The name of the logger to log wire events.- Returns:
this.
-
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-trueto include user data (e.g. data, headers, etc.).falseto exclude user data and log only network events.- Returns:
this.
-
transportObserver
public abstract HttpServerBuilder transportObserver(TransportObserver transportObserver)
Sets aTransportObserverthat provides visibility into transport events.- Parameters:
transportObserver- ATransportObserverthat provides visibility into transport events.- Returns:
this.
-
disableDrainingRequestPayloadBody
public final HttpServerBuilder disableDrainingRequestPayloadBody()
Disables automatic consumption of requestpayload bodywhen 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-trueif 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 theConnectionAcceptorused 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-ConnectionAcceptorFactoryto append. Lifetime of thisConnectionAcceptorFactoryis managed by this builder and the server started thereof.- Returns:
this
-
appendServiceFilter
public final HttpServerBuilder appendServiceFilter(StreamingHttpServiceFilterFactory factory)
Appends the filter to the chain of filters used to decorate theStreamingHttpServiceused by this builder.Note this method will be used to decorate the
StreamingHttpServicepassed tolistenStreaming(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:
factory-StreamingHttpServiceFilterFactoryto append.- 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 theStreamingHttpServiceused by this builder, for every request that passes the providedPredicate.Note this method will be used to decorate the
StreamingHttpServicepassed tolistenStreaming(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- thePredicateto test if the filter must be applied.factory-StreamingHttpServiceFilterFactoryto append.- Returns:
this
-
ioExecutor
public abstract HttpServerBuilder ioExecutor(IoExecutor ioExecutor)
Sets theIoExecutorto be used by this server.- Parameters:
ioExecutor-IoExecutorto use.- Returns:
this.
-
bufferAllocator
public abstract HttpServerBuilder bufferAllocator(BufferAllocator allocator)
Sets theBufferAllocatorto be used by this server.- Parameters:
allocator-BufferAllocatorto use.- Returns:
this.
-
executionStrategy
public final HttpServerBuilder executionStrategy(HttpExecutionStrategy strategy)
Sets theHttpExecutionStrategyto be used by this server.- Parameters:
strategy-HttpExecutionStrategyto use by this server.- Returns:
this.
-
listenAndAwait
public final ServerContext listenAndAwait(HttpService service) throws java.lang.Exception
Starts this server and returns theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
ServerContextby blocking the calling thread until the server is successfully started or throws anExceptionif 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
ServerContextby blocking the calling thread until the server is successfully started or throws anExceptionif 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
ServerContextby blocking the calling thread until the server is successfully started or throws anExceptionif 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
ServerContextby blocking the calling thread until the server is successfully started or throws anExceptionif 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
Singlethat 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
Singlethat 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
Singlethat 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 theServerContextafter 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 returnedServerContextmanages the lifecycle of theservice, ensuring it is closed when theServerContextis closed.- Returns:
- A
Singlethat 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 theServerContextafter 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-ConnectionAcceptorto use for the server.service-StreamingHttpServiceto use for the server.strategy- theHttpExecutionStrategyto use for the service.drainRequestPayloadBody- iftruethe server implementation should automatically subscribe and ignore thepayload bodyof incoming requests.- Returns:
- A
Singlethat completes when the server is successfully started or terminates with an error if the server could not be started.
-
-