Class ContextHandlerBuilder
- java.lang.Object
-
- io.muserver.ContextHandlerBuilder
-
- All Implemented Interfaces:
MuHandlerBuilder<ContextHandler>
public class ContextHandlerBuilder extends java.lang.Object implements MuHandlerBuilder<ContextHandler>
Use this to serve a list of handlers from a base path.
-
-
Constructor Summary
Constructors Constructor Description ContextHandlerBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ContextHandlerBuilder
addHandler(Method method, java.lang.String uriTemplate, RouteHandler handler)
Registers a new handler that will only be called if it matches the given route info (relative to the current context).ContextHandlerBuilder
addHandler(MuHandler handler)
Adds a request handler relative to the context of this builder.ContextHandlerBuilder
addHandler(MuHandlerBuilder handler)
Adds a request handler relative to the context of this builder.ContextHandler
build()
static ContextHandlerBuilder
context(java.lang.String path)
Create a new base path.ContextHandlerBuilder
withPath(java.lang.String path)
Sets the path to serve from.
-
-
-
Method Detail
-
withPath
public ContextHandlerBuilder withPath(java.lang.String path)
Sets the path to serve from.If a null or empty path is given, then it is as if the child handlers are not added to a context.
- Parameters:
path
- The path, such asapi
or/api/
etc- Returns:
- Returns the current builder.
-
context
public static ContextHandlerBuilder context(java.lang.String path)
Create a new base path. Any handlers added with
addHandler(MuHandler)
,addHandler(MuHandlerBuilder)
oraddHandler(Method, String, RouteHandler)
will be served relative to the path given.Request handlers can get the context they are served from by using the
MuRequest.contextPath()
and can get the path relative to handler withMuRequest.relativePath()
.If a null or empty path is given, then it is as if the child handlers are not added to a context.
- Parameters:
path
- The path to serve handlers from, for exampleapi
or/api/
(which are equivalent).- Returns:
- Returns a builder with methods to add handlers to this context.
-
addHandler
public ContextHandlerBuilder addHandler(MuHandlerBuilder handler)
Adds a request handler relative to the context of this builder.
Note that handlers are executed in the order added to the builder, but all async handlers are executed before synchronous handlers.
- Parameters:
handler
- A handler builder. Thebuild()
method will be called on this to create the handler.- Returns:
- The current Mu-Server Handler.
- See Also:
addHandler(Method, String, RouteHandler)
-
addHandler
public ContextHandlerBuilder addHandler(MuHandler handler)
Adds a request handler relative to the context of this builder.
Note that handlers are executed in the order added to the builder, but all async handlers are executed before synchronous handlers.
- Parameters:
handler
- The handler to add.- Returns:
- The current Mu-Server Handler.
- See Also:
addHandler(Method, String, RouteHandler)
-
addHandler
public ContextHandlerBuilder addHandler(Method method, java.lang.String uriTemplate, RouteHandler handler)
Registers a new handler that will only be called if it matches the given route info (relative to the current context).- Parameters:
method
- The method to match, ornull
to accept any method.uriTemplate
- A URL template, relative to the context. 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 thepathParams
parameter in the route handler.handler
- The handler to invoke if the method and URI matches.- Returns:
- Returns the server builder
-
build
public ContextHandler build()
- Specified by:
build
in interfaceMuHandlerBuilder<ContextHandler>
- Returns:
- A newly built
MuHandler
-
-