请输入您要查询的百科知识:

 

词条 Intercepting filter pattern
释义

  1. Structure

     Filter manager  Filter chain  Filters  Target 

  2. Consequences

  3. Sample code

  4. See also

  5. References

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]

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:

  • 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 concerned 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:

public class DebuggingFilter implements Processor {

  public DebuggingFilter(Processor myTarget) {    target = myTarget;  }
  public void execute(ServletRequest req,   ServletResponse res) throws IOException,     ServletException    {    //Do some filter processing here, such as     // displaying request parameters    target.execute(req, res);  }

}

[1]

Code for implementing a filter - core processor:

public class CoreProcessor implements Processor {

  private Processor target;  public CoreProcessor()   {    this(null);  }
  public CoreProcessor(Processor myTarget)   {    target = myTarget;  }
  public void execute(ServletRequest req,       ServletResponse res) throws IOException,       ServletException   {    //Do core processing here  }

}

[1]

Code for handling requests:

public void processRequest(ServletRequest req,

  ServletResponse res)   throws IOException, ServletException {  Processor processors = new DebuggingFilter(     new AuthenticationFilter(new CoreProcessor()));  processors.execute(req, res);
  //Then dispatch to next resource, which is probably   // the View to display  dispatcher.dispatch(req, res);

}

[1]

Code for filter manager:

public void processRequest(ServletRequest req,

  ServletResponse res)   throws IOException, ServletException {  Processor processors = new DebuggingFilter(     new AuthenticationFilter(new CoreProcessor()));  processors.execute(req, res);
  //Then dispatch to next resource, which is probably   // the View to display  dispatcher.dispatch(req, res);

}

[1]

Code for filter chain:

public class FilterChain {

  // filter chain   private List myFilters = new ArrayList<>();
  // Creates new FilterChain   public FilterChain()  {    // plug-in default filter services for example     // only. This would typically be done in the     // FilterManager, but is done here for example     // purposes    myFilters.add(new DebugFilter());    myFilters.add(new LoginFilter());    myFilters.add(new AuditFilter());  }
  public void processFilter(     javax.servlet.http.HttpServletRequest request,    javax.servlet.http.HttpServletResponse response)  throws javax.servlet.ServletException,     java.io.IOException         {    Filter filter;
    // apply filters    for (final Filter filter : filters)    {      // pass request & response through various       // filters      filter.execute(request, response);    }  }

}

[1]

See also

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

References

1. ^10 {{Cite web|url = http://www.oracle.com/technetwork/java/interceptingfilter-142169.html|title = Core J2EE Patterns - Intercepting Filter|date = |accessdate = 6 February 2016|website = Oracle|publisher = Oracle|last = |first = }}
2. ^{{Cite book|title = Pro Java EE Spring Patterns|last = Kayal|first = D.|publisher = Apress|year = 2008|location = New York|volume = |pages = 98–106}}
{{Design Patterns patterns}}

2 : Software design patterns|Articles with example Java code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/12 21:17:38