1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Manager;
import org.apache.catalina.Session;
import org.apache.catalina.util.RequestUtil;
/**
* Wrapper around a <code>javax.servlet.http.HttpServletRequest</code>
* that transforms an application request object (which might be the original
* one passed to a servlet, or might be based on the 2.3
* <code>javax.servlet.http.HttpServletRequestWrapper</code> class)
* back into an internal <code>org.apache.catalina.HttpRequest</code>.
* <p>
* <strong>WARNING</strong>: Due to Java's lack of support for multiple
* inheritance, all of the logic in <code>ApplicationRequest</code> is
* duplicated in <code>ApplicationHttpRequest</code>. Make sure that you
* keep these two classes in synchronization when making changes!
*
* @author Craig R. McClanahan
* @author Remy Maucherat
*/
class ApplicationHttpRequest extends HttpServletRequestWrapper {
// ------------------------------------------------------- Static Variables
/**
* The set of attribute names that are special for request dispatchers.
*/
protected static final String specials[] =
{ RequestDispatcher.INCLUDE_REQUEST_URI,
RequestDispatcher.INCLUDE_CONTEXT_PATH,
RequestDispatcher.INCLUDE_SERVLET_PATH,
RequestDispatcher.INCLUDE_PATH_INFO,
RequestDispatcher.INCLUDE_QUERY_STRING,
RequestDispatcher.FORWARD_REQUEST_URI,
RequestDispatcher.FORWARD_CONTEXT_PATH,
RequestDispatcher.FORWARD_SERVLET_PATH,
RequestDispatcher.FORWARD_PATH_INFO,
RequestDispatcher.FORWARD_QUERY_STRING };
// ----------------------------------------------------------- Constructors
/**
* Construct a new wrapped request around the specified servlet request.
*
* @param request The servlet request being wrapped
*/
public ApplicationHttpRequest(HttpServletRequest request, Context context,
boolean crossContext) {
super(request);
this.context = context;
this.crossContext = crossContext;
setRequest(request);
}
// ----------------------------------------------------- Instance Variables
/**
* The context for this request.
*/
protected Context context = null;
/**
* The context path for this request.
*/
protected String contextPath = null;
/**
* If this request is cross context, since this changes session access
* behavior.
*/
protected boolean crossContext = false;
/**
* The current dispatcher type.
*/
protected DispatcherType dispatcherType = null;
/**
* Descriptive information about this implementation.
*/
protected static final String info =
"org.apache.catalina.core.ApplicationHttpRequest/1.0";
/**
* The request parameters for this request. This is initialized from the
* wrapped request, but updates are allowed.
*/
protected Map<String, String[]> parameters = null;
/**
* Have the parameters for this request already been parsed?
*/
private boolean parsedParams = false;
/**
* The path information for this request.
*/
protected String pathInfo = null;
/**
* The query parameters for the current request.
*/
private String queryParamString = null;
/**
* The query string for this request.
*/
protected String queryString = null;
/**
* The current request dispatcher path.
*/
protected Object requestDispatcherPath = null;
/**
* The request URI for this request.
*/
protected String requestURI = null;
/**
* The servlet path for this request.
*/
protected String servletPath = null;
/**
* The currently active session for this request.
*/
protected Session session = null;
/**
* Special attributes.
*/
protected Object[] specialAttributes = new Object[specials.length];
// ------------------------------------------------- ServletRequest Methods
@Override
public ServletContext getServletContext() {
if (context == null) {
return null;
}
return context.getServletContext();
}
/**
* Override the <code>getAttribute()</code> method of the wrapped request.
*
* @param name Name of the attribute to retrieve
*/
@Override
public Object getAttribute(String name) {
if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
return dispatcherType;
} else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
if ( requestDispatcherPath != null ){
return requestDispatcherPath.toString();
} else {
return null;
}
}
int pos = getSpecial(name);
if (pos == -1) {
return getRequest().getAttribute(name);
} else {
if ((specialAttributes[pos] == null)
&& (specialAttributes[5] == null) && (pos >= 5)) {
// If it's a forward special attribute, and null, it means this
// is an include, so we check the wrapped request since
// the request could have been forwarded before the include
return getRequest().getAttribute(name);
} else {
return specialAttributes[pos];
}
}
}
/**
* Override the <code>getAttributeNames()</code> method of the wrapped
* request.
*/
@Override
public Enumeration<String> getAttributeNames() {
return (new AttributeNamesEnumerator());
}
/**
* Override the <code>removeAttribute()</code> method of the
* wrapped request.
*
* @param name Name of the attribute to remove
*/
@Override
public void removeAttribute(String name) {
if (!removeSpecial(name))
getRequest().removeAttribute(name);
}
/**
* Override the <code>setAttribute()</code> method of the
* wrapped request.
*
* @param name Name of the attribute to set
* @param value Value of the attribute to set
*/
@Override
public void setAttribute(String name, Object value) {
if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) {
dispatcherType = (DispatcherType)value;
return;
} else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) {
requestDispatcherPath = value;
return;
}
if (!setSpecial(name, value)) {
getRequest().setAttribute(name, value);
}
}
/**
* Return a RequestDispatcher that wraps the resource at the specified
* path, which may be interpreted as relative to the current request path.
*
* @param path Path of the resource to be wrapped
*/
@Override
public RequestDispatcher getRequestDispatcher(String path) {
if (context == null)
return (null);
// If the path is already context-relative, just pass it through
if (path == null)
return (null);
else if (path.startsWith("/"))
return (context.getServletContext().getRequestDispatcher(path));
// Convert a request-relative path to a context-relative one
String servletPath =
(String) getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
if (servletPath == null)
servletPath = getServletPath();
// Add the path info, if there is any
String pathInfo = getPathInfo();
String requestPath = null;
if (pathInfo == null) {
requestPath = servletPath;
} else {
requestPath = servletPath + pathInfo;
}
int pos = requestPath.lastIndexOf('/');
String relative = null;
if (pos >= 0) {
relative = requestPath.substring(0, pos + 1) + path;
} else {
relative = requestPath + path;
}
return (context.getServletContext().getRequestDispatcher(relative));
}
/**
* Override the getDispatcherType() method of the wrapped request.
*
*/
@Override
public DispatcherType getDispatcherType() {
return dispatcherType;
}
// --------------------------------------------- HttpServletRequest Methods
/**
* Override the <code>getContextPath()</code> method of the wrapped
* request.
*/
@Override
public String getContextPath() {
return (this.contextPath);
}
/**
* Override the <code>getParameter()</code> method of the wrapped request.
*
* @param name Name of the requested parameter
*/
@Override
public String getParameter(String name) {
parseParameters();
Object value = parameters.get(name);
if (value == null)
return (null);
else if (value instanceof String[])
return (((String[]) value)[0]);
else if (value instanceof String)
return ((String) value);
else
return (value.toString());
}
/**
* Override the <code>getParameterMap()</code> method of the
* wrapped request.
*/
@Override
public Map<String, String[]> getParameterMap() {
parseParameters();
return (parameters);
}
/**
* Override the <code>getParameterNames()</code> method of the
* wrapped request.
*/
@Override
public Enumeration<String> getParameterNames() {
parseParameters();
return Collections.enumeration(parameters.keySet());
}
/**
* Override the <code>getParameterValues()</code> method of the
* wrapped request.
*
* @param name Name of the requested parameter
*/
@Override
public String[] getParameterValues(String name) {
parseParameters();
Object value = parameters.get(name);
if (value == null)
return null;
else if (value instanceof String[])
return ((String[]) value);
else if (value instanceof String) {
String values[] = new String[1];
values[0] = (String) value;
return (values);
} else {
String values[] = new String[1];
values[0] = value.toString();
return (values);
}
}
/**
* Override the <code>getPathInfo()</code> method of the wrapped request.
*/
@Override
public String getPathInfo() {
return (this.pathInfo);
}
/**
* Override the <code>getPathTranslated()</code> method of the wrapped
* request.
*/
@Override
public String getPathTranslated() {
if (getPathInfo() == null || getServletContext() == null) {
return null;
}
return getServletContext().getRealPath(getPathInfo());
}
/**
* Override the <code>getQueryString()</code> method of the wrapped
* request.
*/
@Override
public String getQueryString() {
return (this.queryString);
}
/**
* Override the <code>getRequestURI()</code> method of the wrapped
* request.
*/
@Override
public String getRequestURI() {
return (this.requestURI);
}
/**
* Override the <code>getRequestURL()</code> method of the wrapped
* request.
*/
@Override
public StringBuffer getRequestURL() {
StringBuffer url = new StringBuffer();
String scheme = getScheme();
int port = getServerPort();
if (port < 0)
port = 80; // Work around java.net.URL bug
url.append(scheme);
url.append("://");
url.append(getServerName());
if ((scheme.equals("http") && (port != 80))
|| (scheme.equals("https") && (port != 443))) {
url.append(':');
url.append(port);
}
url.append(getRequestURI());
return (url);
}
/**
* Override the <code>getServletPath()</code> method of the wrapped
* request.
*/
@Override
public String getServletPath() {
return (this.servletPath);
}
/**
* Return the session associated with this Request, creating one
* if necessary.
*/
@Override
public HttpSession getSession() {
return (getSession(true));
}
/**
* Return the session associated with this Request, creating one
* if necessary and requested.
*
* @param create Create a new session if one does not exist
*/
@Override
public HttpSession getSession(boolean create) {
if (crossContext) {
// There cannot be a session if no context has been assigned yet
if (context == null)
return (null);
// Return the current session if it exists and is valid
if (session != null && session.isValid()) {
return (session.getSession());
}
HttpSession other = super.getSession(false);
if (create && (other == null)) {
// First create a session in the first context: the problem is
// that the top level request is the only one which can
// create the cookie safely
other = super.getSession(true);
}
if (other != null) {
Session localSession = null;
try {
localSession =
context.getManager().findSession(other.getId());
if (localSession != null && !localSession.isValid()) {
localSession = null;
}
} catch (IOException e) {
// Ignore
}
if (localSession == null && create) {
localSession =
context.getManager().createSession(other.getId());
}
if (localSession != null) {
localSession.access();
session = localSession;
return session.getSession();
}
}
return null;
} else {
return super.getSession(create);
}
}
/**
* Returns true if the request specifies a JSESSIONID that is valid within
* the context of this ApplicationHttpRequest, false otherwise.
*
* @return true if the request specifies a JSESSIONID that is valid within
* the context of this ApplicationHttpRequest, false otherwise.
*/
@Override
public boolean isRequestedSessionIdValid() {
if (crossContext) {
String requestedSessionId = getRequestedSessionId();
if (requestedSessionId == null)
return (false);
if (context == null)
return (false);
Manager manager = context.getManager();
if (manager == null)
return (false);
Session session = null;
try {
session = manager.findSession(requestedSessionId);
} catch (IOException e) {
// Ignore
}
if ((session != null) && session.isValid()) {
return (true);
} else {
return (false);
}
} else {
return super.isRequestedSessionIdValid();
}
}
// -------------------------------------------------------- Package Methods
/**
* Recycle this request
*/
public void recycle() {
if (session != null) {
session.endAccess();
}
}
/**
* Return descriptive information about this implementation.
*/
public String getInfo() {
return (info);
}
/**
* Perform a shallow copy of the specified Map, and return the result.
*
* @param orig Origin Map to be copied
*/
Map<String, String[]> copyMap(Map<String, String[]> orig) {
if (orig == null)
return (new HashMap<String, String[]>());
HashMap<String, String[]> dest = new HashMap<String, String[]>();
for (Map.Entry<String, String[]> entry : orig.entrySet()) {
dest.put(entry.getKey(), entry.getValue());
}
return (dest);
}
/**
* Set the context path for this request.
*
* @param contextPath The new context path
*/
void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
/**
* Set the path information for this request.
*
* @param pathInfo The new path info
*/
void setPathInfo(String pathInfo) {
this.pathInfo = pathInfo;
}
/**
* Set the query string for this request.
*
* @param queryString The new query string
*/
void setQueryString(String queryString) {
this.queryString = queryString;
}
/**
* Set the request that we are wrapping.
*
* @param request The new wrapped request
*/
void setRequest(HttpServletRequest request) {
super.setRequest(request);
// Initialize the attributes for this request
dispatcherType = (DispatcherType)request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
requestDispatcherPath =
request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);
// Initialize the path elements for this request
contextPath = request.getContextPath();
pathInfo = request.getPathInfo();
queryString = request.getQueryString();
requestURI = request.getRequestURI();
servletPath = request.getServletPath();
}
/**
* Set the request URI for this request.
*
* @param requestURI The new request URI
*/
void setRequestURI(String requestURI) {
this.requestURI = requestURI;
}
/**
* Set the servlet path for this request.
*
* @param servletPath The new servlet path
*/
void setServletPath(String servletPath) {
this.servletPath = servletPath;
}
/**
* Parses the parameters of this request.
*
* If parameters are present in both the query string and the request
* content, they are merged.
*/
void parseParameters() {
if (parsedParams) {
return;
}
parameters = new HashMap<String, String[]>();
parameters = copyMap(getRequest().getParameterMap());
mergeParameters();
parsedParams = true;
}
/**
* Save query parameters for this request.
*
* @param queryString The query string containing parameters for this
* request
*/
void setQueryParams(String queryString) {
this.queryParamString = queryString;
}
// ------------------------------------------------------ Protected Methods
/**
* Is this attribute name one of the special ones that is added only for
* included servlets?
*
* @param name Attribute name to be tested
*/
protected boolean isSpecial(String name) {
for (int i = 0; i < specials.length; i++) {
if (specials[i].equals(name))
return (true);
}
return (false);
}
/**
* Get a special attribute.
*
* @return the special attribute pos, or -1 if it is not a special
* attribute
*/
protected int getSpecial(String name) {
for (int i = 0; i < specials.length; i++) {
if (specials[i].equals(name)) {
return (i);
}
}
return (-1);
}
/**
* Set a special attribute.
*
* @return true if the attribute was a special attribute, false otherwise
*/
protected boolean setSpecial(String name, Object value) {
for (int i = 0; i < specials.length; i++) {
if (specials[i].equals(name)) {
specialAttributes[i] = value;
return (true);
}
}
return (false);
}
/**
* Remove a special attribute.
*
* @return true if the attribute was a special attribute, false otherwise
*/
protected boolean removeSpecial(String name) {
for (int i = 0; i < specials.length; i++) {
if (specials[i].equals(name)) {
specialAttributes[i] = null;
return (true);
}
}
return (false);
}
/**
* Merge the two sets of parameter values into a single String array.
*
* @param values1 First set of values
* @param values2 Second set of values
*/
protected String[] mergeValues(Object values1, Object values2) {
ArrayList<Object> results = new ArrayList<Object>();
if (values1 == null) {
// Skip - nothing to merge
} else if (values1 instanceof String)
results.add(values1);
else if (values1 instanceof String[]) {
String values[] = (String[]) values1;
for (int i = 0; i < values.length; i++)
results.add(values[i]);
} else
results.add(values1.toString());
if (values2 == null) {
// Skip - nothing to merge
} else if (values2 instanceof String)
results.add(values2);
else if (values2 instanceof String[]) {
String values[] = (String[]) values2;
for (int i = 0; i < values.length; i++)
results.add(values[i]);
} else
results.add(values2.toString());
String values[] = new String[results.size()];
return results.toArray(values);
}
// ------------------------------------------------------ Private Methods
/**
* Merge the parameters from the saved query parameter string (if any), and
* the parameters already present on this request (if any), such that the
* parameter values from the query string show up first if there are
* duplicate parameter names.
*/
private void mergeParameters() {
if ((queryParamString == null) || (queryParamString.length() < 1))
return;
HashMap<String, String[]> queryParameters = new HashMap<String, String[]>();
String encoding = getCharacterEncoding();
if (encoding == null)
encoding = "ISO-8859-1";
RequestUtil.parseParameters(queryParameters, queryParamString,
encoding);
Iterator<String> keys = parameters.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Object value = queryParameters.get(key);
if (value == null) {
queryParameters.put(key, parameters.get(key));
continue;
}
queryParameters.put
(key, mergeValues(value, parameters.get(key)));
}
parameters = queryParameters;
}
// ----------------------------------- AttributeNamesEnumerator Inner Class
/**
* Utility class used to expose the special attributes as being available
* as request attributes.
*/
protected class AttributeNamesEnumerator implements Enumeration<String> {
protected int pos = -1;
protected int last = -1;
protected Enumeration<String> parentEnumeration = null;
protected String next = null;
public AttributeNamesEnumerator() {
parentEnumeration = getRequest().getAttributeNames();
for (int i = specialAttributes.length - 1; i >= 0; i--) {
if (getAttribute(specials[i]) != null) {
last = i;
break;
}
}
}
@Override
public boolean hasMoreElements() {
return ((pos != last) || (next != null)
|| ((next = findNext()) != null));
}
@Override
public String nextElement() {
if (pos != last) {
for (int i = pos + 1; i <= last; i++) {
if (getAttribute(specials[i]) != null) {
pos = i;
return (specials[i]);
}
}
}
String result = next;
if (next != null) {
next = findNext();
} else {
throw new NoSuchElementException();
}
return result;
}
protected String findNext() {
String result = null;
while ((result == null) && (parentEnumeration.hasMoreElements())) {
String current = parentEnumeration.nextElement();
if (!isSpecial(current)) {
result = current;
}
}
return result;
}
}
}
|