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.groovy.test.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() |
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 |
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 |
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
Chainpublic Chain delete(String path, Handler handler)
Chainpath matches the given path and the request HTTPMethod
is DELETE.delete in interface Chainpath - 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)
Chainrequest HTTPMethod is DELETE and the path is at the current root.delete in interface Chainhandler - 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)
ChainFileSystemBinding for the given handler.fileSystem in interface Chainpath - the relative path to the new file system binding pointhandler - the handlerpublic Chain fileSystem(String path, Action<? super Chain> action) throws Exception
ChainFileSystemBinding for the given handler chain.fileSystem in interface Chainpath - the relative path to the new file system binding pointaction - the definition of the handler chainException - any thrown by actionpublic Chain get(String path, Handler handler)
Chainpath matches the given path and the request
HTTPMethod is GET.
get in interface Chainpath - 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)
Chainrequest HTTPMethod is GET and the path is at the
current root.get in interface Chainhandler - the handler to delegate toChain.post(Handler),
Chain.put(Handler),
Chain.patch(Handler),
Chain.delete(Handler)protected Chain getChain() throws IllegalStateException
IllegalStateExceptionpublic LaunchConfig getLaunchConfig()
ChaingetLaunchConfig in interface Chain@Nullable public Registry getRegistry()
Chain
The registry that is available is dependent on how the GroovyChain was constructed.
getRegistry in interface Chainnull if this has no registry.Handlers.chain(LaunchConfig, Registry, ratpack.func.Action)public Chain handler(Handler handler)
Chainpublic Chain handler(String path, Handler handler)
Chainpath
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 Chainpath - 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)
Chainpublic Chain patch(String path, Handler handler)
Chainpath matches the given path and the request HTTPMethod
is PATCH.patch in interface Chainpath - 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)
Chainrequest HTTPMethod is PATCH and the path is at the current root.patch in interface Chainhandler - the handler to delegate toChain.get(Handler),
Chain.post(Handler),
Chain.put(Handler),
Chain.delete(Handler)public Chain post(String path, Handler handler)
Chainpath matches the given path and the request HTTPMethod
is POST.
post in interface Chainpath - 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)
Chainrequest HTTPMethod is POST and the path is at the current root.
post in interface Chainhandler - the handler to delegate toChain.get(Handler),
Chain.put(Handler),
Chain.patch(Handler),
Chain.delete(Handler)public Chain prefix(String prefix, Handler handler)
Chainprefix.
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
Chainprefix.
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)
Chainpath matches the given path and the request HTTPMethod
is PUT.put in interface Chainpath - 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)
Chainrequest HTTPMethod is PUT and the path is at the current root.put in interface Chainhandler - the handler to delegate toChain.get(Handler),
Chain.post(Handler),
Chain.patch(Handler),
Chain.delete(Handler)public Chain register(Registry registry, Handler handler)
ChainContext.insert(ratpack.registry.Registry, Handler...).public Chain register(Registry registry, Action<? super Chain> action) throws Exception
ChainContext.insert(ratpack.registry.Registry, Handler...).public Chain register(Action<? super RegistrySpec> registryAction, Handler handler) throws Exception
ChainContext.insert(ratpack.registry.Registry, Handler...).public Chain register(Action<? super RegistrySpec> registryAction, Action<? super Chain> action) throws Exception
ChainContext.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
ChainChain.register(Registry)