1 24 package org.riotfamily.cachius; 25 26 import java.io.IOException ; 27 import java.io.OutputStream ; 28 import java.io.PrintWriter ; 29 import java.io.Writer ; 30 31 import javax.servlet.ServletOutputStream ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpServletResponseWrapper ; 34 35 import org.riotfamily.cachius.support.MultiplexPrintWriter; 36 import org.riotfamily.cachius.support.MultiplexServletOutputStream; 37 38 39 45 public class CachiusResponseWrapper extends HttpServletResponseWrapper { 46 47 private ItemUpdater cacheItemUpdate; 48 49 private ServletOutputStream outputStream; 50 51 private PrintWriter writer; 52 53 54 public CachiusResponseWrapper(HttpServletResponse response, 55 ItemUpdater cacheItemUpdate) { 56 57 super(response); 58 this.cacheItemUpdate = cacheItemUpdate; 59 } 60 61 66 public void setStatus(int sc) { 67 super.setStatus(sc); 68 if (sc != 0 && sc != HttpServletResponse.SC_OK) { 69 cacheItemUpdate.discard(); 70 } 71 } 72 73 74 public void setContentType(String contentType) { 75 super.setContentType(contentType); 76 cacheItemUpdate.setContentType(contentType); 77 } 78 79 80 86 public ServletOutputStream getOutputStream() throws IOException { 87 if (writer != null) { 88 throw new IllegalStateException (); 89 } 90 if (outputStream == null) { 91 OutputStream captureStream = cacheItemUpdate.getOutputStream(); 92 if (captureStream != null) { 93 outputStream = new MultiplexServletOutputStream( 94 captureStream, super.getOutputStream()); 95 } 96 else { 97 outputStream = super.getOutputStream(); 99 } 100 } 101 return outputStream; 102 } 103 104 110 public PrintWriter getWriter() throws IOException { 111 if (writer == null) { 112 if (outputStream != null) { 113 throw new IllegalStateException (); 114 } 115 Writer captureWriter = cacheItemUpdate.getWriter(); 116 if (captureWriter != null) { 117 writer = new MultiplexPrintWriter( 118 captureWriter, 119 super.getWriter()); 120 } 121 else { 122 writer = super.getWriter(); 124 } 125 } 126 return writer; 127 } 128 129 public void flushBuffer() throws IOException { 130 if (writer != null) { 131 writer.flush(); 132 } 133 else if (outputStream != null) { 134 outputStream.flush(); 135 } 136 } 137 138 } 139 | Popular Tags |