public abstract class ChainAction extends Object implements Action<Chain>, Chain
Implementations can naturally use the Chain
DSL in their implementation of execute()
.
import ratpack.handling.Handler; import ratpack.handling.Context; import ratpack.handling.ChainAction; public class MyHandlers extends ChainAction { protected void execute() { get("foo", new Handler() { public void handle(Context context) { context.render("foo") } }); get("bar", new Handler() { public void handle(Context context) { context.render("bar") } }); } } // Tests (Groovy) … import static ratpack.test.http.TestHttpClients.testHttpClient import static ratpack.groovy.test.embed.EmbeddedApplications.embeddedApp def app = embeddedApp { handlers { handler chain(new MyHandlers()) } } def client = testHttpClient(app) assert client.getText("foo") == "foo" assert client.getText("bar") == "bar" app.close() // Factoring out into ChainAction implementations means they can be unit tested in isolation… import ratpack.test.handling.HandlingResult; import ratpack.test.handling.RequestFixtureAction; import static ratpack.test.UnitTest.handle; HandlingResult result = handle(new MyHandlers(), new RequestFixtureAction() { protected void execute() { uri("foo"); } }); assert result.rendered(String.class).equals("foo"); result = handle(new MyHandlers(), new RequestFixtureAction() { protected void execute() { uri("bar"); } }); assert result.rendered(String.class).equals("bar");
This class implements the Chain
interface by delegating each method to the chain returned by getChain()
.
This method only returns a value during execution of Action.execute(Object)
, which is the given chain.
Constructor and Description |
---|
ChainAction() |
Modifier and Type | Method and Description |
---|---|
Chain |
assets(String path,
String... indexFiles)
Adds a handler that serves static assets at the given file system path, relative to the contextual file system binding.
|
Handler |
chain(Action<? super Chain> action)
Constructs a handler using the given action to define a chain.
|
Chain |
delete(Handler handler)
Adds a handler that delegates to the given handler if
the
request HTTPMethod is DELETE and the path is at the current root. |
Chain |
delete(String path,
Handler handler)
Adds a handler that delegates to the given handler if
the relative
path matches the given path and the request HTTPMethod
is DELETE . |
protected abstract void |
execute()
Implementations can naturally use the
Chain DSL for the duration of this method. |
void |
execute(Chain chain)
Delegates to
execute() , using the given chain for delegation. |
Chain |
fileSystem(String path,
Action<? super Chain> action)
Adds a handler to this chain that changes the
FileSystemBinding for the given handler chain. |
Chain |
fileSystem(String path,
Handler handler)
Adds a handler to this chain that changes the
FileSystemBinding for the given handler. |
Chain |
get(Handler handler)
Adds a handler that delegates to the given handler
if the
request HTTPMethod is GET and the path is at the
current root. |
Chain |
get(String path,
Handler handler)
Adds a handler that delegates to the given handler
if the relative
path matches the given path and the request
HTTPMethod is GET . |
protected Chain |
getChain()
The current chain.
|
LaunchConfig |
getLaunchConfig()
The launch config of the application that this chain is being created for.
|
Registry |
getRegistry()
The registry that backs this.
|
Chain |
handler(Handler handler)
Adds the given handler to this.
|
Chain |
handler(String path,
Handler handler)
Adds a handler that delegates to the given handler if the relative
path
matches the given path exactly. |
Chain |
header(String headerName,
String headerValue,
Handler handler)
Adds a handler to the chain that delegates to the given handler if the request has a header with the given name and a its value matches the given value exactly.
|
Chain |
host(String hostName,
Action<? super Chain> action)
Adds a handler to the chain that delegates to the given handler chain if the request has a
Host header that matches the given value exactly. |
Chain |
insert(Action<? super Chain> action)
Inserts the given nested handler chain.
|
Chain |
patch(Handler handler)
Adds a handler that delegates to the given handler if
the
request HTTPMethod is PATCH and the path is at the current root. |
Chain |
patch(String path,
Handler handler)
Adds a handler that delegates to the given handler if
the relative
path matches the given path and the request HTTPMethod
is PATCH . |
Chain |
post(Handler handler)
Adds a handler that delegates to the given handler if
the
request HTTPMethod is POST and the path is at the current root. |
Chain |
post(String path,
Handler handler)
Adds a handler that delegates to the given handler if
the relative
path matches the given path and the request HTTPMethod
is POST . |
Chain |
prefix(String prefix,
Action<? super Chain> action)
Adds a handler that delegates to the given handlers if the
relative path starts with the given
prefix . |
Chain |
prefix(String prefix,
Handler handler)
Adds a handler that delegates to the given handler if the relative path starts with the given
prefix . |
Chain |
put(Handler handler)
Adds a handler that delegates to the given handler if
the
request HTTPMethod is PUT and the path is at the current root. |
Chain |
put(String path,
Handler handler)
Adds a handler that delegates to the given handler if
the relative
path matches the given path and the request HTTPMethod
is PUT . |
Chain |
redirect(int code,
String location)
Sends an HTTP redirect to the specified location.
|
Chain |
register(Action<? super RegistrySpec> action)
Builds a new registry via the given action, then registers it via
Chain.register(Registry) . |
Chain |
register(Action<? super RegistrySpec> registryAction,
Action<? super Chain> action)
Adds a handler that inserts the given handler chain with a registry built by the given action via
Context.insert(ratpack.registry.Registry, Handler...) . |
Chain |
register(Action<? super RegistrySpec> registryAction,
Handler handler)
Adds a handler that inserts the given handler with the a registry built by the given action via
Context.insert(ratpack.registry.Registry, Handler...) . |
Chain |
register(Registry registry)
Makes the contents of the given registry available for downstream handlers of the same nesting level.
|
Chain |
register(Registry registry,
Action<? super Chain> action)
Adds a handler that inserts the given handler chain with the given registry via
Context.insert(ratpack.registry.Registry, Handler...) . |
Chain |
register(Registry registry,
Handler handler)
Adds a handler that inserts the given handler with the given registry via
Context.insert(ratpack.registry.Registry, Handler...) . |
public Chain assets(String path, String... indexFiles)
Chain
See Handlers.assets(LaunchConfig, String, java.util.List)
for more details on the handler created
prefix("foo") { assets("d1", "index.html", "index.xhtml") }In the above configuration a request like "/foo/app.js" will return the static file "app.js" that is located in the directory "d1".
If the request matches a directory e.g. "/foo", an index file may be served. The indexFiles
array specifies the names of files to look for in order to serve.
public Handler chain(Action<? super Chain> action) throws Exception
Chain
public Chain delete(String path, Handler handler)
Chain
path
matches the given path
and the request
HTTPMethod
is DELETE
.delete
in interface Chain
path
- the relative path to match onhandler
- the handler to delegate toChain.get(String, Handler)
,
Chain.post(String, Handler)
,
Chain.put(String, Handler)
,
Chain.patch(String, Handler)
,
Chain.handler(String, Handler)
public Chain delete(Handler handler)
Chain
request
HTTPMethod
is DELETE
and the path
is at the current root.delete
in interface Chain
handler
- the handler to delegate toChain.get(Handler)
,
Chain.post(Handler)
,
Chain.put(Handler)
,
Chain.patch(Handler)
public final void execute(Chain chain) throws Exception
execute()
, using the given chain
for delegation.protected abstract void execute() throws Exception
Chain
DSL for the duration of this method.
See the class level documentation
for an implementation example.
Exception
- Any exception thrown while defining the handlerspublic Chain fileSystem(String path, Handler handler)
Chain
FileSystemBinding
for the given handler.fileSystem
in interface Chain
path
- the relative path to the new file system binding pointhandler
- the handlerpublic Chain fileSystem(String path, Action<? super Chain> action) throws Exception
Chain
FileSystemBinding
for the given handler chain.fileSystem
in interface Chain
path
- the relative path to the new file system binding pointaction
- the definition of the handler chainException
- any thrown by action
public Chain get(String path, Handler handler)
Chain
path
matches the given path
and the request
HTTPMethod
is GET
.
get
in interface Chain
path
- the relative path to match onhandler
- the handler to delegate toChain.post(String, Handler)
,
Chain.put(String, Handler)
,
Chain.patch(String, Handler)
,
Chain.delete(String, Handler)
,
Chain.handler(String, Handler)
public Chain get(Handler handler)
Chain
request
HTTPMethod
is GET
and the path
is at the
current root.get
in interface Chain
handler
- the handler to delegate toChain.post(Handler)
,
Chain.put(Handler)
,
Chain.patch(Handler)
,
Chain.delete(Handler)
protected Chain getChain() throws IllegalStateException
IllegalStateException
- if called outside of the execute()
method (i.e. there is no current chain)public LaunchConfig getLaunchConfig()
Chain
getLaunchConfig
in interface Chain
@Nullable public Registry getRegistry()
Chain
The registry that is available is dependent on how the GroovyChain
was constructed.
getRegistry
in interface Chain
null
if this has no registry.Handlers.chain(LaunchConfig, Registry, ratpack.func.Action)
public Chain handler(Handler handler)
Chain
public Chain handler(String path, Handler handler)
Chain
path
matches the given path
exactly.
Nesting path
handlers will not work due to the exact matching, use a combination of path
and prefix
instead. See Chain.prefix(String, Handler)
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 handler 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 how path
is interpreted.
handler
in interface Chain
path
- the relative path to match exactly onhandler
- the handler to delegate toChain.post(String, Handler)
,
Chain.get(String, Handler)
,
Chain.put(String, Handler)
,
Chain.patch(String, Handler)
,
Chain.delete(String, Handler)
public Chain header(String headerName, String headerValue, Handler handler)
Chain
chain. header("foo", "bar", new Handler() { public void handle(Context context) { context.getResponse().send("Header Handler"); } });
public Chain host(String hostName, Action<? super Chain> action) throws Exception
Chain
Host
header that matches the given value exactly.
chain. host("foo.com", new Action<Chain>() { public void execute(Chain hostChain) { hostChain.handler(new Handler() { public void handle(Context context) { context.getResponse().send("Host Handler"); } }); } });
public Chain patch(String path, Handler handler)
Chain
path
matches the given path
and the request
HTTPMethod
is PATCH
.patch
in interface Chain
path
- the relative path to match onhandler
- the handler to delegate toChain.get(String, Handler)
,
Chain.post(String, Handler)
,
Chain.put(String, Handler)
,
Chain.delete(String, Handler)
,
Chain.handler(String, Handler)
public Chain patch(Handler handler)
Chain
request
HTTPMethod
is PATCH
and the path
is at the current root.patch
in interface Chain
handler
- the handler to delegate toChain.get(Handler)
,
Chain.post(Handler)
,
Chain.put(Handler)
,
Chain.delete(Handler)
public Chain post(String path, Handler handler)
Chain
path
matches the given path
and the request
HTTPMethod
is POST
.
post
in interface Chain
path
- the relative path to match onhandler
- the handler to delegate toChain.get(String, Handler)
,
Chain.put(String, Handler)
,
Chain.patch(String, Handler)
,
Chain.delete(String, Handler)
,
Chain.handler(String, Handler)
public Chain post(Handler handler)
Chain
request
HTTPMethod
is POST
and the path
is at the current root.
post
in interface Chain
handler
- the handler to delegate toChain.get(Handler)
,
Chain.put(Handler)
,
Chain.patch(Handler)
,
Chain.delete(Handler)
public Chain prefix(String prefix, Handler handler)
Chain
prefix
.
All path based handlers become relative to the given prefix
.
See Handlers.prefix(String, Handler)
for format details on the prefix
string.
public Chain prefix(String prefix, Action<? super Chain> action) throws Exception
Chain
prefix
.
All path based handlers become relative to the given prefix
.
chain .prefix("person/:id", new Action<Chain>() { public void execute(Chain personChain) { 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 the prefix
string.
public Chain put(String path, Handler handler)
Chain
path
matches the given path
and the request
HTTPMethod
is PUT
.put
in interface Chain
path
- the relative path to match onhandler
- the handler to delegate toChain.get(String, Handler)
,
Chain.post(String, Handler)
,
Chain.patch(String, Handler)
,
Chain.delete(String, Handler)
,
Chain.handler(String, Handler)
public Chain put(Handler handler)
Chain
request
HTTPMethod
is PUT
and the path
is at the current root.put
in interface Chain
handler
- the handler to delegate toChain.get(Handler)
,
Chain.post(Handler)
,
Chain.patch(Handler)
,
Chain.delete(Handler)
public Chain register(Registry registry, Handler handler)
Chain
Context.insert(ratpack.registry.Registry, Handler...)
.public Chain register(Registry registry, Action<? super Chain> action) throws Exception
Chain
Context.insert(ratpack.registry.Registry, Handler...)
.public Chain register(Action<? super RegistrySpec> registryAction, Handler handler) throws Exception
Chain
Context.insert(ratpack.registry.Registry, Handler...)
.public Chain register(Action<? super RegistrySpec> registryAction, Action<? super Chain> action) throws Exception
Chain
Context.insert(ratpack.registry.Registry, Handler...)
.public Chain register(Registry registry)
Chain
The registry is inserted via the Context.next(Registry)
method.
public Chain register(Action<? super RegistrySpec> action) throws Exception
Chain
Chain.register(Registry)
.public Chain insert(Action<? super Chain> action) throws Exception
Chain
Shorter form of Chain.handler(Handler)
handler}(chain
(action
).
public Chain redirect(int code, String location)
Chain
The handler to add is created via Handlers.redirect(int, String)
.
redirect
in interface Chain
code
- the 3XX HTTP status code.location
- the URL to set in the Location response headerHandlers.redirect(int, String)