Intercepting filter pattern
   HOME

TheInfoList



OR:

Intercepting Filter is a
JavaEE Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web serv ...
pattern A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated li ...
which creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code. The filters intercept incoming requests and outgoing responses, allowing preprocessing and post-processing, and these filters can be added or removed unobtrusively without changing existing code. This pattern applies reusable processing transparently before and after the actual request execution by the front and page controllers.


Structure

Filter manager, filter chain, filters and target are components of the pattern.


Filter manager

This manages filter processing and creates the filter chain with the appropriate filters, in the correct order, and initiates processing.


Filter chain

A Filter Chain is a specific series of filters, composed so as to form a logical chain.


Filters

These are the individual filters that are mapped to a target and their processing is coordinated by filter chain.


Target

This is the resource requested by the client.


Consequences

Following benefits can be considered: * Improved reusability: Common code is centralized in pluggable components enhancing reuse. * Increased flexibility: Generic common components can be applied and removed declaratively, improving flexibility. Reduced performance can be a concern, as unnecessarily long chains of interceptors and filters may hurt performance.


Sample code

Sample code implementation for filters with custom filter strategy is given below. Code for implementing a filter - debugging filter: public class DebuggingFilter implements Processor Code for implementing a filter - core processor: public class CoreProcessor implements Processor Code for handling requests: public void processRequest(ServletRequest req, ServletResponse res) throws IOException, ServletException Code for filter manager: public void processRequest(ServletRequest req, ServletResponse res) throws IOException, ServletException Code for filter chain: public class FilterChain }


See also

*
Front controller The front controller software design pattern is listed in several pattern catalogs and is related to the design of web applications. It is "a controller that handles all requests for a website," which is a useful structure for web application dev ...
*
Decorator pattern In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often ...
*
Template method pattern In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclass, usually an abstract superclass, and defines ...
* Interceptor pattern * Pipeline (software)


References

{{Design Patterns patterns Software design patterns Articles with example Java code