可以随时添加过滤器,并在主方法中添加删除:
Main
package com.tn.filter; public class Main {
public static void main(String[] args) {
String msg="你好,:)哈哈,<HTML>,被加薪,敏感。被加薪,,呵呵。"; MsgProcessor mp=new MsgProcessor();
FilterChain filterChain=new FilterChain();
filterChain.addFilter(new HTMLFilter())
.addFilter(new SmileFaceFilter()); FilterChain filterChain2=new FilterChain();
filterChain2.addFilter(new SensitiveFilter()); filterChain.addFilter(filterChain2); mp.setMsg(msg);
mp.setFilterChain(filterChain); String result=mp.process();
System.out.println(result);
System.out.println(msg);
}
}
MsgProcessor
package com.tn.filter; public class MsgProcessor {
/*
HTMLFilter htmlFilter;
SensitiveFilter sensitiveFilter;
*/
// Filter[] filters={new HTMLFilter(),new SensitiveFilter(),new SmileFaceFilter()};
FilterChain filterChain;
private String msg; public FilterChain getFilterChain() {
return filterChain;
} public void setFilterChain(FilterChain filterChain) {
this.filterChain = filterChain;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
public String process(){
// String r=msg.replaceAll("<", "【")
// .replaceAll(">", "】")
// .replace("被加薪", "加薪");
/*
htmlFilter=new HTMLFilter();
sensitiveFilter=new SensitiveFilter();
String r=htmlFilter.dofilter(msg);
r=sensitiveFilter.dofilter(r);
*/ /*
for(Filter f:filters){
msg=f.dofilter(msg);
}
return msg;
*/
return filterChain.dofilter(msg);
}
}
FilterChain
package com.tn.filter; import java.util.ArrayList;
import java.util.List; public class FilterChain implements Filter{
private List<Filter> filters=new ArrayList<Filter>(); public FilterChain addFilter(Filter f){
filters.add(f);
return this;
} public String dofilter(String str){
String result=str;
for(Filter f:filters){
result=f.dofilter(result);
}
return result;
}
}
Filter
package com.tn.filter; public interface Filter {
public String dofilter(String str);
}
HTMLFilter
package com.tn.filter; public class HTMLFilter implements Filter { @Override
public String dofilter(String str) {
return str.replaceAll("<", "[")
.replaceAll(">", "]");
}
}
SensitiveFilter
package com.tn.filter; public class SensitiveFilter implements Filter { @Override
public String dofilter(String str) {
return str.replaceAll("被加薪", "加薪")
.replaceAll("敏感", "和谐");
} }
SmileFaceFilter
package com.tn.filter; public class SmileFaceFilter implements Filter { @Override
public String dofilter(String str) {
return str.replaceAll(":\\)", "笑脸");
} }
顺序处理request,逆序处理response:
Main
package com.tn.filter2; public class Main {
public static void main(String[] args) {
String msg="你好,:)哈哈,<HTML>,被加薪,敏感。被加薪,,呵呵。";
FilterChain fc=new FilterChain();
fc.addFilter(new HTMLFilter())
.addFilter(new SensitiveFilter());
Request request=new Request();
request.setStr(msg);
Response response=new Response(); System.out.println(request.getStr());
System.out.println(response.getStr()); fc.doFilter(request, response,fc); System.out.println(request.getStr());
System.out.println(response.getStr());
}
}
FilterChain
package com.tn.filter2; import java.util.ArrayList;
import java.util.List; public class FilterChain implements Filter {
List<Filter> filters=new ArrayList<Filter>();
int index=0;
public FilterChain addFilter(Filter f){
filters.add(f);
return this;
}
@Override
public void doFilter(Request request, Response response,FilterChain fc) {
if(index==filters.size()) return;
Filter f=filters.get(index++);
// filters.get(index).doFilter(request, response, fc);//这种写法会死循环,此语句的doFilter会调用FilterChain里的doFilter(),而不是调用具体Filter里的doFilter(),切记!
f.doFilter(request, response, fc);
// for(Filter f:filters){
// f.doFilter(request, response,fc);
// }
} }
Filter
package com.tn.filter2; public interface Filter {
void doFilter(Request request,Response response,FilterChain fc);
}
HTMLFilter
package com.tn.filter2; public class HTMLFilter implements Filter { @Override
public void doFilter(Request request, Response response,FilterChain fc) {
request.setStr(request.getStr().replaceAll("<", "【")+" -HTMLFilter()- ");
fc.doFilter(request, response, fc);
response.setStr(response.getStr()+" -HTMLFilter()- ");
} }
SensitiveFilter
package com.tn.filter2; public class SensitiveFilter implements Filter{
@Override
public void doFilter(Request request, Response response,FilterChain fc) {
request.setStr(request.getStr().replaceAll("被加薪", "加薪")+" -SensitiveFilter()- ");
fc.doFilter(request, response, fc);
response.setStr(response.getStr()+" -SensitiveFilter()- ");
}
}
Request
package com.tn.filter2; public class Request {
String str; public String getStr() {
return str;
} public void setStr(String str) {
this.str = str;
} }
Response
package com.tn.filter2; public class Response {
String str="response"; public String getStr() {
return str;
} public void setStr(String str) {
this.str = str;
} }