Bug 500783 - [http] Content input stream unavailable for form based requests in Neon

actual fix

Signed-off-by: Raymond Augé <raymond.auge@liferay.com>
Change-Id: I04c8ae4a46788513f1b6402964f522f051a3820f
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/Bug500783_Test.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/Bug500783_Test.java
index be73438..871f1a6 100644
--- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/Bug500783_Test.java
+++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/Bug500783_Test.java
@@ -52,8 +52,6 @@
 			protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 				int contentLength = req.getContentLength();
 
-				System.out.println(req.getParameterMap().entrySet().toString());
-
 				InputStream in = req.getInputStream();
 				// Read the input stream
 				int bytesRead = 0;
@@ -100,7 +98,7 @@
 		map.put("x-www-form-urlencoded", Arrays.<Object>asList("fielda=foo&fieldb=bar"));
 
 		Map<String, List<String>> response = requestAdvisor.upload("MyServlet", map);
-		assertEquals("21|fieldb=[bar]|fielda=[foo]|0", response.get("responseBody").get(0));
+		assertEquals("21|21", response.get("responseBody").get(0));
 		assertEquals(HttpServletResponse.SC_OK + "", response.get("responseCode").get(0));
 	}
 
diff --git a/bundles/org.eclipse.equinox.http.servlet/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.http.servlet/META-INF/MANIFEST.MF
index 3f1e1e6..cd1129c 100644
--- a/bundles/org.eclipse.equinox.http.servlet/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.http.servlet/META-INF/MANIFEST.MF
@@ -3,7 +3,7 @@
 Bundle-Name: %bundleName
 Bundle-Vendor: %providerName
 Bundle-SymbolicName: org.eclipse.equinox.http.servlet
-Bundle-Version: 1.6.200.qualifier
+Bundle-Version: 1.6.300.qualifier
 Bundle-Activator: org.eclipse.equinox.http.servlet.internal.Activator
 Bundle-Localization: plugin
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
diff --git a/bundles/org.eclipse.equinox.http.servlet/pom.xml b/bundles/org.eclipse.equinox.http.servlet/pom.xml
index 7a4021d..5c6b2f3 100644
--- a/bundles/org.eclipse.equinox.http.servlet/pom.xml
+++ b/bundles/org.eclipse.equinox.http.servlet/pom.xml
@@ -20,6 +20,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.http.servlet</artifactId>
-  <version>1.6.200-SNAPSHOT</version>
+  <version>1.6.300-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
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 d0dace9..14a1beb 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
@@ -60,22 +60,7 @@
 	}
 
 	public void addRequestParameters(HttpServletRequest request) {
-		if (queryString == null) {
-			parameterMap = request.getParameterMap();
-			queryString = request.getQueryString();
-
-			return;
-		}
-
-		Map<String, String[]> parameterMapCopy = queryStringToParameterMap(queryString);
-
-		for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
-			String[] values = parameterMapCopy.get(entry.getKey());
-			values = Params.append(values, entry.getValue());
-			parameterMapCopy.put(entry.getKey(), values);
-		}
-
-		parameterMap = Collections.unmodifiableMap(parameterMapCopy);
+		currentRequest = request;
 	}
 
 	public void doDispatch(
@@ -154,6 +139,19 @@
 	}
 
 	public Map<String, String[]> getParameterMap() {
+		if ((parameterMap == null) && (currentRequest != null)) {
+			Map<String, String[]> parameterMapCopy = queryStringToParameterMap(queryString);
+
+			for (Map.Entry<String, String[]> entry : currentRequest.getParameterMap().entrySet()) {
+				String[] values = parameterMapCopy.get(entry.getKey());
+				if (!Arrays.equals(values, entry.getValue())) {
+					values = Params.append(values, entry.getValue());
+					parameterMapCopy.put(entry.getKey(), values);
+				}
+			}
+
+			parameterMap = Collections.unmodifiableMap(parameterMapCopy);
+		}
 		return parameterMap;
 	}
 
@@ -162,6 +160,9 @@
 	}
 
 	public String getQueryString() {
+		if ((queryString == null) && (currentRequest != null)) {
+			queryString = currentRequest.getQueryString();
+		}
 		return queryString;
 	}
 
@@ -207,7 +208,7 @@
 
 	private static Map<String, String[]> queryStringToParameterMap(String queryString) {
 		if ((queryString == null) || (queryString.length() == 0)) {
-			return new HashMap<String, String[]>();
+			return new LinkedHashMap<String, String[]>();
 		}
 
 		try {
@@ -264,6 +265,7 @@
 	private final ContextController contextController;
 	private DispatcherType dispatcherType;
 	private final EndpointRegistration<?> endpointRegistration;
+	private volatile HttpServletRequest currentRequest;
 	private final List<FilterRegistration> matchingFilterRegistrations;
 	private final String pathInfo;
 	private Map<String, String[]> parameterMap;