1 package com.genimen.djeneric.web.util.compression; 2 3 18 19 import java.io.IOException ; 20 import java.io.OutputStreamWriter ; 21 import java.io.PrintWriter ; 22 23 import javax.servlet.ServletOutputStream ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpServletResponseWrapper ; 26 27 35 36 public class CompressionServletResponseWrapper extends HttpServletResponseWrapper 37 { 38 39 41 45 46 public CompressionServletResponseWrapper(HttpServletResponse response) 47 { 48 super(response); 49 origResponse = response; 50 if (debug > 1) 51 { 52 System.out.println("CompressionServletResponseWrapper constructor gets called"); 53 } 54 } 55 56 58 61 62 protected HttpServletResponse origResponse = null; 63 64 67 68 protected static final String info = "CompressionServletResponseWrapper"; 69 70 74 75 protected ServletOutputStream stream = null; 76 77 81 82 protected PrintWriter writer = null; 83 84 87 protected int threshold = 0; 88 89 92 private int debug = 0; 93 94 97 protected String contentType = null; 98 99 101 104 public void setContentType(String contentType) 105 { 106 if (debug > 1) 107 { 108 System.out.println("setContentType to " + contentType); 109 } 110 this.contentType = contentType; 111 origResponse.setContentType(contentType); 112 } 113 114 117 public void setCompressionThreshold(int threshold) 118 { 119 if (debug > 1) 120 { 121 System.out.println("setCompressionThreshold to " + threshold); 122 } 123 this.threshold = threshold; 124 } 125 126 129 public void setDebugLevel(int debug) 130 { 131 this.debug = debug; 132 } 133 134 140 public ServletOutputStream createOutputStream() throws IOException 141 { 142 if (debug > 1) 143 { 144 System.out.println("createOutputStream gets called"); 145 } 146 147 CompressionResponseStream stream = new CompressionResponseStream(origResponse); 148 stream.setDebugLevel(debug); 149 stream.setBuffer(threshold); 150 151 return stream; 152 153 } 154 155 158 public void finishResponse() 159 { 160 try 161 { 162 if (writer != null) 163 { 164 writer.close(); 165 } 166 else 167 { 168 if (stream != null) stream.close(); 169 } 170 } 171 catch (IOException e) 172 { 173 } 174 } 175 176 178 183 public void flushBuffer() throws IOException 184 { 185 if (debug > 1) 186 { 187 System.out.println("flush buffer @ CompressionServletResponseWrapper"); 188 } 189 ((CompressionResponseStream) stream).flush(); 190 191 } 192 193 200 public ServletOutputStream getOutputStream() throws IOException 201 { 202 203 if (writer != null) throw new IllegalStateException ("getWriter() has already been called for this response"); 204 205 if (stream == null) stream = createOutputStream(); 206 if (debug > 1) 207 { 208 System.out.println("stream is set to " + stream + " in getOutputStream"); 209 } 210 211 return (stream); 212 213 } 214 215 222 public PrintWriter getWriter() throws IOException 223 { 224 225 if (writer != null) return (writer); 226 227 if (stream != null) throw new IllegalStateException ("getOutputStream() has already been called for this response"); 228 229 stream = createOutputStream(); 230 if (debug > 1) 231 { 232 System.out.println("stream is set to " + stream + " in getWriter"); 233 } 234 String charEnc = origResponse.getCharacterEncoding(); 236 if (debug > 1) 237 { 238 System.out.println("character encoding is " + charEnc); 239 } 240 if (charEnc != null) 243 { 244 writer = new PrintWriter (new OutputStreamWriter (stream, charEnc)); 245 } 246 else 247 { 248 writer = new PrintWriter (stream); 249 } 250 return (writer); 251 252 } 253 254 public void setContentLength(int length) 255 { 256 } 257 258 262 private static String getCharsetFromContentType(String type) 263 { 264 265 if (type == null) 266 { 267 return null; 268 } 269 int semi = type.indexOf(";"); 270 if (semi == -1) 271 { 272 return null; 273 } 274 String afterSemi = type.substring(semi + 1); 275 int charsetLocation = afterSemi.indexOf("charset="); 276 if (charsetLocation == -1) 277 { 278 return null; 279 } 280 else 281 { 282 String afterCharset = afterSemi.substring(charsetLocation + 8); 283 String encoding = afterCharset.trim(); 284 return encoding; 285 } 286 } 287 288 } 289 290 class HW 292 { 293 int j; 295 void method1() 296 { 297 double x; } 299 300 void method2() 301 { 302 double y; } 304 } | Popular Tags |