1 package com.genimen.djeneric.web.util.compression; 2 3 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 22 import javax.servlet.Filter ; 23 import javax.servlet.FilterChain ; 24 import javax.servlet.FilterConfig ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 39 40 public class CompressionFilter implements Filter 41 { 42 43 47 private FilterConfig config = null; 48 49 52 private int minThreshold = 128; 53 54 57 protected int compressionThreshold; 58 59 62 private int debug = 0; 63 64 69 70 public void init(FilterConfig filterConfig) 71 { 72 73 config = filterConfig; 74 if (filterConfig != null) 75 { 76 String value = filterConfig.getInitParameter("debug"); 77 if (value != null) 78 { 79 debug = Integer.parseInt(value); 80 } 81 else 82 { 83 debug = 0; 84 } 85 String str = filterConfig.getInitParameter("compressionThreshold"); 86 if (str != null) 87 { 88 compressionThreshold = Integer.parseInt(str); 89 if (compressionThreshold != 0 && compressionThreshold < minThreshold) 90 { 91 if (debug > 0) 92 { 93 System.out.println("compressionThreshold should be either 0 - no compression or >= " + minThreshold); 94 System.out.println("compressionThreshold set to " + minThreshold); 95 } 96 compressionThreshold = minThreshold; 97 } 98 } 99 else 100 { 101 compressionThreshold = 0; 102 } 103 104 } 105 else 106 { 107 compressionThreshold = 0; 108 } 109 110 } 111 112 115 public void destroy() 116 { 117 118 this.config = null; 119 120 } 121 122 138 139 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , 140 ServletException 141 { 142 143 if (debug > 0) 144 { 145 System.out.println("@doFilter"); 146 } 147 148 if (compressionThreshold == 0) 149 { 150 if (debug > 0) 151 { 152 System.out.println("doFilter gets called, but compressionTreshold is set to 0 - no compression"); 153 } 154 chain.doFilter(request, response); 155 return; 156 } 157 158 boolean supportCompression = false; 159 if (request instanceof HttpServletRequest ) 160 { 161 if (debug > 1) 162 { 163 System.out.println("requestURI = " + ((HttpServletRequest ) request).getRequestURI()); 164 } 165 166 String s = (String ) ((HttpServletRequest ) request).getParameter("gzip"); 168 if ("false".equals(s)) 169 { 170 if (debug > 0) 171 { 172 System.out.println("got parameter gzip=false --> don't compress, just chain filter"); 173 } 174 chain.doFilter(request, response); 175 return; 176 } 177 178 Enumeration e = ((HttpServletRequest ) request).getHeaders("Accept-Encoding"); 179 while (e.hasMoreElements()) 180 { 181 String name = (String ) e.nextElement(); 182 if (name.indexOf("gzip") != -1) 183 { 184 if (debug > 0) 185 { 186 System.out.println("supports compression"); 187 } 188 supportCompression = true; 189 } 190 else 191 { 192 if (debug > 0) 193 { 194 System.out.println("no support for compresion"); 195 } 196 } 197 } 198 } 199 200 if (!supportCompression) 201 { 202 if (debug > 0) 203 { 204 System.out.println("doFilter gets called wo compression"); 205 } 206 chain.doFilter(request, response); 207 return; 208 } 209 else 210 { 211 if (response instanceof HttpServletResponse ) 212 { 213 CompressionServletResponseWrapper wrappedResponse = new CompressionServletResponseWrapper( 214 (HttpServletResponse ) response); 215 wrappedResponse.setDebugLevel(debug); 216 wrappedResponse.setCompressionThreshold(compressionThreshold); 217 if (debug > 0) 218 { 219 System.out.println("doFilter gets called with compression"); 220 } 221 try 222 { 223 chain.doFilter(request, wrappedResponse); 224 } 225 finally 226 { 227 wrappedResponse.finishResponse(); 228 } 229 return; 230 } 231 } 232 } 233 234 240 public void setFilterConfig(FilterConfig filterConfig) 241 { 242 init(filterConfig); 243 } 244 245 249 public FilterConfig getFilterConfig() 250 { 251 return config; 252 } 253 254 } 255 | Popular Tags |