1 5 package com.opensymphony.oscache.web.filter; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import java.io.IOException ; 11 import java.io.OutputStreamWriter ; 12 import java.io.PrintWriter ; 13 14 import java.util.Locale ; 15 16 import javax.servlet.ServletOutputStream ; 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpServletResponseWrapper ; 19 20 26 public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper { 27 private final Log log = LogFactory.getLog(this.getClass()); 28 29 33 private PrintWriter cachedWriter = null; 34 private ResponseContent result = null; 35 private SplitServletOutputStream cacheOut = null; 36 private boolean fragment = false; 37 private int status = SC_OK; 38 private long expires = CacheFilter.EXPIRES_ON; 39 private long lastModified = CacheFilter.LAST_MODIFIED_INITIAL; 40 private long cacheControl = -60; 41 42 47 public CacheHttpServletResponseWrapper(HttpServletResponse response) { 48 this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON, CacheFilter.LAST_MODIFIED_INITIAL, -60); 49 } 50 51 61 public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time, long lastModified, long expires, long cacheControl) { 62 super(response); 63 result = new ResponseContent(); 64 this.fragment = fragment; 65 this.expires = expires; 66 this.lastModified = lastModified; 67 this.cacheControl = cacheControl; 68 69 if (!fragment) { 71 if (lastModified == CacheFilter.LAST_MODIFIED_INITIAL) { 73 long current = System.currentTimeMillis(); 74 current = current - (current % 1000); 75 result.setLastModified(current); 76 super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified()); 77 } 78 if (expires == CacheFilter.EXPIRES_TIME) { 80 result.setExpires(result.getLastModified() + time); 81 super.setDateHeader(CacheFilter.HEADER_EXPIRES, result.getExpires()); 82 } 83 if (cacheControl == CacheFilter.MAX_AGE_TIME) { 85 long maxAge = System.currentTimeMillis(); 87 maxAge = maxAge - (maxAge % 1000) + time; 88 result.setMaxAge(maxAge); 89 super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + time / 1000); 90 } else if (cacheControl != CacheFilter.MAX_AGE_NO_INIT) { 91 result.setMaxAge(cacheControl); 92 super.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-cacheControl)); 93 } 94 } 95 } 96 97 102 public ResponseContent getContent() { 103 result.commit(); 105 106 return result; 108 } 109 110 115 public void setContentType(String value) { 116 if (log.isDebugEnabled()) { 117 log.debug("ContentType: " + value); 118 } 119 120 super.setContentType(value); 121 result.setContentType(value); 122 } 123 124 130 public void setDateHeader(String name, long value) { 131 if (log.isDebugEnabled()) { 132 log.debug("dateheader: " + name + ": " + value); 133 } 134 135 if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) { 137 if (!fragment) { 138 result.setLastModified(value); 139 } } 141 142 if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) { 144 if (!fragment) { 145 result.setExpires(value); 146 } } 148 149 super.setDateHeader(name, value); 150 } 151 152 158 public void addDateHeader(String name, long value) { 159 if (log.isDebugEnabled()) { 160 log.debug("dateheader: " + name + ": " + value); 161 } 162 163 if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) { 165 if (!fragment) { 166 result.setLastModified(value); 167 } } 169 170 if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) { 172 if (!fragment) { 173 result.setExpires(value); 174 } } 176 177 super.addDateHeader(name, value); 178 } 179 180 186 public void setHeader(String name, String value) { 187 if (log.isDebugEnabled()) { 188 log.debug("header: " + name + ": " + value); 189 } 190 191 if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) { 192 result.setContentType(value); 193 } 194 195 if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) { 196 result.setContentEncoding(value); 197 } 198 199 super.setHeader(name, value); 200 } 201 202 208 public void addHeader(String name, String value) { 209 if (log.isDebugEnabled()) { 210 log.debug("header: " + name + ": " + value); 211 } 212 213 if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) { 214 result.setContentType(value); 215 } 216 217 if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) { 218 result.setContentEncoding(value); 219 } 220 221 super.addHeader(name, value); 222 } 223 224 230 public void setIntHeader(String name, int value) { 231 if (log.isDebugEnabled()) { 232 log.debug("intheader: " + name + ": " + value); 233 } 234 235 super.setIntHeader(name, value); 236 } 237 238 243 public void setStatus(int status) { 244 super.setStatus(status); 245 this.status = status; 246 } 247 248 253 public void sendError(int status, String string) throws IOException { 254 super.sendError(status, string); 255 this.status = status; 256 } 257 258 263 public void sendError(int status) throws IOException { 264 super.sendError(status); 265 this.status = status; 266 } 267 268 273 public void setStatus(int status, String string) { 274 super.setStatus(status, string); 275 this.status = status; 276 } 277 278 283 public void sendRedirect(String location) throws IOException { 284 this.status = SC_MOVED_TEMPORARILY; 285 super.sendRedirect(location); 286 } 287 288 291 public int getStatus() { 292 return status; 293 } 294 295 300 public void setLocale(Locale value) { 301 super.setLocale(value); 302 result.setLocale(value); 303 } 304 305 310 public ServletOutputStream getOutputStream() throws IOException { 311 if (cacheOut == null) { 313 cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream()); 314 } 315 316 return cacheOut; 317 } 318 319 324 public PrintWriter getWriter() throws IOException { 325 if (cachedWriter == null) { 326 String encoding = getCharacterEncoding(); 327 if (encoding != null) { 328 cachedWriter = new PrintWriter (new OutputStreamWriter (getOutputStream(), encoding)); 329 } else { cachedWriter = new PrintWriter (new OutputStreamWriter (getOutputStream())); 331 } 332 } 333 334 return cachedWriter; 335 } 336 337 public void flushBuffer() throws IOException { 338 super.flushBuffer(); 339 340 if (cacheOut != null) { 341 cacheOut.flush(); 342 } 343 344 if (cachedWriter != null) { 345 cachedWriter.flush(); 346 } 347 } 348 349 352 public boolean isCommitted() { 353 return super.isCommitted() || (result.getOutputStream() == null); 354 } 355 356 359 public void reset() { 360 if (!isCommitted()) { 361 super.reset(); 362 cachedWriter = null; 363 result = new ResponseContent(); 364 cacheOut = null; 365 fragment = false; 366 status = SC_OK; 367 expires = CacheFilter.EXPIRES_ON; 368 lastModified = CacheFilter.LAST_MODIFIED_INITIAL; 369 cacheControl = -60; 370 } else { 371 throw new IllegalStateException ("Can't reset CacheHttpServletResponseWrapper, because it's already committed!"); 372 } 373 } 374 375 378 public void resetBuffer() { 379 if (!isCommitted()) { 380 super.resetBuffer(); 381 cachedWriter = null; 382 result = new ResponseContent(); 383 cacheOut = null; 384 fragment = false; 385 } else { 388 throw new IllegalStateException ("Can't reset buffer CacheHttpServletResponseWrapper, because it's already committed!"); 389 } 390 } 391 } 392 | Popular Tags |