public class CustomMiddlewareExample {
public void run(WebServer server) throws IOException {
// An example of performing some work before handling control to the next or application
Middleware getFirefox = new AbstractMiddleware() {
public void handle(Request request, Response response) throws Exception {
// Tell IE users to get Firefox
String userAgent = request.header("User-Agent");
if (userAgent != null && userAgent.contains("MSIE")) {
response.redirectTo("http://www.mozilla.org");
} else {
// Hand over control to next application in the stack
forward(request, response);
}
}
};
// An example of performing additional work after getting control back
// (there's already a middleware for that, btw)
Middleware contentLengthHeader = new AbstractMiddleware() {
public void handle(Request request, Response response) throws Exception {
forward(request, response);
// Set content length header on the response
response.contentLength(response.size());
}