1 15 package org.apache.tapestry.test.mock; 16 17 import java.io.BufferedWriter ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.OutputStreamWriter ; 21 import java.io.PrintWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.util.*; 24 25 import javax.servlet.ServletOutputStream ; 26 import javax.servlet.http.Cookie ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.tapestry.util.ContentType; 30 31 37 38 public class MockResponse implements HttpServletResponse 39 { 40 private MockRequest _request; 41 42 private boolean _commited = false; 43 44 private ByteArrayOutputStream _outputByteStream; 45 46 private ServletOutputStream _outputStream; 47 48 private String _outputString; 49 50 private List _cookies = new ArrayList(); 51 52 private String _redirectLocation; 53 54 private String _contentType = "text/html;charset=utf-8"; 55 56 private class ServletOutputStreamImpl extends ServletOutputStream 57 { 58 private ServletOutputStreamImpl() 59 { 60 } 61 62 public void close() throws IOException 63 { 64 super.close(); 65 66 if (_outputByteStream != null) 67 _outputByteStream.close(); 68 } 69 70 public void write(byte[] b, int off, int len) throws IOException 71 { 72 commit(); 73 74 _outputByteStream.write(b, off, len); 75 } 76 77 public void write(byte[] b) throws IOException 78 { 79 commit(); 80 81 _outputByteStream.write(b); 82 } 83 84 public void write(int b) throws IOException 85 { 86 commit(); 87 88 _outputByteStream.write(b); 89 } 90 91 private void commit() 92 { 93 if (!_commited) 94 { 95 _commited = true; 96 _outputByteStream = new ByteArrayOutputStream (); 97 } 98 } 99 } 100 101 public MockResponse(MockRequest request) 102 { 103 _request = request; 104 } 105 106 public void addCookie(Cookie cookie) 107 { 108 _cookies.add(cookie); 109 } 110 111 public boolean containsHeader(String arg0) 112 { 113 return false; 114 } 115 116 public String encodeURL(String path) 117 { 118 return path; 119 } 120 121 public String encodeRedirectURL(String path) 122 { 123 return path; 124 } 125 126 public String encodeUrl(String path) 127 { 128 return encodeURL(path); 129 } 130 131 public String encodeRedirectUrl(String path) 132 { 133 return encodeRedirectURL(path); 134 } 135 136 public void sendError(int code, String message) throws IOException 137 { 138 if (_commited) 139 throw new IllegalStateException ("sendError() when committed."); 140 } 141 142 public void sendError(int code) throws IOException 143 { 144 sendError(code, null); 145 } 146 147 public void sendRedirect(String location) throws IOException 148 { 149 if (_commited) 150 throw new IllegalStateException ("sendRedirect() when committed."); 151 152 if (location.endsWith("/FAIL_IO")) 153 throw new IOException ("Forced IOException in MockResponse.sendRedirect()."); 154 155 _redirectLocation = location; 156 157 _commited = true; 158 159 } 160 161 public String getRedirectLocation() 162 { 163 return _redirectLocation; 164 } 165 166 public void setDateHeader(String name, long value) 167 { 168 } 169 170 public void addDateHeader(String name, long value) 171 { 172 } 173 174 public void setHeader(String name, String value) 175 { 176 } 177 178 public void addHeader(String name, String value) 179 { 180 } 181 182 public void setIntHeader(String name, int value) 183 { 184 } 185 186 public void addIntHeader(String name, int value) 187 { 188 } 189 190 public void setStatus(int name) 191 { 192 } 193 194 public void setStatus(int name, String arg1) 195 { 196 } 197 198 public String getCharacterEncoding() 199 { 200 return null; 201 } 202 203 public ServletOutputStream getOutputStream() throws IOException 204 { 205 if (_outputStream != null) 206 throw new IllegalStateException ("getOutputStream() invoked more than once."); 207 208 _outputStream = new ServletOutputStreamImpl(); 209 210 return _outputStream; 211 } 212 213 public PrintWriter getWriter() throws IOException 214 { 215 ContentType ct = new ContentType(_contentType); 216 217 String encoding = ct.getParameter("charset"); 218 219 return new PrintWriter (new BufferedWriter (new OutputStreamWriter (getOutputStream(), 220 encoding))); 221 } 222 223 public void setContentLength(int arg0) 224 { 225 } 226 227 public void setContentType(String contentType) 228 { 229 _contentType = contentType; 230 } 231 232 public void setBufferSize(int arg0) 233 { 234 } 235 236 public int getBufferSize() 237 { 238 return 0; 239 } 240 241 public void flushBuffer() throws IOException 242 { 243 } 244 245 public void resetBuffer() 246 { 247 } 248 249 public boolean isCommitted() 250 { 251 return _commited; 252 } 253 254 public void reset() 255 { 256 _outputStream = null; 257 } 258 259 public void setLocale(Locale arg0) 260 { 261 } 262 263 public Locale getLocale() 264 { 265 return null; 266 } 267 268 272 273 public void end() throws IOException 274 { 275 277 if (_outputStream != null) 278 _outputStream.close(); 279 } 280 281 284 285 public String getOutputString() 286 { 287 if (_outputString != null) 288 return _outputString; 289 290 if (_outputByteStream == null) 291 return null; 292 293 try 294 { 295 String encoding = _request.getCharacterEncoding(); 296 297 if (encoding != null) 298 _outputString = new String (_outputByteStream.toByteArray(), encoding); 299 } 300 catch (UnsupportedEncodingException e) 301 { 302 } 303 304 if (_outputString == null) 305 _outputString = _outputByteStream.toString(); 306 307 return _outputString; 308 } 309 310 public byte[] getResponseBytes() 311 { 312 return _outputByteStream.toByteArray(); 313 } 314 315 public Cookie [] getCookies() 316 { 317 return (Cookie []) _cookies.toArray(new Cookie [_cookies.size()]); 318 } 319 320 public String getContentType() 321 { 322 return _contentType; 323 } 324 325 public void setCharacterEncoding(String enc) 326 { 327 } 328 329 } | Popular Tags |