I have a Servlet Filter which performs operations before and after the filter chain, something like:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain){
// I do some stuff here...
chain.doFilter(req, res);
// ... and then I do some more stuff here.
}
I am converting this to a Ratpack application and have figured out how to use Handlers as filters (in general)
class MyHandler implements Handler {
void handle(Context ctx){
// so some stuff...
context.next()
}
}
but the call to next()
is non blocking so the followup operations are performed right away rather than after the other handlers have executed.
How can I get this before and after code behavior in Ratpack?
Depending on what your "after" logic is doing and whether you're already rendering a response before your "after" logic you could use context.onClose()
class MyHandler implements Handler {
void handle(Context ctx){
// so some stuff...
context.onClose(requestOutcome -> {
// do some more stuff...
})
context.next()
}
}