1 16 17 package compressionFilters; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PrintWriter ; 23 import java.util.Locale ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.ServletOutputStream ; 28 import javax.servlet.ServletResponse ; 29 import javax.servlet.ServletResponseWrapper ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpServletResponseWrapper ; 32 33 41 42 public class CompressionServletResponseWrapper extends HttpServletResponseWrapper { 43 44 46 50 51 public CompressionServletResponseWrapper(HttpServletResponse response) { 52 super(response); 53 origResponse = response; 54 if (debug > 1) { 55 System.out.println("CompressionServletResponseWrapper constructor gets called"); 56 } 57 } 58 59 60 62 65 66 protected HttpServletResponse origResponse = null; 67 68 71 72 protected static final String info = "CompressionServletResponseWrapper"; 73 74 78 79 protected ServletOutputStream stream = null; 80 81 82 86 87 protected PrintWriter writer = null; 88 89 92 protected int threshold = 0; 93 94 97 private int debug = 0; 98 99 102 protected String contentType = null; 103 104 106 107 110 public void setContentType(String contentType) { 111 if (debug > 1) { 112 System.out.println("setContentType to "+contentType); 113 } 114 this.contentType = contentType; 115 origResponse.setContentType(contentType); 116 } 117 118 119 122 public void setCompressionThreshold(int threshold) { 123 if (debug > 1) { 124 System.out.println("setCompressionThreshold to " + threshold); 125 } 126 this.threshold = threshold; 127 } 128 129 130 133 public void setDebugLevel(int debug) { 134 this.debug = debug; 135 } 136 137 138 144 public ServletOutputStream createOutputStream() throws IOException { 145 if (debug > 1) { 146 System.out.println("createOutputStream gets called"); 147 } 148 149 CompressionResponseStream stream = new CompressionResponseStream(origResponse); 150 stream.setDebugLevel(debug); 151 stream.setBuffer(threshold); 152 153 return stream; 154 155 } 156 157 158 161 public void finishResponse() { 162 try { 163 if (writer != null) { 164 writer.close(); 165 } else { 166 if (stream != null) 167 stream.close(); 168 } 169 } catch (IOException e) { 170 } 171 } 172 173 174 176 177 182 public void flushBuffer() throws IOException { 183 if (debug > 1) { 184 System.out.println("flush buffer @ CompressionServletResponseWrapper"); 185 } 186 ((CompressionResponseStream)stream).flush(); 187 188 } 189 190 197 public ServletOutputStream getOutputStream() throws IOException { 198 199 if (writer != null) 200 throw new IllegalStateException ("getWriter() has already been called for this response"); 201 202 if (stream == null) 203 stream = createOutputStream(); 204 if (debug > 1) { 205 System.out.println("stream is set to "+stream+" in getOutputStream"); 206 } 207 208 return (stream); 209 210 } 211 212 219 public PrintWriter getWriter() throws IOException { 220 221 if (writer != null) 222 return (writer); 223 224 if (stream != null) 225 throw new IllegalStateException ("getOutputStream() has already been called for this response"); 226 227 stream = createOutputStream(); 228 if (debug > 1) { 229 System.out.println("stream is set to "+stream+" in getWriter"); 230 } 231 String charEnc = origResponse.getCharacterEncoding(); 233 if (debug > 1) { 234 System.out.println("character encoding is " + charEnc); 235 } 236 if (charEnc != null) { 239 writer = new PrintWriter (new OutputStreamWriter (stream, charEnc)); 240 } else { 241 writer = new PrintWriter (stream); 242 } 243 244 return (writer); 245 246 } 247 248 249 public void setContentLength(int length) { 250 } 251 252 253 257 private static String getCharsetFromContentType(String type) { 258 259 if (type == null) { 260 return null; 261 } 262 int semi = type.indexOf(";"); 263 if (semi == -1) { 264 return null; 265 } 266 String afterSemi = type.substring(semi + 1); 267 int charsetLocation = afterSemi.indexOf("charset="); 268 if(charsetLocation == -1) { 269 return null; 270 } else { 271 String afterCharset = afterSemi.substring(charsetLocation + 8); 272 String encoding = afterCharset.trim(); 273 return encoding; 274 } 275 } 276 277 } 278 | Popular Tags |