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
Handlerinstances 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 GroovyChainall(Closure<?> handler)Adds the givenClosureas aHandlerto thisGroovyChain.default GroovyChainall(Class<? extends Handler> handler)GroovyChainall(Handler handler)Adds the given handler to this.default Handlerchain(Closure<?> closure)Creates a handler from the given closure.default GroovyChaindelete(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisDELETEand thepathis at the current root.default GroovyChaindelete(Class<? extends Handler> handler)default GroovyChaindelete(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisDELETE.default GroovyChaindelete(String path, Class<? extends Handler> handler)default GroovyChaindelete(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisDELETE.default GroovyChaindelete(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisDELETEand thepathis at the current root.default GroovyChainfiles()Chain.files(Action), using the default config.default GroovyChainfiles(Closure<?> closure)default GroovyChainfiles(Action<? super FileHandlerSpec> config)Adds a handler that serves files from the file system.default GroovyChainfileSystem(String path, Closure<?> handlers)Creates aListofHandlerfrom the givenClosureand adds aHandlerto thisGroovyChainthat changes theFileSystemBindingfor theHandlerlist.default GroovyChainfileSystem(String path, Class<? extends Action<? super Chain>> action)default GroovyChainfileSystem(String path, Action<? super Chain> action)Adds a handler to this chain that changes theFileSystemBindingfor the given handler chain.static GroovyChainfrom(Chain chain)Creates a Groovy chain wrapper over a chain instance.default GroovyChainget(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisGETand thepathis at the current root.default GroovyChainget(Class<? extends Handler> handler)default GroovyChainget(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisGET.default GroovyChainget(String path, Class<? extends Handler> handler)default GroovyChainget(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisGET.default GroovyChainget(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisGETand thepathis at the current root.default GroovyChainhost(String hostName, Closure<?> handler)If the request has aHostheader that matches the given host name exactly, handling will be delegated to the chain defined by the given closure.default GroovyChainhost(String hostName, Class<? extends Action<? super Chain>> action)default GroovyChainhost(String hostName, Action<? super Chain> action)Adds a handler to the chain that delegates to the given handler chain if the request has aHostheader that matches the given value exactly.default GroovyChaininsert(Closure<?> closure)Inserts the given nested handler chain.default GroovyChaininsert(Class<? extends Action<? super Chain>> action)default GroovyChaininsert(Action<? super Chain> action)Inserts the given nested handler chain.default GroovyChainnotFound()Raises a 404Context.clientError(int).default GroovyChainonlyIf(Closure<?> test, Closure<?> handler)default GroovyChainonlyIf(Closure<?> test, Class<? extends Handler> handler)default GroovyChainonlyIf(Closure<?> test, Handler handler)default GroovyChainonlyIf(Predicate<? super Context> test, Closure<?> handler)default GroovyChainonlyIf(Predicate<? super Context> test, Class<? extends Handler> handler)default GroovyChainonlyIf(Predicate<? super Context> test, Handler handler)Invokes the given handler only if the predicate passes.default GroovyChainoptions(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisOPTIONSand thepathis at the current root.default GroovyChainoptions(Class<? extends Handler> handler)default GroovyChainoptions(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisOPTIONS.default GroovyChainoptions(String path, Class<? extends Handler> handler)default GroovyChainoptions(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisOPTIONS.default GroovyChainoptions(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisOPTIONSand thepathis at the current root.default GroovyChainpatch(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPATCHand thepathis at the current root.default GroovyChainpatch(Class<? extends Handler> handler)default GroovyChainpatch(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPATCH.default GroovyChainpatch(String path, Class<? extends Handler> handler)default GroovyChainpatch(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisPATCH.default GroovyChainpatch(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisPATCHand thepathis at the current root.default GroovyChainpath(Closure<?> handler)default GroovyChainpath(Class<? extends Handler> handler)default GroovyChainpath(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathexactly.default GroovyChainpath(String path, Class<? extends Handler> handler)default GroovyChainpath(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathexactly.default GroovyChainpath(Handler handler)default GroovyChainpost(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPOSTand thepathis at the current root.default GroovyChainpost(Class<? extends Handler> handler)default GroovyChainpost(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPOST.default GroovyChainpost(String path, Class<? extends Handler> handler)default GroovyChainpost(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisPOST.default GroovyChainpost(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisPOSTand thepathis at the current root.default GroovyChainprefix(String prefix, Closure<?> chain)Creates aListofHandlerfrom the givenClosureand adds aHandlerto thisGroovyChainthat delegates to theHandlerlist if the relative path starts with the givenprefix.default GroovyChainprefix(String prefix, Class<? extends Action<? super Chain>> action)default GroovyChainprefix(String prefix, Action<? super Chain> action)Adds a handler that delegates to the given handlers if the relative path starts with the givenprefix.default GroovyChainput(Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPUTand thepathis at the current root.default GroovyChainput(Class<? extends Handler> handler)default GroovyChainput(String path, Closure<?> handler)Adds aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPUT.default GroovyChainput(String path, Class<? extends Handler> handler)default GroovyChainput(String path, Handler handler)Adds a handler that delegates to the given handler if the relativepathmatches the givenpathand therequestHTTPMethodisPUT.default GroovyChainput(Handler handler)Adds a handler that delegates to the given handler if therequestHTTPMethodisPUTand thepathis at the current root.default GroovyChainredirect(int code, String location)Sends an HTTP redirect to the specified location.default GroovyChainregister(Closure<?> closure)default GroovyChainregister(Registry registry)Makes the contents of the given registry available for downstream handlers of the same nesting level.default GroovyChainregister(Registry registry, Closure<?> handlers)default GroovyChainregister(Registry registry, Class<? extends Action<? super Chain>> action)default GroovyChainregister(Registry registry, Action<? super Chain> action)Adds a handler that inserts the given handler chain with the given registry viaContext.insert(Registry, Handler...).default GroovyChainregister(Action<? super RegistrySpec> action)Builds a new registry via the given action, then registers it viaChain.register(Registry).default GroovyChainregister(Action<? super RegistrySpec> registryAction, Closure<?> handler)default GroovyChainregister(Action<? super RegistrySpec> registryAction, Class<? extends Action<? super Chain>> action)default GroovyChainregister(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 GroovyChainwhen(boolean test, Closure<?> handlers)Inlines the given handlers iftestistrue.default GroovyChainwhen(boolean test, Closure<?> ifHandlers, Closure<?> elseHandlers)Inlines the appropriate handlers based on the giventest.default GroovyChainwhen(boolean test, Class<? extends Action<? super Chain>> action)Inlines the given chain iftestistrue.default GroovyChainwhen(boolean test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse)Inlines the appropriate chain based on the giventest.default GroovyChainwhen(boolean test, Action<? super Chain> action)Inlines the given chain iftestistrue.default GroovyChainwhen(boolean test, Action<? super Chain> onTrue, Action<? super Chain> onFalse)Inlines the appropriate chain based on the giventest.default GroovyChainwhen(Closure<?> test, Closure<?> handlers)default GroovyChainwhen(Closure<?> test, Closure<?> ifHandlers, Closure<?> elseHandlers)default GroovyChainwhen(Closure<?> test, Class<? extends Action<? super Chain>> action)default GroovyChainwhen(Closure<?> test, Class<? extends Action<? super Chain>> ifAction, Class<? extends Action<? super Chain>> elseAction)default GroovyChainwhen(Closure<?> test, Action<? super Chain> chain)default GroovyChainwhen(Closure<?> test, Action<? super Chain> ifChain, Action<? super Chain> elseChain)default GroovyChainwhen(Predicate<? super Context> test, Closure<?> handlers)default GroovyChainwhen(Predicate<? super Context> test, Closure<?> ifHandlers, Closure<?> elseHandlers)default GroovyChainwhen(Predicate<? super Context> test, Class<? extends Action<? super Chain>> action)default GroovyChainwhen(Predicate<? super Context> test, Class<? extends Action<? super Chain>> onTrue, Class<? extends Action<? super Chain>> onFalse)default GroovyChainwhen(Predicate<? super Context> test, Action<? super Chain> action)default GroovyChainwhen(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 givenClosureas aHandlerto thisGroovyChain.- Parameters:
handler- theClosureto 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisDELETE.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisDELETEand thepathis 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 relativepathmatches the givenpathand therequestHTTPMethodisDELETE.- Specified by:
deletein 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 therequestHTTPMethodisDELETEand thepathis at the current root.- Specified by:
deletein 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 aListofHandlerfrom the givenClosureand adds aHandlerto thisGroovyChainthat changes theFileSystemBindingfor theHandlerlist.See
fileSystem(String, Action)for more details.- Parameters:
path- the relativepathto 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 theFileSystemBindingfor the given handler chain.- Specified by:
fileSystemin 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:
fileSystemin 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 pathand adirectorywithin 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:
filesin 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:ChainChain.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 relativepathmatches the givenpathand therequestHTTPMethodisGET.- Specified by:
getin 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 therequestHTTPMethodisGETand thepathis at the current root.- Specified by:
getin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisGET.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisGETand thepathis 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 aHostheader 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 aHostheader 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 relativepathmatches the givenpathand therequestHTTPMethodisPATCH.- Specified by:
patchin 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 therequestHTTPMethodisPATCHand thepathis at the current root.- Specified by:
patchin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPATCH.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPATCHand thepathis 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 relativepathmatches the givenpathand therequestHTTPMethodisOPTIONS.- Specified by:
optionsin 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 therequestHTTPMethodisOPTIONSand thepathis at the current root.- Specified by:
optionsin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisOPTIONS.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisOPTIONSand thepathis 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 relativepathmatches the givenpathexactly.Nesting
pathhandlers will not work due to the exact matching, use a combination ofpathandprefixinstead. 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 howpathis interpreted.- Specified by:
pathin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathexactly.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 relativepathmatches the givenpathand therequestHTTPMethodisPOST.- Specified by:
postin 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 therequestHTTPMethodisPOSTand thepathis at the current root.- Specified by:
postin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPOST.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPOSTand thepathis 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 theprefixstring.
-
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 aListofHandlerfrom the givenClosureand adds aHandlerto thisGroovyChainthat delegates to theHandlerlist 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 relativepathmatches the givenpathand therequestHTTPMethodisPUT.- Specified by:
putin 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 therequestHTTPMethodisPUTand thepathis at the current root.- Specified by:
putin 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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif the relativepathmatches the givenpathand therequestHTTPMethodisPUT.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 aHandlerto thisGroovyChainthat delegates to the givenClosureas aHandlerif therequestHTTPMethodisPUTand thepathis 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:
redirectin 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 iftestistrue.This is literally just sugar for wrapping the given action in an
ifstatement. 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 iftestistrue.This is literally just sugar for wrapping the given action in an
ifstatement. 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 iftestistrue.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
truewill result in the givenifHandlersbeing used. A value offalsewill result in the givenelseHandlersbeing 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/elsestatement. 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"); }); } }
-
-