1 24 package org.jresearch.gossip.filters.cache; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintWriter ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpServletResponseWrapper ; 34 35 public class CacheResponseWrapper extends HttpServletResponseWrapper { 36 37 protected HttpServletResponse origResponse = null; 38 39 protected ServletOutputStream stream = null; 40 41 protected PrintWriter writer = null; 42 43 protected OutputStream cache = null; 44 45 public CacheResponseWrapper(HttpServletResponse response, OutputStream cache) { 46 super(response); 47 origResponse = response; 48 this.cache = cache; 49 } 50 51 public ServletOutputStream createOutputStream() throws IOException { 52 return (new CacheResponseStream(origResponse, cache)); 53 } 54 55 public void flushBuffer() throws IOException { 56 stream.flush(); 57 } 58 59 public ServletOutputStream getOutputStream() throws IOException { 60 if (writer != null) { 61 throw new IllegalStateException ( 62 "getWriter() has already been called!"); 63 } 64 65 if (stream == null) 66 stream = createOutputStream(); 67 return (stream); 68 } 69 70 public PrintWriter getWriter() throws IOException { 71 if (writer != null) { 72 return (writer); 73 } 74 75 if (stream != null) { 76 throw new IllegalStateException ( 77 "getOutputStream() has already been called!"); 78 } 79 80 stream = createOutputStream(); 81 writer = new PrintWriter (new OutputStreamWriter (stream, origResponse 83 .getCharacterEncoding())); 84 return (writer); 85 } 86 } | Popular Tags |