Interception filter mode


Intercepting Filter Pattern

The Intercepting Filter Pattern is used to do some pre-processing/post-processing of the application's request or response. Filters are defined and applied to requests before they are passed to the actual target application. Filters can do authentication/authorization/logging, or track requests, and then pass the requests to the appropriate handler. The following are the entities of this design pattern.

  • Filter - A filter performs certain tasks before or after the request handler executes the request.

  • Filter Chain - A filter chain has multiple filters and executes them on the Target in a defined order.

  • Target - The Target object is the request handler.

  • Filter Manager - Filter Manager manages filters and filter chains.

  • Client - Client is the object that sends requests to the Target object.

Implementation

We will create FilterChain, FilterManager, Target, Client As various objects representing entities. AuthenticationFilter and DebugFilter represent entity filters.

InterceptingFilterDemo, our demo class uses Client to demonstrate the intercepting filter design pattern.

interceptingfilter_pattern_uml_diagram.jpg

Step 1

Create the filter interface Filter.

Filter.java

public interface Filter {
   public void execute(String request);
}

Step 2

Create an entity filter.

AuthenticationFilter.java

public class AuthenticationFilter implements Filter {
   public void execute(String request){
      System.out.println("Authenticating request: " + request);
   }
}

DebugFilter.java

public class DebugFilter implements Filter {
   public void execute(String request){
      System.out.println("request log: " + request);
   }
}

Step 3

Create Target.

Target.java

public class Target {
   public void execute(String request){
      System.out.println("Executing request: " + request);
   }
}

Step 4

Create the filter chain.

FilterChain.java

import java.util.ArrayList;
import java.util.List;

public class FilterChain {
   private List<Filter> filters = new ArrayList<Filter>();
   private Target target;

   public void addFilter(Filter filter){
      filters.add(filter);
   }

   public void execute(String request){
      for (Filter filter : filters) {
         filter.execute(request);
      }
      target.execute(request);
   }

   public void setTarget(Target target){
      this.target = target;
   }
}

Step 5

Create the filter manager.

FilterManager.java

public class FilterManager {
   FilterChain filterChain;

   public FilterManager(Target target){
      filterChain = new FilterChain();
      filterChain.setTarget(target);
   }
   public void setFilter(Filter filter){
      filterChain.addFilter(filter);
   }

   public void filterRequest(String request){
      filterChain.execute(request);
   }
}

Step 6

Create client Client.

Client.java

public class Client {
   FilterManager filterManager;

   public void setFilterManager(FilterManager filterManager){
      this.filterManager = filterManager;
   }

   public void sendRequest(String request){
      filterManager.filterRequest(request);
   }
}

Step 7

Use Client to demonstrate the interception filter design pattern.

FrontControllerPatternDemo.java

public class InterceptingFilterDemo {
   public static void main(String[] args) {
      FilterManager filterManager = new FilterManager(new Target());
      filterManager.setFilter(new AuthenticationFilter());
      filterManager.setFilter(new DebugFilter());

      Client client = new Client();
      client.setFilterManager(filterManager);
      client.sendRequest("HOME");
   }
}

Step 8

Verify the output.

Authenticating request: HOME
request log: HOME
Executing request: HOME