IFilter:
package com.rain.filter; public interface IFilter { void doFilter(Request request,Response response,FilterChain chain); }
HtmlFilter:
package com.rain.filter; public class HtmlFilter implements IFilter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr().replaceAll("<", "[") .replaceAll(">", "]")+ "HtmlFilter, "); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr()+"HtmlFilter, "); } }
FaceFilter:
package com.rain.filter; public class FaceFilter implements IFilter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr().replaceAll("笑脸", "^-^")+ "FaceFilter, "); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr() + "FaceFilter, "); } }
SensitiveFilter:
package com.rain.filter; public class SensitiveFilter implements IFilter { @Override public void doFilter(Request request, Response response, FilterChain chain) { request.setRequestStr(request.getRequestStr().toLowerCase().replaceAll("fuck", "和谐")+"SensitiveFilter, "); chain.doFilter(request, response, chain); response.setResponseStr(response.getResponseStr()+"SensitiveFilter, "); } }
Request:
package com.rain.filter; public class Request { private String requestStr; public Request(String requestStr) { super(); this.requestStr = requestStr; } public String getRequestStr() { return requestStr; } public void setRequestStr(String requestStr) { this.requestStr = requestStr; } }
Response:
package com.rain.filter; public class Response { private String responseStr; public Response(String responseStr) { super(); this.responseStr = responseStr; } public String getResponseStr() { return responseStr; } public void setResponseStr(String responseStr) { this.responseStr = responseStr; } }
FilterChain:
package com.rain.filter; import java.util.ArrayList; import java.util.List; public class FilterChain implements IFilter{ private List<IFilter> filterList=new ArrayList<IFilter>(); private int index=0; public FilterChain addFilter(IFilter filter){ this.filterList.add(filter); return this; } public void doFilter(Request request, Response response,FilterChain chain) { if(index==filterList.size()) return;//如果链条里没有filter或是链条里的filter都调用过了(有点象递归) filterList.get(index++).doFilter(request, response, chain); } }
Test:
package com.rain.filter; public class Test { public static void main(String[] args) { String requestStr="requesr:<Scrip>,fuck ,笑脸"; String responseStr="response:"; FilterChain chain=new FilterChain(); FilterChain chain1=new FilterChain(); chain1.addFilter(new FaceFilter()); //链式调用 chain.addFilter(new HtmlFilter()) .addFilter(new SensitiveFilter()) .addFilter(chain1);//链条里面可以嵌套链条因为过滤器链条也实现了Ifilter接口也是过滤器 Request request=new Request(requestStr); Response response=new Response(responseStr); chain.doFilter(request, response, chain); System.out.println(request.getRequestStr()); System.out.println(response.getResponseStr()); } }