在Java Web开发中,我们经常需要拦截http请求,去做一些转发或者是校验等事情。这类工作通常filter可以很轻松的搞定,但这一般都是拦截请求。而我们有时候也会有一些需求,比如说是拦截服务器的返回(Response),希望在数据流在服务器端处理完毕,且返回浏览器之前,在这中间做一些自己的事情。网上搜了搜,发现资源很少。于是自己实现了一版,跟大家分享一下。主要用到了HttpServletResponseWrapper获取到数据流,然后通过response.getWriter().write来回写数据。
1 HttpStreamFilter.java 2 3 package test; 4 5 import java.io.BufferedReader; 6 import java.io.FileReader; 7 import java.io.File; 8 import java.io.IOException; 9 import javax.servlet.Filter; 10 import javax.servlet.FilterChain; 11 import javax.servlet.FilterConfig; 12 import javax.servlet.ServletException; 13 import javax.servlet.ServletRequest; 14 import javax.servlet.ServletResponse; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 import java.util.regex.Pattern; 18 import java.util.regex.Matcher; 19 import java.util.concurrent.ConcurrentHashMap; 20 21 public class HttpStreamFilter implements Filter { 22 23 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) 24 throws IOException, ServletException 25 { 26 HttpServletRequest request = (HttpServletRequest)arg0; 27 HttpServletResponse response = (HttpServletResponse)arg1; 28 29 WrapperResponse wrapperResponse = new WrapperResponse(response); 30 chain.doFilter(request, wrapperResponse); 31 String html = wrapperResponse.getContent(); 32 response.setContentLength(-1); 33 response.getWriter().write( filterHTML(html) ); 34 } 35 36 public void init(FilterConfig filterConfig) throws ServletException { 37 38 } 39 40 public void destroy() {} 41 42 public String filterHTML(String html) throws IOException 43 { 44 return html.replace("script", ""); 45 } 46 } 47 WrapperResponse.java 48 49 package test; 50 51 import java.io.ByteArrayOutputStream; 52 import java.io.IOException; 53 import java.io.PrintWriter; 54 import java.io.UnsupportedEncodingException; 55 import javax.servlet.http.HttpServletResponse; 56 import javax.servlet.http.HttpServletResponseWrapper; 57 58 public class WrapperResponse extends HttpServletResponseWrapper { 59 60 private class ResponsePrintWriter extends PrintWriter 61 { 62 ByteArrayOutputStream output; 63 64 public ResponsePrintWriter(ByteArrayOutputStream output) 65 { 66 super(output); 67 this.output = output; 68 } 69 70 public ByteArrayOutputStream getByteArrayOutputStream() 71 { 72 return output; 73 } 74 } 75 76 private ResponsePrintWriter writer; 77 private ByteArrayOutputStream output; 78 79 public WrapperResponse(HttpServletResponse httpServletResponse) 80 { 81 super(httpServletResponse); 82 output = new ByteArrayOutputStream(); 83 writer = new ResponsePrintWriter(output); 84 } 85 86 public void finalize() throws Throwable 87 { 88 super.finalize(); 89 output.close(); 90 writer.close(); 91 } 92 93 public String getContent() 94 { 95 try 96 { 97 writer.flush(); 98 return writer.getByteArrayOutputStream().toString("GBK"); 99 } 100 catch(UnsupportedEncodingException e) 101 { 102 return "UnsupportedEncoding"; 103 } 104 } 105 106 public void close() throws IOException 107 { 108 writer.close(); 109 } 110 111 public PrintWriter getWriter() throws IOException 112 { 113 return writer; 114 } 115 }