1 package jodd.servlet.filters; 2 3 import java.io.IOException; 4 import java.util.StringTokenizer; 5 6 import javax.servlet.Filter; 7 import javax.servlet.FilterChain; 8 import javax.servlet.FilterConfig; 9 import javax.servlet.ServletException; 10 import javax.servlet.ServletRequest; 11 import javax.servlet.ServletResponse; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 45 public class GzipFilter implements Filter { 46 47 private FilterConfig config; 48 49 protected FilterConfig getFilterConfig() { 50 return (config); 51 } 52 53 68 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { 69 HttpServletRequest req = (HttpServletRequest) request; 70 HttpServletResponse res = (HttpServletResponse) response; 71 if ( (threshold == 0) || (isGzipSupported(req) == false) || (isGzipEligible(req) == false) ) { 72 chain.doFilter(request, response); 73 return; 74 } 75 GzipResponseWrapper wrappedResponse = new GzipResponseWrapper(res); 76 wrappedResponse.setCompressionThreshold(threshold); 77 try { 78 chain.doFilter(request, wrappedResponse); 79 } finally { 80 wrappedResponse.finishResponse(); 81 } 82 return; 83 } 84 85 88 protected String uriMatch = null; 89 90 94 protected String uriExclude = null; 95 96 99 private int minThreshold = 128; 100 101 104 protected int threshold; 105 106 private String[] extensions = null; 107 private String[] excludes = null; 108 109 116 public void init(FilterConfig config) throws ServletException { 117 this.config = config; 118 extensions = null; 119 120 try { 122 threshold = Integer.parseInt(config.getInitParameter("threshold")); 123 } catch (NumberFormatException nfe) { 124 threshold = 0; 125 } 126 if (threshold < minThreshold) { 127 threshold = 0; 128 } 129 130 uriMatch = config.getInitParameter("match"); 132 if ((uriMatch != null) && (uriMatch.equals("*") == false)) { 133 StringTokenizer st = new StringTokenizer(uriMatch, ","); 134 int i = st.countTokens(); 135 if (i >= 1) { 136 extensions = new String[i]; 137 i = 0; 138 while (st.hasMoreTokens()) { 139 extensions[i] = st.nextToken().trim(); 140 i++; 141 } 142 } 143 } 144 uriExclude = config.getInitParameter("exclude"); 146 if (uriExclude != null) { 147 StringTokenizer st = new StringTokenizer(uriExclude, ","); 148 int i = st.countTokens(); 149 if (i >= 1) { 150 excludes = new String[i]; 151 i = 0; 152 while (st.hasMoreTokens()) { 153 excludes[i] = st.nextToken().trim(); 154 i++; 155 } 156 } 157 } 158 } 159 160 public void destroy() { 161 config = null; 162 extensions = null; 163 excludes = null; 164 } 165 166 private boolean isGzipSupported(HttpServletRequest req) { 167 String browserEncodings = req.getHeader("Accept-Encoding"); 168 if ( (browserEncodings != null) && (browserEncodings.indexOf("gzip") != -1) ) { 169 return true; 170 } 171 return false; 172 } 173 174 181 private boolean isGzipEligible(HttpServletRequest req) { 182 String uri = req.getRequestURI(); 183 if (uri == null) { 184 return false; 185 } 186 boolean result = false; 187 188 if (extensions == null) { result = true; 190 } else { 191 for (int i = 0; i < extensions.length; i++) { 192 if (uri.indexOf(extensions[i]) != -1) { 193 result = true; break; 195 } 196 } 197 } 198 199 if ((result == true) && (excludes != null)) { 200 for (int i = 0; i < excludes.length; i++) { 201 if (uri.indexOf(excludes[i]) != -1) { 202 result = false; break; 204 } 205 } 206 } 207 return result; 208 } 209 } 210 | Popular Tags |