• Source: Intercepting filter pattern
  • Intercepting Filter is a JavaEE pattern 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:

    Code for implementing a filter - core processor:

    Code for handling requests:

    Code for filter manager:

    Code for filter chain:


    See also


    Front controller
    Decorator pattern
    Template method pattern
    Interceptor pattern
    Pipeline (software)


    References

Kata Kunci Pencarian: