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 ContextHandlerBuilderaddHandler(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).ContextHandlerBuilderaddHandler(MuHandler handler)Adds a request handler relative to the context of this builder.ContextHandlerBuilderaddHandler(MuHandlerBuilder handler)Adds a request handler relative to the context of this builder.ContextHandlerbuild()static ContextHandlerBuildercontext(java.lang.String path)Create a new base path.ContextHandlerBuilderwithPath(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 asapior/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 exampleapior/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, ornullto accept any method.uriTemplate- A URL template, relative to the context. Supports plain URLs like/abcor 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 thepathParamsparameter 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:
buildin interfaceMuHandlerBuilder<ContextHandler>- Returns:
- A newly built
MuHandler
-
-