Intercepting filter pattern

Last updated

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. [1] This pattern applies reusable processing transparently before and after the actual request execution by the front and page controllers. [2]

Contents

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. [1]

Filter chain

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

Filters

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

Target

This is the resource requested by the client. [1]

Consequences

Following benefits can be considered:

Reduced performance can be a concern, as unnecessarily long chains of interceptors and filters may hurt performance. [2]

Sample code

Sample code implementation for filters with custom filter strategy is given below.

Code for implementing a filter - debugging filter:

importjava.io.IOException;importjavax.annotation.processing.Processor;importjakarta.servlet.ServletException;importjakarta.servlet.ServletRequest;importjakarta.servlet.ServletResponse;publicclassDebuggingFilterimplementsProcessor{privateProcessortarget;publicDebuggingFilter(ProcessormyTarget){target=myTarget;}publicvoidexecute(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{// Do some filter processing here, such as // displaying request parameterstarget.execute(req,res);}}

[1]

Code for implementing a filter - core processor:

importjava.io.IOException;importjavax.annotation.processing.Processor;importjakarta.servlet.ServletException;importjakarta.servlet.ServletRequest;importjakarta.servlet.ServletResponse;publicclassCoreProcessorimplementsProcessor{privateProcessortarget;publicCoreProcessor(){this(null);}publicCoreProcessor(ProcessormyTarget){target=myTarget;}publicvoidexecute(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{// Do core processing here}}

[1]

Code for handling requests:

importjava.io.IOException;importjavax.annotation.processing.Processor;importjakarta.servlet.ServletException;importjakarta.servlet.ServletRequest;importjakarta.servlet.ServletResponse;publicvoidprocessRequest(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{Processorprocessors=newDebuggingFilter(newAuthenticationFilter(newCoreProcessor()));processors.execute(req,res);//Then dispatch to next resource, which is probably // the View to displaydispatcher.dispatch(req,res);}

[1]

Code for filter manager:

importjava.io.IOException;importjavax.annotation.processing.Processor;importjakarta.servlet.ServletException;importjakarta.servlet.ServletRequest;importjakarta.servlet.ServletResponse;publicvoidprocessRequest(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{Processorprocessors=newDebuggingFilter(newAuthenticationFilter(newCoreProcessor()));processors.execute(req,res);// Then dispatch to next resource, which is probably // the View to displaydispatcher.dispatch(req,res);}

[1]

Code for filter chain:

importjakarta.servlet.Filter;publicclassFilterChain{// filter chain // apply filtersfor(finalFilterfilter:filters){// pass request & response through various // filtersfilter.execute(request,response);}}}

[1]

See also

References

  1. 1 2 3 4 5 6 7 8 9 10 11 "Core J2EE Patterns - Intercepting Filter". Oracle. Oracle. Retrieved 6 February 2016.
  2. 1 2 3 Kayal, D. (2008). Pro Java EE Spring Patterns. New York: Apress. pp. 98–106.