Router.Match
Constructor and Description |
---|
Jooby()
Creates a new Jooby instance.
|
Modifier and Type | Method and Description |
---|---|
Jooby |
after(Route.After after)
Add an after route decorator to the route pipeline.
|
<T> T |
attribute(String key)
Get an attribute by his key.
|
Jooby |
attribute(String key,
Object value)
Set an application attribute.
|
Jooby |
before(Route.Before before)
Add a before route decorator to the route pipeline.
|
Jooby |
converter(BeanConverter converter)
Add a custom bean value converter.
|
Jooby |
converter(ValueConverter converter)
Add a custom string value converter.
|
static Jooby |
createApp(String[] args,
ExecutionMode executionMode,
Class<? extends Jooby> applicationType)
Setup default environment, logging (logback or log4j2) and run application.
|
static Jooby |
createApp(String[] args,
ExecutionMode executionMode,
Supplier<Jooby> provider)
Setup default environment, logging (logback or log4j2) and run application.
|
Jooby |
decoder(MediaType contentType,
MessageDecoder decoder)
Register a decoder for the given content type.
|
Jooby |
decorator(Route.Decorator decorator)
Add a route decorator to the route pipeline.
|
Jooby |
dispatch(Executor executor,
Runnable action)
Dispatch route pipeline to the given executor.
|
Jooby |
dispatch(Runnable body)
Dispatch route pipeline to the
Router.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.
|
Jooby |
encoder(MediaType contentType,
MessageEncoder encoder)
Register a route response encoder.
|
Jooby |
encoder(MessageEncoder encoder)
Register a route response encoder.
|
Jooby |
error(ErrorHandler handler)
Add a custom error handler.
|
Jooby |
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.
|
Jooby |
executor(String name,
Executor executor)
Put an executor into the application registry.
|
Map<String,Object> |
getAttributes()
Mutable map of application attributes.
|
String |
getBasePackage()
Get base application package.
|
List<BeanConverter> |
getBeanConverters()
Get all complex/bean value converters.
|
ClassLoader |
getClassLoader()
Application class loader.
|
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.
|
ExecutionMode |
getExecutionMode()
Application execution mode.
|
String |
getFlashCookie()
Name of the flash cookie.
|
org.slf4j.Logger |
getLog()
Application logger.
|
String |
getName()
Get application's name.
|
Router |
getRouter()
The underlying router.
|
Set<RouterOption> |
getRouterOptions()
Router options.
|
List<Route> |
getRoutes()
Returns all routes.
|
ServerOptions |
getServerOptions()
Server options or
null . |
ServiceRegistry |
getServices()
Application service registry.
|
SessionStore |
getSessionStore()
Session store.
|
Path |
getTmpdir()
Application temporary directory.
|
String |
getVersion()
Get application version (description/debugging purpose only).
|
Executor |
getWorker()
Returns the worker thread pool.
|
Jooby |
install(Extension extension)
Install extension module.
|
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).
|
Router.Match |
match(Context ctx)
Find a matching route using the given context.
|
boolean |
match(String pattern,
String path)
Find a matching route using the given context.
|
Jooby |
mvc(Class router)
Import all route method from the given controller class.
|
<T> Jooby |
mvc(Class<T> router,
javax.inject.Provider<T> provider)
Import all route method from the given controller class.
|
Jooby |
mvc(Object router)
Import all route methods from given controller instance.
|
Jooby |
onStarted(SneakyThrows.Runnable body)
Event is fire once all components has been initialized, for example router and web-server
are up and running, extension installed, etc...
|
Jooby |
onStarting(SneakyThrows.Runnable body)
Event fired before starting router and web-server.
|
Jooby |
onStop(AutoCloseable body)
Stop event is fire at application shutdown time.
|
RouteSet |
path(String pattern,
Runnable action)
Group one or more routes under a common path prefix.
|
Jooby |
ready(Server server)
Callback method that indicates application was successfully started it and listening for
connections.
|
Jooby |
registry(Registry registry)
Set application registry.
|
<T> T |
require(Class<T> type)
Provides an instance of the given type.
|
<T> T |
require(Class<T> type,
String name)
Provides an instance of the given type where name matches it.
|
<T> T |
require(ServiceKey<T> key)
Provides an instance of the given type.
|
Jooby |
responseHandler(ResponseHandler handler)
Add a response handler factory.
|
Route |
route(String method,
String pattern,
Route.Handler handler)
Add a route.
|
RouteSet |
routes(Runnable action)
Group one or more routes.
|
static void |
runApp(String[] args,
Class<? extends Jooby> applicationType)
Setup default environment, logging (logback or log4j2) and run application.
|
static void |
runApp(String[] args,
Consumer<Jooby> consumer)
Setup default environment, logging (logback or log4j2) and run application.
|
static void |
runApp(String[] args,
ExecutionMode executionMode,
Class<? extends Jooby> applicationType)
Setup default environment, logging (logback or log4j2) and run application.
|
static void |
runApp(String[] args,
ExecutionMode executionMode,
Consumer<Jooby> consumer)
Setup default environment, logging (logback or log4j2) and run application.
|
static void |
runApp(String[] args,
ExecutionMode executionMode,
Supplier<Jooby> provider)
Setup default environment, logging (logback or log4j2) and run application.
|
static void |
runApp(String[] args,
Supplier<Jooby> provider)
Setup default environment, logging (logback or log4j2) and run application.
|
Jooby |
setContextPath(String basePath)
Set application context path.
|
Jooby |
setDefaultWorker(Executor worker)
Set the default worker thread pool.
|
Jooby |
setEnvironment(Environment environment)
Set application environment.
|
Environment |
setEnvironmentOptions(EnvironmentOptions options)
Set environment options and initialize/overrides the environment.
|
Jooby |
setExecutionMode(ExecutionMode mode)
Set application execution mode.
|
Jooby |
setFlashCookie(String name)
Set flash cookie name.
|
Jooby |
setHiddenMethod(Function<Context,Optional<String>> provider)
Provides a way to override the current HTTP method using lookup strategy.
|
Jooby |
setHiddenMethod(String parameterName)
Provides a way to override the current HTTP method.
|
Jooby |
setLateInit(boolean lateInit)
Force all module to be initialized lazily at application startup time (not at
creation/instantiation time).
|
Jooby |
setName(String name)
Set application name.
|
Jooby |
setRouterOptions(RouterOption... options)
Set router options.
|
Jooby |
setServerOptions(ServerOptions serverOptions)
Set server options.
|
Jooby |
setSessionStore(SessionStore store)
Set session store.
|
Jooby |
setTmpdir(Path tmpdir)
Set application temporary directory.
|
Jooby |
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).
|
Jooby |
setVersion(String version)
Set application version.
|
Jooby |
setWorker(Executor worker)
Set a worker thread pool.
|
Route |
sse(String pattern,
ServerSentEmitter.Handler handler)
Add a server-sent event handler.
|
Server |
start()
Start application, find a web server, deploy application, start router, extension modules,
etc..
|
Jooby |
start(Server server)
Call back method that indicates application was deploy it in the given server.
|
Jooby |
stop()
Stop application, fire the stop event to cleanup resources.
|
String |
toString() |
Jooby |
use(Predicate<Context> predicate,
Router subrouter)
Import routes from given router.
|
RouteSet |
use(Predicate<Context> predicate,
Runnable body)
Import routes from given action.
|
Jooby |
use(Router router)
Import all routes from the given router.
|
Jooby |
use(String path,
Router router)
Import all routes from the given router and prefix them with the given path.
|
Route |
ws(String pattern,
WebSocket.Initializer handler)
Add a websocket handler.
|
@Nullable public ServerOptions getServerOptions()
null
.getServerOptions
in interface Router
null
.@Nonnull public Jooby setServerOptions(@Nonnull ServerOptions serverOptions)
serverOptions
- Server options.@Nonnull public Set<RouterOption> getRouterOptions()
Router
getRouterOptions
in interface Router
@Nonnull public Jooby setRouterOptions(@Nonnull RouterOption... options)
Router
setRouterOptions
in interface Router
options
- router options.@Nonnull public Environment getEnvironment()
Environment.loadEnvironment(EnvironmentOptions)
.@Nonnull public ClassLoader getClassLoader()
@Nonnull public com.typesafe.config.Config getConfig()
Environment.getConfig()
.@Nonnull public Jooby setEnvironment(@Nonnull Environment environment)
environment
- Application environment.@Nonnull public Environment setEnvironmentOptions(@Nonnull EnvironmentOptions options)
options
- Environment options.@Nonnull public Jooby onStarting(@Nonnull SneakyThrows.Runnable body)
body
- Start body.@Nonnull public Jooby onStarted(@Nonnull SneakyThrows.Runnable body)
body
- Start body.@Nonnull public Jooby onStop(@Nonnull AutoCloseable body)
body
- Stop body.@Nonnull public Jooby setContextPath(@Nonnull String basePath)
Router
/
.setContextPath
in interface Router
basePath
- Context path.@Nonnull public String getContextPath()
Router
getContextPath
in interface Router
@Nonnull public Jooby use(@Nonnull Router router)
Router
public boolean isTrustProxy()
Router
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)
.isTrustProxy
in interface Router
@Nonnull public Jooby setTrustProxy(boolean trustProxy)
Router
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)
.setTrustProxy
in interface Router
trustProxy
- True to enabled.@Nonnull public Router domain(@Nonnull String domain, @Nonnull Router subrouter)
Router
host
header.
{
domain("foo.com", new FooApp());
domain("bar.com", new BarApp());
}
NOTE: if you run behind a reverse proxy you might to enabled Router.setTrustProxy(boolean)
.@Nonnull public RouteSet domain(@Nonnull String domain, @Nonnull Runnable body)
Router
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 Router.setTrustProxy(boolean)
.@Nonnull public RouteSet use(@Nonnull Predicate<Context> predicate, @Nonnull Runnable body)
Router
{
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 Router.setTrustProxy(boolean)
.@Nonnull public Jooby use(@Nonnull Predicate<Context> predicate, @Nonnull Router subrouter)
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 Router.setTrustProxy(boolean)
.@Nonnull public Jooby use(@Nonnull String path, @Nonnull Router router)
Router
@Nonnull public Jooby mvc(@Nonnull Object router)
Router
@Nonnull public Jooby mvc(@Nonnull Class router)
Router
require(Class)
.@Nonnull public <T> Jooby mvc(@Nonnull Class<T> router, @Nonnull javax.inject.Provider<T> provider)
Router
@Nonnull public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler)
Router
@Nonnull public Route sse(@Nonnull String pattern, @Nonnull ServerSentEmitter.Handler handler)
Router
@Nonnull public List<Route> getRoutes()
Router
@Nonnull public Jooby error(@Nonnull ErrorHandler handler)
Router
@Nonnull public Jooby decorator(@Nonnull Route.Decorator decorator)
Router
@Nonnull public Jooby before(@Nonnull Route.Before before)
Router
@Nonnull public Jooby after(@Nonnull Route.After after)
Router
@Nonnull public Jooby encoder(@Nonnull MessageEncoder encoder)
Router
@Nonnull public Jooby decoder(@Nonnull MediaType contentType, @Nonnull MessageDecoder decoder)
Router
@Nonnull public Jooby encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder encoder)
Router
@Nonnull public Jooby install(@Nonnull Extension extension)
extension
- Extension module.@Nonnull public Jooby dispatch(@Nonnull Runnable body)
Router
Router.getWorker()
worker thread pool. After dispatch
application code is allowed to do blocking calls.@Nonnull public Jooby dispatch(@Nonnull Executor executor, @Nonnull Runnable action)
Router
dispatch
in interface Router
executor
- Executor. ExecutorService
instances automatically
shutdown at application exit.action
- Dispatch body.@Nonnull public RouteSet path(@Nonnull String pattern, @Nonnull Runnable action)
Router
@Nonnull public RouteSet routes(@Nonnull Runnable action)
Router
@Nonnull public Route route(@Nonnull String method, @Nonnull String pattern, @Nonnull Route.Handler handler)
Router
@Nonnull public Router.Match match(@Nonnull Context ctx)
Router
404
handler.
See Route.NOT_FOUND
.public boolean match(@Nonnull String pattern, @Nonnull String path)
Router
404
handler.
See Route.NOT_FOUND
.@Nonnull public Jooby errorCode(@Nonnull Class<? extends Throwable> type, @Nonnull StatusCode statusCode)
Router
@Nonnull public StatusCode errorCode(@Nonnull Throwable cause)
Router
@Nonnull public Executor getWorker()
Router
@Nonnull public Jooby setWorker(@Nonnull Executor worker)
Router
@Nonnull public Jooby setDefaultWorker(@Nonnull Executor worker)
Router
Router.getWorker()
returns the default thread pool, unless you explicitly set one.setDefaultWorker
in interface Router
worker
- Default worker thread pool.@Nonnull public org.slf4j.Logger getLog()
Router
@Nonnull public Jooby responseHandler(ResponseHandler handler)
Router
responseHandler
in interface Router
handler
- Response handler factory.@Nonnull public ErrorHandler getErrorHandler()
Router
getErrorHandler
in interface Router
@Nonnull public Path getTmpdir()
Router
Environment
when isn't
set manually.@Nonnull public Jooby setTmpdir(@Nonnull Path tmpdir)
tmpdir
- Temp directory.@Nonnull public ExecutionMode getExecutionMode()
@Nonnull public Jooby setExecutionMode(@Nonnull ExecutionMode mode)
mode
- Application execution mode.@Nonnull public Map<String,Object> getAttributes()
Router
getAttributes
in interface Router
@Nonnull public Jooby attribute(@Nonnull String key, @Nonnull Object value)
Router
@Nonnull public <T> T attribute(@Nonnull String key)
Router
Router.getAttributes()
.@Nonnull public <T> T require(@Nonnull Class<T> type, @Nonnull String name)
Registry
@Nonnull public <T> T require(@Nonnull Class<T> type)
Registry
@Nonnull public <T> T require(@Nonnull ServiceKey<T> key)
Registry
@Nonnull public Jooby registry(@Nonnull Registry registry)
registry
- Application registry.@Nonnull public ServiceRegistry getServices()
Router
require(Class)
calls.
This method returns a mutable registry. You are free to modify/alter the registry.getServices
in interface Router
@Nullable public String getBasePackage()
@Nonnull public SessionStore getSessionStore()
Router
SessionStore.memory()
.getSessionStore
in interface Router
@Nonnull public Jooby setSessionStore(@Nonnull SessionStore store)
Router
setSessionStore
in interface Router
store
- Session store.@Nonnull public Jooby executor(@Nonnull String name, @Nonnull Executor executor)
Router
@Nonnull public String getFlashCookie()
Router
jooby.flash
.getFlashCookie
in interface Router
jooby.flash
.@Nonnull public Jooby setFlashCookie(@Nonnull String name)
Router
setFlashCookie
in interface Router
name
- Flash cookie name.@Nonnull public Jooby converter(@Nonnull ValueConverter converter)
Router
@Nonnull public Jooby converter(@Nonnull BeanConverter converter)
Router
@Nonnull public List<ValueConverter> getConverters()
Router
getConverters
in interface Router
@Nonnull public List<BeanConverter> getBeanConverters()
Router
getBeanConverters
in interface Router
@Nonnull public Jooby setHiddenMethod(@Nonnull Function<Context,Optional<String>> provider)
Router
setHiddenMethod
in interface Router
provider
- Lookup strategy.@Nonnull public Jooby setHiddenMethod(@Nonnull String parameterName)
Router
Router.setHiddenMethod(Function)
method.setHiddenMethod
in interface Router
parameterName
- Form field name.@Nonnull public Server start()
@Nonnull public Jooby start(@Nonnull Server server)
server
- Server.@Nonnull public Jooby ready(@Nonnull Server server)
server
- Server.@Nonnull public Jooby stop()
Server.stop()
using a shutdown hook.
The next example shows how to successfully stop the web server and application:
Jooby app = new Jooby();
Server server = app.start();
...
server.stop();
public Jooby setLateInit(boolean lateInit)
lateInit
- True for late init.@Nonnull public String getName()
Package.getImplementationTitle()
.
- Otherwise fallback to class name.@Nonnull public Jooby setName(@Nonnull String name)
name
- Application's name.@Nonnull public String getVersion()
Package.getImplementationVersion()
. This attribute is present
when application has been packaged (jar file).
- Otherwise, fallback to 0.0.0
.@Nonnull public Jooby setVersion(@Nonnull String version)
version
- Application's version.public static void runApp(@Nonnull String[] args, @Nonnull Class<? extends Jooby> applicationType)
args
- Application arguments.applicationType
- Application type.public static void runApp(@Nonnull String[] args, @Nonnull ExecutionMode executionMode, @Nonnull Class<? extends Jooby> applicationType)
args
- Application arguments.executionMode
- Application execution mode.applicationType
- Application type.public static Jooby createApp(@Nonnull String[] args, @Nonnull ExecutionMode executionMode, @Nonnull Class<? extends Jooby> applicationType)
args
- Application arguments.executionMode
- Application execution mode.applicationType
- Application type.public static void runApp(@Nonnull String[] args, @Nonnull Supplier<Jooby> provider)
args
- Application arguments.provider
- Application provider.public static void runApp(@Nonnull String[] args, @Nonnull Consumer<Jooby> consumer)
args
- Application arguments.consumer
- Application consumer.public static void runApp(@Nonnull String[] args, @Nonnull ExecutionMode executionMode, @Nonnull Consumer<Jooby> consumer)
args
- Application arguments.executionMode
- Application execution mode.consumer
- Application consumer.public static void runApp(@Nonnull String[] args, @Nonnull ExecutionMode executionMode, @Nonnull Supplier<Jooby> provider)
args
- Application arguments.executionMode
- Application execution mode.provider
- Application provider.public static Jooby createApp(@Nonnull String[] args, @Nonnull ExecutionMode executionMode, @Nonnull Supplier<Jooby> provider)
args
- Application arguments.executionMode
- Application execution mode.provider
- Application provider.Copyright © 2020. All rights reserved.