1 16 17 package org.springframework.mock.web; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PrintWriter ; 23 import java.io.UnsupportedEncodingException ; 24 import java.io.Writer ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import javax.servlet.ServletOutputStream ; 35 import javax.servlet.http.Cookie ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.springframework.util.Assert; 39 import org.springframework.web.util.WebUtils; 40 41 52 public class MockHttpServletResponse implements HttpServletResponse { 53 54 public static final int DEFAULT_SERVER_PORT = 80; 55 56 private static final String CHARSET_PREFIX = "charset="; 57 58 59 63 private boolean outputStreamAccessAllowed = true; 64 65 private boolean writerAccessAllowed = true; 66 67 private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; 68 69 private final ByteArrayOutputStream content = new ByteArrayOutputStream (); 70 71 private final DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(this.content); 72 73 private PrintWriter writer; 74 75 private int contentLength = 0; 76 77 private String contentType; 78 79 private int bufferSize = 4096; 80 81 private boolean committed; 82 83 private Locale locale = Locale.getDefault(); 84 85 86 90 private final List cookies = new ArrayList (); 91 92 95 private final Map headers = new HashMap (); 96 97 private int status = HttpServletResponse.SC_OK; 98 99 private String errorMessage; 100 101 private String redirectedUrl; 102 103 private String forwardedUrl; 104 105 private String includedUrl; 106 107 108 112 116 public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) { 117 this.outputStreamAccessAllowed = outputStreamAccessAllowed; 118 } 119 120 123 public boolean isOutputStreamAccessAllowed() { 124 return this.outputStreamAccessAllowed; 125 } 126 127 131 public void setWriterAccessAllowed(boolean writerAccessAllowed) { 132 this.writerAccessAllowed = writerAccessAllowed; 133 } 134 135 138 public boolean isWriterAccessAllowed() { 139 return this.writerAccessAllowed; 140 } 141 142 public void setCharacterEncoding(String characterEncoding) { 143 this.characterEncoding = characterEncoding; 144 } 145 146 public String getCharacterEncoding() { 147 return this.characterEncoding; 148 } 149 150 public ServletOutputStream getOutputStream() { 151 if (!this.outputStreamAccessAllowed) { 152 throw new IllegalStateException ("OutputStream access not allowed"); 153 } 154 return this.outputStream; 155 } 156 157 public PrintWriter getWriter() throws UnsupportedEncodingException { 158 if (!this.writerAccessAllowed) { 159 throw new IllegalStateException ("Writer access not allowed"); 160 } 161 if (this.writer == null) { 162 Writer targetWriter = (this.characterEncoding != null ? 163 new OutputStreamWriter (this.content, this.characterEncoding) : new OutputStreamWriter (this.content)); 164 this.writer = new PrintWriter (targetWriter); 165 } 166 return this.writer; 167 } 168 169 public byte[] getContentAsByteArray() { 170 flushBuffer(); 171 return this.content.toByteArray(); 172 } 173 174 public String getContentAsString() throws UnsupportedEncodingException { 175 flushBuffer(); 176 return (this.characterEncoding != null) ? 177 this.content.toString(this.characterEncoding) : this.content.toString(); 178 } 179 180 public void setContentLength(int contentLength) { 181 this.contentLength = contentLength; 182 } 183 184 public int getContentLength() { 185 return this.contentLength; 186 } 187 188 public void setContentType(String contentType) { 189 this.contentType = contentType; 190 if (contentType != null) { 191 int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); 192 if (charsetIndex != -1) { 193 String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); 194 setCharacterEncoding(encoding); 195 } 196 } 197 } 198 199 public String getContentType() { 200 return this.contentType; 201 } 202 203 public void setBufferSize(int bufferSize) { 204 this.bufferSize = bufferSize; 205 } 206 207 public int getBufferSize() { 208 return this.bufferSize; 209 } 210 211 public void flushBuffer() { 212 if (this.writer != null) { 213 this.writer.flush(); 214 } 215 if (this.outputStream != null) { 216 try { 217 this.outputStream.flush(); 218 } 219 catch (IOException ex) { 220 throw new IllegalStateException ("Could not flush OutputStream: " + ex.getMessage()); 221 } 222 } 223 this.committed = true; 224 } 225 226 public void resetBuffer() { 227 if (this.committed) { 228 throw new IllegalStateException ("Cannot reset buffer - response is already committed"); 229 } 230 this.content.reset(); 231 } 232 233 public void setCommitted(boolean committed) { 234 this.committed = committed; 235 } 236 237 public boolean isCommitted() { 238 return this.committed; 239 } 240 241 public void reset() { 242 resetBuffer(); 243 this.characterEncoding = null; 244 this.contentLength = 0; 245 this.contentType = null; 246 this.locale = null; 247 this.cookies.clear(); 248 this.headers.clear(); 249 this.status = HttpServletResponse.SC_OK; 250 this.errorMessage = null; 251 } 252 253 public void setLocale(Locale locale) { 254 this.locale = locale; 255 } 256 257 public Locale getLocale() { 258 return this.locale; 259 } 260 261 262 266 public void addCookie(Cookie cookie) { 267 Assert.notNull(cookie, "Cookie must not be null"); 268 this.cookies.add(cookie); 269 } 270 271 public Cookie [] getCookies() { 272 return (Cookie []) this.cookies.toArray(new Cookie [this.cookies.size()]); 273 } 274 275 public Cookie getCookie(String name) { 276 Assert.notNull(name, "Cookie name must not be null"); 277 for (Iterator it = this.cookies.iterator(); it.hasNext();) { 278 Cookie cookie = (Cookie ) it.next(); 279 if (name.equals(cookie.getName())) { 280 return cookie; 281 } 282 } 283 return null; 284 } 285 286 public boolean containsHeader(String name) { 287 return (HeaderValueHolder.getByName(this.headers, name) != null); 288 } 289 290 294 public Set getHeaderNames() { 295 return this.headers.keySet(); 296 } 297 298 304 public Object getHeader(String name) { 305 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 306 return (header != null ? header.getValue() : null); 307 } 308 309 314 public List getHeaders(String name) { 315 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 316 return (header != null ? header.getValues() : Collections.EMPTY_LIST); 317 } 318 319 public String encodeURL(String url) { 320 return url; 321 } 322 323 public String encodeRedirectURL(String url) { 324 return url; 325 } 326 327 public String encodeUrl(String url) { 328 return url; 329 } 330 331 public String encodeRedirectUrl(String url) { 332 return url; 333 } 334 335 public void sendError(int status, String errorMessage) throws IOException { 336 if (this.committed) { 337 throw new IllegalStateException ("Cannot set error status - response is already committed"); 338 } 339 this.status = status; 340 this.errorMessage = errorMessage; 341 this.committed = true; 342 } 343 344 public void sendError(int status) throws IOException { 345 if (this.committed) { 346 throw new IllegalStateException ("Cannot set error status - response is already committed"); 347 } 348 this.status = status; 349 this.committed = true; 350 } 351 352 public void sendRedirect(String url) throws IOException { 353 if (this.committed) { 354 throw new IllegalStateException ("Cannot send redirect - response is already committed"); 355 } 356 Assert.notNull(url, "Redirect URL must not be null"); 357 this.redirectedUrl = url; 358 this.committed = true; 359 } 360 361 public String getRedirectedUrl() { 362 return this.redirectedUrl; 363 } 364 365 public void setDateHeader(String name, long value) { 366 setHeaderValue(name, new Long (value)); 367 } 368 369 public void addDateHeader(String name, long value) { 370 addHeaderValue(name, new Long (value)); 371 } 372 373 public void setHeader(String name, String value) { 374 setHeaderValue(name, value); 375 } 376 377 public void addHeader(String name, String value) { 378 addHeaderValue(name, value); 379 } 380 381 public void setIntHeader(String name, int value) { 382 setHeaderValue(name, new Integer (value)); 383 } 384 385 public void addIntHeader(String name, int value) { 386 addHeaderValue(name, new Integer (value)); 387 } 388 389 private void setHeaderValue(String name, Object value) { 390 doAddHeaderValue(name, value, true); 391 } 392 393 private void addHeaderValue(String name, Object value) { 394 doAddHeaderValue(name, value, false); 395 } 396 397 private void doAddHeaderValue(String name, Object value, boolean replace) { 398 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 399 Assert.notNull(value, "Header value must not be null"); 400 if (header == null) { 401 header = new HeaderValueHolder(); 402 this.headers.put(name, header); 403 } 404 if (replace) { 405 header.setValue(value); 406 } 407 else { 408 header.addValue(value); 409 } 410 } 411 412 public void setStatus(int status) { 413 this.status = status; 414 } 415 416 public void setStatus(int status, String errorMessage) { 417 this.status = status; 418 this.errorMessage = errorMessage; 419 } 420 421 public int getStatus() { 422 return this.status; 423 } 424 425 public String getErrorMessage() { 426 return this.errorMessage; 427 } 428 429 430 434 public void setForwardedUrl(String forwardedUrl) { 435 this.forwardedUrl = forwardedUrl; 436 } 437 438 public String getForwardedUrl() { 439 return this.forwardedUrl; 440 } 441 442 public void setIncludedUrl(String includedUrl) { 443 this.includedUrl = includedUrl; 444 } 445 446 public String getIncludedUrl() { 447 return this.includedUrl; 448 } 449 450 } 451 | Popular Tags |