1 23 24 25 package org.apache.slide.webdav.logger; 26 27 import java.io.IOException ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintWriter ; 30 import java.io.Writer ; 31 import java.util.Enumeration ; 32 import java.util.Vector ; 33 34 import javax.servlet.ServletOutputStream ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpServletResponseWrapper ; 38 39 import org.apache.slide.common.Domain; 40 41 54 public class XHttpServletResponseFacade extends HttpServletResponseWrapper 55 { 56 57 58 public static final String DEFAULT_CHAR_ENCODING = "8859_1"; 59 60 61 StringBuffer body=null; 63 64 protected HttpServletRequest request; 65 66 private int statusCode = -1; 67 private String statusText = ""; 68 69 public void setRequest(HttpServletRequest request) { 70 this.request = request; 71 } 72 73 public HttpServletRequest getRequest() { 74 return request; 75 } 76 77 80 private boolean usingStream = false; 81 82 85 private boolean usingWriter = false; 86 87 92 XServletOutputStreamFacade osFacade = null; 93 94 99 PrintWriter writer = null; 100 101 104 Vector responseHeaders = new Vector (); 105 public Enumeration getResponseHeaders() { 106 return responseHeaders.elements(); 107 } 108 109 110 121 public XHttpServletResponseFacade(HttpServletResponse response) { 122 super(response); 123 Domain.debug("Create XHttpServletResponseFacade"); 124 } 125 126 127 129 136 public void addCookie(javax.servlet.http.Cookie cookie) { 137 responseHeaders.add( 138 new XResponseHeader( 139 "Cookie", 140 cookie.getName() + " " + 141 cookie.getValue() + " " + 142 cookie.getDomain() 143 ) 144 ); 145 super.addCookie(cookie); 146 } 147 148 153 public ServletOutputStream getOutputStream() 154 throws IOException { 155 Domain.debug("ENTER: XHttpServletResponseFacade:getOutputStream()"); 156 if ( usingWriter ) { 157 throw new IllegalStateException ("Writer is already being used for this response"); 158 } 159 usingStream = true; 160 161 if( osFacade == null ) { 162 osFacade = new XServletOutputStreamFacade( 163 super.getOutputStream(), 164 super.getCharacterEncoding()); 165 } 166 167 Domain.debug("LEAVE: XHttpServletResponseFacade:getOutputStream()"); 168 return osFacade; 169 } 170 171 176 public PrintWriter getWriter() throws IOException { 177 Domain.debug("ENTER: XHttpServletResponseFacade:getWriter()"); 178 if (usingStream) { 179 throw new IllegalStateException ("OutputStream is already being used for this response"); 180 } 181 usingWriter= true ; 182 183 if( writer == null ) { 184 writer = new XServletWriterFacade( super.getWriter() ); 185 } 186 187 Domain.debug("LEAVE: XHttpServletResponseFacade:getWriter()"); 188 return writer; 189 } 190 191 public PrintWriter getWriter(ServletOutputStream outs) throws IOException { 192 Domain.debug("ENTER: XHttpServletResponseFacade:getWriter(ServletOutputStream)"); 193 if(writer!=null) return writer; 194 196 usingWriter = true; 198 199 Domain.debug("LEAVE: XHttpServletResponseFacade:getWriter(ServletOutputStream)"); 201 return writer; 202 } 203 204 public Writer getConverter( ServletOutputStream outs ) throws IOException { 205 String encoding = getCharacterEncoding(); 206 OutputStreamWriter osWriter; 207 if (encoding == null) { 208 osWriter = new OutputStreamWriter (outs); 210 } else { 211 try { 212 osWriter = new OutputStreamWriter (outs, encoding); 213 } catch (java.io.UnsupportedEncodingException ex) { 214 System.out.println("Unsuported encoding: " + encoding ); 216 return new OutputStreamWriter (outs); 217 } 218 } 219 return osWriter; 220 } 221 222 228 public void setDateHeader(String name, long date) { 229 responseHeaders.add(new XResponseHeader(name, String.valueOf(date))); 230 super.setDateHeader(name, date); 231 } 232 233 239 public void addDateHeader(String name, long date) { 240 responseHeaders.add(new XResponseHeader(name, String.valueOf(date))); 241 super.addDateHeader(name, date); 242 } 243 244 250 public void setHeader(String name, String value) { 251 responseHeaders.add(new XResponseHeader(name, value)); 252 super.setHeader(name, value); 253 } 254 255 261 public void addHeader(String name, String value) { 262 responseHeaders.add(new XResponseHeader(name, value)); 263 super.addHeader(name, value); 264 } 265 266 272 public void setIntHeader(String name, int value) { 273 responseHeaders.add(new XResponseHeader(name, String.valueOf(value))); 274 super.setHeader(name, Integer.toString(value)); 275 } 276 277 283 public void addIntHeader(String name, int value) { 284 responseHeaders.add(new XResponseHeader(name, String.valueOf(value))); 285 super.addHeader(name, Integer.toString(value)); 286 } 287 288 public int getStatus() { 289 return statusCode; 290 } 291 public String getStatusText() { 292 return statusText; 293 } 294 public void setStatus(int sc) { 295 statusCode = sc; 296 super.setStatus(sc); 297 } 298 public void setStatus(int sc, String msg) { 299 statusCode = sc; 300 statusText = msg; 301 super.setStatus(sc, msg); 302 } 303 public void sendError( int sc ) throws IOException { 304 statusCode = sc; 305 super.sendError(sc); 306 } 307 public void sendError( int sc, String msg ) throws IOException { 308 statusCode = sc; 309 statusText = msg; 310 super.sendError(sc, msg); 311 } 312 313 318 public String getResponseBody() throws IOException { 319 if ( usingStream ) { 320 Domain.debug("XHttpServletResponseFacade:getResponseBody() - usingStream"); 321 return osFacade.getOutputBuffer(); 322 } else if ( usingWriter ) { 323 Domain.debug("XHttpServletResponseFacade:getResponseBody() - usingWriter"); 324 return (( XServletWriterFacade )writer).getOutputBuffer(); 325 } else { 326 Domain.debug("XHttpServletResponseFacade:getResponseBody() - nor writer nor stream - osFacade = " + osFacade + " writer = " + writer); 327 return ""; 328 } 329 } 330 331 332 } 333 | Popular Tags |