1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.NoSuchElementException ; 29 30 import javax.servlet.RequestDispatcher ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletRequestWrapper ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.apache.catalina.Context; 36 import org.apache.catalina.Globals; 37 import org.apache.catalina.Session; 38 import org.apache.catalina.Manager; 39 import org.apache.catalina.util.Enumerator; 40 import org.apache.catalina.util.RequestUtil; 41 import org.apache.catalina.util.StringManager; 42 43 44 60 61 class ApplicationHttpRequest extends HttpServletRequestWrapper { 62 63 64 66 67 70 protected static final String specials[] = 71 { Globals.INCLUDE_REQUEST_URI_ATTR, Globals.INCLUDE_CONTEXT_PATH_ATTR, 72 Globals.INCLUDE_SERVLET_PATH_ATTR, Globals.INCLUDE_PATH_INFO_ATTR, 73 Globals.INCLUDE_QUERY_STRING_ATTR, Globals.FORWARD_REQUEST_URI_ATTR, 74 Globals.FORWARD_CONTEXT_PATH_ATTR, Globals.FORWARD_SERVLET_PATH_ATTR, 75 Globals.FORWARD_PATH_INFO_ATTR, Globals.FORWARD_QUERY_STRING_ATTR }; 76 77 78 81 protected static StringManager sm = 82 StringManager.getManager(Constants.Package); 83 84 85 87 88 93 public ApplicationHttpRequest(HttpServletRequest request, Context context, 94 boolean crossContext) { 95 96 super(request); 97 this.context = context; 98 this.crossContext = crossContext; 99 setRequest(request); 100 101 } 102 103 104 106 107 110 protected Context context = null; 111 112 113 116 protected String contextPath = null; 117 118 119 123 protected boolean crossContext = false; 124 125 126 129 protected Object dispatcherType = null; 130 131 132 135 protected static final String info = 136 "org.apache.catalina.core.ApplicationHttpRequest/1.0"; 137 138 139 143 protected Map parameters = null; 144 145 146 149 private boolean parsedParams = false; 150 151 152 155 protected String pathInfo = null; 156 157 158 161 private String queryParamString = null; 162 163 164 167 protected String queryString = null; 168 169 170 173 protected Object requestDispatcherPath = null; 174 175 176 179 protected String requestURI = null; 180 181 182 185 protected String servletPath = null; 186 187 188 191 protected Session session = null; 192 193 194 197 protected Object [] specialAttributes = new Object [specials.length]; 198 199 200 202 203 208 public Object getAttribute(String name) { 209 210 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 211 return dispatcherType; 212 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 213 if ( requestDispatcherPath != null ){ 214 return requestDispatcherPath.toString(); 215 } else { 216 return null; 217 } 218 } 219 220 int pos = getSpecial(name); 221 if (pos == -1) { 222 return getRequest().getAttribute(name); 223 } else { 224 if ((specialAttributes[pos] == null) 225 && (specialAttributes[5] == null) && (pos >= 5)) { 226 return getRequest().getAttribute(name); 230 } else { 231 return specialAttributes[pos]; 232 } 233 } 234 235 } 236 237 238 242 public Enumeration getAttributeNames() { 243 return (new AttributeNamesEnumerator()); 244 } 245 246 247 253 public void removeAttribute(String name) { 254 255 if (!removeSpecial(name)) 256 getRequest().removeAttribute(name); 257 258 } 259 260 261 268 public void setAttribute(String name, Object value) { 269 270 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 271 dispatcherType = value; 272 return; 273 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 274 requestDispatcherPath = value; 275 return; 276 } 277 278 if (!setSpecial(name, value)) { 279 getRequest().setAttribute(name, value); 280 } 281 282 } 283 284 285 291 public RequestDispatcher getRequestDispatcher(String path) { 292 293 if (context == null) 294 return (null); 295 296 if (path == null) 298 return (null); 299 else if (path.startsWith("/")) 300 return (context.getServletContext().getRequestDispatcher(path)); 301 302 String servletPath = 304 (String ) getAttribute(Globals.INCLUDE_SERVLET_PATH_ATTR); 305 if (servletPath == null) 306 servletPath = getServletPath(); 307 308 String pathInfo = getPathInfo(); 310 String requestPath = null; 311 312 if (pathInfo == null) { 313 requestPath = servletPath; 314 } else { 315 requestPath = servletPath + pathInfo; 316 } 317 318 int pos = requestPath.lastIndexOf('/'); 319 String relative = null; 320 if (pos >= 0) { 321 relative = RequestUtil.normalize 322 (requestPath.substring(0, pos + 1) + path); 323 } else { 324 relative = RequestUtil.normalize(requestPath + path); 325 } 326 327 return (context.getServletContext().getRequestDispatcher(relative)); 328 329 } 330 331 332 334 335 339 public String getContextPath() { 340 341 return (this.contextPath); 342 343 } 344 345 346 351 public String getParameter(String name) { 352 353 parseParameters(); 354 355 Object value = parameters.get(name); 356 if (value == null) 357 return (null); 358 else if (value instanceof String []) 359 return (((String []) value)[0]); 360 else if (value instanceof String ) 361 return ((String ) value); 362 else 363 return (value.toString()); 364 365 } 366 367 368 372 public Map getParameterMap() { 373 374 parseParameters(); 375 return (parameters); 376 377 } 378 379 380 384 public Enumeration getParameterNames() { 385 386 parseParameters(); 387 return (new Enumerator(parameters.keySet())); 388 389 } 390 391 392 398 public String [] getParameterValues(String name) { 399 400 parseParameters(); 401 Object value = parameters.get(name); 402 if (value == null) 403 return ((String []) null); 404 else if (value instanceof String []) 405 return ((String []) value); 406 else if (value instanceof String ) { 407 String values[] = new String [1]; 408 values[0] = (String ) value; 409 return (values); 410 } else { 411 String values[] = new String [1]; 412 values[0] = value.toString(); 413 return (values); 414 } 415 416 } 417 418 419 422 public String getPathInfo() { 423 424 return (this.pathInfo); 425 426 } 427 428 429 433 public String getQueryString() { 434 435 return (this.queryString); 436 437 } 438 439 440 444 public String getRequestURI() { 445 446 return (this.requestURI); 447 448 } 449 450 451 455 public StringBuffer getRequestURL() { 456 457 StringBuffer url = new StringBuffer (); 458 String scheme = getScheme(); 459 int port = getServerPort(); 460 if (port < 0) 461 port = 80; 463 url.append(scheme); 464 url.append("://"); 465 url.append(getServerName()); 466 if ((scheme.equals("http") && (port != 80)) 467 || (scheme.equals("https") && (port != 443))) { 468 url.append(':'); 469 url.append(port); 470 } 471 url.append(getRequestURI()); 472 473 return (url); 474 475 } 476 477 478 482 public String getServletPath() { 483 484 return (this.servletPath); 485 486 } 487 488 489 493 public HttpSession getSession() { 494 return (getSession(true)); 495 } 496 497 498 504 public HttpSession getSession(boolean create) { 505 506 if (crossContext) { 507 508 if (context == null) 510 return (null); 511 512 if (session != null && session.isValid()) { 514 return (session.getSession()); 515 } 516 517 HttpSession other = super.getSession(false); 518 if (create && (other == null)) { 519 other = super.getSession(true); 523 } 524 if (other != null) { 525 Session localSession = null; 526 try { 527 localSession = 528 context.getManager().findSession(other.getId()); 529 } catch (IOException e) { 530 } 532 if (localSession == null && create) { 533 localSession = 534 context.getManager().createSession(other.getId()); 535 } 536 if (localSession != null) { 537 localSession.access(); 538 session = localSession; 539 return session.getSession(); 540 } 541 } 542 return null; 543 544 } else { 545 return super.getSession(create); 546 } 547 548 } 549 550 551 558 public boolean isRequestedSessionIdValid() { 559 560 if (crossContext) { 561 562 String requestedSessionId = getRequestedSessionId(); 563 if (requestedSessionId == null) 564 return (false); 565 if (context == null) 566 return (false); 567 Manager manager = context.getManager(); 568 if (manager == null) 569 return (false); 570 Session session = null; 571 try { 572 session = manager.findSession(requestedSessionId); 573 } catch (IOException e) { 574 session = null; 575 } 576 if ((session != null) && session.isValid()) { 577 return (true); 578 } else { 579 return (false); 580 } 581 582 } else { 583 return super.isRequestedSessionIdValid(); 584 } 585 } 586 587 588 590 591 594 public void recycle() { 595 if (session != null) { 596 session.endAccess(); 597 } 598 } 599 600 601 604 public String getInfo() { 605 606 return (info); 607 608 } 609 610 611 616 Map copyMap(Map orig) { 617 618 if (orig == null) 619 return (new HashMap ()); 620 HashMap dest = new HashMap (); 621 Iterator keys = orig.keySet().iterator(); 622 while (keys.hasNext()) { 623 String key = (String ) keys.next(); 624 dest.put(key, orig.get(key)); 625 } 626 return (dest); 627 628 } 629 630 631 636 void setContextPath(String contextPath) { 637 638 this.contextPath = contextPath; 639 640 } 641 642 643 648 void setPathInfo(String pathInfo) { 649 650 this.pathInfo = pathInfo; 651 652 } 653 654 655 660 void setQueryString(String queryString) { 661 662 this.queryString = queryString; 663 664 } 665 666 667 672 void setRequest(HttpServletRequest request) { 673 674 super.setRequest(request); 675 676 dispatcherType = request.getAttribute(Globals.DISPATCHER_TYPE_ATTR); 678 requestDispatcherPath = 679 request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR); 680 681 contextPath = request.getContextPath(); 683 pathInfo = request.getPathInfo(); 684 queryString = request.getQueryString(); 685 requestURI = request.getRequestURI(); 686 servletPath = request.getServletPath(); 687 688 } 689 690 691 696 void setRequestURI(String requestURI) { 697 698 this.requestURI = requestURI; 699 700 } 701 702 703 708 void setServletPath(String servletPath) { 709 710 this.servletPath = servletPath; 711 712 } 713 714 715 721 void parseParameters() { 722 723 if (parsedParams) { 724 return; 725 } 726 727 parameters = new HashMap (); 728 parameters = copyMap(getRequest().getParameterMap()); 729 mergeParameters(); 730 parsedParams = true; 731 } 732 733 734 740 void setQueryParams(String queryString) { 741 this.queryParamString = queryString; 742 } 743 744 745 747 748 754 protected boolean isSpecial(String name) { 755 756 for (int i = 0; i < specials.length; i++) { 757 if (specials[i].equals(name)) 758 return (true); 759 } 760 return (false); 761 762 } 763 764 765 771 protected int getSpecial(String name) { 772 for (int i = 0; i < specials.length; i++) { 773 if (specials[i].equals(name)) { 774 return (i); 775 } 776 } 777 return (-1); 778 } 779 780 781 786 protected boolean setSpecial(String name, Object value) { 787 for (int i = 0; i < specials.length; i++) { 788 if (specials[i].equals(name)) { 789 specialAttributes[i] = value; 790 return (true); 791 } 792 } 793 return (false); 794 } 795 796 797 802 protected boolean removeSpecial(String name) { 803 for (int i = 0; i < specials.length; i++) { 804 if (specials[i].equals(name)) { 805 specialAttributes[i] = null; 806 return (true); 807 } 808 } 809 return (false); 810 } 811 812 813 819 protected String [] mergeValues(Object values1, Object values2) { 820 821 ArrayList results = new ArrayList (); 822 823 if (values1 == null) 824 ; 825 else if (values1 instanceof String ) 826 results.add(values1); 827 else if (values1 instanceof String []) { 828 String values[] = (String []) values1; 829 for (int i = 0; i < values.length; i++) 830 results.add(values[i]); 831 } else 832 results.add(values1.toString()); 833 834 if (values2 == null) 835 ; 836 else if (values2 instanceof String ) 837 results.add(values2); 838 else if (values2 instanceof String []) { 839 String values[] = (String []) values2; 840 for (int i = 0; i < values.length; i++) 841 results.add(values[i]); 842 } else 843 results.add(values2.toString()); 844 845 String values[] = new String [results.size()]; 846 return ((String []) results.toArray(values)); 847 848 } 849 850 851 853 854 860 private void mergeParameters() { 861 862 if ((queryParamString == null) || (queryParamString.length() < 1)) 863 return; 864 865 HashMap queryParameters = new HashMap (); 866 String encoding = getCharacterEncoding(); 867 if (encoding == null) 868 encoding = "ISO-8859-1"; 869 try { 870 RequestUtil.parseParameters 871 (queryParameters, queryParamString, encoding); 872 } catch (Exception e) { 873 ; 874 } 875 Iterator keys = parameters.keySet().iterator(); 876 while (keys.hasNext()) { 877 String key = (String ) keys.next(); 878 Object value = queryParameters.get(key); 879 if (value == null) { 880 queryParameters.put(key, parameters.get(key)); 881 continue; 882 } 883 queryParameters.put 884 (key, mergeValues(value, parameters.get(key))); 885 } 886 parameters = queryParameters; 887 888 } 889 890 891 893 894 898 protected class AttributeNamesEnumerator implements Enumeration { 899 900 protected int pos = -1; 901 protected int last = -1; 902 protected Enumeration parentEnumeration = null; 903 protected String next = null; 904 905 public AttributeNamesEnumerator() { 906 parentEnumeration = getRequest().getAttributeNames(); 907 for (int i = 0; i < specialAttributes.length; i++) { 908 if (getAttribute(specials[i]) != null) { 909 last = i; 910 } 911 } 912 } 913 914 public boolean hasMoreElements() { 915 return ((pos != last) || (next != null) 916 || ((next = findNext()) != null)); 917 } 918 919 public Object nextElement() { 920 if (pos != last) { 921 for (int i = pos + 1; i <= last; i++) { 922 if (getAttribute(specials[i]) != null) { 923 pos = i; 924 return (specials[i]); 925 } 926 } 927 } 928 String result = next; 929 if (next != null) { 930 next = findNext(); 931 } else { 932 throw new NoSuchElementException (); 933 } 934 return result; 935 } 936 937 protected String findNext() { 938 String result = null; 939 while ((result == null) && (parentEnumeration.hasMoreElements())) { 940 String current = (String ) parentEnumeration.nextElement(); 941 if (!isSpecial(current)) { 942 result = current; 943 } 944 } 945 return result; 946 } 947 948 } 949 950 951 } 952 | Popular Tags |