Chain of responsibility (ChainOfResponsibiliby, Filter), filterchain

Source: Internet
Author: User

Chain of responsibility (ChainOfResponsibiliby, Filter), filterchain

Request class:

 1 package com.demo; 2  3 public class Request { 4      5     private String requestStr; 6  7     public String getRequestStr() { 8         return requestStr; 9     }10 11     public void setRequestStr(String requestStr) {12         this.requestStr = requestStr;13     }14 15 }
View Code

Response class:

 1 package com.demo; 2  3 public class Response { 4      5     private String responseStr; 6  7     public String getResponseStr() { 8         return responseStr; 9     }10 11     public void setResponseStr(String responseStr) {12         this.responseStr = responseStr;13     }14     15 }
View Code

Filter interface:

1 package com.demo;2 3 public interface Filter {4     void doFilter(Request request,Response response,FilterChain chain);5 }
View Code

FilterChain class, implements the Filter interface, and overwrites the doFilter () method to traverse all filters in the filter chain:

1 package com. demo; 2 3 import java. util. arrayList; 4 5 public class FilterChain implements Filter {6 7 // Filter chain 8 private ArrayList <Filter> chain = new ArrayList <Filter> (); 9 10 // index of the Filter chain 11 int index = 0; 12 13 // add Filter 14 public ArrayList to the filter chain <Filter> add (Filter filter Filter) {15 chain. add (filter); 16 return chain; 17} 18 19/** 20 * rewrite doFilter Method 21 * If the traversal is not complete, call the doFilter Method 22 */23 @ Override24 public void doFilter (Request request, Response response, FilterChain chain) of a specific filter {25 // If the filter chain is traversed, 26 if (index = this. chain. size () return; 27 28 Filter filter = this. chain. get (index); 29 index ++; 30 filter. doFilter (request, response, chain); 31} 32 33}
View Code

HTMLFilter (Fiter interface implementation class ):

1 package com. demo; 2 3 public class HTMLFilter implements Filter {4 5 @ Override 6 public void doFilter (Request request, Response response, FilterChain chain) {7 // process what the Filter does 8 request. setRequestStr (request. getRequestStr (). replace ('<','['). replace ('>', ']') 9 + "--- HTMLFilter ()"); 10 11 // jump to the next filter 12 chain. doFilter (request, response, chain); 13 14 // callback 15 response. setResponseStr (response. getResponseStr () + "--- HTMLFilter ()"); 16} 17 18}
View Code

SensitiveFilter (Fiter interface implementation class ):

1 package com. demo; 2 3 public class SensitiveFilter implements Filter {4 5 @ Override 6 public void doFilter (Request request, Response response, FilterChain chain) {7 // process what the Filter should do 8 request. setRequestStr (request. getRequestStr (). replace ("employed", "employed "). replace ("sensitive", "") 9 + "--- SensitiveFilter ()"); 10 11 // jump to the next filter 12 chain. doFilter (request, response, chain); 13 14 // callback 15 response. setResponseStr (response. getResponseStr () + "--- SensitiveFilter ()"); 16} 17 18}
View Code

Main:

1 package com. demo; 2 3 public class Main {4 public static void main (String [] args) {5 // request information 6 String msg = "Hello everyone :), <script>, sensitive, being employed, I don't feel like teaching on the Internet, because I can't see the big guy "; 7 8 // instantiate the Request object 9 Request request = new Request (); 10 request. setRequestStr (msg); 11 12 // instantiate the Response object 13 Response response = new Response (); 14 response. setResponseStr ("response"); 15 16 // filter chain 17 FilterChain chain = new FilterChain (); 18 chain. add (new HTMLFilter ()). add (new SensitiveFilter (); 19 20 // start filtering 21 chain. doFilter (request, response, chain); 22 23 // print the processing result 24 System. out. println (request. getRequestStr (); 25 System. out. println (response. getResponseStr (); 26 27} 28}
View Code

Ideas:

After the doFilter method of the filter chain is called in the operation class, the filter is traversed;

The chain. doFilter () method immediately jumps to the next filter after each operation of the filter is completed,

Of course, the operation logic for the next filter is the same, until all the requests are completed, the filter chain returns;

All filters are followed by the response operation after doFiter is executed, and they are in reverse order. In this way, the filter is followed in and the action is reversed.

If it is not clear, debug in debug mode, starting with the doFilter of the main method.

Code: http://pan.baidu.com/s/1kUGVy2F extract code: 9q6z

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.