YouTip LogoYouTip

Intercepting Filter Pattern

## Intercepting Filter Pattern The Intercepting Filter Pattern is used to perform pre-processing or post-processing on requests or responses in an application. It defines filters that are applied to requests before they are passed to the actual target application. Filters can handle tasks like authentication/authorization, logging, or request tracking, and then pass the request to the appropriate handler. Below 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 contains multiple filters and executes them on the Target in a defined order. * **Target** - The Target object is the request handler. * **Filter Manager** - The filter manager manages the filters and the filter chain. * **Client** - The Client is the object that sends requests to the Target object. * * * ## Summary ### Intent Used to pre-process and post-process requests through a series of filters before they reach their final destination. ### Main Problem Solved * Addresses the need in web applications to perform common operations (like logging, security checks, transaction management, etc.) before and after request handling. ### Use Cases * When additional logic needs to be inserted into the request processing flow, and this logic is unrelated to the business logic. ### Implementation Approach * **Filter Chain**: A series of filters executed in a specific order. * **Filter**: Components that pre-process and post-process requests and responses. * **Target Object**: The final destination for request processing. ### Key Code * **Filter Interface**: Defines the methods that filters must implement, such as `before()` and `after()`. * **Concrete Filter**: Implements the filter interface and contains specific processing logic. * **Filter Manager**: Responsible for maintaining the filter chain and the order of filter invocation. ### Application Examples * **Web Application Servers**: Such as Tomcat, which use filters for intercepting requests, for example, for permission checks, logging, etc. ### Advantages 1. **Separation of Concerns**: Separates common operations from business logic, improving code maintainability. 2. **Scalability**: Filters can be flexibly added, removed, or modified. 3. **Reusability**: Filters can be reused across different request processing flows. ### Disadvantages * **Performance Impact**: If the filter chain is too long or the operations performed by filters are complex, it may affect performance. ### Usage Recommendations * Consider using the Intercepting Filter Pattern when common operations unrelated to business logic need to be inserted into the request processing flow. ### Considerations * Ensure the execution order of filters is as expected to avoid logical errors. ### Main Roles Involved 1. **Filter Interface**: * Defines the methods that filters must implement. 2. **Concrete Filter**: * Implements the filter interface and contains specific processing logic. 3. **Filter Manager**: * Responsible for maintaining the filter chain and invoking the filters. 4. **Target**: * The final destination for request processing, where the business logic is implemented. 5. **Client (Optional)**: * The web browser or API client that initiates the request. The Intercepting Filter Pattern helps maintain clarity and focus in business logic by inserting common operations into the request processing flow. * * * ## Implementation We will create _FilterChain_, _FilterManager_, _Target_, and _Client_ as various objects representing the entities. _AuthenticationFilter_ and _DebugFilter_ represent the concrete filters. The _InterceptingFilterDemo_ class uses the _Client_ to demonstrate the Intercepting Filter design pattern. ![Image 1: UML diagram of the Intercepting Filter Pattern](#) ## Step 1 Create the Filter interface. ## Filter.java public interface Filter{public void execute(String request); } ## Step 2 Create the concrete filters. ## 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 the 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 Listfilters = new ArrayList(); 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 the 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 the _Client_ to 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 the output will be: Authenticating request: HOME request log: HOME Executing request: HOME
← Service Locator PatternFront Controller Pattern β†’