1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import javax.servlet.*; 27 import javax.servlet.http.*; 28 29 37 public class CacheResponseWrapper extends HttpServletResponseWrapper { 38 HttpServletResponse response; 39 ServletOutputStream stream; 40 PrintWriter writer; 41 OutputStream cacheOutput; 42 43 49 public CacheResponseWrapper(HttpServletResponse response, 50 OutputStream cacheOutput) { 51 super(response); 52 this.response = response; 53 this.cacheOutput = cacheOutput; 54 } 56 57 62 public ServletOutputStream createOutputStream() throws IOException { 63 return new CacheResponseStream(response, cacheOutput); 64 } 65 66 69 public void finishResponse() { 70 try { 71 if (writer != null) { 72 writer.close(); 73 } else if (stream != null) { 74 stream.close(); 75 } 76 } catch (IOException e) {} 77 } 78 79 public void flushBuffer() throws IOException { 80 stream.flush(); 81 } 82 83 public ServletOutputStream getOutputStream() throws IOException { 84 if (writer != null) { 85 throw new IllegalStateException ("getWriter() has already been called!"); 86 } 87 88 if (stream == null) { 89 stream = createOutputStream(); 90 } 91 92 return stream; 93 } 94 95 public PrintWriter getWriter() throws IOException { 96 if (writer != null) { 97 return writer; 98 } 99 100 if (stream != null) { 101 throw new IllegalStateException ("getOutputStream() has already been called!"); 102 } 103 104 stream = createOutputStream(); 105 writer = new PrintWriter(stream); 106 return writer; 107 } 108 } 109 | Popular Tags |