diff options
author | Raymond Auge | 2015-08-13 04:17:47 +0000 |
---|---|---|
committer | Raymond Auge | 2015-08-13 04:27:34 +0000 |
commit | 01abbce8c9cb27eaa2b8fd9baabd1fd39a9e77d7 (patch) | |
tree | 544813afc3c74470030da5a598a89008bc31d887 /bundles/org.eclipse.equinox.http.servlet/src | |
parent | 14b77e49a4a25351725b3dad145f24c2b2cb141b (diff) | |
download | rt.equinox.bundles-01abbce8c9cb27eaa2b8fd9baabd1fd39a9e77d7.tar.gz rt.equinox.bundles-01abbce8c9cb27eaa2b8fd9baabd1fd39a9e77d7.tar.xz rt.equinox.bundles-01abbce8c9cb27eaa2b8fd9baabd1fd39a9e77d7.zip |
Bug 467859 - [http whiteboard] request/response wrappers are not visible ...
... during later request processing
Signed-off-by: Raymond Auge <raymond.auge@liferay.com>
Diffstat (limited to 'bundles/org.eclipse.equinox.http.servlet/src')
9 files changed, 206 insertions, 44 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/HttpServiceRuntimeImpl.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/HttpServiceRuntimeImpl.java index fb3b5651c..9c892375f 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/HttpServiceRuntimeImpl.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/HttpServiceRuntimeImpl.java @@ -199,29 +199,32 @@ public class HttpServiceRuntimeImpl public DispatchTargets getDispatchTargets( String path, RequestInfoDTO requestInfoDTO) { + String queryString = Path.findQueryString(path); + String requestURI = Path.stripQueryString(path); + // perfect match DispatchTargets dispatchTargets = getDispatchTargets( - path, null, Match.EXACT, requestInfoDTO); + requestURI, null, queryString, Match.EXACT, requestInfoDTO); if (dispatchTargets == null) { // extension match - String extension = Path.findExtension(path); + String extension = Path.findExtension(requestURI); dispatchTargets = getDispatchTargets( - path, extension, Match.EXTENSION, + requestURI, extension, queryString, Match.EXTENSION, requestInfoDTO); } if (dispatchTargets == null) { // regex match dispatchTargets = getDispatchTargets( - path, null, Match.REGEX, requestInfoDTO); + requestURI, null, queryString, Match.REGEX, requestInfoDTO); } if (dispatchTargets == null) { // handle '/' aliases dispatchTargets = getDispatchTargets( - path, null, Match.DEFAULT_SERVLET, + requestURI, null, queryString, Match.DEFAULT_SERVLET, requestInfoDTO); } @@ -371,7 +374,7 @@ public class HttpServiceRuntimeImpl } private DispatchTargets getDispatchTargets( - String requestURI, String extension, Match match, + String requestURI, String extension, String queryString, Match match, RequestInfoDTO requestInfoDTO) { Collection<ContextController> contextControllers = getContextControllers( @@ -401,7 +404,7 @@ public class HttpServiceRuntimeImpl DispatchTargets dispatchTargets = contextController.getDispatchTargets( null, requestURI, servletPath, pathInfo, - extension, match, requestInfoDTO); + extension, queryString, match, requestInfoDTO); if (dispatchTargets != null) { return dispatchTargets; diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java index d11198bb2..cb78b8fbe 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java @@ -596,29 +596,32 @@ public class ContextController { public DispatchTargets getDispatchTargets( String path, RequestInfoDTO requestInfoDTO) { + String queryString = Path.findQueryString(path); + String requestURI = Path.stripQueryString(path); + // perfect match DispatchTargets dispatchTargets = getDispatchTargets( - path, null, Match.EXACT, requestInfoDTO); + requestURI, null, queryString, Match.EXACT, requestInfoDTO); if (dispatchTargets == null) { // extension match - String extension = Path.findExtension(path); + String extension = Path.findExtension(requestURI); dispatchTargets = getDispatchTargets( - path, extension, Match.EXTENSION, + requestURI, extension, queryString, Match.EXTENSION, requestInfoDTO); } if (dispatchTargets == null) { // regex match dispatchTargets = getDispatchTargets( - path, null, Match.REGEX, requestInfoDTO); + requestURI, null, queryString, Match.REGEX, requestInfoDTO); } if (dispatchTargets == null) { // handle '/' aliases dispatchTargets = getDispatchTargets( - path, null, Match.DEFAULT_SERVLET, + requestURI, null, queryString, Match.DEFAULT_SERVLET, requestInfoDTO); } @@ -626,12 +629,12 @@ public class ContextController { } private DispatchTargets getDispatchTargets( - String path, String extension, Match match, + String requestURI, String extension, String queryString, Match match, RequestInfoDTO requestInfoDTO) { - int pos = path.lastIndexOf('/'); + int pos = requestURI.lastIndexOf('/'); - String servletPath = path; + String servletPath = requestURI; String pathInfo = null; if (match == Match.DEFAULT_SERVLET) { @@ -641,8 +644,8 @@ public class ContextController { do { DispatchTargets dispatchTargets = getDispatchTargets( - null, path, servletPath, pathInfo, - extension, match, requestInfoDTO); + null, requestURI, servletPath, pathInfo, + extension, queryString, match, requestInfoDTO); if (dispatchTargets != null) { return dispatchTargets; @@ -653,8 +656,8 @@ public class ContextController { } if (pos > -1) { - String newServletPath = path.substring(0, pos); - pathInfo = path.substring(pos); + String newServletPath = requestURI.substring(0, pos); + pathInfo = requestURI.substring(pos); servletPath = newServletPath; pos = servletPath.lastIndexOf('/'); @@ -670,18 +673,14 @@ public class ContextController { public DispatchTargets getDispatchTargets( String servletName, String requestURI, String servletPath, - String pathInfo, String extension, Match match, + String pathInfo, String extension, String queryString, Match match, RequestInfoDTO requestInfoDTO) { checkShutdown(); EndpointRegistration<?> endpointRegistration = null; - String pattern = null; - for (EndpointRegistration<?> curEndpointRegistration : endpointRegistrations) { - if ((pattern = curEndpointRegistration.match( - servletName, servletPath, pathInfo, extension, match)) != null) { - + if (curEndpointRegistration.match(servletName, servletPath, pathInfo, extension, match) != null) { endpointRegistration = curEndpointRegistration; break; @@ -702,7 +701,7 @@ public class ContextController { if (filterRegistrations.isEmpty()) { return new DispatchTargets( - this, endpointRegistration, servletPath, pathInfo, pattern); + this, endpointRegistration, requestURI, servletPath, pathInfo, queryString); } if (requestURI != null) { @@ -724,8 +723,8 @@ public class ContextController { matchingFilterRegistrations, requestInfoDTO); return new DispatchTargets( - this, endpointRegistration, matchingFilterRegistrations, servletPath, - pathInfo, pattern); + this, endpointRegistration, matchingFilterRegistrations, requestURI, servletPath, + pathInfo, queryString); } private void collectFilters( diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java index 79956aca8..784ae31f3 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java @@ -12,14 +12,17 @@ package org.eclipse.equinox.http.servlet.internal.context; import java.io.IOException; -import java.util.Collections; -import java.util.List; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.*; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration; import org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration; import org.eclipse.equinox.http.servlet.internal.servlet.*; +import org.eclipse.equinox.http.servlet.internal.util.Const; +import org.eclipse.equinox.http.servlet.internal.util.Params; /** * @author Raymond Augé @@ -29,37 +32,40 @@ public class DispatchTargets { public DispatchTargets( ContextController contextController, EndpointRegistration<?> endpointRegistration, - String servletPath, String pathInfo, String pattern) { + String requestURI, String servletPath, String pathInfo, String queryString) { this( contextController, endpointRegistration, - Collections.<FilterRegistration>emptyList(), servletPath, pathInfo, - pattern); + Collections.<FilterRegistration>emptyList(), requestURI, servletPath, pathInfo, + queryString); } public DispatchTargets( ContextController contextController, EndpointRegistration<?> endpointRegistration, List<FilterRegistration> matchingFilterRegistrations, - String servletPath, String pathInfo, String pattern) { + String requestURI, String servletPath, String pathInfo, String queryString) { this.contextController = contextController; this.endpointRegistration = endpointRegistration; this.matchingFilterRegistrations = matchingFilterRegistrations; + this.requestURI = requestURI; this.servletPath = servletPath; this.pathInfo = pathInfo; + this.parameterMap = queryStringToParameterMap(queryString); + this.queryString = queryString; } public boolean doDispatch( HttpServletRequest request, HttpServletResponse response, - String requestURI, DispatcherType dispatcherType) + String path, DispatcherType dispatcherType) throws ServletException, IOException { if (dispatcherType == DispatcherType.INCLUDE) { request.setAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH, contextController.getContextPath()); request.setAttribute(RequestDispatcher.INCLUDE_PATH_INFO, getPathInfo()); - request.setAttribute(RequestDispatcher.INCLUDE_QUERY_STRING, request.getQueryString()); - request.setAttribute(RequestDispatcher.INCLUDE_REQUEST_URI, requestURI); + request.setAttribute(RequestDispatcher.INCLUDE_QUERY_STRING, getQueryString()); + request.setAttribute(RequestDispatcher.INCLUDE_REQUEST_URI, getRequestURI()); request.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH, getServletPath()); } else if (dispatcherType == DispatcherType.FORWARD) { @@ -115,10 +121,22 @@ public class DispatchTargets { return matchingFilterRegistrations; } + public Map<String, String[]> getParameterMap() { + return parameterMap; + } + public String getPathInfo() { return pathInfo; } + public String getQueryString() { + return queryString; + } + + public String getRequestURI() { + return requestURI; + } + public String getServletPath() { return servletPath; } @@ -127,10 +145,41 @@ public class DispatchTargets { return endpointRegistration; } + private Map<String, String[]> queryStringToParameterMap(String queryString) { + if ((queryString == null) || (queryString.length() == 0)) { + return emptyMap; + } + + try { + Map<String, String[]> parameterMap = new LinkedHashMap<String, String[]>(); + String[] parameters = queryString.split(Const.AMP); + for (String parameter : parameters) { + int index = parameter.indexOf('='); + String name = (index > 0) ? URLDecoder.decode(parameter.substring(0, index), Const.UTF8) : parameter; + String[] values = parameterMap.get(name); + if (values == null) { + values = new String[0]; + } + String value = ((index > 0) && (parameter.length() > index + 1)) ? URLDecoder.decode(parameter.substring(index + 1), Const.UTF8) : null; + values = Params.append(values, value); + parameterMap.put(name, values); + } + return Collections.unmodifiableMap(parameterMap); + } + catch (UnsupportedEncodingException unsupportedEncodingException) { + throw new RuntimeException(unsupportedEncodingException); + } + } + + private static final Map<String, String[]> emptyMap = new HashMap<String, String[]>(); + private final ContextController contextController; private final EndpointRegistration<?> endpointRegistration; private final List<FilterRegistration> matchingFilterRegistrations; private final String pathInfo; + private final Map<String, String[]> parameterMap; + private final String queryString; + private final String requestURI; private final String servletPath; }
\ No newline at end of file diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestBuilder.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestBuilder.java index 4edc2eb0b..2fb41c0a4 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestBuilder.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestBuilder.java @@ -12,14 +12,12 @@ *******************************************************************************/ package org.eclipse.equinox.http.servlet.internal.servlet; -import java.util.List; -import java.util.Stack; +import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.eclipse.equinox.http.servlet.internal.context.ContextController; import org.eclipse.equinox.http.servlet.internal.context.DispatchTargets; -import org.eclipse.equinox.http.servlet.internal.util.Const; -import org.eclipse.equinox.http.servlet.internal.util.EventListeners; +import org.eclipse.equinox.http.servlet.internal.util.*; import org.osgi.service.http.HttpContext; public class HttpServletRequestBuilder extends HttpServletRequestWrapper { @@ -27,6 +25,7 @@ public class HttpServletRequestBuilder extends HttpServletRequestWrapper { private Stack<DispatchTargets> dispatchTargets = new Stack<DispatchTargets>(); private final HttpServletRequest request; private final DispatcherType dispatcherType; + private Map<String, String[]> parameterMap; static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri"; //$NON-NLS-1$ static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path"; //$NON-NLS-1$ @@ -81,6 +80,51 @@ public class HttpServletRequestBuilder extends HttpServletRequestWrapper { return dispatcherType; } + public String getParameter(String name) { + String[] values = getParameterValues(name); + if ((values == null) || (values.length == 0)) { + return null; + } + return values[0]; + } + + public Map<String, String[]> getParameterMap() { + if (this.parameterMap != null) { + return this.parameterMap; + } + Map<String, String[]> parameterMap = new HashMap<String, String[]>(this.dispatchTargets.peek().getParameterMap()); + for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) { + String[] values = parameterMap.get(entry.getKey()); + if (values == null) { + values = new String[0]; + } + values = Params.append(values, entry.getValue()); + parameterMap.put(entry.getKey(), values); + } + this.parameterMap = Collections.unmodifiableMap(parameterMap); + return this.parameterMap; + } + + public Enumeration<String> getParameterNames() { + return Collections.enumeration(getParameterMap().keySet()); + } + + public String[] getParameterValues(String name) { + return getParameterMap().get(name); + } + + @Override + public String getQueryString() { + String queryStringA = this.dispatchTargets.peek().getQueryString(); + String queryStringB = request.getQueryString(); + if ((queryStringA != null) && (queryStringA.length() > 0) && + (queryStringB != null) && (queryStringB.length() > 0)) { + + return queryStringA + Const.AMP + queryStringB; + } + return queryStringB; + } + public ServletContext getServletContext() { return dispatchTargets.peek().getServletRegistration().getServletContext(); } @@ -204,10 +248,14 @@ public class HttpServletRequestBuilder extends HttpServletRequestWrapper { public synchronized void pop() { this.dispatchTargets.pop(); + this.parameterMap = null; + getParameterMap(); } public synchronized void push(DispatchTargets dispatchTargets) { this.dispatchTargets.push(dispatchTargets); + this.parameterMap = null; + getParameterMap(); } public void removeAttribute(String name) { diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ResponseStateHandler.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ResponseStateHandler.java index f4cbe2e81..01fa54bf7 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ResponseStateHandler.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ResponseStateHandler.java @@ -157,7 +157,7 @@ public class ResponseStateHandler { String className = clazz.getName(); DispatchTargets errorDispatchTargets = contextController.getDispatchTargets( - className, null, null, null, null, Match.EXACT, null); + className, null, null, null, null, null, Match.EXACT, null); if (errorDispatchTargets == null) { throwException(exception); @@ -234,7 +234,7 @@ public class ResponseStateHandler { ContextController contextController = dispatchTargets.getContextController(); DispatchTargets errorDispatchTargets = contextController.getDispatchTargets( - String.valueOf(status), null, null, null, null, Match.EXACT, null); + String.valueOf(status), null, null, null, null, null, Match.EXACT, null); if (errorDispatchTargets == null) { wrappedResponse.sendError(status, wrapperImpl.getMessage()); diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java index cb1366f50..2dc96970c 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java @@ -139,7 +139,7 @@ public class ServletContextAdaptor { public RequestDispatcher getNamedDispatcher(String servletName) { DispatchTargets dispatchTargets = contextController.getDispatchTargets( - servletName, null, null, null, null, Match.EXACT, null); + servletName, null, null, null, null, null, Match.EXACT, null); if (dispatchTargets == null) { return null; diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Const.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Const.java index 0db8bfc08..ce9fa10df 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Const.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Const.java @@ -16,6 +16,7 @@ package org.eclipse.equinox.http.servlet.internal.util; */ public class Const { + public static final String AMP = "&"; //$NON-NLS-1$ public static final String BLANK = ""; //$NON-NLS-1$ public static final String CLOSE_PAREN = ")"; //$NON-NLS-1$ public static final String DOT = "."; //$NON-NLS-1$ @@ -35,6 +36,7 @@ public class Const { public static final String EQUINOX_LEGACY_CONTEXT_SELECT = "equinox.context.select"; //$NON-NLS-1$ public static final String EQUINOX_LEGACY_CONTEXT_HELPER = "equinox.legacy.context.helper"; //$NON-NLS-1$ public static final String EQUINOX_LEGACY_HTTP_CONTEXT_INITIATING_ID = "equinox.legacy.http.context.initiating.id"; //$NON-NLS-1$ + public static final String UTF8 = "UTF-8"; //$NON-NLS-1$ public static enum Dispatcher { diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Params.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Params.java new file mode 100644 index 000000000..ec8a4b554 --- /dev/null +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Params.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2015 Raymond Augé and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Raymond Augé <raymond.auge@liferay.com> - Bug 467859 + ******************************************************************************/ + +package org.eclipse.equinox.http.servlet.internal.util; + +public class Params { + + public static String[] append(String[] params, String value) { + if (params.length == 0) { + return new String[] {value}; + } + String[] tmp = new String[params.length + 1]; + System.arraycopy(params, 0, tmp, 0, params.length); + tmp[params.length] = (value == null) ? Const.BLANK : value; + return tmp; + } + + public static String[] append(String[] params, String... values) { + for (String value : values) { + params = append(params, value); + } + return params; + } + +} diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Path.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Path.java index 53b0e962e..939b003ee 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Path.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/util/Path.java @@ -17,6 +17,12 @@ package org.eclipse.equinox.http.servlet.internal.util; public class Path { public static String findExtension(String path) { + int index = path.indexOf('?'); + + if (index != -1) { + path = path.substring(0, index); + } + String lastSegment = path.substring(path.lastIndexOf('/') + 1); int dot = lastSegment.lastIndexOf('.'); @@ -28,4 +34,26 @@ public class Path { return lastSegment.substring(dot + 1); } + public static String findQueryString(String path) { + String queryString = null; + + int index = path.indexOf('?'); + + if (index != -1) { + queryString = path.substring(index + 1); + } + + return queryString; + } + + public static String stripQueryString(String path) { + int index = path.indexOf('?'); + + if (index != -1) { + path = path.substring(0, index); + } + + return path; + } + }
\ No newline at end of file |