1 5 package org.apache.roller.ui.core.util; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import java.io.IOException ; 10 import java.io.OutputStreamWriter ; 11 import java.io.PrintWriter ; 12 import java.util.Locale ; 13 import javax.servlet.ServletOutputStream ; 14 import javax.servlet.http.HttpServletResponse ; 15 import javax.servlet.http.HttpServletResponseWrapper ; 16 17 23 public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper { 24 private final Log log = LogFactory.getLog(this.getClass()); 25 26 30 private PrintWriter cachedWriter; 31 private ResponseContent result = null; 32 private SplitServletOutputStream cacheOut = null; 33 private int status = SC_OK; 34 35 40 public CacheHttpServletResponseWrapper(HttpServletResponse response) { 41 super(response); 42 result = new ResponseContent(); 43 } 44 45 50 public ResponseContent getContent() { 51 result.commit(); 53 54 return result; 56 } 57 58 63 public void setContentType(String value) { 64 super.setContentType(value); 65 result.setContentType(value); 66 } 67 68 74 public void setDateHeader(String name, long value) { 75 if (log.isDebugEnabled()) { 76 log.debug("dateheader: " + name + ": " + value); 77 } 78 79 if ("last-modified".equalsIgnoreCase(name)) { 80 result.setLastModified(value); 81 } 82 83 super.setDateHeader(name, value); 84 } 85 86 92 public void setHeader(String name, String value) { 93 if (log.isDebugEnabled()) { 94 log.debug("header: " + name + ": " + value); 95 } 96 97 super.setHeader(name, value); 98 } 99 100 106 public void setIntHeader(String name, int value) { 107 if (log.isDebugEnabled()) { 108 log.debug("intheader: " + name + ": " + value); 109 } 110 111 super.setIntHeader(name, value); 112 } 113 114 119 public void setStatus(int status) { 120 super.setStatus(status); 121 this.status = status; 122 } 123 124 129 public void sendError(int status, String string) throws IOException { 130 super.sendError(status, string); 131 this.status = status; 132 } 133 134 139 public void sendError(int status) throws IOException { 140 super.sendError(status); 141 this.status = status; 142 } 143 144 149 public void setStatus(int status, String string) { 150 super.setStatus(status, string); 151 this.status = status; 152 } 153 154 public void sendRedirect(String location) throws IOException { 155 this.status = SC_MOVED_TEMPORARILY; 156 super.sendRedirect(location); 157 } 158 159 162 public int getStatus() { 163 return status; 164 } 165 166 171 public void setLocale(Locale value) { 172 super.setLocale(value); 173 result.setLocale(value); 174 } 175 176 181 public ServletOutputStream getOutputStream() throws IOException { 182 if (cacheOut == null) { 184 cacheOut = new SplitServletOutputStream( 185 result.getOutputStream(), super.getOutputStream()); 186 } 187 188 return cacheOut; 189 } 190 191 196 public PrintWriter getWriter() throws IOException { 197 if (cachedWriter == null) { 198 cachedWriter = new SplitPrintWriter( 199 new PrintWriter (new OutputStreamWriter (result.getOutputStream(),"UTF-8")), 200 super.getWriter()); 201 } 202 return cachedWriter; 203 } 204 205 public void flushBuffer() throws IOException { 206 super.flushBuffer(); 207 208 if (cacheOut != null) { 209 cacheOut.flush(); 210 } 211 212 if (cachedWriter != null) { 213 cachedWriter.flush(); 214 } 215 } 216 217 public String getCharacterEncoding() 218 { 219 return "UTF-8"; 220 } 221 } 222 | Popular Tags |