看了一下设计模式中的责任链设计模式,主要实现类似拦截器的功能,记下来方便自己以后复习。
假设要处理爬虫趴下来的内容,你就需要发这些内容进行去噪,例如:取格式化html标签,去页面脚本,去敏感信息等等,如果将这些过滤内容都写在一个方法中,那么势必造成代码耦合性强,如果以后不断的添加内容,将造成很大的维护成本。
所以,我们需要把变化的内容抽取出来定义为接口,需要使用时我们只需实现其对应的操作。
先定义过滤接口
package org.andy.filter; /** * 创建时间:2015-1-25 下午10:55:03 * * @author andy * @version 2.2 */ public interface Filter { /** * 定义过滤器 * @return */ public String doFilter(String src); }
html内容过滤:
package org.andy.filter; /** * 创建时间:2015-1-25 下午10:56:41 * @author andy * @version 2.2 */ public class HtmlFilter implements Filter { @Override public String doFilter(String src) { // 实现过滤html内容 return src.replace("<", "[").replace(">", "]"); } }
package org.andy.filter; /** * 创建时间:2015-1-25 下午11:01:24 * @author andy * @version 2.2 */ public class JavascriptFilter implements Filter { @Override public String doFilter(String src) { // 实现过滤javascript脚本 return src.replace("javascript", "js"); } }
package org.andy.filter; /** * 创建时间:2015-1-25 下午11:09:00 * @author andy * @version 2.2 */ public class SensitiveFilter implements Filter{ @Override public String doFilter(String src) { // 处理敏感内容 return src.replace("逗叉", "**"); } }
package org.andy.filter; import java.util.ArrayList; import java.util.List; /** * 创建时间:2015-1-25 下午11:14:58 * @author andy * @version 2.2 * 责任链 */ public class FilterChain implements Filter { private List<Filter> filters = new ArrayList<Filter>(); public FilterChain addFilter(Filter filter){ this.filters.add(filter); //返回过滤器链 return this; } @Override public String doFilter(String src) { for (Filter filter : filters) { src = filter.doFilter(src); } return src; } }
package org.andy.filter; /** * 创建时间:2015-1-25 下午11:12:49 * * @author andy * @version 2.2 */ public class TestFilter { /** * @param args */ public static void main(String[] args) { String src = "<html/> <javascript>alert('你是个逗叉');</javascript>"; System.out.println(src); FilterChain chain = new FilterChain(); chain.addFilter(new HtmlFilter()) .addFilter(new JavascriptFilter()); System.out.println(chain.doFilter(src)); FilterChain chain1 = new FilterChain(); chain1.addFilter(new SensitiveFilter()); System.out.println(chain1.doFilter(src)); chain.addFilter(chain1); System.out.println(chain.doFilter(src)); } }
JavaEE中filter的拦截,当有request请求时,会先执行所有的request请求,在逐个进行response内容过滤响应。
定义request:
package org.andy.web.filter; /** * 创建时间:2015-1-25 下午11:47:06 * * @author andy * @version 2.2 */ public class Request { String requestStr; }
package org.andy.web.filter; /** * 创建时间:2015-1-25 下午11:48:09 * * @author andy * @version 2.2 */ public class Response { String responseStr; }
package org.andy.web.filter; /** * 创建时间:2015-1-25 下午11:45:31 * * @author andy * @version 2.2 * Filter接口,响应request和 response的请求 */ public interface Filter { public void doFilter(Request request, Response response, FilterChain chain); }
package org.andy.web.filter; import java.util.ArrayList; import java.util.List; /** * 创建时间:2015-1-26 上午10:07:08 * * @author andy * @version 2.2 */ public class FilterChain implements Filter { private List<Filter> filters = new ArrayList<Filter>(); private int index; public FilterChain addFilter(Filter filter) { this.filters.add(filter); return this; } @Override public void doFilter(Request request, Response response, FilterChain chain) { // TODO Auto-generated method stub if (index == filters.size()) return; Filter filter = filters.get(index); index++; filter.doFilter(request, response, chain); } }
package org.andy.web.filter; /** * 创建时间:2015-1-25 下午10:56:41 * @author andy * @version 2.2 */ public class HtmlFilter implements Filter{ @Override public void doFilter(Request request, Response response, FilterChain chain) { // 先执行request再执行response request.requestStr = request.requestStr.replace("<", "[").replace(">", "]") + "-----htmlFilterRequest"; //执行下一个链的filter chain.doFilter(request, response, chain); response.responseStr += "-----htmlFilterResponse"; } }
package org.andy.web.filter; /** * 创建时间:2015-1-25 下午11:01:24 * @author andy * @version 2.2 */ public class JavascriptFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain chain) { // 先执行request再执行response request.requestStr = request.requestStr.replace("javascript", "js") + "-----javascriptFilterRequest"; //执行下一个链的filter chain.doFilter(request, response, chain); response.responseStr += "-----javascriptFilterResponse"; } }
package org.andy.web.filter; /** * 创建时间:2015-1-26 上午11:10:11 * @author andy * @version 2.2 */ public class TestFilter { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String src = "<html/> <javascript>alert('你是个逗叉');</javascript>"; System.out.println(src); Request request = new Request(); request.requestStr = src; Response response = new Response(); response.responseStr = "response"; FilterChain chain = new FilterChain(); chain.addFilter(new HtmlFilter()) .addFilter(new JavascriptFilter()); chain.doFilter(request, response, chain); System.out.println(request.requestStr); System.out.println(response.responseStr); } }
原文地址:http://blog.csdn.net/fengshizty/article/details/43154075