public static interface Route.Complete extends Route.Filter
{
complete((req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the complete
handler is to probably cleanup request object and log
responses.
Please note that the complete
handler is just syntax sugar for
Route.Filter
.
For example, the complete
handler was implemented as:
{
use("*", "*", (req, rsp, chain) -> {
Optional<Throwable> err = Optional.empty();
try {
chain.next(req, rsp);
} catch (Throwable cause) {
err = Optional.of(cause);
} finally {
complete(req, rsp, err);
}
});
}
An complete
handler must to be registered before the actual handler you want to
intercept.
{
complete((req, rsp, cause) -> {
// your code goes here
});
get(req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection").get()) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/api/something", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
Modifier and Type | Method and Description |
---|---|
void |
handle(Request req,
Response rsp,
Optional<Throwable> cause)
Allows for log and cleanup a request.
|
default void |
handle(Request req,
Response rsp,
Route.Chain chain)
The
handle method of the Filter is called by the server each time a
request/response pair is passed through the chain due to a client request for a resource at
the end of the chain. |
default void handle(Request req, Response rsp, Route.Chain chain) throws Throwable
Route.Filter
handle
method of the Filter is called by the server each time a
request/response pair is passed through the chain due to a client request for a resource at
the end of the chain.
The Route.Chain
passed in to this method allows the Filter to pass on the request and
response to the next entity in the chain.
A typical implementation of this method would follow the following pattern:
Route.Chain
object (chain.next(req, rsp)
),handle
in interface Route.Filter
req
- A HTTP request.rsp
- A HTTP response.chain
- A route chain.Throwable
- If something goes wrong.Copyright © 2017. All rights reserved.