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 |
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 |
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(SSLContext sslContext)
Sets the HTTPS config.
|
MuServerBuilder |
withHttpsConfig(SSLContextBuilder sslContext)
Sets the HTTPS config.
|
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 |
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 |
withMaxUrlSize(int size)
The maximum length that a URL can be.
|
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 Contextpublic MuServerBuilder withHttpsConfig(SSLContext sslContext)
SSLContextBuilder.unsignedLocalhostCert()
sslContext
- An SSL Context.SSLContextBuilder
public MuServerBuilder withHttpsConfig(SSLContextBuilder sslContext)
SSLContextBuilder.unsignedLocalhostCert()
sslContext
- An SSL Context builder.SSLContextBuilder
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 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.@Deprecated public MuServerBuilder addAsyncHandler(AsyncMuHandler handler)
MuHandler
and call MuRequest.handleAsync()
xAdds a new Asynchronous handler. This is for cases where async handling of requests and
responses is required; in other cases use addHandler(MuHandler)
,
addHandler(MuHandlerBuilder)
or addHandler(Method, String, RouteHandler)
.
Note that async handlers are executed in the order added to the builder, but all async handlers are executed before synchronous handlers.
handler
- An Async Handlerpublic 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 MuServer start()
public 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()
Copyright © 2017–2019. All rights reserved.