public interface HandlerDecoratingModule extends Module
Modules can implement this interface to decorate the application handler before user handlers.
The following example adds a global logging handler so that all requests are logged.
import ratpack.handling.*;
import ratpack.guice.*;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// A service interface
interface LoggerI {
void log(String str);
}
// A service impl
class LoggerImpl implements LoggerI {
private final static Logger LOGGER = LoggerFactory.getLogger(LoggerImpl.class);
public void log(String str) {
LOGGER.info(str);
}
}
// A handler that uses the service, and delegates
class LoggingHandler implements Handler {
private final Handler rest;
private final LoggerI loggeri;
public LoggingHandler(LoggerI logger, Handler rest) {
this.loggeri = logger;
this.rest = rest;
}
public void handle(Context exchange) throws Exception {
loggeri.log("Request: " + exchange.getRequest().getPath());
rest.handle(exchange);
}
}
// A module that binds the service impl, and decorates the application handler
class LoggingModule extends AbstractModule implements HandlerDecoratingModule {
protected void configure() {
bind(LoggerI.class).to(LoggerImpl.class);
}
public Handler decorate(Injector injector, Handler handler) {
return new LoggingHandler(injector.getInstance(LoggerI.class), handler);
}
}
Guice.builder(ratpack.registry.Registry)| Modifier and Type | Method and Description |
|---|---|
Handler |
decorate(Injector injector,
Handler handler)
Decorate the given handler with any global logic.
|