Interface Filter

  • All Superinterfaces:
    java.util.function.Function<HttpHandler,​HttpHandler>
    All Known Implementing Classes:
    AddSeleniumUserAgent, DumpHttpExchangeFilter, RetryRequest
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface Filter
    extends java.util.function.Function<HttpHandler,​HttpHandler>
    Can be wrapped around an HttpHandler in order to either modify incoming HttpRequests or outgoing HttpResponses using the well-known "Filter" pattern. This is very similar to the Servlet spec's javax.servlet.Filter, but takes advantage of lambdas:
    
     Filter filter = next -> {
       return req -> {
         req.addHeader("cheese", "brie");
         HttpResponse res = next.apply(req);
         res.addHeader("vegetable", "peas");
         return res;
       };
     }
     

    Because each filter returns an HttpHandler, it's easy to do processing before, or after each request, as well as short-circuit things if necessary.