1 23 24 package com.sun.appserv.web.cache.filter; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintWriter ; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Locale ; 34 35 import javax.servlet.ServletOutputStream ; 36 import javax.servlet.http.Cookie ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpServletResponseWrapper ; 39 40 import org.apache.catalina.util.RequestUtil; 41 43 49 public class CachingResponseWrapper extends HttpServletResponseWrapper { 50 51 int statusCode = HttpCacheEntry.VALUE_NOT_SET; 52 53 60 HashMap headers = new HashMap (); 61 64 HashMap dateHeaders = new HashMap (); 65 68 ArrayList cookies = new ArrayList (); 69 70 int contentLength = HttpCacheEntry.VALUE_NOT_SET; 71 String contentType; 72 Locale locale; 73 74 79 boolean error = true; 80 81 84 CachingOutputStreamWrapper cosw; 85 PrintWriter writer; 86 87 91 public CachingResponseWrapper(HttpServletResponse response) { 92 super(response); 93 } 94 95 101 private CachingOutputStreamWrapper createCachingOutputStreamWrapper() 102 throws IOException { 103 return new CachingOutputStreamWrapper(); 104 } 105 106 113 public ServletOutputStream getOutputStream() throws IOException { 114 if (writer != null) 115 throw new IllegalStateException ("getOutputStream<>getWriter"); 116 117 if (cosw == null) { 118 cosw = createCachingOutputStreamWrapper(); 119 } 120 121 error = false; 122 return (ServletOutputStream )cosw; 123 } 124 125 132 public PrintWriter getWriter() throws IOException { 133 134 if (writer != null) 135 return (writer); 136 137 if (cosw != null) 138 throw new IllegalStateException ("getWriter<>getOutputStream"); 139 140 cosw = createCachingOutputStreamWrapper(); 141 142 OutputStreamWriter osw = 143 new OutputStreamWriter (cosw, getCharacterEncoding()); 144 145 writer = new PrintWriter (osw); 146 147 error = false; 148 return (writer); 149 } 150 151 156 public void setContentLength(int len) { 157 super.setContentLength(len); 158 159 this.contentLength = len; 160 } 161 162 167 public void setContentType(String type) { 168 super.setContentType(type); 169 170 this.contentType = type; 171 } 172 173 179 public void setLocale(Locale locale) { 180 super.setLocale(locale); 181 182 this.locale = locale; 183 } 184 185 189 public void addCookie(Cookie cookie) { 190 super.addCookie(cookie); 191 192 synchronized (cookies) { 193 cookies.add(cookie); 194 } 195 } 196 197 203 public void setHeader(String name, String value) { 204 super.setHeader(name, value); 205 206 ArrayList values = new ArrayList (); 207 values.add(value); 208 209 synchronized (headers) { 210 headers.put(name, values); 211 } 212 } 213 214 220 public void setIntHeader(String name, int value) { 221 setHeader(name, "" + value); 222 } 223 224 230 public void addHeader(String name, String value) { 231 super.addHeader(name, value); 232 233 ArrayList values = (ArrayList ) headers.get(name); 234 if (values == null) { 235 values = new ArrayList (); 236 237 synchronized (headers) { 238 headers.put(name, values); 239 } 240 } 241 242 values.add(value); 243 } 244 245 251 public void addIntHeader(String name, int value) { 252 addHeader(name, "" + value); 253 } 254 255 261 public void setDateHeader(String name, long value) { 262 super.setDateHeader(name, value); 263 264 ArrayList values = new ArrayList (); 265 values.add(new Long (value)); 266 267 synchronized (dateHeaders) { 268 dateHeaders.put(name, values); 269 } 270 } 271 272 278 public void addDateHeader(String name, long value) { 279 super.addDateHeader(name, value); 280 281 ArrayList values = (ArrayList ) dateHeaders.get(name); 282 if (values == null) { 283 values = new ArrayList (); 284 285 synchronized (dateHeaders) { 286 dateHeaders.put(name, values); 287 } 288 } 289 290 values.add(new Long (value)); 291 } 292 293 298 public void setStatus(int sc) { 299 super.setStatus(sc); 300 301 this.statusCode = sc; 302 } 303 304 314 public void sendError(int status) throws IOException { 315 super.sendError(status); 316 317 error = true; 318 } 319 320 330 public void sendError(int status, String message) throws IOException { 331 super.sendError(status, message); 332 333 error = true; 334 } 335 336 339 public boolean isError() { 340 return error; 341 } 342 343 346 public Long getExpiresDateHeader() { 347 return (Long )dateHeaders.get("Expires"); 348 } 349 350 354 public HttpCacheEntry cacheResponse() throws IOException { 355 HttpCacheEntry entry = new HttpCacheEntry(); 357 entry.responseHeaders = headers; 358 entry.dateHeaders = dateHeaders; 359 entry.cookies = cookies; 360 361 entry.contentLength = contentLength; 362 entry.contentType = contentType; 363 entry.locale = locale; 364 365 entry.statusCode = statusCode; 366 367 if (writer != null) { 369 writer.flush(); 370 } 371 372 entry.bytes = cosw.getBytes(); 373 return entry; 374 } 375 376 379 public void clear() { 380 cosw = null; 381 writer = null; 382 } 383 } 384 | Popular Tags |