public interface Router extends Registry
Modifier and Type | Interface and Description |
---|---|
static interface |
Router.Match
Find route result.
|
Modifier and Type | Field and Description |
---|---|
static String |
DELETE
HTTP DELETE.
|
static String |
GET
HTTP GET.
|
static String |
HEAD
HTTP HEAD.
|
static List<String> |
METHODS
HTTP Methods.
|
static String |
OPTIONS
HTTP OPTIONS.
|
static String |
PATCH
HTTP PATCH.
|
static String |
POST
HTTP POST.
|
static String |
PUT
HTTP PUT.
|
static String |
SSE
Sever-Sent events.
|
static String |
TRACE
HTTP TRACE.
|
static String |
WS
Web socket.
|
Modifier and Type | Method and Description |
---|---|
Router |
after(Route.After after)
Add an after route decorator to the route pipeline.
|
default Route |
assets(String pattern,
AssetHandler handler)
Add a static resource handler.
|
default Route |
assets(String pattern,
AssetSource... source)
Add a static resource handler.
|
default Route |
assets(String pattern,
Path source)
Add a static resource handler.
|
default Route |
assets(String pattern,
String source)
Add a static resource handler.
|
default <T> T |
attribute(String key)
Get an attribute by his key.
|
default Router |
attribute(String key,
Object value)
Set an application attribute.
|
Router |
before(Route.Before before)
Add a before route decorator to the route pipeline.
|
Router |
converter(BeanConverter converter)
Add a custom bean value converter.
|
Router |
converter(ValueConverter converter)
Add a custom string value converter.
|
Router |
decoder(MediaType contentType,
MessageDecoder decoder)
Register a decoder for the given content type.
|
Router |
decorator(Route.Decorator decorator)
Add a route decorator to the route pipeline.
|
default Route |
delete(String pattern,
Route.Handler handler)
Add a HTTP DELETE handler.
|
Router |
dispatch(Executor executor,
Runnable body)
Dispatch route pipeline to the given executor.
|
Router |
dispatch(Runnable body)
Dispatch route pipeline to the
getWorker() worker thread pool. |
Router |
domain(String domain,
Router subrouter)
Enabled routes for specific domain.
|
RouteSet |
domain(String domain,
Runnable body)
Enabled routes for specific domain.
|
Router |
encoder(MediaType contentType,
MessageEncoder encoder)
Register a route response encoder.
|
Router |
encoder(MessageEncoder encoder)
Register a route response encoder.
|
default Router |
error(Class<? extends Throwable> type,
ErrorHandler handler)
Add a custom error handler that matches the given exception type.
|
Router |
error(ErrorHandler handler)
Add a custom error handler.
|
default Router |
error(Predicate<StatusCode> predicate,
ErrorHandler handler)
Add a custom error handler that matches the given predicate.
|
default Router |
error(StatusCode statusCode,
ErrorHandler handler)
Add a custom error handler that matches the given status code.
|
Router |
errorCode(Class<? extends Throwable> type,
StatusCode statusCode)
Map an exception type to a status code.
|
StatusCode |
errorCode(Throwable cause)
Computes the status code for the given exception.
|
default Executor |
executor(String name)
Get an executor from application registry.
|
Router |
executor(String name,
Executor executor)
Put an executor into the application registry.
|
static List<String> |
expandOptionalVariables(String pattern)
Look for optional path parameter and expand the given pattern into multiple pattern.
|
default Route |
get(String pattern,
Route.Handler handler)
Add a HTTP GET handler.
|
Map<String,Object> |
getAttributes()
Mutable map of application attributes.
|
List<BeanConverter> |
getBeanConverters()
Get all complex/bean value converters.
|
com.typesafe.config.Config |
getConfig()
Application configuration.
|
String |
getContextPath()
Get application context path (a.k.a as base path).
|
List<ValueConverter> |
getConverters()
Get all simple/string value converters.
|
Environment |
getEnvironment()
Application environment.
|
ErrorHandler |
getErrorHandler()
Get the error handler.
|
String |
getFlashCookie()
Name of the flash cookie.
|
org.slf4j.Logger |
getLog()
Application logger.
|
Set<RouterOption> |
getRouterOptions()
Router options.
|
List<Route> |
getRoutes()
Returns all routes.
|
ServerOptions |
getServerOptions()
Available server options.
|
ServiceRegistry |
getServices()
Application service registry.
|
SessionStore |
getSessionStore()
Session store.
|
Path |
getTmpdir()
Application temporary directory.
|
Executor |
getWorker()
Returns the worker thread pool.
|
default Route |
head(String pattern,
Route.Handler handler)
Add a HTTP HEAD handler.
|
boolean |
isTrustProxy()
When true handles X-Forwarded-* headers by updating the values on the current context to
match what was sent in the header(s).
|
static String |
leadingSlash(String path)
Ensure path start with a
/ (leading slash). |
Router.Match |
match(Context ctx)
Find a matching route using the given context.
|
boolean |
match(String method,
String path)
Find a matching route using the given context.
|
Router |
mount(Predicate<Context> predicate,
Router router)
Import routes from given router.
|
RouteSet |
mount(Predicate<Context> predicate,
Runnable body)
Import routes from given action.
|
Router |
mount(Router router)
Import all routes from the given router.
|
Router |
mount(String path,
Router router)
Import all routes from the given router and prefix them with the given path.
|
Router |
mvc(Class router)
Import all route method from the given controller class.
|
<T> Router |
mvc(Class<T> router,
javax.inject.Provider<T> provider)
Import all route method from the given controller class.
|
Router |
mvc(Object router)
Import all route methods from given controller instance.
|
static String |
normalizePath(String path)
Normalize a path by removing consecutive
/ (slashes). |
static String |
noTrailingSlash(String path)
Strip trailing slashes.
|
default Route |
options(String pattern,
Route.Handler handler)
Add a HTTP OPTIONS handler.
|
default Route |
patch(String pattern,
Route.Handler handler)
Add a HTTP PATCH handler.
|
RouteSet |
path(String pattern,
Runnable body)
Group one or more routes under a common path prefix.
|
static List<String> |
pathKeys(String pattern)
Extract path keys from given path pattern.
|
static List<String> |
pathKeys(String pattern,
BiConsumer<String,String> consumer)
Extract path keys from given path pattern.
|
default Route |
post(String pattern,
Route.Handler handler)
Add a HTTP POST handler.
|
default Route |
put(String pattern,
Route.Handler handler)
Add a HTTP PUT handler.
|
Router |
responseHandler(ResponseHandler factory)
Add a response handler factory.
|
static String |
reverse(String pattern,
Map<String,Object> keys)
Recreate a path pattern using the given variables.
|
static String |
reverse(String pattern,
Object... values)
Recreate a path pattern using the given variables.
|
Route |
route(String method,
String pattern,
Route.Handler handler)
Add a route.
|
RouteSet |
routes(Runnable body)
Group one or more routes.
|
Router |
setContextAsService(boolean contextAsService)
If enabled, allows to retrieve the
Context object associated with the current
request via the service registry while the request is being processed. |
Router |
setContextPath(String contextPath)
Set application context path.
|
Router |
setCurrentUser(Function<Context,Object> provider)
Provides a way to set the current user from a
Context . |
Router |
setDefaultWorker(Executor worker)
Set the default worker thread pool.
|
Router |
setFlashCookie(String name)
Set flash cookie name.
|
Router |
setHiddenMethod(Function<Context,Optional<String>> provider)
Provides a way to override the current HTTP method using lookup strategy.
|
Router |
setHiddenMethod(String parameterName)
Provides a way to override the current HTTP method.
|
Router |
setRouterOptions(RouterOption... options)
Set router options.
|
Router |
setSessionStore(SessionStore store)
Set session store.
|
Router |
setTrustProxy(boolean trustProxy)
When true handles X-Forwarded-* headers by updating the values on the current context to
match what was sent in the header(s).
|
Router |
setWorker(Executor worker)
Set a worker thread pool.
|
Route |
sse(String pattern,
ServerSentEmitter.Handler handler)
Add a server-sent event handler.
|
default Route |
trace(String pattern,
Route.Handler handler)
Add a HTTP TRACE handler.
|
default Router |
use(Predicate<Context> predicate,
Router router)
Deprecated.
|
default RouteSet |
use(Predicate<Context> predicate,
Runnable body)
Deprecated.
|
default Router |
use(Router router)
Deprecated.
Use
mount(Router) |
default Router |
use(String path,
Router router)
Deprecated.
|
Route |
ws(String pattern,
WebSocket.Initializer handler)
Add a websocket handler.
|
static final String GET
static final String POST
static final String PUT
static final String DELETE
static final String PATCH
static final String HEAD
static final String OPTIONS
static final String TRACE
static final String WS
static final String SSE
@Nonnull com.typesafe.config.Config getConfig()
@Nonnull Environment getEnvironment()
@Nonnull Map<String,Object> getAttributes()
@Nonnull default <T> T attribute(@Nonnull String key)
getAttributes()
.T
- Attribute type.key
- Attribute key.@Nonnull default Router attribute(@Nonnull String key, Object value)
key
- Attribute key.value
- Attribute value.@Nonnull ServiceRegistry getServices()
Jooby.require(Class)
calls.
This method returns a mutable registry. You are free to modify/alter the registry.@Nonnull Router setContextPath(@Nonnull String contextPath)
/
.contextPath
- Context path.@Nonnull String getContextPath()
boolean isTrustProxy()
X-Forwarded-*
header, otherwise a remote user can spoof their address by
sending a header with bogus values.
The headers that are read/set are:
Context.setRemoteAddress(String)
.Context.setScheme(String)
.Context.setHost(String)
.Context.setPort(int)
.@Nonnull Router setTrustProxy(boolean trustProxy)
X-Forwarded-*
header, otherwise a remote user can spoof their address by
sending a header with bogus values.
The headers that are read/set are:
Context.setRemoteAddress(String)
.Context.setScheme(String)
.Context.setHost(String)
.Context.setPort(int)
.trustProxy
- True to enabled.@Nonnull Router setHiddenMethod(@Nonnull String parameterName)
setHiddenMethod(Function)
method.parameterName
- Form field name.@Nonnull Router setHiddenMethod(@Nonnull Function<Context,Optional<String>> provider)
provider
- Lookup strategy.@Nonnull Router setCurrentUser(@Nullable Function<Context,Object> provider)
Context
. Current user can be retrieve it
using Context.getUser()
.provider
- User provider/factory.@Nonnull Router setContextAsService(boolean contextAsService)
Context
object associated with the current
request via the service registry while the request is being processed.contextAsService
- whether to enable or disable this feature@Nonnull Router domain(@Nonnull String domain, @Nonnull Router subrouter)
host
header.
{
domain("foo.com", new FooApp());
domain("bar.com", new BarApp());
}
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.
NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.domain
- Predicatesubrouter
- Subrouter.@Nonnull RouteSet domain(@Nonnull String domain, @Nonnull Runnable body)
host
header.
{
domain("foo.com", () -> {
get("/", ctx -> "foo");
});
domain("bar.com", () -> {
get("/", ctx -> "bar");
});
}
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.domain
- Predicatebody
- Route action.@Deprecated @Nonnull default Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
mount(Predicate, Router)
{
use(ctx -> ctx.getHost().equals("foo.com"), new FooApp());
use(ctx -> ctx.getHost().equals("bar.com"), new BarApp());
}
Imported routes are matched only when predicate pass.
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.
NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.predicate
- Context predicate.router
- Router to import.@Nonnull Router mount(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
{
use(ctx -> ctx.getHost().equals("foo.com"), new FooApp());
use(ctx -> ctx.getHost().equals("bar.com"), new BarApp());
}
Imported routes are matched only when predicate pass.
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.
NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.predicate
- Context predicate.router
- Router to import.@Deprecated @Nonnull default RouteSet use(@Nonnull Predicate<Context> predicate, @Nonnull Runnable body)
mount(Predicate, Runnable)
{
use(ctx -> ctx.getHost().equals("foo.com"), () -> {
get("/", ctx -> "foo");
});
use(ctx -> ctx.getHost().equals("bar.com"), () -> {
get("/", ctx -> "bar");
});
}
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.predicate
- Context predicate.body
- Route action.@Nonnull RouteSet mount(@Nonnull Predicate<Context> predicate, @Nonnull Runnable body)
{
mount(ctx -> ctx.getHost().equals("foo.com"), () -> {
get("/", ctx -> "foo");
});
mount(ctx -> ctx.getHost().equals("bar.com"), () -> {
get("/", ctx -> "bar");
});
}
NOTE: if you run behind a reverse proxy you might to enabled setTrustProxy(boolean)
.
NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.predicate
- Context predicate.body
- Route action.@Deprecated @Nonnull default Router use(@Nonnull String path, @Nonnull Router router)
mount(String, Router)
path
- Prefix path.router
- Router to import.@Nonnull Router mount(@Nonnull String path, @Nonnull Router router)
path
- Prefix path.router
- Router to import.@Deprecated @Nonnull default Router use(@Nonnull Router router)
mount(Router)
router
- Router to import.@Nonnull Router mount(@Nonnull Router router)
router
- Router to import.@Nonnull Router mvc(@Nonnull Class router)
Jooby.require(Class)
.router
- Controller class.@Nonnull <T> Router mvc(@Nonnull Class<T> router, @Nonnull javax.inject.Provider<T> provider)
T
- Controller type.router
- Controller class.provider
- Controller provider.@Nonnull Router mvc(@Nonnull Object router)
router
- Controller instance.@Nonnull Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler)
pattern
- WebSocket path pattern.handler
- WebSocket handler.@Nonnull Route sse(@Nonnull String pattern, @Nonnull ServerSentEmitter.Handler handler)
pattern
- Path pattern.handler
- Handler.@Nonnull Router encoder(@Nonnull MessageEncoder encoder)
encoder
- MessageEncoder instance.@Nonnull Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder encoder)
contentType
- Accept header should matches the content-type.encoder
- MessageEncoder instance.@Nonnull Path getTmpdir()
Environment
when isn't
set manually.@Nonnull Router decoder(@Nonnull MediaType contentType, @Nonnull MessageDecoder decoder)
contentType
- Content type to match.decoder
- MessageDecoder.@Nonnull Executor getWorker()
@Nonnull Router setWorker(@Nonnull Executor worker)
worker
- Worker thread pool.@Nonnull Router setDefaultWorker(@Nonnull Executor worker)
getWorker()
returns the default thread pool, unless you explicitly set one.worker
- Default worker thread pool.@Nonnull Router decorator(@Nonnull Route.Decorator decorator)
decorator
- Decorator.@Nonnull Router before(@Nonnull Route.Before before)
before
- Before decorator.@Nonnull Router after(@Nonnull Route.After after)
after
- After decorator.@Nonnull Router dispatch(@Nonnull Runnable body)
getWorker()
worker thread pool. After dispatch
application code is allowed to do blocking calls.body
- Dispatch body.@Nonnull Router dispatch(@Nonnull Executor executor, @Nonnull Runnable body)
executor
- Executor. ExecutorService
instances automatically
shutdown at application exit.body
- Dispatch body.@Nonnull RouteSet routes(@Nonnull Runnable body)
body
- Route body.@Nonnull RouteSet path(@Nonnull String pattern, @Nonnull Runnable body)
pattern
- Path pattern.body
- Route body.@Nonnull default Route get(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route post(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route put(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route delete(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route patch(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route head(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route options(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route trace(@Nonnull String pattern, @Nonnull Route.Handler handler)
pattern
- Path pattern.handler
- Application code.@Nonnull default Route assets(@Nonnull String pattern, @Nonnull Path source)
pattern
- Path pattern.source
- File system directory.@Nonnull default Route assets(@Nonnull String pattern, @Nonnull String source)
pattern
- Path pattern.source
- File-System folder when exists, or fallback to a classpath folder.@Nonnull default Route assets(@Nonnull String pattern, @Nonnull AssetSource... source)
pattern
- Path pattern.source
- Asset sources.@Nonnull default Route assets(@Nonnull String pattern, @Nonnull AssetHandler handler)
pattern
- Path pattern.handler
- Asset handler.@Nonnull Route route(@Nonnull String method, @Nonnull String pattern, @Nonnull Route.Handler handler)
method
- HTTP method.pattern
- Path pattern.handler
- Application code.@Nonnull Router.Match match(@Nonnull Context ctx)
404
handler.
See Route.NOT_FOUND
.ctx
- Web Context.boolean match(@Nonnull String method, @Nonnull String path)
404
handler.
See Route.NOT_FOUND
.method
- Method to match.path
- Path to match.@Nonnull Router errorCode(@Nonnull Class<? extends Throwable> type, @Nonnull StatusCode statusCode)
type
- Exception type.statusCode
- Status code.@Nonnull StatusCode errorCode(@Nonnull Throwable cause)
cause
- Exception.@Nonnull default Router error(@Nonnull StatusCode statusCode, @Nonnull ErrorHandler handler)
statusCode
- Status code.handler
- Error handler.@Nonnull default Router error(@Nonnull Class<? extends Throwable> type, @Nonnull ErrorHandler handler)
type
- Exception type.handler
- Error handler.@Nonnull default Router error(@Nonnull Predicate<StatusCode> predicate, @Nonnull ErrorHandler handler)
predicate
- Status code filter.handler
- Error handler.@Nonnull Router error(@Nonnull ErrorHandler handler)
handler
- Error handler.@Nonnull ErrorHandler getErrorHandler()
@Nonnull org.slf4j.Logger getLog()
@Nonnull Router responseHandler(@Nonnull ResponseHandler factory)
factory
- Response handler factory.@Nonnull Set<RouterOption> getRouterOptions()
@Nonnull Router setRouterOptions(@Nonnull RouterOption... options)
options
- router options.@Nonnull SessionStore getSessionStore()
SessionStore.memory()
.@Nonnull Router setSessionStore(@Nonnull SessionStore store)
store
- Session store.@Nonnull default Executor executor(@Nonnull String name)
name
- Executor name.@Nonnull Router executor(@Nonnull String name, @Nonnull Executor executor)
name
- Executor's name.executor
- Executor.@Nonnull String getFlashCookie()
jooby.flash
.jooby.flash
.@Nonnull Router setFlashCookie(@Nonnull String name)
name
- Flash cookie name.@Nonnull Router converter(@Nonnull ValueConverter converter)
converter
- Custom value converter.@Nonnull Router converter(@Nonnull BeanConverter converter)
converter
- Custom value converter.@Nonnull List<ValueConverter> getConverters()
@Nonnull List<BeanConverter> getBeanConverters()
@Nonnull ServerOptions getServerOptions()
@Nonnull static String leadingSlash(@Nullable String path)
/
(leading slash).path
- Path to process.@Nonnull static String noTrailingSlash(@Nonnull String path)
path
- Path to process.@Nonnull static String normalizePath(@Nullable String path)
/
(slashes).path
- Path to process.@Nonnull static List<String> pathKeys(@Nonnull String pattern)
/product/{id}
pattern
- Path pattern.@Nonnull static List<String> pathKeys(@Nonnull String pattern, BiConsumer<String,String> consumer)
/product/{id}
pattern
- Path pattern.consumer
- Listen for key and regex variables found.@Nonnull static List<String> expandOptionalVariables(@Nonnull String pattern)
/path => [/path] /{id} => [/{id}] /path/{id} => [/path/{id}] /{id}? => [/, /{id}] /path/{id}? => [/path, /path/{id}] /path/{id}/{start}?/{end}? => [/path/{id}, /path/{id}/{start}, /path/{id}/{start}/{end}] /path/{id}?/suffix => [/path, /path/{id}, /path/suffix]
pattern
- Pattern.@Nonnull static String reverse(@Nonnull String pattern, @Nonnull Object... values)
pattern
- Path pattern.values
- Path keys.Copyright © 2020. All rights reserved.