Interface GroovyChain
-
- All Superinterfaces:
Chain
- All Known Implementing Classes:
GroovyChainAction
public interface GroovyChain extends Chain
A Groovy oriented handler chain builder DSL.The methods specific to this subclass create
Handler
instances from closures and add them to the underlying chain.These methods are generally shortcuts for
all(Handler)
on this underlying chain.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default GroovyChain
all(Closure<?> handler)
Adds the givenClosure
as aHandler
to thisGroovyChain
.default GroovyChain
all(Class<? extends Handler> handler)
GroovyChain
all(Handler handler)
Adds the given handler to this.default Handler
chain(Closure<?> closure)
Creates a handler from the given closure.default GroovyChain
delete(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isDELETE
and thepath
is at the current root.default GroovyChain
delete(Class<? extends Handler> handler)
default GroovyChain
delete(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isDELETE
.default GroovyChain
delete(String path, Class<? extends Handler> handler)
default GroovyChain
delete(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isDELETE
.default GroovyChain
delete(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isDELETE
and thepath
is at the current root.default GroovyChain
files()
Chain.files(Action)
, using the default config.default GroovyChain
files(Closure<?> closure)
default GroovyChain
files(Action<? super FileHandlerSpec> config)
Adds a handler that serves files from the file system.default GroovyChain
fileSystem(String path, Closure<?> handlers)
Creates aList
ofHandler
from the givenClosure
and adds aHandler
to thisGroovyChain
that changes theFileSystemBinding
for theHandler
list.default GroovyChain
fileSystem(String path, Class<? extends Action<? super Chain>> action)
default GroovyChain
fileSystem(String path, Action<? super Chain> action)
Adds a handler to this chain that changes theFileSystemBinding
for the given handler chain.static GroovyChain
from(Chain chain)
Creates a Groovy chain wrapper over a chain instance.default GroovyChain
get(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isGET
and thepath
is at the current root.default GroovyChain
get(Class<? extends Handler> handler)
default GroovyChain
get(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isGET
.default GroovyChain
get(String path, Class<? extends Handler> handler)
default GroovyChain
get(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isGET
.default GroovyChain
get(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isGET
and thepath
is at the current root.default GroovyChain
host(String hostName, Closure<?> handler)
If the request has aHost
header that matches the given host name exactly, handling will be delegated to the chain defined by the given closure.default GroovyChain
host(String hostName, Class<? extends Action<? super Chain>> action)
default GroovyChain
host(String hostName, Action<? super Chain> action)
Adds a handler to the chain that delegates to the given handler chain if the request has aHost
header that matches the given value exactly.default GroovyChain
insert(Closure<?> closure)
Inserts the given nested handler chain.default GroovyChain
insert(Class<? extends Action<? super Chain>> action)
default GroovyChain
insert(Action<? super Chain> action)
Inserts the given nested handler chain.default GroovyChain
notFound()
Raises a 404Context.clientError(int)
.default GroovyChain
onlyIf(Closure<?> test, Closure<?> handler)
default GroovyChain
onlyIf(Closure<?> test, Class<? extends Handler> handler)
default GroovyChain
onlyIf(Closure<?> test, Handler handler)
default GroovyChain
onlyIf(Predicate<? super Context> test, Closure<?> handler)
default GroovyChain
onlyIf(Predicate<? super Context> test, Class<? extends Handler> handler)
default GroovyChain
onlyIf(Predicate<? super Context> test, Handler handler)
Invokes the given handler only if the predicate passes.default GroovyChain
options(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isOPTIONS
and thepath
is at the current root.default GroovyChain
options(Class<? extends Handler> handler)
default GroovyChain
options(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isOPTIONS
.default GroovyChain
options(String path, Class<? extends Handler> handler)
default GroovyChain
options(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isOPTIONS
.default GroovyChain
options(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isOPTIONS
and thepath
is at the current root.default GroovyChain
patch(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPATCH
and thepath
is at the current root.default GroovyChain
patch(Class<? extends Handler> handler)
default GroovyChain
patch(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPATCH
.default GroovyChain
patch(String path, Class<? extends Handler> handler)
default GroovyChain
patch(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPATCH
.default GroovyChain
patch(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPATCH
and thepath
is at the current root.default GroovyChain
path(Closure<?> handler)
default GroovyChain
path(Class<? extends Handler> handler)
default GroovyChain
path(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
exactly.default GroovyChain
path(String path, Class<? extends Handler> handler)
default GroovyChain
path(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
exactly.default GroovyChain
path(Handler handler)
default GroovyChain
post(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPOST
and thepath
is at the current root.default GroovyChain
post(Class<? extends Handler> handler)
default GroovyChain
post(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPOST
.default GroovyChain
post(String path, Class<? extends Handler> handler)
default GroovyChain
post(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPOST
.default GroovyChain
post(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPOST
and thepath
is at the current root.default GroovyChain
prefix(String prefix, Closure<?> chain)
Creates aList
ofHandler
from the givenClosure
and adds aHandler
to thisGroovyChain
that delegates to theHandler
list if the relative path starts with the givenprefix
.default GroovyChain
prefix(String prefix, Class<? extends Action<? super Chain>> action)
default GroovyChain
prefix(String prefix, Action<? super Chain> action)
Adds a handler that delegates to the given handlers if the relative path starts with the givenprefix
.default GroovyChain
put(Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPUT
and thepath
is at the current root.default GroovyChain
put(Class<? extends Handler> handler)
default GroovyChain
put(String path, Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPUT
.default GroovyChain
put(String path, Class<? extends Handler> handler)
default GroovyChain
put(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPUT
.default GroovyChain
put(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPUT
and thepath
is at the current root.default GroovyChain
redirect(int code, String location)
Sends an HTTP redirect to the specified location.default GroovyChain
register(Closure<?> closure)
default GroovyChain
register(Registry registry)
Makes the contents of the given registry available for downstream handlers of the same nesting level.default GroovyChain
register(Registry registry, Closure<?> handlers)
default GroovyChain
register(Registry registry, Class<? extends Action<? super Chain>> action)
default GroovyChain
register(Registry registry, Action<? super Chain> action)
Adds a handler that inserts the given handler chain with the given registry viaContext.insert(Registry, Handler...)
.default GroovyChain
register(Action<? super RegistrySpec> action)
Builds a new registry via the given action, then registers it viaChain.register(Registry)
.default GroovyChain
register(Action<? super RegistrySpec> registryAction, Closure<?> handler)
default GroovyChain
register(Action<? super RegistrySpec> registryAction, Class<? extends Action<? super Chain>> action)
default GroovyChain
register(Action<? super RegistrySpec> registryAction, Action<? super Chain> chainAction)
Adds a handler that inserts the given handler chain with a registry built by the given action viaContext.insert(Registry, Handler...)
.default GroovyChain
when(boolean test, Closure<?> handlers)
Inlines the given handlers iftest
istrue
.default GroovyChain
when(boolean test, Closure<?> ifHandlers, Closure<?> elseHandlers)
Inlines the appropriate handlers based on the giventest
.default GroovyChain
when(boolean test, Class<? extends Action<? super Chain>> action)
Inlines the given chain iftest
istrue
.default GroovyChain
when(boolean test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse)
Inlines the appropriate chain based on the giventest
.default GroovyChain
when(boolean test, Action<? super Chain> action)
Inlines the given chain iftest
istrue
.default GroovyChain
when(boolean test, Action<? super Chain> onTrue, Action<? super Chain> onFalse)
Inlines the appropriate chain based on the giventest
.default GroovyChain
when(Closure<?> test, Closure<?> handlers)
default GroovyChain
when(Closure<?> test, Closure<?> ifHandlers, Closure<?> elseHandlers)
default GroovyChain
when(Closure<?> test, Class<? extends Action<? super Chain>> action)
default GroovyChain
when(Closure<?> test, Class<? extends Action<? super Chain>> ifAction, Class<? extends Action<? super Chain>> elseAction)
default GroovyChain
when(Closure<?> test, Action<? super Chain> chain)
default GroovyChain
when(Closure<?> test, Action<? super Chain> ifChain, Action<? super Chain> elseChain)
default GroovyChain
when(Predicate<? super Context> test, Closure<?> handlers)
default GroovyChain
when(Predicate<? super Context> test, Closure<?> ifHandlers, Closure<?> elseHandlers)
default GroovyChain
when(Predicate<? super Context> test, Class<? extends Action<? super Chain>> action)
default GroovyChain
when(Predicate<? super Context> test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse)
default GroovyChain
when(Predicate<? super Context> test, Action<? super Chain> action)
default GroovyChain
when(Predicate<? super Context> test, Action<? super Chain> onTrue, Action<? super Chain> onFalse)
-
Methods inherited from interface ratpack.core.handling.Chain
chain, chain, getRegistry, getServerConfig
-
-
-
-
Method Detail
-
from
static GroovyChain from(Chain chain)
Creates a Groovy chain wrapper over a chain instance.- Parameters:
chain
- a chain instance- Returns:
- a Groovy wrapper
-
all
default GroovyChain all(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds the givenClosure
as aHandler
to thisGroovyChain
.- Parameters:
handler
- theClosure
to add- Returns:
- this
GroovyChain
-
all
GroovyChain all(Handler handler)
Adds the given handler to this.
-
all
default GroovyChain all(Class<? extends Handler> handler)
-
chain
default Handler chain(@DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> closure) throws Exception
Creates a handler from the given closure.- Parameters:
closure
- a chain definition- Returns:
- a new handler
- Throws:
Exception
- any thrown byclosure
-
delete
default GroovyChain delete(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isDELETE
.See
delete(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
delete
default GroovyChain delete(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isDELETE
and thepath
is at the current root.See
delete(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
-
delete
default GroovyChain delete(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isDELETE
.- Specified by:
delete
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(String, Handler)
,Chain.post(String, Handler)
,Chain.put(String, Handler)
,Chain.patch(String, Handler)
,Chain.path(String, Handler)
-
delete
default GroovyChain delete(String path, Class<? extends Handler> handler)
-
delete
default GroovyChain delete(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isDELETE
and thepath
is at the current root.- Specified by:
delete
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(Handler)
,Chain.post(Handler)
,Chain.put(Handler)
,Chain.patch(Handler)
-
delete
default GroovyChain delete(Class<? extends Handler> handler)
-
fileSystem
default GroovyChain fileSystem(String path, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handlers) throws Exception
Creates aList
ofHandler
from the givenClosure
and adds aHandler
to thisGroovyChain
that changes theFileSystemBinding
for theHandler
list.See
fileSystem(String, Action)
for more details.- Parameters:
path
- the relativepath
to the new file system binding pointhandlers
- the definition of the handler chain- Returns:
- this
GroovyChain
- Throws:
Exception
- any thrown byclosure
-
fileSystem
default GroovyChain fileSystem(String path, Action<? super Chain> action) throws Exception
Adds a handler to this chain that changes theFileSystemBinding
for the given handler chain.- Specified by:
fileSystem
in interfaceChain
- Parameters:
path
- the relative path to the new file system binding pointaction
- the definition of the all chain- Returns:
- this
- Throws:
Exception
- any thrown byaction
-
fileSystem
default GroovyChain fileSystem(String path, Class<? extends Action<? super Chain>> action) throws Exception
- Specified by:
fileSystem
in interfaceChain
- Throws:
Exception
-
files
default GroovyChain files(Action<? super FileHandlerSpec> config) throws Exception
Adds a handler that serves files from the file system.The given action configures how and what files will be served. The handler binds to a
request path
and adirectory
within the current filesystem binding. The portion of the request path past the path binding identifies the target file within the directory.import ratpack.test.embed.EphemeralBaseDir; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EphemeralBaseDir.tmpDir().use(baseDir -> { baseDir.write("public/some.text", "foo"); baseDir.write("public/index.html", "bar"); EmbeddedApp.of(s -> s .serverConfig(c -> c.baseDir(baseDir.getRoot())) .handlers(c -> c .files(f -> f.dir("public").indexFiles("index.html")) ) ).test(httpClient -> { assertEquals("foo", httpClient.getText("some.text")); assertEquals("bar", httpClient.getText()); assertEquals(404, httpClient.get("no-file-here").getStatusCode()); }); }); } }
- Specified by:
files
in interfaceChain
- Parameters:
config
- the file handler configuration- Returns:
this
- Throws:
Exception
- any thrown byconfig
- See Also:
Handlers.files(ServerConfig, Action)
,FileHandlerSpec
-
files
default GroovyChain files()
Description copied from interface:Chain
Chain.files(Action)
, using the default config.
-
files
default GroovyChain files(@DelegatesTo(value=FileHandlerSpec.class,strategy=1) Closure<?> closure) throws Exception
- Throws:
Exception
-
get
default GroovyChain get(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isGET
.- Specified by:
get
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.post(String, Handler)
,Chain.put(String, Handler)
,Chain.patch(String, Handler)
,Chain.delete(String, Handler)
,Chain.path(String, Handler)
-
get
default GroovyChain get(String path, Class<? extends Handler> handler)
-
get
default GroovyChain get(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isGET
and thepath
is at the current root.- Specified by:
get
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.post(Handler)
,Chain.put(Handler)
,Chain.patch(Handler)
,Chain.delete(Handler)
-
get
default GroovyChain get(Class<? extends Handler> handler)
-
get
default GroovyChain get(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isGET
.See
get(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
get
default GroovyChain get(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isGET
and thepath
is at the current root.See
get(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
-
host
default GroovyChain host(String hostName, Action<? super Chain> action) throws Exception
Adds a handler to the chain that delegates to the given handler chain if the request has aHost
header that matches the given value exactly.chain. host("foo.com", new Action<Chain>() { public void execute(Chain hostChain) { hostChain.all(new Handler() { public void handle(Context context) { context.getResponse().send("Host Handler"); } }); } });
-
host
default GroovyChain host(String hostName, Class<? extends Action<? super Chain>> action) throws Exception
-
host
default GroovyChain host(String hostName, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handler) throws Exception
If the request has aHost
header that matches the given host name exactly, handling will be delegated to the chain defined by the given closure.- Parameters:
hostName
- the name of the HTTP Header to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
- Throws:
Exception
- any thrown byclosure
- See Also:
host(String, Action)
-
insert
default GroovyChain insert(Action<? super Chain> action) throws Exception
Inserts the given nested handler chain.Shorter form of
Chain.all(Handler)
handler}(chain
(action
).
-
insert
default GroovyChain insert(Class<? extends Action<? super Chain>> action) throws Exception
-
insert
default GroovyChain insert(@DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> closure) throws Exception
Inserts the given nested handler chain.Shorter form of
all(Handler)
handler}(chain
(closure
).- Parameters:
closure
- the handler chain to insert- Returns:
- this
- Throws:
Exception
- any thrown byclosure
-
patch
default GroovyChain patch(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPATCH
.- Specified by:
patch
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(String, Handler)
,Chain.post(String, Handler)
,Chain.put(String, Handler)
,Chain.delete(String, Handler)
,Chain.path(String, Handler)
-
patch
default GroovyChain patch(String path, Class<? extends Handler> handler)
-
patch
default GroovyChain patch(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPATCH
and thepath
is at the current root.- Specified by:
patch
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(Handler)
,Chain.post(Handler)
,Chain.put(Handler)
,Chain.delete(Handler)
-
patch
default GroovyChain patch(Class<? extends Handler> handler)
-
patch
default GroovyChain patch(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPATCH
.See
put(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
patch
default GroovyChain patch(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPATCH
and thepath
is at the current root.See
put(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
-
options
default GroovyChain options(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isOPTIONS
.- Specified by:
options
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(String, Handler)
,Chain.post(String, Handler)
,Chain.put(String, Handler)
,Chain.delete(String, Handler)
,Chain.path(String, Handler)
-
options
default GroovyChain options(String path, Class<? extends Handler> handler)
-
options
default GroovyChain options(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isOPTIONS
and thepath
is at the current root.- Specified by:
options
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(Handler)
,Chain.post(Handler)
,Chain.put(Handler)
,Chain.delete(Handler)
-
options
default GroovyChain options(Class<? extends Handler> handler)
-
options
default GroovyChain options(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isOPTIONS
.See
put(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
- Since:
- 1.1
-
options
default GroovyChain options(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isOPTIONS
and thepath
is at the current root.See
put(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
- Since:
- 1.1
-
path
default GroovyChain path(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
exactly.Nesting
path
handlers will not work due to the exact matching, use a combination ofpath
andprefix
instead. SeeChain.prefix(String, Action)
for details.// this will not work path("person/:id") { path("child/:childId") { // a request of /person/2/child/1 will not get passed the first all as it will try // to match "person/2/child/1" with "person/2" which does not match } // this will work prefix("person/:id") { path("child/:childId") { // a request of /person/2/child/1 will work this time } }
See
Handlers.path(String, Handler)
for the details on howpath
is interpreted.- Specified by:
path
in interfaceChain
- Parameters:
path
- the relative path to match exactly onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.post(String, Handler)
,Chain.get(String, Handler)
,Chain.put(String, Handler)
,Chain.patch(String, Handler)
,Chain.delete(String, Handler)
-
path
default GroovyChain path(Handler handler)
-
path
default GroovyChain path(String path, Class<? extends Handler> handler)
-
path
default GroovyChain path(Class<? extends Handler> handler)
-
path
default GroovyChain path(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
exactly.See
path(String, Handler)
for more details.- Parameters:
path
- the relative path to match exactly onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
path
default GroovyChain path(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
-
post
default GroovyChain post(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPOST
.- Specified by:
post
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(String, Handler)
,Chain.put(String, Handler)
,Chain.patch(String, Handler)
,Chain.delete(String, Handler)
,Chain.path(String, Handler)
-
post
default GroovyChain post(String path, Class<? extends Handler> handler)
-
post
default GroovyChain post(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPOST
and thepath
is at the current root.- Specified by:
post
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(Handler)
,Chain.put(Handler)
,Chain.patch(Handler)
,Chain.delete(Handler)
-
post
default GroovyChain post(Class<? extends Handler> handler)
-
post
default GroovyChain post(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPOST
.See
post(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
post
default GroovyChain post(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPOST
and thepath
is at the current root.See
post(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
-
prefix
default GroovyChain prefix(String prefix, Action<? super Chain> action) throws Exception
Adds a handler that delegates to the given handlers if the relative path starts with the givenprefix
.All path based handlers become relative to the given
prefix
.chain .prefix("person/:id", new Action<Chain>() { public void execute(Chain personChain) throws Exception { personChain .get("info", new Handler() { public void handle(Context context) { // e.g. /person/2/info } }) .post("save", new Handler() { public void handle(Context context) { // e.g. /person/2/save } }) .prefix("child/:childId", new Action<Chain>() { public void execute(Chain childChain) { childChain .get("info", new Handler() { public void handle(Context context) { // e.g. /person/2/child/1/info } }); } }); } });
See
Handlers.prefix(String, Handler)
for format details on theprefix
string.
-
prefix
default GroovyChain prefix(String prefix, Class<? extends Action<? super Chain>> action) throws Exception
-
prefix
default GroovyChain prefix(String prefix, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> chain) throws Exception
Creates aList
ofHandler
from the givenClosure
and adds aHandler
to thisGroovyChain
that delegates to theHandler
list if the relative path starts with the givenprefix
.See
Chain.prefix(String, Action)
for more details.- Parameters:
prefix
- the relative path to match onchain
- the definition of the chain to delegate to- Returns:
- this
GroovyChain
- Throws:
Exception
- any exception thrown by the given closure
-
put
default GroovyChain put(String path, Handler handler)
Adds a handler that delegates to the given handler if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPUT
.- Specified by:
put
in interfaceChain
- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(String, Handler)
,Chain.post(String, Handler)
,Chain.patch(String, Handler)
,Chain.delete(String, Handler)
,Chain.path(String, Handler)
-
put
default GroovyChain put(String path, Class<? extends Handler> handler)
-
put
default GroovyChain put(Handler handler)
Adds a handler that delegates to the given handler if therequest
HTTPMethod
isPUT
and thepath
is at the current root.- Specified by:
put
in interfaceChain
- Parameters:
handler
- the handler to delegate to- Returns:
- this
- See Also:
Chain.get(Handler)
,Chain.post(Handler)
,Chain.patch(Handler)
,Chain.delete(Handler)
-
put
default GroovyChain put(Class<? extends Handler> handler)
-
put
default GroovyChain put(String path, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if the relativepath
matches the givenpath
and therequest
HTTPMethod
isPUT
.See
put(String, Handler)
for more details.- Parameters:
path
- the relative path to match onhandler
- the handler to delegate to- Returns:
- this
GroovyChain
-
put
default GroovyChain put(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
Adds aHandler
to thisGroovyChain
that delegates to the givenClosure
as aHandler
if therequest
HTTPMethod
isPUT
and thepath
is at the current root.See
put(Handler)
for more details.- Parameters:
handler
- the handler to delegate to- Returns:
- this
GroovyChain
-
redirect
default GroovyChain redirect(int code, String location)
Sends an HTTP redirect to the specified location.The handler to add is created via
Handlers.redirect(int, String)
.- Specified by:
redirect
in interfaceChain
- Parameters:
code
- the 3XX HTTP status code.location
- the URL to set in the Location response header- Returns:
- this
- See Also:
Handlers.redirect(int, String)
-
register
default GroovyChain register(Registry registry)
Makes the contents of the given registry available for downstream handlers of the same nesting level.The registry is inserted via the
Context.next(Registry)
method.
-
register
default GroovyChain register(Action<? super RegistrySpec> action) throws Exception
Builds a new registry via the given action, then registers it viaChain.register(Registry)
.
-
register
default GroovyChain register(Registry registry, Action<? super Chain> action) throws Exception
Adds a handler that inserts the given handler chain with the given registry viaContext.insert(Registry, Handler...)
.
-
register
default GroovyChain register(Registry registry, Class<? extends Action<? super Chain>> action) throws Exception
-
register
default GroovyChain register(Action<? super RegistrySpec> registryAction, Action<? super Chain> chainAction) throws Exception
Adds a handler that inserts the given handler chain with a registry built by the given action viaContext.insert(Registry, Handler...)
.
-
register
default GroovyChain register(Action<? super RegistrySpec> registryAction, Class<? extends Action<? super Chain>> action) throws Exception
-
register
default GroovyChain register(Action<? super RegistrySpec> registryAction, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handler) throws Exception
- Throws:
Exception
-
register
default GroovyChain register(Registry registry, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handlers) throws Exception
- Throws:
Exception
-
register
default GroovyChain register(@DelegatesTo(value=RegistrySpec.class,strategy=1) Closure<?> closure) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(Predicate<? super Context> test, Action<? super Chain> action) throws Exception
-
when
default GroovyChain when(Predicate<? super Context> test, Class<? extends Action<? super Chain>> action) throws Exception
-
when
default GroovyChain when(Predicate<? super Context> test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handlers) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handlers) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Action<? super Chain> chain) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Class<? extends Action<? super Chain>> action) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(boolean test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> handlers) throws Exception
Inlines the given handlers iftest
istrue
.This is literally just sugar for wrapping the given action in an
if
statement. It can be useful when conditionally adding handlers based on state available when building the chain.- Parameters:
test
- whether to include the given chain actionhandlers
- the handlers to maybe include- Returns:
- this
- Throws:
Exception
- any thrown byaction
- Since:
- 1.4
- See Also:
Chain.when(boolean, Action)
-
when
default GroovyChain when(boolean test, Action<? super Chain> action) throws Exception
Inlines the given chain iftest
istrue
.This is literally just sugar for wrapping the given action in an
if
statement. It can be useful when conditionally adding handlers based on state available when building the chain.import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EmbeddedApp.of(a -> a .registryOf(r -> r.add(1)) .handlers(c -> c .when(c.getRegistry().get(Integer.class) == 0, i -> i .get(ctx -> ctx.render("ok")) ) ) ).test(httpClient -> assertEquals(httpClient.get().getStatusCode(), 404) ); EmbeddedApp.of(a -> a .registryOf(r -> r.add(0)) .handlers(c -> c .when(c.getRegistry().get(Integer.class) == 0, i -> i .get(ctx -> ctx.render("ok")) ) ) ).test(httpClient -> assertEquals(httpClient.getText(), "ok") ); } }
-
when
default GroovyChain when(boolean test, Class<? extends Action<? super Chain>> action) throws Exception
Inlines the given chain iftest
istrue
.Similar to
Chain.when(boolean, Action)
, except obtains the action instance from the registry by the given type.
-
when
default GroovyChain when(Predicate<? super Context> test, Action<? super Chain> onTrue, Action<? super Chain> onFalse) throws Exception
-
when
default GroovyChain when(Predicate<? super Context> test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse) throws Exception
-
when
default GroovyChain when(Predicate<? super Context> test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> ifHandlers, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> elseHandlers) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> ifHandlers, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> elseHandlers) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Action<? super Chain> ifChain, Action<? super Chain> elseChain) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Class<? extends Action<? super Chain>> ifAction, Class<? extends Action<? super Chain>> elseAction) throws Exception
- Throws:
Exception
-
when
default GroovyChain when(boolean test, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> ifHandlers, @DelegatesTo(value=GroovyChain.class,strategy=1) Closure<?> elseHandlers) throws Exception
Inlines the appropriate handlers based on the giventest
.A value of
true
will result in the givenifHandlers
being used. A value offalse
will result in the givenelseHandlers
being used.This is literally just sugar for wrapping the given action in an if/else statement. It can be useful when conditionally adding handlers based on state available when building the chain.
- Parameters:
test
- predicate to decide which handlers to includeifHandlers
- the handlers to include when the test is trueelseHandlers
- the handlers to include when the test is false- Returns:
- this
- Throws:
Exception
- any thrown byaction
- Since:
- 1.5
- See Also:
Chain.when(boolean, Action, Action)
-
when
default GroovyChain when(boolean test, Action<? super Chain> onTrue, Action<? super Chain> onFalse) throws Exception
Inlines the appropriate chain based on the giventest
.This is literally just sugar for wrapping the given action in an
if/else
statement. It can be useful when conditionally adding handlers based on state available when building the chain.import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EmbeddedApp.of(a -> a .registryOf(r -> r.add(1)) .handlers(c -> c .when(c.getRegistry().get(Integer.class) == 0, i -> i.get(ctx -> ctx.render("ok")), i -> i.get(ctx -> ctx.render("ko")) ) ) ).test(httpClient -> assertEquals(httpClient.getText(), "ko") ); EmbeddedApp.of(a -> a .registryOf(r -> r.add(0)) .handlers(c -> c .when(c.getRegistry().get(Integer.class) == 0, i -> i.get(ctx -> ctx.render("ok")), i -> i.get(ctx -> ctx.render("ko")) ) ) ).test(httpClient -> assertEquals(httpClient.getText(), "ok") ); } }
-
when
default GroovyChain when(boolean test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse) throws Exception
Inlines the appropriate chain based on the giventest
.Similar to
Chain.when(boolean, Action, Action)
, except obtains the action instance from the registry by the given type.
-
onlyIf
default GroovyChain onlyIf(Predicate<? super Context> test, Handler handler)
Invokes the given handler only if the predicate passes.This method differs from
when()
in that it does not insert the handler; but directly calls itsHandler.handle(Context)
method.
-
onlyIf
default GroovyChain onlyIf(Predicate<? super Context> test, Class<? extends Handler> handler)
-
onlyIf
default GroovyChain onlyIf(Predicate<? super Context> test, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
-
onlyIf
default GroovyChain onlyIf(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, @DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> handler)
-
onlyIf
default GroovyChain onlyIf(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Handler handler)
-
onlyIf
default GroovyChain onlyIf(@DelegatesTo(value=GroovyContext.class,strategy=1) Closure<?> test, Class<? extends Handler> handler)
-
notFound
default GroovyChain notFound()
Raises a 404Context.clientError(int)
.This can be used to effectively terminate processing early. This is sometimes useful when using a scoped client error handler.
import ratpack.core.error.ClientErrorHandler; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static void main(String... args) throws Exception { EmbeddedApp.of(s -> s .registryOf(r -> r .add(ClientErrorHandler.class, (ctx, code) -> ctx.render("global")) ) .handlers(c -> c .prefix("api", api -> api .register(r -> r.add(ClientErrorHandler.class, (ctx, code) -> ctx.render("scoped"))) .get("foo", ctx -> ctx.render("foo")) .notFound() ) ) ).test(http -> { assertEquals(http.getText("not-there"), "global"); assertEquals(http.getText("api/foo"), "foo"); assertEquals(http.getText("api/not-there"), "scoped"); }); } }
-
-