1 5 6 package org.infohazard.maverick.util; 7 8 import java.io.IOException ; 9 import java.io.PrintWriter ; 10 import java.io.Reader ; 11 import java.io.UnsupportedEncodingException ; 12 13 import javax.servlet.ServletOutputStream ; 14 import javax.servlet.ServletResponse ; 15 import javax.servlet.http.Cookie ; 16 import javax.servlet.http.HttpServletResponse ; 17 import javax.servlet.http.HttpServletResponseWrapper ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 25 public class FakeHttpServletResponse extends HttpServletResponseWrapper 26 { 27 30 private static Log log = LogFactory.getLog(FakeHttpServletResponse.class); 31 32 34 protected static final String NO_BUFFER_MSG = "The previous step never tried to write anything. Perhaps the document path is incorrect?"; 35 36 41 protected String charset; 42 43 46 protected Buffer buffer; 47 48 51 public FakeHttpServletResponse(HttpServletResponse wrapMe) 52 { 53 super(wrapMe); 54 55 if (log.isDebugEnabled()) 56 log.debug("Creating fake response, original buffer size is " 57 + wrapMe.getBufferSize()); 58 } 59 60 64 public boolean prefersReader() 65 { 66 if (this.buffer == null) 67 throw new IllegalStateException (NO_BUFFER_MSG); 68 69 return this.buffer.prefersReader(); 70 } 71 72 78 public Reader getOutputAsReader() throws UnsupportedEncodingException 79 { 80 log.debug("Getting output as Reader"); 81 82 if (this.buffer == null) 83 throw new IllegalStateException (NO_BUFFER_MSG); 84 85 return this.buffer.getAsReader(); 86 } 87 88 94 public String getOutputAsString() throws UnsupportedEncodingException 95 { 96 log.debug("Getting output as String"); 97 98 if (this.buffer == null) 99 throw new IllegalStateException (NO_BUFFER_MSG); 100 101 return this.buffer.getAsString(); 102 } 103 104 107 public int outputSize() 108 { 109 if (this.buffer == null) 110 return 0; 111 else 112 return this.buffer.size(); 113 } 114 115 118 public ServletOutputStream getOutputStream() throws java.io.IOException 119 { 120 log.debug("View is using ServletOutputStream"); 121 122 if (this.buffer == null) 123 this.buffer = new ServletOutputStreamBuffer(this.charset); 124 else 125 { 126 if (!(this.buffer instanceof ServletOutputStreamBuffer)) 127 throw new IllegalStateException ("getWriter() already called"); 128 } 129 130 return (ServletOutputStream )this.buffer; 131 } 132 133 136 public PrintWriter getWriter() throws java.io.IOException 137 { 138 log.debug("View is using PrintWriter"); 139 140 if (this.buffer == null) 141 this.buffer = new PrintWriterBuffer(); 142 else 143 { 144 if (!(this.buffer instanceof PrintWriterBuffer)) 145 throw new IllegalStateException ("getOutputStream() already called"); 146 } 147 148 return (PrintWriter )this.buffer; 149 } 150 151 154 public void setContentType(String type) 155 { 156 if (log.isDebugEnabled()) 157 log.debug("Setting contentType to " + type); 158 159 161 int semicolonIndex = type.lastIndexOf(';'); 164 if (semicolonIndex >= 0 && semicolonIndex != (type.length()-1)) 165 { 166 String working = type.substring(semicolonIndex + 1); 167 working = working.trim(); 168 169 if (working.toLowerCase().startsWith("charset")) 170 { 171 working = working.substring(working.indexOf("=") + 1).trim(); 172 173 if (working.length() > 0) 174 this.charset = working; 175 } 176 } 177 } 178 179 182 public void setContentLength(int len) 183 { 184 if (log.isDebugEnabled()) 185 log.debug("Someone wanted to set contentLength to " + len); 186 187 } 189 190 193 public void flushBuffer() throws IOException 194 { 195 if (log.isDebugEnabled()) 196 log.debug("Someone wanted to flush the buffer"); 197 198 } 200 201 204 public void setLocale(java.util.Locale loc) 205 { 206 if (log.isDebugEnabled()) 207 log.debug("Set locale to " + loc); 208 209 super.setLocale(loc); 210 } 211 212 215 public void addCookie(Cookie cookie) 216 { 217 if (log.isDebugEnabled()) 218 log.debug("Added cookie " + cookie); 219 220 super.addCookie(cookie); 221 } 222 223 226 public void setDateHeader(java.lang.String name, long date) 227 { 228 if (log.isDebugEnabled()) 229 log.debug("Set date header " + name + " to " + date); 230 231 super.setDateHeader(name, date); 232 } 233 234 237 public void addDateHeader(java.lang.String name, long date) 238 { 239 if (log.isDebugEnabled()) 240 log.debug("Add date header " + name + " to " + date); 241 242 super.addDateHeader(name, date); 243 } 244 245 248 public void setHeader(java.lang.String name, java.lang.String value) 249 { 250 if (log.isDebugEnabled()) 251 log.debug("Set header " + name + " to " + value); 252 253 super.setHeader(name, value); 254 } 255 256 259 public void addHeader(java.lang.String name, java.lang.String value) 260 { 261 if (log.isDebugEnabled()) 262 log.debug("Add header " + name + " to " + value); 263 264 super.addHeader(name, value); 265 } 266 267 270 public void setIntHeader(java.lang.String name, int value) 271 { 272 if (log.isDebugEnabled()) 273 log.debug("Set int header " + name + " to " + value); 274 275 super.setIntHeader(name, value); 276 } 277 278 281 public void addIntHeader(java.lang.String name, int value) 282 { 283 if (log.isDebugEnabled()) 284 log.debug("Add int header " + name + " to " + value); 285 286 super.addIntHeader(name, value); 287 } 288 289 294 public void sendError(int sc) throws IOException 295 { 296 log.warn("Sending error " + sc); 297 298 super.sendError(sc); 299 } 300 301 306 public void sendError(int sc, java.lang.String msg) throws IOException 307 { 308 log.warn("Sending error " + sc + ", " + msg); 309 310 super.sendError(sc, msg); 311 } 312 313 316 public void setStatus(int sc) 317 { 318 if (log.isDebugEnabled()) 319 log.debug("Setting status " + sc); 320 321 super.setStatus(sc); 322 } 323 324 327 public void setStatus(int sc, java.lang.String sm) 328 { 329 if (log.isDebugEnabled()) 330 log.debug("Setting status " + sc + ", " + sm); 331 332 super.setStatus(sc, sm); 333 } 335 336 338 protected HttpServletResponse getHttpResponse() 339 { 340 return (HttpServletResponse )this.getResponse(); 341 } 342 } 343 | Popular Tags |