1 9 package org.snipsnap.net.filter; 10 11 import javax.servlet.Filter ; 12 import javax.servlet.FilterChain ; 13 import javax.servlet.FilterConfig ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.ServletRequest ; 16 import javax.servlet.ServletResponse ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import java.io.IOException ; 20 21 public class GZIPFilter implements Filter { 22 23 public void doFilter(ServletRequest req, ServletResponse res, 24 FilterChain chain) throws IOException , ServletException { 25 if (req instanceof HttpServletRequest && 28 !isHttp10(req) && !isIncludedRequest(req)) { 29 HttpServletRequest request = (HttpServletRequest ) req; 30 HttpServletResponse response = (HttpServletResponse ) res; 31 String ae = request.getHeader("accept-encoding"); 32 if (ae != null && ae.indexOf("gzip") != -1) { 33 GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response); 35 chain.doFilter(req, wrappedResponse); 36 wrappedResponse.finishResponse(); 37 return; 38 } 39 } 40 chain.doFilter(req, res); 41 } 42 43 49 private boolean isHttp10(ServletRequest request) { 50 return "HTTP/1.0".equals(request.getProtocol()); 51 } 52 53 59 private boolean isIncludedRequest(ServletRequest request) { 60 return request.getAttribute("javax.servlet.include.request_uri") != null; 61 } 62 63 64 public void init(FilterConfig filterConfig) { 65 } 67 68 public void destroy() { 69 } 71 } 72 | Popular Tags |