//过滤器接口 public interface Filter { public void doFilter(Request req,Response res,FilterChain c); } //客户端传递的内容到服务器 //模拟javaweb中的request public class Request { public String requestStr; } //服务器反馈的内容到客户端 //模拟javaweb中的response public class Response { public String responseStr; } //HTML的过滤器 public class HTMLFilter implements Filter{ @Override public void doFilter(Request req, Response res,FilterChain fc) { req.requestStr=req.requestStr.replace("<", "["); req.requestStr=req.requestStr.replace(">", "]")+"--HTMLFilter"; fc.doFilter(req, res, fc); //在这里它会调用过滤链中的下一个filter //等整个过滤链处理完了之后也就是fc.doFilter(req, res, fc);这段 //处理完了之后就会执行下面的方法了res.responseStr+="--HTMLFilter"; //然后依次的过滤器都执行这列的这行代码,也就会变成了倒序执行 res.responseStr+="--HTMLFilter"; } } //敏感词的过滤器 public class SensitiveFilter implements Filter{ @Override public void doFilter(Request req, Response res,FilterChain fc) { req.requestStr=req.requestStr.replace("敏感", "mingan")+"--SensitiveFilter"; fc.doFilter(req, res, fc); res.responseStr+="--SensitiveFilter"; } } //过滤器链 public class FilterChain implements Filter{ private List<Filter> list=new ArrayList<Filter>(); public FilterChain addfilter(Filter filter){ list.add(filter); return this; } int index=0; @Override public void doFilter(Request req, Response res,FilterChain fc) { if(index==list.size())return; Filter f=list.get(index); index++; //依次执行下一个过滤器,直到整个过滤器链执行完 f.doFilter(req, res, fc); } } public class Client { /* * Request:客户端发送给服务端的信息 * Response:服务端反馈给客户端的信息 * 要实现的功能是:Request携带的信息所经过的过滤器 * 要跟Response所携带的信息所经过的过滤器相反 */ public static void main(String[] args) { Request req=new Request(); Response res=new Response(); req.requestStr="这是我的一个敏感链接<a>优酷</a>"; res.responseStr="response"; FilterChain fc=new FilterChain(); fc.addfilter(new HTMLFilter()); FilterChain fc1=new FilterChain(); fc1.addfilter(new SensitiveFilter()); fc.addfilter(fc1);//在一个过滤链上面再加上一个过滤链 fc.doFilter(req, res,fc); System.out.println(req.requestStr); System.out.println(res.responseStr); } /** * 执行结果: * 这是我的一个mingan链接[a]优酷[/a]--HTMLFilter--SensitiveFilter * response--SensitiveFilter--HTMLFilter */ }
时间: 2024-10-12 03:32:31