| 1 10 11 package mondrian.tui; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.BufferedReader ; 16 import java.io.StringReader ; 17 import java.io.UnsupportedEncodingException ; 18 import java.text.SimpleDateFormat ; 19 import java.text.ParseException ; 20 import java.util.Locale ; 21 import java.util.Collections ; 22 import java.util.Enumeration ; 23 import java.util.LinkedHashMap ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Date ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.security.Principal ; 30 import javax.servlet.RequestDispatcher ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import javax.servlet.ServletInputStream ; 35 import javax.servlet.http.Cookie ; 36 import javax.servlet.http.HttpSession ; 37 import javax.servlet.http.HttpServletRequest ; 38 39 50 public class MockHttpServletRequest implements HttpServletRequest { 51 public static String AUTHORIZATION = "Authorization"; 52 public final static String DATE_FORMAT_HEADER = "EEE, d MMM yyyy HH:mm:ss Z"; 53 54 public class MockRequestDispatcher implements RequestDispatcher { 55 private ServletRequest forwardedRequest; 56 private ServletResponse forwardedResponse; 57 private ServletRequest includedRequest; 58 private ServletResponse includedResponse; 59 private String path; 60 61 MockRequestDispatcher() { 62 } 63 public void setPath(String path) { 64 this.path = path; 65 } 66 public String getPath() { 67 return this.path; 68 } 69 public void forward(ServletRequest request, ServletResponse response) 70 throws ServletException , IOException { 71 this.forwardedRequest = request; 72 this.forwardedResponse = response; 73 } 74 public void include(ServletRequest request, ServletResponse response) 75 throws ServletException , IOException { 76 this.includedRequest = request; 77 this.includedResponse = response; 78 } 79 80 public ServletRequest getForwardedRequest() { 81 return this.forwardedRequest; 82 } 83 84 public ServletResponse getForwardedResponse() { 85 return this.forwardedResponse; 86 } 87 public ServletRequest getIncludedRequest() { 88 return this.includedRequest; 89 } 90 91 public ServletResponse getIncludedResponse() { 92 return this.includedResponse; 93 } 94 } 95 static class MockServletInputStream extends ServletInputStream { 96 private ByteArrayInputStream stream; 97 98 public MockServletInputStream(byte[] data) { 99 stream = new ByteArrayInputStream (data); 100 } 101 102 public int read() throws IOException { 103 return stream.read(); 104 } 105 } 106 107 108 private HttpSession session; 109 private Map <String , String []> parameters; 111 private Map <String , RequestDispatcher > requestDispatchers; 112 private List <Locale > locales; 113 private String serverName; 114 private String charEncoding; 115 private String method; 116 private String pathInfo; 117 private String pathTranslated; 118 private String contextPath; 119 private String queryString; 120 private String remoteUser; 121 private String requestedSessionId; 122 private String servletPath; 123 private String scheme; 124 private String localName; 125 private String localAddr; 126 private String authType; 127 private String protocol; 128 private String schema; 129 private Principal principal; 130 private List <Cookie > cookies; 131 private boolean requestedSessionIdIsFromCookie; 132 private int remotePort; 133 private int localPort; 134 private int serverPort; 135 private String remoteAddr; 136 private String remoteHost; 137 private Map <String , Object > attributes; 138 private final LinkedHashMap <String , List <String >> headers; 139 private boolean sessionCreated; 140 private String requestedURI; 141 private StringBuffer requestUrl; 142 private String bodyContent; 143 private Map <String , Boolean > roles; 144 145 public MockHttpServletRequest() { 146 this(new byte[0]); 147 } 148 public MockHttpServletRequest(byte[] bytes) { 149 this(new String (bytes)); 150 } 151 public MockHttpServletRequest(String bodyContent) { 152 this.bodyContent = bodyContent; 153 this.attributes = Collections.emptyMap(); 154 this.headers = new LinkedHashMap <String , List <String >>(); 156 this.requestDispatchers = new HashMap <String , RequestDispatcher >(); 157 this.parameters = new HashMap <String , String []>(); 158 this.cookies = new ArrayList <Cookie >(); 159 this.locales = new ArrayList <Locale >(); 160 this.roles = new HashMap <String , Boolean >(); 161 this.requestedSessionIdIsFromCookie = true; 162 this.method = "GET"; 163 this.protocol = "HTTP/1.1"; 164 this.serverName = "localhost"; 165 this.serverPort = 8080; 166 this.scheme = "http"; 167 this.remoteHost = "localhost"; 168 this.remoteAddr = "127.0.0.1"; 169 this.localAddr = "127.0.0.1"; 170 this.localName = "localhost"; 171 this.localPort = 8080; 172 this.remotePort = 5000; 173 174 this.sessionCreated = false; 175 176 } 177 178 179 184 public Object getAttribute(String name) { 185 return this.attributes.get(name); 186 } 187 188 192 public Enumeration getAttributeNames() { 193 return Collections.enumeration(attributes.keySet()); 194 } 195 196 201 public String getCharacterEncoding() { 202 return charEncoding; 203 } 204 205 209 public void setCharacterEncoding(String charEncoding) 210 throws UnsupportedEncodingException { 211 this.charEncoding = charEncoding; 212 } 213 214 219 public int getContentLength() { 220 return getIntHeader("Content-Length"); 221 } 222 223 228 public String getContentType() { 229 return getHeader("Content-Type"); 230 } 231 232 238 public ServletInputStream getInputStream() throws IOException { 239 return new MockServletInputStream(bodyContent.getBytes()); 240 } 241 242 247 public String getParameter(String name) { 248 String [] values = getParameterValues(name); 249 return (null != values && 0 < values.length) 250 ? values[0] : null; 251 } 252 253 258 public Enumeration getParameterNames() { 259 return Collections.enumeration(parameters.keySet()); 260 } 261 262 267 public String [] getParameterValues(String name) { 268 return parameters.get(name); 269 } 270 271 272 277 public String getProtocol() { 278 return protocol; 279 } 280 281 286 public String getScheme() { 287 return schema; 288 } 289 290 294 public String getServerName() { 295 return serverName; 296 } 297 298 302 public int getServerPort() { 303 return serverPort; 304 } 305 306 312 public BufferedReader getReader() throws IOException { 313 return (bodyContent == null) 314 ? null 315 : new BufferedReader (new StringReader (bodyContent)); 316 } 317 318 323 public String getRemoteAddr() { 324 return remoteAddr; 325 } 326 327 332 public String getRemoteHost() { 333 return remoteHost; 334 } 335 336 340 public void setAttribute(String name, Object obj) { 341 if (attributes == Collections.EMPTY_MAP) { 342 attributes = new HashMap <String , Object >(); 343 } 344 this.attributes.put(name, obj); 345 } 346 347 351 public void removeAttribute(String name) { 352 this.attributes.remove(name); 353 } 354 355 360 public Locale getLocale() { 361 return (locales.size() < 1) 362 ? Locale.getDefault() 363 : locales.get(0); 364 } 365 366 372 public Enumeration getLocales() { 373 return Collections.enumeration(locales); 374 } 375 376 381 public boolean isSecure() { 382 String scheme = getScheme(); 383 return (scheme == null) 384 ? false 385 : scheme.equals("https"); 386 } 387 388 393 public RequestDispatcher getRequestDispatcher(String path) { 394 RequestDispatcher dispatcher = 395 requestDispatchers.get(path); 396 if (dispatcher == null) { 397 dispatcher = new MockRequestDispatcher(); 398 setRequestDispatcher(path, dispatcher); 399 } 400 return dispatcher; 401 } 402 403 409 public String getRealPath(String path) { 410 HttpSession session = getSession(); 411 return (session == null) 412 ? null 413 : session.getServletContext().getRealPath(path); 414 415 } 416 417 421 public int getRemotePort() { 422 return remotePort; 423 } 424 425 429 public String getLocalName() { 430 return localName; 431 } 432 433 437 public String getLocalAddr() { 438 return localAddr; 439 } 440 441 445 public int getLocalPort() { 446 return localPort; 447 } 448 449 455 public String getAuthType() { 456 return authType; 457 } 458 459 464 public Cookie [] getCookies() { 465 return cookies.toArray(new Cookie [cookies.size()]); 466 } 467 468 473 public long getDateHeader(String name) { 474 String header = getHeader(name); 475 if (header == null) { 476 return -1; 477 } 478 try { 479 Date dateValue = new SimpleDateFormat (DATE_FORMAT_HEADER, Locale.US).parse(header); 480 return dateValue.getTime(); 481 } catch (ParseException exc) { 482 throw new IllegalArgumentException (exc.getMessage()); 483 } 484 } 485 486 490 public String getHeader(String name) { 491 492 List <String > headerList = headers.get(name); 493 494 return ((headerList == null) || (headerList.size() ==0)) 495 ? null 496 : headerList.get(0); 497 } 498 499 504 public Enumeration getHeaders(String name) { 505 List <String > headerList = headers.get(name); 506 return (headerList == null) 507 ? null 508 : Collections.enumeration(headerList); 509 } 510 511 515 public Enumeration getHeaderNames() { 516 return Collections.enumeration(headers.keySet()); 517 } 518 519 523 public int getIntHeader(String name) { 524 String header = getHeader(name); 525 return (header == null) 526 ? -1 527 : Integer.parseInt(header); 528 } 529 530 535 public String getMethod() { 536 return this.method; 537 } 538 539 544 public String getPathInfo() { 545 return pathInfo; 546 } 547 548 553 public String getPathTranslated() { 554 return pathTranslated; 555 } 556 557 562 public String getContextPath() { 563 return contextPath; 564 } 565 566 571 public String getQueryString() { 572 return queryString; 573 } 574 575 580 public String getRemoteUser() { 581 return remoteUser; 582 } 583 584 589 public boolean isUserInRole(String role) { 590 return roles.get(role); 591 } 592 593 598 public Principal getUserPrincipal() { 599 return principal; 600 } 601 602 606 public String getRequestedSessionId() { 607 HttpSession session = getSession(); 608 return (session == null) 609 ? null 610 : session.getId(); 611 } 612 613 618 public String getRequestURI() { 619 return requestedURI; 620 } 621 622 626 public StringBuffer getRequestURL() { 627 return requestUrl; 628 } 629 630 634 public String getServletPath() { 635 return servletPath; 636 } 637 638 643 public HttpSession getSession(boolean create) { 644 if (! create && ! sessionCreated) { 645 return null; 646 } 647 return getSession(); 648 } 649 650 655 public HttpSession getSession() { 656 sessionCreated = true; 657 return session; 658 } 659 660 664 public boolean isRequestedSessionIdValid() { 665 HttpSession session = getSession(); 666 return (session != null); 667 } 668 669 673 public boolean isRequestedSessionIdFromCookie() { 674 return requestedSessionIdIsFromCookie; 675 } 676 677 682 public boolean isRequestedSessionIdFromURL() { 683 return !requestedSessionIdIsFromCookie; 684 } 685 686 693 public boolean isRequestedSessionIdFromUrl() { 694 return isRequestedSessionIdFromURL(); 695 } 696 697 707 711 public Map getParameterMap() { 712 return Collections.unmodifiableMap(parameters); 713 } 714 715 public void setServerName(String serverName) { 716 this.serverName = serverName; 717 } 718 public void setRemoteHost(String remoteHost) { 719 this.remoteHost = remoteHost; 720 } 721 public void setRemoteAddr(String remoteAddr) { 722 this.remoteAddr = remoteAddr; 723 } 724 public void setMethod(String method) { 725 this.method = method; 726 } 727 public void setPathInfo(String pathInfo) { 728 this.pathInfo = pathInfo; 729 } 730 public void setPathTranslated(String pathTranslated) { 731 this.pathTranslated = pathTranslated; 732 } 733 public void setContextPath(String contextPath) { 734 this.contextPath = contextPath; 735 } 736 public void setQueryString(String queryString) { 737 this.queryString = queryString; 738 } 739 public void setRemoteUser(String remoteUser) { 740 this.remoteUser = remoteUser; 741 } 742 public void setRequestedSessionId(String requestedSessionId) { 743 this.requestedSessionId = requestedSessionId; 744 } 745 public void setRequestURI(String requestedURI) { 746 this.requestedURI = requestedURI; 747 } 748 public void setServletPath(String servletPath) { 749 this.servletPath = servletPath; 750 } 751 public void setLocalName(String localName) { 752 this.localName = localName; 753 } 754 public void setLocalAddr(String localAddr) { 755 this.localAddr = localAddr; 756 } 757 public void setAuthType(String authType) { 758 this.authType = authType; 759 } 760 public void setProtocol(String protocol) { 761 this.protocol = protocol; 762 } 763 public void setScheme(String schema) { 764 this.schema = schema; 765 } 766 767 public void setRemotePort(int remotePort) { 768 this.remotePort = remotePort; 769 } 770 public void setLocalPort(int localPort) { 771 this.localPort = localPort; 772 } 773 public void setServerPort(int serverPort) { 774 this.serverPort = serverPort; 775 } 776 777 public void setContentType(String contentType) { 778 setHeader("Content-Type", contentType); 779 } 780 public void setHeader(String name, String value) { 781 List <String > valueList = headers.get(name); 782 if (valueList == null) { 783 valueList = new ArrayList <String >(); 784 headers.put(name, valueList); 785 } 786 valueList.add(value); 787 } 788 794 public void clearParameters() { 795 parameters.clear(); 796 } 797 798 public void setupAddParameter(String key, String [] values) { 799 parameters.put(key, values); 800 } 801 public void setupAddParameter(String key, String value) { 802 setupAddParameter(key, new String [] { value }); 803 } 804 805 public void clearAttributes() { 806 attributes.clear(); 807 } 808 809 public void setSession(HttpSession session) { 810 this.session = session; 811 } 812 813 public Map <String , RequestDispatcher > getRequestDispatcherMap() { 814 return Collections.unmodifiableMap(requestDispatchers); 815 } 816 817 public void setRequestDispatcher(String path, RequestDispatcher dispatcher) { 818 if(dispatcher instanceof MockRequestDispatcher) { 819 ((MockRequestDispatcher)dispatcher).setPath(path); 820 } 821 requestDispatchers.put(path, dispatcher); 822 } 823 824 public void addLocale(Locale locale) { 825 locales.add(locale); 826 } 827 828 public void addLocales(List <Locale > localeList) { 829 locales.addAll(localeList); 830 } 831 832 public void addHeader(String key, String value) { 833 List <String > valueList = headers.get(key); 834 if (valueList == null) { 835 valueList = new ArrayList <String >(); 836 headers.put(key, valueList); 837 } 838 valueList.add(value); 839 } 840 public void clearHeader(String key) { 841 headers.remove(key); 842 } 843 844 public void setRequestURL(String requestUrl) { 845 this.requestUrl = new StringBuffer (requestUrl); 846 } 847 public void setUserPrincipal(Principal principal) { 848 this.principal = principal; 849 } 850 public void addCookie(Cookie cookie) { 851 cookies.add(cookie); 852 } 853 854 public void setRequestedSessionIdFromCookie(boolean requestedSessionIdIsFromCookie) { 855 this.requestedSessionIdIsFromCookie = requestedSessionIdIsFromCookie; 856 } 857 public void setUserInRole(String role, boolean isInRole) { 858 roles.put(role, isInRole); 859 } 860 861 public void setBodyContent(byte[] data) { 862 setBodyContent(new String (data)); 863 } 864 865 public void setBodyContent(String bodyContent) { 866 this.bodyContent = bodyContent; 867 } 868 869 870 } 871 | Popular Tags |