1 25 26 27 package compressionFilters; 28 29 import java.io.IOException ; 30 import java.util.Enumeration ; 31 import javax.servlet.Filter ; 32 import javax.servlet.FilterChain ; 33 import javax.servlet.FilterConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.ServletRequest ; 36 import javax.servlet.ServletResponse ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 41 49 50 public class CompressionFilter implements Filter { 51 52 56 private FilterConfig config = null; 57 58 61 private int minThreshold = 128; 62 63 64 67 protected int compressionThreshold; 68 69 72 private int debug = 0; 73 74 79 80 public void init(FilterConfig filterConfig) { 81 82 config = filterConfig; 83 if (filterConfig != null) { 84 String value = filterConfig.getInitParameter("debug"); 85 if (value!=null) { 86 debug = Integer.parseInt(value); 87 } else { 88 debug = 0; 89 } 90 String str = filterConfig.getInitParameter("compressionThreshold"); 91 if (str!=null) { 92 compressionThreshold = Integer.parseInt(str); 93 if (compressionThreshold != 0 && compressionThreshold < minThreshold) { 94 if (debug > 0) { 95 System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold); 96 System.out.println("compressionThreshold set to " + minThreshold); 97 } 98 compressionThreshold = minThreshold; 99 } 100 } else { 101 compressionThreshold = 0; 102 } 103 104 } else { 105 compressionThreshold = 0; 106 } 107 108 } 109 110 113 public void destroy() { 114 115 this.config = null; 116 117 } 118 119 135 136 public void doFilter ( ServletRequest request, ServletResponse response, 137 FilterChain chain ) throws IOException , ServletException { 138 139 if (debug > 0) { 140 System.out.println("@doFilter"); 141 } 142 143 if (compressionThreshold == 0) { 144 if (debug > 0) { 145 System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression"); 146 } 147 chain.doFilter(request, response); 148 return; 149 } 150 151 boolean supportCompression = false; 152 if (request instanceof HttpServletRequest ) { 153 if (debug > 1) { 154 System.out.println("requestURI = " + ((HttpServletRequest )request).getRequestURI()); 155 } 156 157 String s = (String ) ((HttpServletRequest )request).getParameter("gzip"); 159 if ("false".equals(s)) { 160 if (debug > 0) { 161 System.out.println("got parameter gzip=false --> don't compress, just chain filter"); 162 } 163 chain.doFilter(request, response); 164 return; 165 } 166 167 Enumeration e = 168 ((HttpServletRequest )request).getHeaders("Accept-Encoding"); 169 while (e.hasMoreElements()) { 170 String name = (String )e.nextElement(); 171 if (name.indexOf("gzip") != -1) { 172 if (debug > 0) { 173 System.out.println("supports compression"); 174 } 175 supportCompression = true; 176 } else { 177 if (debug > 0) { 178 System.out.println("no support for compresion"); 179 } 180 } 181 } 182 } 183 184 if (!supportCompression) { 185 if (debug > 0) { 186 System.out.println("doFilter gets called wo compression"); 187 } 188 chain.doFilter(request, response); 189 return; 190 } else { 191 if (response instanceof HttpServletResponse ) { 192 CompressionServletResponseWrapper wrappedResponse = 193 new CompressionServletResponseWrapper((HttpServletResponse )response); 194 wrappedResponse.setDebugLevel(debug); 195 wrappedResponse.setCompressionThreshold(compressionThreshold); 196 if (debug > 0) { 197 System.out.println("doFilter gets called with compression"); 198 } 199 try { 200 chain.doFilter(request, wrappedResponse); 201 } finally { 202 wrappedResponse.finishResponse(); 203 } 204 return; 205 } 206 } 207 } 208 209 215 public void setFilterConfig(FilterConfig filterConfig) { 216 init(filterConfig); 217 } 218 219 223 public FilterConfig getFilterConfig() { 224 return config; 225 } 226 227 } 228 229 | Popular Tags |