public class MuServerBuilder extends Object
A builder for creating a web server.
Use the withXXX()
methods to set the ports, config, and request handlers needed.
Constructor and Description |
---|
MuServerBuilder() |
Modifier and Type | Method and Description |
---|---|
MuServerBuilder |
addAsyncHandler(AsyncMuHandler handler)
Deprecated.
For async handling, add a normal
MuHandler and call MuRequest.handleAsync() |
MuServerBuilder |
addHandler(Method method,
String uriTemplate,
RouteHandler handler)
Registers a new handler that will only be called if it matches the given route info
|
MuServerBuilder |
addHandler(MuHandler handler)
Adds a request handler.
|
MuServerBuilder |
addHandler(MuHandlerBuilder handler)
Adds a request handler.
|
MuServerBuilder |
addResponseCompleteListener(ResponseCompleteListener listener)
Adds a listener that is notified when each response completes
|
MuServerBuilder |
addShutdownHook(boolean stopServerOnShutdown) |
static MuServerBuilder |
httpServer()
Creates a new server builder which will run as HTTP on a random port.
|
static MuServerBuilder |
httpsServer()
Creates a new server builder which will run as HTTPS on a random port.
|
static MuServerBuilder |
muServer()
Creates a new server builder.
|
MuServer |
start()
Creates and starts this server.
|
MuServerBuilder |
withGzip(long minimumGzipSize,
Set<String> mimeTypesToGzip)
Enables gzip for files of at least the specified size that match the given mime-types.
|
MuServerBuilder |
withGzipEnabled(boolean enabled)
Enables gzip for certain resource types.
|
MuServerBuilder |
withHandlerExecutor(ExecutorService executor)
Sets the thread executor service to run requests on.
|
MuServerBuilder |
withHttp2Config(Http2Config http2Config)
Sets the configuration for HTTP2
|
MuServerBuilder |
withHttp2Config(Http2ConfigBuilder http2Config)
Sets the configuration for HTTP2
|
MuServerBuilder |
withHttpConnection(int port)
Deprecated.
Use
withHttpPort(int) instead |
MuServerBuilder |
withHttpDisabled()
Deprecated.
It is off by default so this is not needed.
|
MuServerBuilder |
withHttpPort(int port) |
MuServerBuilder |
withHttpsConfig(HttpsConfigBuilder httpsConfig)
Sets the HTTPS config.
|
MuServerBuilder |
withHttpsConfig(SSLContext sslContext)
Deprecated.
This won't be supported in the future. Instead, use a
HttpsConfigBuilder to set up HTTPS. |
MuServerBuilder |
withHttpsConfig(SSLContextBuilder sslContext)
Deprecated.
Use
withHttpsConfig(HttpsConfigBuilder) instead |
MuServerBuilder |
withHttpsConnection(int port,
SSLContext sslEngine)
Deprecated.
use
withHttpsPort(int) and withHttpsConfig(SSLContext) instead. |
MuServerBuilder |
withHttpsPort(int port)
Sets the HTTPS port to use.
|
MuServerBuilder |
withIdleTimeout(long duration,
TimeUnit unit)
Sets the idle timeout for requests and responses.
|
MuServerBuilder |
withInterface(String host)
Use this to specify which network interface to bind to.
|
MuServerBuilder |
withMaxHeadersSize(int size)
Specifies the maximum size in bytes of the HTTP request headers.
|
MuServerBuilder |
withMaxRequestSize(long maxSizeInBytes)
The maximum allowed request body size.
|
MuServerBuilder |
withMaxUrlSize(int size)
The maximum length that a URL can be.
|
MuServerBuilder |
withRateLimiter(RateLimitSelector selector)
Adds a rate limiter to incoming requests.
|
public MuServerBuilder withHttpPort(int port)
port
- The HTTP port to use. A value of 0 will have a random port assigned; a value of -1 will
result in no HTTP connector.public MuServerBuilder withInterface(String host)
host
- The host to bind to, for example "127.0.0.1"
to restrict connections from localhost
only, or "0.0.0.0"
to allow connections from the local network.public MuServerBuilder addShutdownHook(boolean stopServerOnShutdown)
stopServerOnShutdown
- If true, then a shutdown hook which stops this server will be added to the JVM Runtime@Deprecated public MuServerBuilder withHttpConnection(int port)
withHttpPort(int)
insteadport
- The HTTP port to use. A value of 0 will have a random port assigned; a value of -1 will
result in no HTTP connector.public MuServerBuilder withGzipEnabled(boolean enabled)
true
. By default, the
gzippable resource types are taken from ResourceType.getResourceTypes()
where
ResourceType.gzip
is true
.enabled
- True to enable; false to disablewithGzip(long, Set)
public MuServerBuilder withGzip(long minimumGzipSize, Set<String> mimeTypesToGzip)
minimumGzipSize
- The size in bytes before gzip is used. The default is 1400.mimeTypesToGzip
- The mime-types that should be gzipped. In general, only text
files should be gzipped.@Deprecated public MuServerBuilder withHttpDisabled()
@Deprecated public MuServerBuilder withHttpsConnection(int port, SSLContext sslEngine)
withHttpsPort(int)
and withHttpsConfig(SSLContext)
instead.port
- The portsslEngine
- The SSL Context@Deprecated public MuServerBuilder withHttpsConfig(SSLContext sslContext)
HttpsConfigBuilder
to set up HTTPS.SSLContextBuilder.unsignedLocalhostCert()
sslContext
- An SSL Context.SSLContextBuilder
@Deprecated public MuServerBuilder withHttpsConfig(SSLContextBuilder sslContext)
withHttpsConfig(HttpsConfigBuilder)
insteadHttpsConfigBuilder.unsignedLocalhost()
}sslContext
- An SSL Context builder.SSLContextBuilder
public MuServerBuilder withHttpsConfig(HttpsConfigBuilder httpsConfig)
HttpsConfigBuilder.unsignedLocalhost()
}httpsConfig
- An HTTPS Config builder.public MuServerBuilder withHttpsPort(int port)
withHttpsConfig(SSLContextBuilder)
port
- A value of 0 will result in a random port being assigned; a value of -1 will
disable HTTPS.public MuServerBuilder withHttp2Config(Http2Config http2Config)
http2Config
- A configHttp2ConfigBuilder
public MuServerBuilder withHttp2Config(Http2ConfigBuilder http2Config)
http2Config
- A configHttp2ConfigBuilder
public MuServerBuilder withHandlerExecutor(ExecutorService executor)
Executors.newCachedThreadPool()
is used.executor
- The executor service to use to handle requestspublic MuServerBuilder withMaxHeadersSize(int size)
Specifies the maximum size in bytes of the HTTP request headers. Defaults to 8192.
If a request has headers exceeding this value, it will be rejected and a 431
status code will be returned. Large values increase the risk of Denial-of-Service attacks
due to the extra memory allocated in each request.
It is recommended to not specify a value unless you are finding legitimate requests are
being rejected with 413
errors.
size
- The maximum size in bytes that can be used for headers.public MuServerBuilder withMaxUrlSize(int size)
414
error is
returned to the client. The default value is 8175.size
- The maximum number of characters allowed in URLs sent to this server.public MuServerBuilder withMaxRequestSize(long maxSizeInBytes)
maxSizeInBytes
- The maximum request body size allowed, in bytes. The default is 24MB.public MuServerBuilder withIdleTimeout(long duration, TimeUnit unit)
The default is 5 minutes.
duration
- The allowed timeout duration, or 0 to disable timeouts.unit
- The unit of the duration.@Deprecated public MuServerBuilder addAsyncHandler(AsyncMuHandler handler)
MuHandler
and call MuRequest.handleAsync()
Throws an exception. Do not use.
handler
- Ignoredpublic MuServerBuilder addHandler(MuHandlerBuilder handler)
Adds a request handler.
Note that handlers are executed in the order added to the builder, but all async handlers are executed before synchronous handlers.
handler
- A handler builder. The build()
method will be called on this
to create the handler. If null, then no handler is added.addHandler(Method, String, RouteHandler)
public MuServerBuilder addHandler(MuHandler handler)
Adds a request handler.
Note that handlers are executed in the order added to the builder, but all async handlers are executed before synchronous handlers.
handler
- The handler to add. If null, then no handler is added.addHandler(Method, String, RouteHandler)
public MuServerBuilder addHandler(Method method, String uriTemplate, RouteHandler handler)
method
- The method to match, or null
to accept any method.uriTemplate
- A URL template. Supports plain URLs like /abc
or paths
with named parameters such as /abc/{id}
or named parameters
with regexes such as /abc/{id : [0-9]+}
where the named
parameter values can be accessed with the pathParams
parameter in the route handler.handler
- The handler to invoke if the method and URI matches. If null, then no handler is added.public MuServerBuilder addResponseCompleteListener(ResponseCompleteListener listener)
listener
- A listener. If null, then nothing is added.public MuServerBuilder withRateLimiter(RateLimitSelector selector)
Adds a rate limiter to incoming requests.
The selector specified in this method allows you to control the limit buckets that are used. For
example, to set a limit on client IP addresses the selector would return MuRequest.remoteAddress()
.
The selector also specifies the number of requests allowed for the bucket per time period, such that different buckets can have different limits.
The following example shows how to allow 100 requests per second per IP address:
MuServerBuilder.httpsServer()
.withRateLimiter(request -> RateLimit.builder()
.withBucket(request.remoteAddress())
.withRate(100)
.withWindow(1, TimeUnit.SECONDS)
.build())
Note that multiple limiters can be added which allows different limits across different dimensions. For example, you may allow 100 requests per second based on IP address and also a limit based on a cookie, request path, or other value.
selector
- A function that returns a string based on the request, or null to not have a limit appliedpublic static MuServerBuilder muServer()
withHttpsPort(int)
or withHttpPort(int)
to specify
the port to use, and call start()
to start the server.public static MuServerBuilder httpServer()
public static MuServerBuilder httpsServer()
public MuServer start()
Copyright © 2017–2020. All rights reserved.