1 16 17 package org.directwebremoting.util; 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.LinkedList ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.servlet.ServletOutputStream ; 36 import javax.servlet.http.Cookie ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 45 public class FakeHttpServletResponse implements HttpServletResponse 46 { 47 50 public void setCharacterEncoding(String characterEncoding) 51 { 52 this.characterEncoding = characterEncoding; 53 } 54 55 58 public String getCharacterEncoding() 59 { 60 return characterEncoding; 61 } 62 63 66 public ServletOutputStream getOutputStream() 67 { 68 return outputStream; 69 } 70 71 74 public PrintWriter getWriter() throws UnsupportedEncodingException 75 { 76 if (writer == null) 77 { 78 Writer targetWriter = (characterEncoding != null ? new OutputStreamWriter (content, characterEncoding) : new OutputStreamWriter (content)); 79 writer = new PrintWriter (targetWriter); 80 } 81 82 return writer; 83 } 84 85 88 public void flushBuffer() 89 { 90 if (writer != null) 91 { 92 writer.flush(); 93 } 94 95 if (outputStream != null) 96 { 97 try 98 { 99 outputStream.flush(); 100 } 101 catch (IOException ex) 102 { 103 throw new IllegalStateException ("Could not flush OutputStream: " + ex.getMessage()); 104 } 105 } 106 107 committed = true; 108 } 109 110 113 public void sendError(int newStatus, String newErrorMessage) throws IOException 114 { 115 if (committed) 116 { 117 throw new IllegalStateException ("Cannot set error status - response is already committed"); 118 } 119 120 status = newStatus; 121 errorMessage = newErrorMessage; 122 committed = true; 123 } 124 125 128 public void sendError(int newStatus) throws IOException 129 { 130 if (committed) 131 { 132 throw new IllegalStateException ("Cannot set error status - response is already committed"); 133 } 134 135 status = newStatus; 136 committed = true; 137 } 138 139 144 public String getErrorMessage() 145 { 146 return errorMessage; 147 } 148 149 152 public void sendRedirect(String url) throws IOException 153 { 154 if (committed) 155 { 156 throw new IllegalStateException ("Cannot send redirect - response is already committed"); 157 } 158 159 redirectedUrl = url; 160 committed = true; 161 } 162 163 167 public String getRedirectedUrl() 168 { 169 return redirectedUrl; 170 } 171 172 175 public void setStatus(int status) 176 { 177 this.status = status; 178 } 179 180 183 public void setStatus(int status, String errorMessage) 184 { 185 this.status = status; 186 this.errorMessage = errorMessage; 187 } 188 189 193 public int getStatus() 194 { 195 return status; 196 } 197 198 202 public byte[] getContentAsByteArray() 203 { 204 flushBuffer(); 205 return content.toByteArray(); 206 } 207 208 213 public String getContentAsString() throws UnsupportedEncodingException 214 { 215 flushBuffer(); 216 return (characterEncoding != null) ? content.toString(characterEncoding) : content.toString(); 217 } 218 219 222 public void setContentLength(int contentLength) 223 { 224 this.contentLength = contentLength; 225 } 226 227 231 public int getContentLength() 232 { 233 return contentLength; 234 } 235 236 239 public void setContentType(String contentType) 240 { 241 this.contentType = contentType; 242 if (contentType != null) 243 { 244 int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); 245 246 if (charsetIndex != -1) 247 { 248 String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); 249 setCharacterEncoding(encoding); 250 } 251 } 252 } 253 254 257 public String getContentType() 258 { 259 return contentType; 260 } 261 262 265 public void setBufferSize(int bufferSize) 266 { 267 this.bufferSize = bufferSize; 268 } 269 270 273 public int getBufferSize() 274 { 275 return bufferSize; 276 } 277 278 281 public void setCommitted(boolean committed) 282 { 283 this.committed = committed; 284 } 285 286 289 public boolean isCommitted() 290 { 291 return committed; 292 } 293 294 297 public void resetBuffer() 298 { 299 if (committed) 300 { 301 throw new IllegalStateException ("Cannot reset buffer - response is already committed"); 302 } 303 304 content.reset(); 305 } 306 307 310 public void reset() 311 { 312 resetBuffer(); 313 314 characterEncoding = null; 315 contentLength = 0; 316 contentType = null; 317 locale = null; 318 cookies.clear(); 319 headers.clear(); 320 status = HttpServletResponse.SC_OK; 321 errorMessage = null; 322 } 323 324 327 public void setLocale(Locale locale) 328 { 329 this.locale = locale; 330 } 331 332 335 public Locale getLocale() 336 { 337 return locale; 338 } 339 340 343 public void addCookie(Cookie cookie) 344 { 345 cookies.add(cookie); 346 } 347 348 352 public Cookie [] getCookies() 353 { 354 return (Cookie []) cookies.toArray(new Cookie [cookies.size()]); 355 } 356 357 362 public Cookie getCookie(String name) 363 { 364 for (Iterator it = cookies.iterator(); it.hasNext();) 365 { 366 Cookie cookie = (Cookie ) it.next(); 367 if (name.equals(cookie.getName())) 368 { 369 return cookie; 370 } 371 } 372 return null; 373 } 374 375 378 public String encodeUrl(String url) 379 { 380 return url; 381 } 382 383 386 public String encodeURL(String url) 387 { 388 return url; 389 } 390 391 394 public String encodeRedirectUrl(String url) 395 { 396 return url; 397 } 398 399 402 public String encodeRedirectURL(String url) 403 { 404 return url; 405 } 406 407 410 public void addHeader(String name, String value) 411 { 412 doAddHeader(name, value); 413 } 414 415 418 public void setHeader(String name, String value) 419 { 420 headers.put(name, value); 421 } 422 423 426 public void addDateHeader(String name, long value) 427 { 428 doAddHeader(name, new Long (value)); 429 } 430 431 434 public void setDateHeader(String name, long value) 435 { 436 headers.put(name, new Long (value)); 437 } 438 439 442 public void addIntHeader(String name, int value) 443 { 444 doAddHeader(name, new Integer (value)); 445 } 446 447 450 public void setIntHeader(String name, int value) 451 { 452 headers.put(name, new Integer (value)); 453 } 454 455 459 private void doAddHeader(String name, Object value) 460 { 461 Object oldValue = headers.get(name); 462 if (oldValue instanceof List ) 463 { 464 List list = (List ) oldValue; 465 list.add(value); 466 } 467 else if (oldValue != null) 468 { 469 List list = new LinkedList (); 470 list.add(oldValue); 471 list.add(value); 472 headers.put(name, list); 473 } 474 else 475 { 476 headers.put(name, value); 477 } 478 } 479 480 483 public boolean containsHeader(String name) 484 { 485 return headers.containsKey(name); 486 } 487 488 492 public Set getHeaderNames() 493 { 494 return headers.keySet(); 495 } 496 497 502 public Object getHeader(String name) 503 { 504 return headers.get(name); 505 } 506 507 512 public List getHeaders(String name) 513 { 514 Object value = headers.get(name); 515 if (value instanceof List ) 516 { 517 return (List ) value; 518 } 519 else if (value != null) 520 { 521 return Collections.singletonList(value); 522 } 523 else 524 { 525 return Collections.EMPTY_LIST; 526 } 527 } 528 529 533 537 public void setForwardedUrl(String forwardedUrl) 538 { 539 this.forwardedUrl = forwardedUrl; 540 } 541 542 546 public String getForwardedUrl() 547 { 548 return forwardedUrl; 549 } 550 551 555 public void setIncludedUrl(String includedUrl) 556 { 557 this.includedUrl = includedUrl; 558 } 559 560 564 public String getIncludedUrl() 565 { 566 return includedUrl; 567 } 568 569 private static final String CHARSET_PREFIX = "charset="; 570 571 private String characterEncoding = "ISO-8859-1"; 572 573 private final ByteArrayOutputStream content = new ByteArrayOutputStream (); 574 575 private final DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(this.content); 576 577 private PrintWriter writer; 578 579 private int contentLength = 0; 580 581 private String contentType; 582 583 private int bufferSize = 4096; 584 585 private boolean committed; 586 587 private Locale locale = Locale.getDefault(); 588 589 private final List cookies = new ArrayList (); 590 591 private final Map headers = new HashMap (); 592 593 private int status = HttpServletResponse.SC_OK; 594 595 private String errorMessage; 596 597 private String redirectedUrl; 598 599 private String forwardedUrl; 600 601 private String includedUrl; 602 } 603 | Popular Tags |