Intercepting Filter Pattern

The Intercepting Filter Pattern is used to perform some pre-processing/post-processing on a request or response of an application. Define filters, and apply them on the request before passing it to the actual target application. Filters can do authentication/authorization/logging, or track the request, then pass the request to the corresponding handler. The following are the entities of this design pattern.

  • Filter- The Filter performs certain tasks before or after the request handler executes the request.
  • Filter Chain- The Filter Chain carries multiple filters and executes them on the Target in the defined order.
  • Target- The Target object is the request handler.
  • Filter管managedevice(Filter Manager)- The Filter Manager manages filters and the filter chain.
  • Client- The Client is the object that sends the request to the Target object.

summary

intent

Used to pre-process and post-process the request through a series of filters before it reaches the final destination.

Primary Problem Solved

  • Solves the problem of needing to perform common operations (such as logging, security checks, transaction management, etc.) before and after request handling in Web applications.

Use Case

  • When it is necessary to insert additional logic into the request processing flow, and this logic is unrelated to business logic.

Implementation Approach

  • Filter Chain: A series of filters execute in a specific order.
  • Filter: Components that perform pre-processing and post-processing on requests and responses.
  • Target ObjectThe final destination of request processing.

Key code

  • Filter Interface: Defines the methods that filters must implement, such asbefore()andafter()。
  • Concrete Filter: Implements the filter interface and contains specific processing logic.
  • Filter Manager: Responsible for maintaining the filter chain and the invocation order of filters.

Application example

  • Web Application Server: For example, Tomcat uses filters for request interception processing, such as permission checks, logging, etc.

Advantages

  1. Separation of Concerns: Separates common operations from business logic, improving code maintainability.
  2. Extensibility: Filters can be flexibly added, removed, or modified.
  3. Reusability: Filters can be reused across different request processing.

Disadvantages

  • Performance Impact: If the filter chain is too long or the operations performed by filters are complex, performance may be affected.

Usage suggestions

  • When you need to insert common operations unrelated to business logic into the request processing flow, consider using the Intercepting Filter Pattern.

Notes

  • Ensure that the execution order of filters matches expectations to avoid logic errors.

Main roles involved

  1. Filter Interface:

    • Defines the methods that filters must implement.
  2. Concrete Filter:

    • Implement the filter interface, containing specific processing logic.
  3. Filter Manager:

    • Responsible for maintaining the filter chain and invoking filters.
  4. Target Object:

    • The final destination of request processing, the implementation of business logic.
  5. Client (Optional):

    • The Web browser or API client that sends the request.

The Intercepting Filter Pattern helps keep business logic clear and focused by inserting common operations into the request processing flow.


Implementation

We will createFilterChain、FilterManager、Target、ClientVarious objects acting as representation entities.AuthenticationFilterandDebugFilterRepresents the entity filter.

InterceptingFilterDemoClass usageClientTo demonstrate the Intercepting Filter design pattern.

拦截过滤器模式的 UML 图

Step 1

Create the Filter interface.

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 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 a 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 a 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

UsageClientTo demonstrate the Intercepting Filter design pattern.

InterceptingFilterDemo.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

Execute the program and output the result:

Authenticating request: HOME
request log: HOME
Executing request: HOME
other extensions