责任链模式(ChainOfResponsibiliby、Filter)
Request 类:
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 }
Response 类:
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 }
Filter 接口:
1 package com.demo; 2 3 public interface Filter { 4 void doFilter(Request request,Response response,FilterChain chain); 5 }
FilterChain 类,实现Filter接口,重写doFilter()方法,用来遍历过滤器链中所有的filter:
1 package com.demo; 2 3 import java.util.ArrayList; 4 5 public class FilterChain implements Filter{ 6 7 //过滤器链 8 private ArrayList<Filter> chain = new ArrayList<Filter>(); 9 10 //过滤器链的索引 11 int index = 0; 12 13 //往过滤器链中添加过滤器 14 public ArrayList<Filter> add(Filter filter){ 15 chain.add(filter); 16 return chain; 17 } 18 19 /** 20 * 重写 doFilter方法 21 * 如果没有遍历完,调用特定过滤器的doFilter方法 22 */ 23 @Override 24 public void doFilter(Request request, Response response, FilterChain chain) { 25 //如果遍历完过滤器链,则返回 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 }
HTMLFilter (Fiter接口的实现类):
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 //处理过滤器该干的事情 8 request.setRequestStr(request.getRequestStr().replace('<', '[').replace('>', ']') 9 + "---HTMLFilter()"); 10 11 //跳往下一个过滤器 12 chain.doFilter(request, response, chain); 13 14 //回调 15 response.setResponseStr(response.getResponseStr() + "---HTMLFilter()"); 16 } 17 18 }
SensitiveFilter (Fiter接口的实现类):
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 //处理过滤器该干的事情 8 request.setRequestStr(request.getRequestStr().replace("被就业", "就业").replace("敏感", "") 9 + "---SensitiveFilter()"); 10 11 //跳往下一个过滤器 12 chain.doFilter(request, response, chain); 13 14 //回调 15 response.setResponseStr(response.getResponseStr() + "---SensitiveFilter()"); 16 } 17 18 }
Main 执行过滤器操作的类:
1 package com.demo; 2 3 public class Main { 4 public static void main(String[] args) { 5 //请求信息 6 String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿"; 7 8 //实例化Request对象 9 Request request = new Request(); 10 request.setRequestStr(msg); 11 12 //实例化Response对象 13 Response response = new Response(); 14 response.setResponseStr("response"); 15 16 //过滤器链 17 FilterChain chain = new FilterChain(); 18 chain.add(new HTMLFilter()).add(new SensitiveFilter()); 19 20 //开始过滤 21 chain.doFilter(request, response, chain); 22 23 //打印处理结果 24 System.out.println(request.getRequestStr()); 25 System.out.println(response.getResponseStr()); 26 27 } 28 }
思路:
在操作类中调用过滤器链的doFilter方法后,开始遍历过滤器;
当每执行完该过滤器的操作后马上chain.doFilter()方法跳往下一个filter,
当然,下一个filter的操作思路也是一致的,直至所有的request做完后,过滤器链返回;
所有的filter 接着执行doFiter之后的response操作了,而且是逆序的。这样就实现了过滤器的顺着进来,逆着出去的操作。
如果不是很清楚,用debug模式进行调试,从main方法的doFilter开始。
代码:http://pan.baidu.com/s/1kUGVy2F 提取码:9q6z