Bug 540970 - ArrayIndexOutOfboundsException in ProxyServlet

Signed-off-by: Raymond Auge <raymond.auge@liferay.com>
diff --git a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java
index daac630..f738af0 100644
--- a/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java
+++ b/bundles/org.eclipse.equinox.http.servlet.tests/src/org/eclipse/equinox/http/servlet/tests/ServletTest.java
@@ -2685,6 +2685,34 @@
 	}
 
 	@Test
+	public void test_PathEncodings_Bug540970() throws Exception {
+		Servlet servlet = new HttpServlet() {
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			protected void service(HttpServletRequest req, HttpServletResponse resp)
+					throws ServletException, IOException {
+
+				PrintWriter writer = resp.getWriter();
+
+				writer.write(req.getRequestURI());
+			}
+		};
+
+		Dictionary<String, Object> props = new Hashtable<String, Object>();
+		props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S16");
+		props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet16/*");
+		registrations.add(getBundleContext().registerService(Servlet.class, servlet, props));
+
+		Map<String, List<String>> map = new HashMap<String, List<String>>();
+
+		Map<String, List<String>> result = requestAdvisor.request("Servlet16/NEEO-a5056097%2Fdevice%2Fapt-neeo_io%3Avirtual%3A6jzOoAtL%2FTemperature_GF_Living%2Fnone%2F1%2Fdirectory%2Factor/default", map);
+
+		Assert.assertEquals("200", result.get("responseCode").get(0));
+		Assert.assertEquals("/Servlet16/NEEO-a5056097%2Fdevice%2Fapt-neeo_io%3Avirtual%3A6jzOoAtL%2FTemperature_GF_Living%2Fnone%2F1%2Fdirectory%2Factor/default", result.get("responseBody").get(0));
+	}
+
+	@Test
 	public void test_ServletContext1() throws Exception {
 		String expected = "/org/eclipse/equinox/http/servlet/tests/tb1/resource1.txt";
 		String actual;
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ProxyServlet.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ProxyServlet.java
index 7f5b3c8..9e97b0a 100644
--- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ProxyServlet.java
+++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ProxyServlet.java
@@ -60,9 +60,9 @@
 	public void sessionIdChanged(String oldSessionId) {
 		httpServiceRuntimeImpl.fireSessionIdChanged(oldSessionId);
 	}
-	
+
 	/**
-	 * get the value of path info, not decoded by the server 
+	 * get the value of path info, not decoded by the server
 	 */
 	private String getNotDecodedAlias(HttpServletRequest request) {
 		String pathInfo = HttpServletRequestWrapperImpl.getDispatchPathInfo(request);
@@ -70,20 +70,7 @@
 			return null;
 		}
 		String requestUri = HttpServletRequestWrapperImpl.getDispatchRequestURI(request);
-		
-		// NOTE use split that takes a max to preserve ending SLASH
-		String[] pathInfoSegments = pathInfo.split(Const.SLASH, Integer.MAX_VALUE - 1);
-		String[] requestUriSegments = requestUri.split(Const.SLASH, Integer.MAX_VALUE - 1);
-		
-		if(pathInfoSegments.length == requestUriSegments.length) {
-			return requestUri;
-		}
-		
-		StringBuilder aliasBuilder = new StringBuilder();
-		for(int i=(requestUriSegments.length - pathInfoSegments.length + 1);i<requestUriSegments.length;i++) {
-			aliasBuilder.append(Const.SLASH).append(requestUriSegments[i]);
-		}
-		return aliasBuilder.toString();
+		return requestUri.substring(request.getContextPath().length() + request.getServletPath().length());
 	}
 
 	/**