1 5 package org.roller.presentation.pagecache.rollercache; 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; 34 private ResponseContent result = null; 35 private SplitServletOutputStream cacheOut = null; 36 private int status = SC_OK; 37 38 43 public CacheHttpServletResponseWrapper(HttpServletResponse response) { 44 super(response); 45 result = new ResponseContent(); 46 } 47 48 53 public ResponseContent getContent() { 54 result.commit(); 56 57 return result; 59 } 60 61 66 public void setContentType(String value) { 67 super.setContentType(value); 68 result.setContentType(value); 69 } 70 71 77 public void setDateHeader(String name, long value) { 78 if (log.isDebugEnabled()) { 79 log.debug("dateheader: " + name + ": " + value); 80 } 81 82 if ("last-modified".equalsIgnoreCase(name)) { 83 result.setLastModified(value); 84 } 85 86 super.setDateHeader(name, value); 87 } 88 89 95 public void setHeader(String name, String value) { 96 if (log.isDebugEnabled()) { 97 log.debug("header: " + name + ": " + value); 98 } 99 100 super.setHeader(name, value); 101 } 102 103 109 public void setIntHeader(String name, int value) { 110 if (log.isDebugEnabled()) { 111 log.debug("intheader: " + name + ": " + value); 112 } 113 114 super.setIntHeader(name, value); 115 } 116 117 122 public void setStatus(int status) { 123 super.setStatus(status); 124 this.status = status; 125 } 126 127 132 public void sendError(int status, String string) throws IOException { 133 super.sendError(status, string); 134 this.status = status; 135 } 136 137 142 public void sendError(int status) throws IOException { 143 super.sendError(status); 144 this.status = status; 145 } 146 147 152 public void setStatus(int status, String string) { 153 super.setStatus(status, string); 154 this.status = status; 155 } 156 157 public void sendRedirect(String location) throws IOException { 158 this.status = SC_MOVED_TEMPORARILY; 159 super.sendRedirect(location); 160 } 161 162 165 public int getStatus() { 166 return status; 167 } 168 169 174 public void setLocale(Locale value) { 175 super.setLocale(value); 176 result.setLocale(value); 177 } 178 179 184 public ServletOutputStream getOutputStream() throws IOException { 185 if (cacheOut == null) { 187 cacheOut = new SplitServletOutputStream( 188 result.getOutputStream(), super.getOutputStream()); 189 } 190 191 return cacheOut; 192 } 193 194 199 public PrintWriter getWriter() throws IOException { 200 if (cachedWriter == null) { 201 cachedWriter = new SplitPrintWriter( 202 new PrintWriter (new OutputStreamWriter (result.getOutputStream(),"UTF-8")), 203 super.getWriter()); 204 } 205 return cachedWriter; 206 } 207 208 public void flushBuffer() throws IOException { 209 super.flushBuffer(); 210 211 if (cacheOut != null) { 212 cacheOut.flush(); 213 } 214 215 if (cachedWriter != null) { 216 cachedWriter.flush(); 217 } 218 } 219 220 public String getCharacterEncoding() 221 { 222 return "UTF-8"; 223 } 224 } 225 | Popular Tags |