1 64 65 package net.myvietnam.mvncore.servlet.filter; 66 67 import java.io.*; 68 import javax.servlet.ServletOutputStream ; 69 import javax.servlet.http.HttpServletResponse ; 70 import javax.servlet.http.HttpServletResponseWrapper ; 71 import org.apache.commons.logging.*; 72 73 81 82 public class CompressionServletResponseWrapper extends HttpServletResponseWrapper { 83 84 private static Log log = LogFactory.getLog(CompressionServletResponseWrapper.class); 86 87 89 93 94 public CompressionServletResponseWrapper(HttpServletResponse response) { 95 super(response); 96 origResponse = response; 97 } 99 100 101 103 106 107 protected HttpServletResponse origResponse = null; 108 109 112 113 protected static final String info = "CompressionServletResponseWrapper"; 114 115 119 120 protected ServletOutputStream stream = null; 121 122 123 127 128 protected PrintWriter writer = null; 129 130 133 protected int threshold = 0; 134 135 138 140 143 protected String contentType = null; 144 145 147 148 151 public void setContentType(String contentType) { 152 this.contentType = contentType; 154 origResponse.setContentType(contentType); 155 } 156 157 158 161 public void setCompressionThreshold(int threshold) { 162 this.threshold = threshold; 164 } 165 166 167 170 174 175 181 public ServletOutputStream createOutputStream() throws IOException { 182 184 CompressionResponseStream stream = new CompressionResponseStream(origResponse); 185 stream.setBuffer(threshold); 187 188 return stream; 189 190 } 191 192 193 196 public void finishResponse() { 197 try { 198 if (writer != null) { 199 writer.close(); 200 } else { 201 if (stream != null) 202 stream.close(); 203 } 204 } catch (IOException e) { 205 } 206 } 207 208 209 211 212 217 public void flushBuffer() throws IOException { 218 220 if (stream != null) { 223 ((CompressionResponseStream)stream).flush(); 225 } 226 227 } 228 229 236 public ServletOutputStream getOutputStream() throws IOException { 237 238 if (writer != null) 239 throw new IllegalStateException ("getWriter() has already been called for this response"); 240 241 if (stream == null) { 242 stream = createOutputStream(); 243 } 244 246 return (stream); 247 248 } 249 250 257 public PrintWriter getWriter() throws IOException { 258 259 if (writer != null) 260 return (writer); 261 262 if (stream != null) 263 throw new IllegalStateException ("getOutputStream() has already been called for this response"); 264 265 stream = createOutputStream(); 266 String charEnc = origResponse.getCharacterEncoding(); 269 if (charEnc != null) { 273 writer = new PrintWriter(new OutputStreamWriter(stream, charEnc)); 274 } else { 275 writer = new PrintWriter(stream); 276 } 277 278 return (writer); 279 280 } 281 282 283 public void setContentLength(int length) { 284 } 285 286 287 291 private static String getCharsetFromContentType(String type) { 292 293 if (type == null) { 294 return null; 295 } 296 int semi = type.indexOf(";"); 297 if (semi == -1) { 298 return null; 299 } 300 String afterSemi = type.substring(semi + 1); 301 int charsetLocation = afterSemi.indexOf("charset="); 302 if(charsetLocation == -1) { 303 return null; 304 } else { 305 String afterCharset = afterSemi.substring(charsetLocation + 8); 306 String encoding = afterCharset.trim(); 307 return encoding; 308 } 309 } 310 311 } 312 | Popular Tags |