1 16 17 package org.springframework.mock.web; 18 19 import java.io.BufferedReader ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.Reader ; 24 import java.io.UnsupportedEncodingException ; 25 import java.security.Principal ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Date ; 29 import java.util.Enumeration ; 30 import java.util.HashSet ; 31 import java.util.Hashtable ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.Vector ; 36 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletInputStream ; 40 import javax.servlet.http.Cookie ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpSession ; 43 44 import org.springframework.core.CollectionFactory; 45 import org.springframework.util.Assert; 46 47 59 public class MockHttpServletRequest implements HttpServletRequest { 60 61 64 public static final String DEFAULT_PROTOCOL = "http"; 65 66 69 public static final String DEFAULT_SERVER_ADDR = "127.0.0.1"; 70 71 74 public static final String DEFAULT_SERVER_NAME = "localhost"; 75 76 79 public static final int DEFAULT_SERVER_PORT = 80; 80 81 84 public static final String DEFAULT_REMOTE_ADDR = "127.0.0.1"; 85 86 89 public static final String DEFAULT_REMOTE_HOST = "localhost"; 90 91 92 private boolean active = true; 93 94 95 99 private final Hashtable attributes = new Hashtable (); 100 101 private String characterEncoding; 102 103 private byte[] content; 104 105 private String contentType; 106 107 private final Map parameters = CollectionFactory.createLinkedMapIfPossible(16); 108 109 private String protocol = DEFAULT_PROTOCOL; 110 111 private String scheme = DEFAULT_PROTOCOL; 112 113 private String serverName = DEFAULT_SERVER_NAME; 114 115 private int serverPort = DEFAULT_SERVER_PORT; 116 117 private String remoteAddr = DEFAULT_REMOTE_ADDR; 118 119 private String remoteHost = DEFAULT_REMOTE_HOST; 120 121 122 private final Vector locales = new Vector (); 123 124 private boolean secure = false; 125 126 private final ServletContext servletContext; 127 128 private int remotePort = DEFAULT_SERVER_PORT; 129 130 private String localName = DEFAULT_SERVER_NAME; 131 132 private String localAddr = DEFAULT_SERVER_ADDR; 133 134 private int localPort = DEFAULT_SERVER_PORT; 135 136 137 141 private String authType; 142 143 private Cookie [] cookies; 144 145 148 private final Hashtable headers = new Hashtable (); 149 150 private String method; 151 152 private String pathInfo; 153 154 private String contextPath = ""; 155 156 private String queryString; 157 158 private String remoteUser; 159 160 private final Set userRoles = new HashSet (); 161 162 private Principal userPrincipal; 163 164 private String requestURI; 165 166 private String servletPath = ""; 167 168 private HttpSession session; 169 170 private boolean requestedSessionIdValid = true; 171 172 private boolean requestedSessionIdFromCookie = true; 173 174 private boolean requestedSessionIdFromURL = false; 175 176 177 181 186 public MockHttpServletRequest() { 187 this(null, "", ""); 188 } 189 190 199 public MockHttpServletRequest(String method, String requestURI) { 200 this(null, method, requestURI); 201 } 202 203 209 public MockHttpServletRequest(ServletContext servletContext) { 210 this(servletContext, "", ""); 211 } 212 213 223 public MockHttpServletRequest(ServletContext servletContext, String method, String requestURI) { 224 this.servletContext = (servletContext != null ? servletContext : new MockServletContext()); 225 this.method = method; 226 this.requestURI = requestURI; 227 this.locales.add(Locale.ENGLISH); 228 } 229 230 231 235 238 public boolean isActive() { 239 return this.active; 240 } 241 242 245 public void close() { 246 this.active = false; 247 } 248 249 252 public void invalidate() { 253 close(); 254 clearAttributes(); 255 } 256 257 261 protected void checkActive() throws IllegalStateException { 262 if (!this.active) { 263 throw new IllegalStateException ("Request is not active anymore"); 264 } 265 } 266 267 268 272 public Object getAttribute(String name) { 273 checkActive(); 274 return this.attributes.get(name); 275 } 276 277 public Enumeration getAttributeNames() { 278 checkActive(); 279 return this.attributes.keys(); 280 } 281 282 public String getCharacterEncoding() { 283 return this.characterEncoding; 284 } 285 286 public void setCharacterEncoding(String characterEncoding) { 287 this.characterEncoding = characterEncoding; 288 } 289 290 public void setContent(byte[] content) { 291 this.content = content; 292 } 293 294 public int getContentLength() { 295 return (this.content != null ? this.content.length : -1); 296 } 297 298 public void setContentType(String contentType) { 299 this.contentType = contentType; 300 } 301 302 public String getContentType() { 303 return this.contentType; 304 } 305 306 public ServletInputStream getInputStream() { 307 if (this.content != null) { 308 return new DelegatingServletInputStream(new ByteArrayInputStream (this.content)); 309 } 310 else { 311 return null; 312 } 313 } 314 315 320 public void setParameter(String name, String value) { 321 setParameter(name, new String [] {value}); 322 } 323 324 329 public void setParameter(String name, String [] values) { 330 Assert.notNull(name, "Parameter name must not be null"); 331 this.parameters.put(name, values); 332 } 333 334 339 public void addParameter(String name, String value) { 340 addParameter(name, new String [] {value}); 341 } 342 343 348 public void addParameter(String name, String [] values) { 349 Assert.notNull(name, "Parameter name must not be null"); 350 String [] oldArr = (String []) this.parameters.get(name); 351 if (oldArr != null) { 352 String [] newArr = new String [oldArr.length + values.length]; 353 System.arraycopy(oldArr, 0, newArr, 0, oldArr.length); 354 System.arraycopy(values, 0, newArr, oldArr.length, values.length); 355 this.parameters.put(name, newArr); 356 } 357 else { 358 this.parameters.put(name, values); 359 } 360 } 361 362 365 public void removeParameter(String name) { 366 Assert.notNull(name, "Parameter name must not be null"); 367 this.parameters.remove(name); 368 } 369 370 public String getParameter(String name) { 371 Assert.notNull(name, "Parameter name must not be null"); 372 String [] arr = (String []) this.parameters.get(name); 373 return (arr != null && arr.length > 0 ? arr[0] : null); 374 } 375 376 public Enumeration getParameterNames() { 377 return Collections.enumeration(this.parameters.keySet()); 378 } 379 380 public String [] getParameterValues(String name) { 381 Assert.notNull(name, "Parameter name must not be null"); 382 return (String []) this.parameters.get(name); 383 } 384 385 public Map getParameterMap() { 386 return Collections.unmodifiableMap(this.parameters); 387 } 388 389 public void setProtocol(String protocol) { 390 this.protocol = protocol; 391 } 392 393 public String getProtocol() { 394 return this.protocol; 395 } 396 397 public void setScheme(String scheme) { 398 this.scheme = scheme; 399 } 400 401 public String getScheme() { 402 return this.scheme; 403 } 404 405 public void setServerName(String serverName) { 406 this.serverName = serverName; 407 } 408 409 public String getServerName() { 410 return this.serverName; 411 } 412 413 public void setServerPort(int serverPort) { 414 this.serverPort = serverPort; 415 } 416 417 public int getServerPort() { 418 return this.serverPort; 419 } 420 421 public BufferedReader getReader() throws UnsupportedEncodingException { 422 if (this.content != null) { 423 InputStream sourceStream = new ByteArrayInputStream (this.content); 424 Reader sourceReader = (this.characterEncoding != null) ? 425 new InputStreamReader (sourceStream, this.characterEncoding) : new InputStreamReader (sourceStream); 426 return new BufferedReader (sourceReader); 427 } 428 else { 429 return null; 430 } 431 } 432 433 public void setRemoteAddr(String remoteAddr) { 434 this.remoteAddr = remoteAddr; 435 } 436 437 public String getRemoteAddr() { 438 return this.remoteAddr; 439 } 440 441 public void setRemoteHost(String remoteHost) { 442 this.remoteHost = remoteHost; 443 } 444 445 public String getRemoteHost() { 446 return this.remoteHost; 447 } 448 449 public void setAttribute(String name, Object value) { 450 checkActive(); 451 Assert.notNull(name, "Attribute name must not be null"); 452 if (value != null) { 453 this.attributes.put(name, value); 454 } 455 else { 456 this.attributes.remove(name); 457 } 458 } 459 460 public void removeAttribute(String name) { 461 checkActive(); 462 Assert.notNull(name, "Attribute name must not be null"); 463 this.attributes.remove(name); 464 } 465 466 469 public void clearAttributes() { 470 this.attributes.clear(); 471 } 472 473 476 public void addPreferredLocale(Locale locale) { 477 Assert.notNull(locale, "Locale must not be null"); 478 this.locales.add(0, locale); 479 } 480 481 public Locale getLocale() { 482 return (Locale ) this.locales.get(0); 483 } 484 485 public Enumeration getLocales() { 486 return this.locales.elements(); 487 } 488 489 public void setSecure(boolean secure) { 490 this.secure = secure; 491 } 492 493 public boolean isSecure() { 494 return this.secure; 495 } 496 497 public RequestDispatcher getRequestDispatcher(String path) { 498 return new MockRequestDispatcher(path); 499 } 500 501 public String getRealPath(String path) { 502 return this.servletContext.getRealPath(path); 503 } 504 505 public void setRemotePort(int remotePort) { 506 this.remotePort = remotePort; 507 } 508 509 public int getRemotePort() { 510 return this.remotePort; 511 } 512 513 public void setLocalName(String localName) { 514 this.localName = localName; 515 } 516 517 public String getLocalName() { 518 return this.localName; 519 } 520 521 public void setLocalAddr(String localAddr) { 522 this.localAddr = localAddr; 523 } 524 525 public String getLocalAddr() { 526 return this.localAddr; 527 } 528 529 public void setLocalPort(int localPort) { 530 this.localPort = localPort; 531 } 532 533 public int getLocalPort() { 534 return this.localPort; 535 } 536 537 538 542 public void setAuthType(String authType) { 543 this.authType = authType; 544 } 545 546 public String getAuthType() { 547 return this.authType; 548 } 549 550 public void setCookies(Cookie [] cookies) { 551 this.cookies = cookies; 552 } 553 554 public Cookie [] getCookies() { 555 return this.cookies; 556 } 557 558 575 public void addHeader(String name, Object value) { 576 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 577 Assert.notNull(value, "Header value must not be null"); 578 if (header == null) { 579 header = new HeaderValueHolder(); 580 this.headers.put(name, header); 581 } 582 if (value instanceof Collection ) { 583 header.addValues((Collection ) value); 584 } 585 else if (value.getClass().isArray()) { 586 header.addValueArray(value); 587 } 588 else { 589 header.addValue(value); 590 } 591 } 592 593 public long getDateHeader(String name) { 594 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 595 Object value = (header != null ? header.getValue() : null); 596 if (value instanceof Date ) { 597 return ((Date ) value).getTime(); 598 } 599 else if (value instanceof Number ) { 600 return ((Number ) value).longValue(); 601 } 602 else if (value != null) { 603 throw new IllegalArgumentException ( 604 "Value for header '" + name + "' is neither a Date nor a Number: " + value); 605 } 606 else { 607 return -1L; 608 } 609 } 610 611 public String getHeader(String name) { 612 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 613 return (header != null ? header.getValue().toString() : null); 614 } 615 616 public Enumeration getHeaders(String name) { 617 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 618 return Collections.enumeration(header != null ? header.getValues() : Collections.EMPTY_LIST); 619 } 620 621 public Enumeration getHeaderNames() { 622 return this.headers.keys(); 623 } 624 625 public int getIntHeader(String name) { 626 HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name); 627 Object value = (header != null ? header.getValue() : null); 628 if (value instanceof Number ) { 629 return ((Number ) value).intValue(); 630 } 631 else if (value instanceof String ) { 632 return Integer.parseInt((String ) value); 633 } 634 else if (value != null) { 635 throw new NumberFormatException ("Value for header '" + name + "' is not a Number: " + value); 636 } 637 else { 638 return -1; 639 } 640 } 641 642 public void setMethod(String method) { 643 this.method = method; 644 } 645 646 public String getMethod() { 647 return this.method; 648 } 649 650 public void setPathInfo(String pathInfo) { 651 this.pathInfo = pathInfo; 652 } 653 654 public String getPathInfo() { 655 return this.pathInfo; 656 } 657 658 public String getPathTranslated() { 659 return (this.pathInfo != null ? getRealPath(this.pathInfo) : null); 660 } 661 662 public void setContextPath(String contextPath) { 663 this.contextPath = contextPath; 664 } 665 666 public String getContextPath() { 667 return this.contextPath; 668 } 669 670 public void setQueryString(String queryString) { 671 this.queryString = queryString; 672 } 673 674 public String getQueryString() { 675 return this.queryString; 676 } 677 678 public void setRemoteUser(String remoteUser) { 679 this.remoteUser = remoteUser; 680 } 681 682 public String getRemoteUser() { 683 return this.remoteUser; 684 } 685 686 690 public void addRole(String role) { 691 addUserRole(role); 692 } 693 694 public void addUserRole(String role) { 695 this.userRoles.add(role); 696 } 697 698 public boolean isUserInRole(String role) { 699 return this.userRoles.contains(role); 700 } 701 702 public void setUserPrincipal(Principal userPrincipal) { 703 this.userPrincipal = userPrincipal; 704 } 705 706 public Principal getUserPrincipal() { 707 return this.userPrincipal; 708 } 709 710 public String getRequestedSessionId() { 711 HttpSession session = getSession(); 712 return (session != null ? session.getId() : null); 713 } 714 715 public void setRequestURI(String requestURI) { 716 this.requestURI = requestURI; 717 } 718 719 public String getRequestURI() { 720 return this.requestURI; 721 } 722 723 public StringBuffer getRequestURL() { 724 StringBuffer url = new StringBuffer (this.scheme); 725 url.append("://").append(this.serverName).append(':').append(this.serverPort); 726 url.append(getRequestURI()); 727 return url; 728 } 729 730 public void setServletPath(String servletPath) { 731 this.servletPath = servletPath; 732 } 733 734 public String getServletPath() { 735 return this.servletPath; 736 } 737 738 public void setSession(HttpSession session) { 739 this.session = session; 740 if (session instanceof MockHttpSession) { 741 MockHttpSession mockSession = ((MockHttpSession) session); 742 mockSession.access(); 743 } 744 } 745 746 public HttpSession getSession(boolean create) { 747 checkActive(); 748 if (this.session instanceof MockHttpSession && ((MockHttpSession) this.session).isInvalid()) { 750 this.session = null; 751 } 752 if (this.session == null && create) { 754 this.session = new MockHttpSession(this.servletContext); 755 } 756 return this.session; 757 } 758 759 public HttpSession getSession() { 760 return getSession(true); 761 } 762 763 public void setRequestedSessionIdValid(boolean requestedSessionIdValid) { 764 this.requestedSessionIdValid = requestedSessionIdValid; 765 } 766 767 public boolean isRequestedSessionIdValid() { 768 return this.requestedSessionIdValid; 769 } 770 771 public void setRequestedSessionIdFromCookie(boolean requestedSessionIdFromCookie) { 772 this.requestedSessionIdFromCookie = requestedSessionIdFromCookie; 773 } 774 775 public boolean isRequestedSessionIdFromCookie() { 776 return this.requestedSessionIdFromCookie; 777 } 778 779 public void setRequestedSessionIdFromURL(boolean requestedSessionIdFromURL) { 780 this.requestedSessionIdFromURL = requestedSessionIdFromURL; 781 } 782 783 public boolean isRequestedSessionIdFromURL() { 784 return this.requestedSessionIdFromURL; 785 } 786 787 public boolean isRequestedSessionIdFromUrl() { 788 return isRequestedSessionIdFromURL(); 789 } 790 791 } 792 | Popular Tags |