Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Intercepting filter pattern

From Wikipedia, the free encyclopedia

Intercepting Filter is aJavaEEpattern 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]

Structure

[edit]

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

Filter manager

[edit]

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

Filter chain

[edit]

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

Filters

[edit]

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

Target

[edit]

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

Consequences

[edit]

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

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

Sample code

[edit]

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

Code for implementing a filter - debugging filter:

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:

publicclassCoreProcessorimplementsProcessor{privateProcessortarget;publicCoreProcessor(){this(null);}publicCoreProcessor(ProcessormyTarget){target=myTarget;}publicvoidexecute(ServletRequestreq,ServletResponseres)throwsIOException,ServletException{//Do core processing here}}

[1]

Code for handling requests:

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:

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:

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

[1]

See also

[edit]

References

[edit]
  1. ^abcdefghijk"Core J2EE Patterns - Intercepting Filter".Oracle. Oracle. Retrieved6 February 2016.
  2. ^abcKayal, D. (2008).Pro Java EE Spring Patterns. New York: Apress. pp. 98–106.
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Intercepting_filter_pattern&oldid=1152706018"
Category:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp