Commits

tschneider22  committed 456919e

Added code so URL tag can handle duplicated query string parameters

Issue number: WW-1376
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2826573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits 392965a

Comments (0)

Files changed (3)

File src/java/com/opensymphony/webwork/views/util/UrlHelper.java

 package com.opensymphony.webwork.views.util;
 
-import com.opensymphony.webwork.ServletActionContext;
-import com.opensymphony.webwork.WebWorkConstants;
-import com.opensymphony.webwork.config.Configuration;
-import com.opensymphony.xwork.ActionContext;
-import com.opensymphony.xwork.util.OgnlValueStack;
-import com.opensymphony.xwork.util.TextParseUtil;
-import com.opensymphony.xwork.util.XWorkContinuationConfig;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import com.opensymphony.webwork.ServletActionContext;
+import com.opensymphony.webwork.WebWorkConstants;
+import com.opensymphony.webwork.config.Configuration;
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.util.TextParseUtil;
+import com.opensymphony.xwork.util.XWorkContinuationConfig;
+
 
 /**
  * UrlHelper
     }
 
     public static Map parseQueryString(String queryString) {
-    	Map queryParams = new LinkedHashMap();
-    	if (queryString != null) {
-    		String[] params = queryString.split("&");
-    		for (int a=0; a< params.length; a++) {
-    			if (params[a].trim().length() > 0) {
-    				String[] tmpParams = params[a].split("=");
-    				String paramName = null;
-    				String paramValue = "";
-    				if (tmpParams.length > 0) {
-    					paramName = tmpParams[0];
-    				}
-    				if (tmpParams.length > 1) {
-    					paramValue = tmpParams[1];
-    				}
-    				if (paramName != null) {
-    					String translatedParamValue = translateAndDecode(paramValue);
-    					queryParams.put(paramName, translatedParamValue);
-    				}
-    			}
-    		}
-    	}
-    	return queryParams;
+        Map queryParams = new LinkedHashMap();
+        if(queryString != null) {
+            String[] params = queryString.split("&");
+            for(int a = 0; a < params.length; a++) {
+                if(params[a].trim().length() > 0) {
+                    String[] tmpParams = params[a].split("=");
+                    String paramName = null;
+                    String paramValue = "";
+                    if(tmpParams.length > 0) {
+                        paramName = tmpParams[0];
+                    }
+                    if(tmpParams.length > 1) {
+                        paramValue = tmpParams[1];
+                    }
+                    if(paramName != null) {
+                        String translatedParamValue = translateAndDecode(paramValue);
+                        
+                        if(queryParams.containsKey(paramName)) {
+                            // WW-1376 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);
+                        }
+                    }
+                }
+            }
+        }
+        return queryParams;
     }
 }

File src/test/com/opensymphony/webwork/components/URLTest.java

+package com.opensymphony.webwork.components;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.opensymphony.webwork.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&amp;param=2&amp;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&amp;param=2&amp;param=3",
+            writer.toString());
+    }
+}

File src/test/com/opensymphony/webwork/views/util/UrlHelperTest.java

  */
 package com.opensymphony.webwork.views.util;
 
-import com.mockobjects.dynamic.Mock;
-import com.opensymphony.webwork.WebWorkTestCase;
-import com.opensymphony.webwork.WebWorkConstants;
-import com.opensymphony.webwork.config.Configuration;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import com.opensymphony.webwork.views.util.UrlHelper;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.HashMap;
+import com.mockobjects.dynamic.Mock;
+import com.opensymphony.webwork.WebWorkConstants;
+import com.opensymphony.webwork.WebWorkTestCase;
+import com.opensymphony.webwork.config.Configuration;
 
 
 /**
     }
     
     public void testParseNullQuery() throws Exception {
-    	Map result = UrlHelper.parseQueryString(null);
-    	
-    	assertNotNull(result);
-    	assertEquals(result.size(), 0);
+        Map result = UrlHelper.parseQueryString(null);
+        
+        assertNotNull(result);
+        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 {