struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hus...@apache.org
Subject svn commit: r517363 - in /struts/struts2/branches/STRUTS_2_0_X/core/src: main/java/org/apache/struts2/views/util/UrlHelper.java test/java/org/apache/struts2/components/URLTest.java test/java/org/apache/struts2/views/util/UrlHelperTest.java
Date Mon, 12 Mar 2007 20:05:04 GMT
Author: husted
Date: Mon Mar 12 13:05:02 2007
New Revision: 517363

URL: http://svn.apache.org/viewvc?view=rev&rev=517363
Log:
WW-1619 URL component and URLHelper class do not handle multiple request parameters with the
same name correctly when includeParams="get". Apply patch submitted by Tom Schneider.

Added:
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/components/URLTest.java
Modified:
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java?view=diff&rev=517363&r1=517362&r2=517363
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java
(original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java
Mon Mar 12 13:05:02 2007
@@ -24,9 +24,11 @@
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
-import java.util.Collections;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -37,7 +39,6 @@
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
 
-import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.TextParseUtil;
 import com.opensymphony.xwork2.util.ValueStack;
@@ -61,21 +62,21 @@
     private static final int DEFAULT_HTTPS_PORT = 443;
 
     private static final String AMP = "&";
-    
+
     private static int httpPort = DEFAULT_HTTP_PORT;
     private static int httpsPort = DEFAULT_HTTPS_PORT;
     private static String customEncoding;
-    
+
     @Inject(StrutsConstants.STRUTS_URL_HTTP_PORT)
     public static void setHttpPort(String val) {
         httpPort = Integer.parseInt(val);
     }
-    
+
     @Inject(StrutsConstants.STRUTS_URL_HTTPS_PORT)
     public static void setHttpsPort(String val) {
         httpsPort = Integer.parseInt(val);
     }
-    
+
     @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
     public static void setCustomEncoding(String val) {
         customEncoding = val;
@@ -297,7 +298,26 @@
                     }
                     if (paramName != null) {
                         String translatedParamValue = translateAndDecode(paramValue);
-                        queryParams.put(paramName, translatedParamValue);
+
+                        if(queryParams.containsKey(paramName)) {
+                            // WW-1619 append new param value to existing value(s)
+                            Object currentParam = queryParams.get(paramName);
+                            if(currentParam instanceof String) {
+                                queryParams.put(paramName, new String[] {
+                                        (String) currentParam, translatedParamValue});
+                            } else {
+                                String currentParamValues[] = (String[]) currentParam;
+                                List paramList = new ArrayList(Arrays
+                                    .asList(currentParamValues));
+                                paramList.add(translatedParamValue);
+                                String newParamValues[] = new String[paramList
+                                    .size()];
+                                queryParams.put(paramName, paramList
+                                    .toArray(newParamValues));
+                            }
+                        } else {
+                            queryParams.put(paramName, translatedParamValue);
+                        }
                     }
                 }
             }

Added: struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/components/URLTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/components/URLTest.java?view=auto&rev=517363
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/components/URLTest.java
(added)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/components/URLTest.java
Mon Mar 12 13:05:02 2007
@@ -0,0 +1,57 @@
+package org.apache.struts2.components;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.struts2.views.jsp.AbstractTagTest;
+
+/**
+ * Verifies correct operation of parameter merging.
+ * 
+ * Contributed by: Daniel Uribe
+ */
+public class URLTest extends AbstractTagTest {
+    public void testIncludeGetDuplicateRequestParams() throws Exception {
+        String body = "";
+
+        Map parameterMap = new HashMap();
+        parameterMap.put("param", new String[] { "1", "2", "3" });
+
+        request.setQueryString("param=1&param=2&param=3");
+        request.setScheme("http");
+        request.setParameterMap(parameterMap);
+        URL url = new URL(stack, request, response);
+        url.setIncludeParams(URL.GET);
+        url.setIncludeContext(false);
+        url.setValue("myAction.action");
+        url.setNamespace("");
+
+        url.start(writer);
+        url.end(writer, body);
+
+        assertEquals("myAction.action?param=1&param=2&param=3",
+            writer.toString());
+    }
+
+    public void testIncludeAllDuplicateRequestParams() throws Exception {
+        String body = "";
+
+        Map parameterMap = new HashMap();
+        parameterMap.put("param", new String[] { "1", "2", "3" });
+
+        request.setQueryString("param=1&param=2&param=3");
+        request.setScheme("http");
+        request.setParameterMap(parameterMap);
+        URL url = new URL(stack, request, response);
+        url.setIncludeParams(URL.ALL);
+        url.setIncludeContext(false);
+        url.setValue("myAction.action");
+        url.setNamespace("");
+
+        url.start(writer);
+        url.end(writer, body);
+
+        assertEquals("myAction.action?param=1&param=2&param=3",
+            writer.toString());
+    }
+}

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java?view=diff&rev=517363&r1=517362&r2=517363
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java
(original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/util/UrlHelperTest.java
Mon Mar 12 13:05:02 2007
@@ -20,6 +20,7 @@
  */
 package org.apache.struts2.views.util;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -28,7 +29,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.struts2.StrutsConstants;
 import org.apache.struts2.StrutsTestCase;
 
 import com.mockobjects.dynamic.Mock;
@@ -110,8 +110,6 @@
            expectedUrl, url.toString());
     }
 
-
-
     public void testBuildWithRootContext() {
         String expectedUrl = "/MyAction.action";
 
@@ -320,6 +318,31 @@
         assertEquals(result.size(), 0);
     }
 
+    public void testParseMultiQuery() throws Exception {
+        Map result = UrlHelper.parseQueryString("param=1&param=1&param=1");
+        
+        assertNotNull(result);
+        assertEquals(result.size(), 1);
+        String values[] = (String[]) result.get("param");
+        Arrays.sort(values);
+        assertEquals(values.length, 3);
+        assertEquals(values[0], "1");
+        assertEquals(values[1], "1");
+        assertEquals(values[2], "1");
+    }
+
+    public void testParseDuplicateQuery() throws Exception {
+        Map result = UrlHelper.parseQueryString("param=1&param=2&param=3");
+        
+        assertNotNull(result);
+        assertEquals(result.size(), 1);
+        String values[] = (String[]) result.get("param");
+        Arrays.sort(values);
+        assertEquals(values.length, 3);
+        assertEquals(values[0], "1");
+        assertEquals(values[1], "2");
+        assertEquals(values[2], "3");
+    }
 
     public void testTranslateAndEncode() throws Exception {
         UrlHelper.setCustomEncoding("UTF-8");



Mime
View raw message