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