- All Superinterfaces:
Registry
- All Known Implementing Classes:
Jooby
- Since:
- 2.0.0
- Author:
- edgar
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
HTTP DELETE.static final String
HTTP GET.static final String
HTTP HEAD.HTTP Methods.static final String
HTTP OPTIONS.static final String
HTTP PATCH.static final String
HTTP POST.static final String
HTTP PUT.static final String
Sever-Sent events.static final String
HTTP TRACE.static final String
Web socket. -
Method Summary
Modifier and TypeMethodDescriptionafter
(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, AssetSource... sources) Add a static resource handler.default Route
Add a static resource handler.default Route
Add a static resource handler.default <T> T
Get an attribute by his key.default Router
Set an application attribute.before
(Route.Before before) Add a before route decorator to the route pipeline.converter
(ValueConverter converter) Add a custom string value converter.decoder
(MediaType contentType, MessageDecoder decoder) Register a decoder for the given content type.default Router
decorator
(Route.Decorator filter) Deprecated.default Route
delete
(String pattern, Route.Handler handler) Add a HTTP DELETE handler.Dispatch route pipeline to thegetWorker()
worker thread pool.Dispatch route pipeline to the given executor.Enabled routes for specific domain.Enabled routes for specific domain.encoder
(MediaType contentType, MessageEncoder encoder) Register a route response encoder.encoder
(MessageEncoder encoder) Register a route response encoder.error
(ErrorHandler handler) Add a custom error handler.default Router
error
(StatusCode statusCode, ErrorHandler handler) Add a custom error handler that matches the given status code.default Router
error
(Class<? extends Throwable> type, ErrorHandler handler) Add a custom error handler that matches the given exception type.default Router
error
(Predicate<StatusCode> predicate, ErrorHandler handler) Add a custom error handler that matches the given predicate.errorCode
(Class<? extends Throwable> type, StatusCode statusCode) Map an exception type to a status code.Computes the status code for the given exception.default Executor
Get an executor from application registry.Put an executor into the application registry.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.Mutable map of application attributes.Get all complex/bean value converters.com.typesafe.config.Config
Application configuration.Get application context path (a.k.a as base path).Get all simple/string value converters.Application environment.Get the error handler.Template for the flash cookie.Returns the supported locales.org.slf4j.Logger
getLog()
Application logger.Router options.Returns all routes.Available server options.Application service registry.Session store.Application temporary directory.Returns the worker thread pool.default Route
head
(String pattern, Route.Handler handler) Add a HTTP HEAD handler.boolean
boolean
boolean
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).Find a matching route using the given context.boolean
Find a matching route using the given context.Import all routes from the given router.Import all routes from the given router and prefix them with the given path.Import routes from given router.Import routes from given action.Import all route method from the given controller class.<T> Router
Import all route method from the given controller class.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.Group one or more routes under a common path prefix.Extract path keys from given path pattern.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.resultHandler
(ResultHandler factory) Add a response handler factory.static String
Recreate a path pattern using the given variables.static String
Recreate a path pattern using the given variables.route
(String method, String pattern, Route.Handler handler) Add a route.Group one or more routes.setBufferFactory
(DataBufferFactory bufferFactory) setContextAsService
(boolean contextAsService) If enabled, allows to retrieve theContext
object associated with the current request via the service registry while the request is being processed.setContextPath
(String contextPath) Set application context path.setCurrentUser
(Function<Context, Object> provider) Provides a way to set the current user from aContext
.setDefaultWorker
(Executor worker) Set the default worker thread pool.setFlashCookie
(Cookie flashCookie) Sets a cookie used as a template to generate the flash cookie, allowing to customize the cookie name and other cookie parameters.setHiddenMethod
(String parameterName) Provides a way to override the current HTTP method.setHiddenMethod
(Function<Context, Optional<String>> provider) Provides a way to override the current HTTP method using lookup strategy.setRouterOptions
(RouterOption... options) Set router options.setSessionStore
(SessionStore store) Set session store.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).Set a worker thread pool.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.use
(Route.Filter filter) Attach a filter to the route pipeline.ws
(String pattern, WebSocket.Initializer handler) Add a websocket handler.
-
Field Details
-
GET
HTTP GET.- See Also:
-
POST
HTTP POST.- See Also:
-
PUT
HTTP PUT.- See Also:
-
DELETE
HTTP DELETE.- See Also:
-
PATCH
HTTP PATCH.- See Also:
-
HEAD
HTTP HEAD.- See Also:
-
OPTIONS
HTTP OPTIONS.- See Also:
-
TRACE
HTTP TRACE.- See Also:
-
METHODS
HTTP Methods. -
WS
Web socket.- See Also:
-
SSE
Sever-Sent events.- See Also:
-
-
Method Details
-
getConfig
@NonNull com.typesafe.config.Config getConfig()Application configuration.- Returns:
- Application configuration.
-
getEnvironment
Application environment.- Returns:
- Application environment.
-
getLocales
Returns the supported locales.- Returns:
- The supported locales.
-
getAttributes
Mutable map of application attributes.- Returns:
- Mutable map of application attributes.
-
attribute
Get an attribute by his key. This is just a utility method aroundgetAttributes()
.- Type Parameters:
T
- Attribute type.- Parameters:
key
- Attribute key.- Returns:
- Attribute value.
-
attribute
Set an application attribute.- Parameters:
key
- Attribute key.value
- Attribute value.- Returns:
- This router.
-
getServices
Application service registry. Services are accessible via this registry orJooby.require(Class)
calls.This method returns a mutable registry. You are free to modify/alter the registry.
- Returns:
- Service registry.
-
setContextPath
Set application context path. Context path is the base path for all routes. Default is:/
.- Parameters:
contextPath
- Context path.- Returns:
- This router.
-
getContextPath
Get application context path (a.k.a as base path).- Returns:
- Application context path (a.k.a as base path).
-
isTrustProxy
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).This should only be installed behind a reverse proxy that has been configured to send the
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:
- X-Forwarded-For: Set/update the remote address
Context.setRemoteAddress(String)
. - X-Forwarded-Proto: Set/update request scheme
Context.setScheme(String)
. - X-Forwarded-Host: Set/update the request host
Context.setHost(String)
. - X-Forwarded-Port: Set/update the request port
Context.setPort(int)
.
- Returns:
- True when enabled. Default is false.
- X-Forwarded-For: Set/update the remote address
-
isStarted
boolean isStarted() -
isStopped
boolean isStopped() -
setTrustProxy
When true handles X-Forwarded-* headers by updating the values on the current context to match what was sent in the header(s).This should only be installed behind a reverse proxy that has been configured to send the
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:
- X-Forwarded-For: Set/update the remote address
Context.setRemoteAddress(String)
. - X-Forwarded-Proto: Set/update request scheme
Context.setScheme(String)
. - X-Forwarded-Host: Set/update the request host
Context.setHost(String)
. - X-Forwarded-Port: Set/update the request port
Context.setPort(int)
.
- Parameters:
trustProxy
- True to enable.- Returns:
- This router.
- X-Forwarded-For: Set/update the remote address
-
setHiddenMethod
Provides a way to override the current HTTP method. Request must be:- POST Form/multipart request
For alternative strategy use the
setHiddenMethod(Function)
method.- Parameters:
parameterName
- Form field name.- Returns:
- This router.
-
setHiddenMethod
Provides a way to override the current HTTP method using lookup strategy.- Parameters:
provider
- Lookup strategy.- Returns:
- This router.
-
setCurrentUser
Provides a way to set the current user from aContext
. Current user can be retrieve it usingContext.getUser()
.- Parameters:
provider
- User provider/factory.- Returns:
- This router.
-
setContextAsService
If enabled, allows to retrieve theContext
object associated with the current request via the service registry while the request is being processed.- Parameters:
contextAsService
- whether to enable or disable this feature- Returns:
- This router.
-
domain
Enabled routes for specific domain. Domain matching is done using thehost
header.
NOTE: if you run behind a reverse proxy you might to enabled{ domain("foo.com", new FooApp()); domain("bar.com", new BarApp()); }
setTrustProxy(boolean)
.NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
- Parameters:
domain
- Predicatesubrouter
- Subrouter.- Returns:
- This router.
-
domain
Enabled routes for specific domain. Domain matching is done using thehost
header.
NOTE: if you run behind a reverse proxy you might to enabled{ domain("foo.com", () -> { get("/", ctx -> "foo"); }); domain("bar.com", () -> { get("/", ctx -> "bar"); }); }
setTrustProxy(boolean)
.- Parameters:
domain
- Predicatebody
- Route action.- Returns:
- This router.
-
mount
Import routes from given router. Predicate works like a filter and only when predicate pass the routes match against the current request.Example of domain predicate filter:
Imported routes are matched only when predicate pass.{ use(ctx -> ctx.getHost().equals("foo.com"), new FooApp()); use(ctx -> ctx.getHost().equals("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.
- Parameters:
predicate
- Context predicate.router
- Router to import.- Returns:
- This router.
-
mount
Import routes from given action. Predicate works like a filter and only when predicate pass the routes match against the current request.Example of domain predicate filter:
NOTE: if you run behind a reverse proxy you might to enabled{ mount(ctx -> ctx.getHost().equals("foo.com"), () -> { get("/", ctx -> "foo"); }); mount(ctx -> ctx.getHost().equals("bar.com"), () -> { get("/", ctx -> "bar"); }); }
setTrustProxy(boolean)
.NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
- Parameters:
predicate
- Context predicate.body
- Route action.- Returns:
- This router.
-
mount
Import all routes from the given router and prefix them with the given path.NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
- Parameters:
path
- Prefix path.router
- Router to import.- Returns:
- This router.
-
mount
Import all routes from the given router.NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
- Parameters:
router
- Router to import.- Returns:
- This router.
-
mvc
Import all route method from the given controller class. At runtime the controller instance is resolved by callingJooby.require(Class)
.- Parameters:
router
- Controller class.- Returns:
- This router.
-
mvc
Import all route method from the given controller class.- Type Parameters:
T
- Controller type.- Parameters:
router
- Controller class.provider
- Controller provider.- Returns:
- This router.
-
mvc
Import all route methods from given controller instance.- Parameters:
router
- Controller instance.- Returns:
- This routes.
-
ws
Add a websocket handler.- Parameters:
pattern
- WebSocket path pattern.handler
- WebSocket handler.- Returns:
- A new route.
-
sse
Add a server-sent event handler.- Parameters:
pattern
- Path pattern.handler
- Handler.- Returns:
- A new route.
-
getRoutes
Returns all routes.- Returns:
- All routes.
-
encoder
Register a route response encoder.- Parameters:
encoder
- MessageEncoder instance.- Returns:
- This router.
-
encoder
Register a route response encoder.- Parameters:
contentType
- Accept header should match the content-type.encoder
- MessageEncoder instance.- Returns:
- This router.
-
getTmpdir
Application temporary directory. This method initialize theEnvironment
when isn't set manually.- Returns:
- Application temporary directory.
-
decoder
Register a decoder for the given content type.- Parameters:
contentType
- Content type to match.decoder
- MessageDecoder.- Returns:
- This router.
-
getWorker
Returns the worker thread pool. This thread pool is used to run application blocking code.- Returns:
- Worker thread pool.
-
setWorker
Set a worker thread pool. This thread pool is used to run application blocking code.- Parameters:
worker
- Worker thread pool.- Returns:
- This router.
-
setDefaultWorker
Set the default worker thread pool. Via this method the underlying web server set/suggests the worker thread pool that should be used it.A call to
getWorker()
returns the default thread pool, unless you explicitly set one.- Parameters:
worker
- Default worker thread pool.- Returns:
- This router.
-
getBufferFactory
-
setBufferFactory
-
use
Attach a filter to the route pipeline.- Parameters:
filter
- Filter.- Returns:
- This router.
-
decorator
Deprecated.Useuse(Route.Filter)
.Attach a filter to the route pipeline.- Parameters:
filter
- Filter.- Returns:
- This router.
-
before
Add a before route decorator to the route pipeline.- Parameters:
before
- Before decorator.- Returns:
- This router.
-
after
Add an after route decorator to the route pipeline.- Parameters:
after
- After decorator.- Returns:
- This router.
-
dispatch
Dispatch route pipeline to thegetWorker()
worker thread pool. After dispatch application code is allowed to do blocking calls.- Parameters:
body
- Dispatch body.- Returns:
- This router.
-
dispatch
Dispatch route pipeline to the given executor. After dispatch application code is allowed to do blocking calls.- Parameters:
executor
- Executor.ExecutorService
instances automatically shutdown at application exit.body
- Dispatch body.- Returns:
- This router.
-
routes
Group one or more routes. Useful for applying cross cutting concerns to the enclosed routes.- Parameters:
body
- Route body.- Returns:
- All routes created.
-
path
Group one or more routes under a common path prefix. Useful for applying cross-cutting concerns to the enclosed routes.- Parameters:
pattern
- Path pattern.body
- Route body.- Returns:
- All routes created.
-
get
Add a HTTP GET handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
post
Add a HTTP POST handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
put
Add a HTTP PUT handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
delete
Add a HTTP DELETE handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
patch
Add a HTTP PATCH handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
head
Add a HTTP HEAD handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
options
Add a HTTP OPTIONS handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
trace
Add a HTTP TRACE handler.- Parameters:
pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
assets
Add a static resource handler. Static resources are resolved from file system.- Parameters:
pattern
- Path pattern.source
- File system directory.- Returns:
- A route.
-
assets
Add a static resource handler. Static resources are resolved from:- file-system if the source folder exists in the current user directory - or fallback to classpath when file-system folder doesn't exist.
NOTE: This method choose file-system or classpath, it doesn't merge them.
- Parameters:
pattern
- Path pattern.source
- File-System folder when exists, or fallback to a classpath folder.- Returns:
- A route.
-
assets
@NonNull default Route assets(@NonNull String pattern, @NonNull AssetSource source, @NonNull AssetSource... sources) Add a static resource handler.- Parameters:
pattern
- Path pattern.source
- Asset source.sources
- additional Asset sources.- Returns:
- A route.
-
assets
Add a static resource handler.- Parameters:
pattern
- Path pattern.handler
- Asset handler.- Returns:
- A route.
-
route
@NonNull Route route(@NonNull String method, @NonNull String pattern, @NonNull Route.Handler handler) Add a route.- Parameters:
method
- HTTP method.pattern
- Path pattern.handler
- Application code.- Returns:
- A route.
-
match
Find a matching route using the given context.If no match exists this method returns a route with a
404
handler. SeeRoute.NOT_FOUND
.- Parameters:
ctx
- Web Context.- Returns:
- A route match result.
-
match
Find a matching route using the given context.If no match exists this method returns a route with a
404
handler. SeeRoute.NOT_FOUND
.- Parameters:
method
- Method to match.path
- Path to match.- Returns:
- A route match result.
-
errorCode
Map an exception type to a status code.- Parameters:
type
- Exception type.statusCode
- Status code.- Returns:
- This router.
-
errorCode
Computes the status code for the given exception.- Parameters:
cause
- Exception.- Returns:
- Status code.
-
error
Add a custom error handler that matches the given status code.- Parameters:
statusCode
- Status code.handler
- Error handler.- Returns:
- This router.
-
error
@NonNull default Router error(@NonNull Class<? extends Throwable> type, @NonNull ErrorHandler handler) Add a custom error handler that matches the given exception type.- Parameters:
type
- Exception type.handler
- Error handler.- Returns:
- This router.
-
error
@NonNull default Router error(@NonNull Predicate<StatusCode> predicate, @NonNull ErrorHandler handler) Add a custom error handler that matches the given predicate.- Parameters:
predicate
- Status code filter.handler
- Error handler.- Returns:
- This router.
-
error
Add a custom error handler.- Parameters:
handler
- Error handler.- Returns:
- This router.
-
getErrorHandler
Get the error handler.- Returns:
- An error handler.
-
getLog
@NonNull org.slf4j.Logger getLog()Application logger.- Returns:
- Application logger.
-
resultHandler
Add a response handler factory.- Parameters:
factory
- Response handler factory.- Returns:
- This router.
-
getRouterOptions
Router options.- Returns:
- Router options.
-
setRouterOptions
Set router options.- Parameters:
options
- router options.- Returns:
- This router.
-
getSessionStore
Session store. Default use a cookie ID with a memory storage.- Returns:
- Session store.
-
setSessionStore
Set session store.- Parameters:
store
- Session store.- Returns:
- This router.
-
executor
Get an executor from application registry.- Parameters:
name
- Executor name.- Returns:
- Executor.
-
executor
Put an executor into the application registry.- Parameters:
name
- Executor's name.executor
- Executor.- Returns:
- This router.
-
getFlashCookie
Template for the flash cookie. Default name is:jooby.flash
.- Returns:
- Template for the flash cookie.
-
setFlashCookie
Sets a cookie used as a template to generate the flash cookie, allowing to customize the cookie name and other cookie parameters.- Parameters:
flashCookie
- The cookie template.- Returns:
- This router.
-
converter
Add a custom string value converter.- Parameters:
converter
- Custom value converter.- Returns:
- This router.
-
getConverters
Get all simple/string value converters.- Returns:
- All simple/string value converters.
-
getBeanConverters
Get all complex/bean value converters.- Returns:
- All complex/bean value converters.
-
getServerOptions
Available server options.- Returns:
- Server options.
-
leadingSlash
Ensure path start with a/
(leading slash).- Parameters:
path
- Path to process.- Returns:
- Path with leading slash.
-
noTrailingSlash
Strip trailing slashes.- Parameters:
path
- Path to process.- Returns:
- Path without trailing slashes.
-
normalizePath
Normalize a path by removing consecutive/
(slashes).- Parameters:
path
- Path to process.- Returns:
- Safe path pattern.
-
pathKeys
Extract path keys from given path pattern. A path key (a.k.a path variable) looks like:/product/{id}
- Parameters:
pattern
- Path pattern.- Returns:
- Path keys.
-
pathKeys
Extract path keys from given path pattern. A path key (a.k.a path variable) looks like:/product/{id}
- Parameters:
pattern
- Path pattern.consumer
- Listen for key and regex variables found.- Returns:
- Path keys.
-
expandOptionalVariables
Look for optional path parameter and expand the given pattern into multiple 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]
- Parameters:
pattern
- Pattern.- Returns:
- One or more patterns.
-
reverse
Recreate a path pattern using the given variables. Variable replacement is done using the current index.- Parameters:
pattern
- Path pattern.values
- Path keys.- Returns:
- Path.
-
reverse
Recreate a path pattern using the given variables.- Parameters:
pattern
- Path pattern.keys
- Path keys.- Returns:
- Path.
-
use(Route.Filter)
.