Class Handlers
- java.lang.Object
-
- ratpack.core.handling.Handlers
-
public abstract class Handlers extends Object
Factory methods for handler decorations.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Handler
accepts(String... contentTypes)
A handler that delegates to the next handler if the request claims that it can accept one of the given types, otherwise raises a 406 client error.static Handler
byContent(Registry registry, Action<? super ByContentSpec> action)
Builds a content negotiating handler.static Handler
byMethod(Registry registry, Action<? super ByMethodSpec> action)
Builds a multi method handler.static Handler
chain(List<? extends Handler> handlers)
Creates a handler chain from the given handlers.static Handler
chain(Handler... handlers)
Creates a handler chain from the given handlers.static Handler
chain(ServerConfig serverConfig, Registry registry, Action<? super Chain> action)
Builds a chain, backed by the given registry.static Handler
chain(ServerConfig serverConfig, Action<? super Chain> action)
Builds a handler chain, with no backing registry.static Handler
chain(Registry registry, Action<? super Chain> action)
Builds a chain, backed by the given registry.static Handler
clientError(int statusCode)
A handler that simply callsContext.clientError(int)
with the given status code.static Handler
contentTypes(String... contentTypes)
A handler that delegates to the next handler if the content type of the request is one of the given types, otherwise raises a 415 client error.static Handler
delete()
A handler that delegates to the next handler if the request is DELETE, otherwise raises a 405 client error.static Handler
files(ServerConfig serverConfig, Action<? super FileHandlerSpec> config)
Creates a handler that serves files from the file system.static Handler
fileSystem(ServerConfig serverConfig, String path, Handler handler)
A handlers that changes theFileSystemBinding
for the given handlers.static Handler
get()
A handler that delegates to the next handler if the request is GET, otherwise raises a 405 client error.static Handler
next()
A handler that simply delegates to the next handler.static Handler
notFound()
Convenience forclientError(404)
.static Handler
of(Block block)
Creates a handler from the given blockstatic Handler
onlyIf(Predicate<? super Context> test, Handler handler)
Creates a handler that delegates to the given handler if the predicate applies to the context.static Handler
options()
A handler that delegates to the next handler if the request is OPTIONS, otherwise raises a 405 client error.static Handler
patch()
A handler that delegates to the next handler if the request is PATCH, otherwise raises a 405 client error.static Handler
path(String path, Handler handler)
Creates a handler that delegates to the given handlers if the request matches the given path exactly.static Handler
path(PathBinder pathBinder, Handler handler)
Creates a handler that delegates to the given handlers if the request can be bound by the given path binder.static Handler
post()
A handler that delegates to the next handler if the request is POST, otherwise raises a 405 client error.static Handler
prefix(String prefix, Handler handler)
Creates a handler that delegates to the given handlers if the request path starts with the given prefix.static Handler
put()
A handler that delegates to the next handler if the request is PUT, otherwise raises a 405 client error.static Handler
redirect(int code, String location)
Creates a handler that always issues a redirect usingContext.redirect(int, Object)
with exactly the given code and location.static Handler
register(Registry registry)
A handler that simply callsContext.next(Registry)
with the given registry.static Handler
register(Registry registry, Handler handler)
A handler that simply callsContext.insert(Registry, Handler...)
with the given registry and handler.static Handler
when(Predicate<? super Context> test, Handler handler)
Creates a handler that inserts and delegates the given handler if the predicate applies to the context.static Handler
whenOrElse(Predicate<? super Context> test, Handler ifHandler, Handler elseHandler)
Creates a handler that inserts and delegates to the appropriate handler depending if the predicate applies to the context.
-
-
-
Method Detail
-
accepts
public static Handler accepts(String... contentTypes)
A handler that delegates to the next handler if the request claims that it can accept one of the given types, otherwise raises a 406 client error.- Parameters:
contentTypes
- The content types to verify that the request can support for the response- Returns:
- A handler
-
files
public static Handler files(ServerConfig serverConfig, Action<? super FileHandlerSpec> config) throws Exception
Creates a handler that serves files from the file system.This method is a standalone version of
Chain.files(Action)
.- Parameters:
serverConfig
- the server configconfig
- the configuration of the file handler- Returns:
- a file serving handler
- Throws:
Exception
- any thrown byconfig
-
chain
public static Handler chain(ServerConfig serverConfig, Action<? super Chain> action) throws Exception
Builds a handler chain, with no backing registry.- Parameters:
serverConfig
- The server configaction
- The chain definition- Returns:
- A handler
- Throws:
Exception
- any thrown byaction
-
chain
public static Handler chain(@Nullable ServerConfig serverConfig, @Nullable Registry registry, Action<? super Chain> action) throws Exception
Builds a chain, backed by the given registry.- Parameters:
serverConfig
- The server configregistry
- The registry.action
- The chain building action.- Returns:
- A handler
- Throws:
Exception
- any thrown byaction
-
chain
public static Handler chain(Registry registry, Action<? super Chain> action) throws Exception
Builds a chain, backed by the given registry.- Parameters:
registry
- The registry.action
- The chain building action.- Returns:
- A handler
- Throws:
Exception
- any thrown byaction
-
chain
public static Handler chain(List<? extends Handler> handlers)
Creates a handler chain from the given handlers.- Parameters:
handlers
- The handlers to connect into a chain- Returns:
- A new handler that is the given handlers connected into a chain
-
chain
public static Handler chain(Handler... handlers)
Creates a handler chain from the given handlers.- Parameters:
handlers
- The handlers to connect into a chain- Returns:
- A new handler that is the given handlers connected into a chain
-
clientError
public static Handler clientError(int statusCode)
A handler that simply callsContext.clientError(int)
with the given status code.- Parameters:
statusCode
- The 4xx client error status code- Returns:
- A handler
-
contentTypes
public static Handler contentTypes(String... contentTypes)
A handler that delegates to the next handler if the content type of the request is one of the given types, otherwise raises a 415 client error.- Parameters:
contentTypes
- The request content types to require- Returns:
- A handler
-
delete
public static Handler delete()
A handler that delegates to the next handler if the request is DELETE, otherwise raises a 405 client error.- Returns:
- A handler
-
fileSystem
public static Handler fileSystem(ServerConfig serverConfig, String path, Handler handler)
A handlers that changes theFileSystemBinding
for the given handlers.The new file system binding will be created by the
FileSystemBinding.binding(String)
method of the contextual binding.- Parameters:
serverConfig
- The application server configpath
- The relative path to the new file system binding pointhandler
- The handler to execute with the new file system binding- Returns:
- A handler
-
get
public static Handler get()
A handler that delegates to the next handler if the request is GET, otherwise raises a 405 client error.- Returns:
- A handler
-
next
public static Handler next()
A handler that simply delegates to the next handler.Effectively a noop.
- Returns:
- A handler
-
notFound
public static Handler notFound()
Convenience forclientError(404)
.- Returns:
- A handler
-
patch
public static Handler patch()
A handler that delegates to the next handler if the request is PATCH, otherwise raises a 405 client error.- Returns:
- A handler
-
options
public static Handler options()
A handler that delegates to the next handler if the request is OPTIONS, otherwise raises a 405 client error.- Returns:
- A handler
- Since:
- 1.1
-
path
public static Handler path(String path, Handler handler)
Creates a handler that delegates to the given handlers if the request matches the given path exactly.The
path
is relative to the contextualPathBinding
of the exchange.A new contextual
PathBinding
will be established for the given handlers, using the given path as the bind point.- Parameters:
path
- The exact path to match tohandler
- The handlers to delegate to if the path matches- Returns:
- A handler
-
path
public static Handler path(PathBinder pathBinder, Handler handler)
Creates a handler that delegates to the given handlers if the request can be bound by the given path binder.- Parameters:
pathBinder
- The path binder that may bind to the request pathhandler
- The handlers to delegate to if path binder does bind to the path- Returns:
- A handler
-
post
public static Handler post()
A handler that delegates to the next handler if the request is POST, otherwise raises a 405 client error.- Returns:
- A handler
-
prefix
public static Handler prefix(String prefix, Handler handler)
Creates a handler that delegates to the given handlers if the request path starts with the given prefix.The
prefix
is relative to the contextualPathBinding
of the exchange.A new contextual
PathBinding
will be established for the given handlers, using the given prefix as the bind point.- Parameters:
prefix
- The path prefix to matchhandler
- The handler to delegate to- Returns:
- A handler
-
put
public static Handler put()
A handler that delegates to the next handler if the request is PUT, otherwise raises a 405 client error.- Returns:
- A handler
-
register
public static Handler register(Registry registry, Handler handler)
A handler that simply callsContext.insert(Registry, Handler...)
with the given registry and handler.- Parameters:
registry
- the registry to inserthandler
- The handler to insert- Returns:
- A handler
-
register
public static Handler register(Registry registry)
A handler that simply callsContext.next(Registry)
with the given registry.- Parameters:
registry
- The registry to make available to the next handlers- Returns:
- A handler
- See Also:
Context.next(Registry)
-
redirect
public static Handler redirect(int code, String location)
Creates a handler that always issues a redirect usingContext.redirect(int, Object)
with exactly the given code and location.This method will immediate throw an
IllegalArgumentException
if code is < 300 || > 399.- Parameters:
code
- the 3XX HTTP status codelocation
- the URL to set in the Location response header- Returns:
- a handler
- See Also:
Context.redirect(int, Object)
-
when
public static Handler when(Predicate<? super Context> test, Handler handler)
Creates a handler that inserts and delegates the given handler if the predicate applies to the context.If the predicate does not apply, calls
Context.next()
.- Parameters:
test
- the test whether to when to the given handlerhandler
- the handler to insert if the predicate applies- Returns:
- a handler
-
whenOrElse
public static Handler whenOrElse(Predicate<? super Context> test, Handler ifHandler, Handler elseHandler)
Creates a handler that inserts and delegates to the appropriate handler depending if the predicate applies to the context.- Parameters:
test
- the test whether to delegate to the appropriate handlerifHandler
- the handler to insert if the predicate applieselseHandler
- the handler to insert if the predicate doesn't apply- Returns:
- a handler
- Since:
- 1.5
-
onlyIf
public static Handler onlyIf(Predicate<? super Context> test, Handler handler)
Creates a handler that delegates to the given handler if the predicate applies to the context.If the predicate does not apply, calls
Context.next()
.This method does not
insert
the handler aswhen(Predicate, Handler)
does; it calls itsHandler.handle(Context)
method directly- Parameters:
test
- the test whether to when to the given handlerhandler
- the handler to call if the predicate applies- Returns:
- a handler
-
of
public static Handler of(Block block)
Creates a handler from the given blockThe created handler simply invokes the block.
- Parameters:
block
- the block to invoke- Returns:
- a handler
- Since:
- 1.5
-
byContent
public static Handler byContent(Registry registry, Action<? super ByContentSpec> action) throws Exception
Builds a content negotiating handler.- Parameters:
registry
- the registry to obtain handlers from for by-class lookupsaction
- the spec action- Returns:
- a content negotiating handler
- Throws:
Exception
- any thrown byaction
- Since:
- 1.5
-
byMethod
public static Handler byMethod(Registry registry, Action<? super ByMethodSpec> action) throws Exception
Builds a multi method handler.- Parameters:
registry
- the registry to obtain handlers from for by-class lookupsaction
- the spec action- Returns:
- a multi method handler
- Throws:
Exception
- any thrown byaction
- Since:
- 1.5
-
-