Author: jbq
Date: Sun May  6 10:33:33 2007
New Revision: 535637

URL: http://svn.apache.org/viewvc?view=rev&rev=535637
Log:
WICKET-539 QueryStringUrlCodingStrategy does not handle multi-valued parameters

Also related to WICKET-65 Handle String array in PageParameters

Added:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
   (with props)
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
   (with props)
Modified:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategy.java
    incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/log4j.properties
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/UrlMountingTest.java

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategy.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategy.java?view=diff&rev=535637&r1=535636&r2=535637
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategy.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategy.java
 Sun May  6 10:33:33 2007
@@ -134,37 +134,14 @@
                        {
                                entries = parameters.entrySet().iterator();     
                        
                        }
-
+                       WebRequestEncoder encoder = new WebRequestEncoder(url);
                        while (entries.hasNext())
                        {
                                Map.Entry entry = (Entry)entries.next();
 
                                if (entry.getValue() != null)
                                {
-
-                                       String escapedValue = 
urlEncode(entry.getValue().toString());
-
-                                       if (!Strings.isEmpty(escapedValue))
-                                       {
-                                               if (firstParam)
-                                               {
-                                                       url.append("?"); /* 
Begin query string. */
-                                                       firstParam = false;
-                                               }
-                                               else
-                                               {
-                                                       /*
-                                                        * Separate new 
key=value(s) pair from previous pair
-                                                        * with an ampersand.
-                                                        */
-                                                       url.append("&");
-                                               }
-
-                                               /* Append key=value(s) pair. */
-                                               url.append(entry.getKey());
-                                               url.append("=");
-                                               url.append(escapedValue);
-                                       }
+                                       
encoder.addValue(entry.getKey().toString(), entry.getValue());
                                }
                        }
                }

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java?view=auto&rev=535637
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
 Sun May  6 10:33:33 2007
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.request.target.coding;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import org.apache.wicket.Application;
+import org.apache.wicket.util.string.AppendingStringBuffer;
+
+/**
+ * [EMAIL PROTECTED] AppendingStringBuffer}-based query string encoder, 
handles String[] and String properly, and properly URL-encodes the values
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jean-Baptiste Quenot</a>
+ */
+public class WebRequestEncoder
+{
+       boolean firstParameter = true;
+       AppendingStringBuffer url;
+       Application application;
+
+       /**
+        * Construct.
+        * @param url the [EMAIL PROTECTED] AppendingStringBuffer} where to 
append query string
+        */
+       public WebRequestEncoder(AppendingStringBuffer url)
+       {
+               this.url = url;
+               this.application = Application.get();
+       }
+
+       /**
+        * Add an [EMAIL PROTECTED] Object}
+        * @param key
+        * @param value
+        */
+       public void addValue(String key, Object value)
+       {
+               if (value instanceof String[])
+               {
+                       String[] values = (String[])value;
+                       for (int i = 0; i < values.length; i++)
+                       {
+                               addValue(key, values[i]);
+                       }
+               }
+               else if (value instanceof String)
+               {
+                       addValue(key, (String)value);
+               }
+               else
+               {
+                       throw new IllegalArgumentException("PageParameters can 
only contain String or String[]");
+               }
+       }
+
+       /**
+        * Add a [EMAIL PROTECTED] String}
+        * @param key
+        * @param value
+        */
+       public void addValue(String key, String value)
+       {
+               String escapedValue = value;
+               try
+               {
+                       escapedValue = URLEncoder.encode(escapedValue, 
application.getRequestCycleSettings()
+                                       .getResponseRequestEncoding());
+               }
+               catch (UnsupportedEncodingException ex)
+               {
+                       // ignore
+               }
+               if (!firstParameter)
+               {
+                       url.append('&');
+               }
+               else
+               {
+                       firstParameter = false;
+                       url.append('?');
+               }
+               url.append(key);
+               url.append('=');
+               url.append(escapedValue);
+       }
+
+}

Propchange: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/request/target/coding/WebRequestEncoder.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/log4j.properties
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/log4j.properties?view=diff&rev=535637&r1=535636&r2=535637
==============================================================================
--- incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/log4j.properties 
(original)
+++ incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/log4j.properties Sun 
May  6 10:33:33 2007
@@ -9,7 +9,7 @@
 # please keep this setting FATAL to avoid questions from users
 # why there are stacktraces in the test output. You can turn it
 # down if you need to when testing, but don't check it in. (eelco)
-log4j.logger.wicket=FATAL
+log4j.logger.org.apache.wicket=FATAL
 #log4j.logger.org.apache.wicket.resource=FATAL
 #log4j.logger.org.apache.wicket.Localizer=FATAL
 

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java?view=auto&rev=535637
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
 Sun May  6 10:33:33 2007
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.request.target.coding;
+
+import org.apache.wicket.IRequestTarget;
+import org.apache.wicket.PageParameters;
+import org.apache.wicket.WicketTestCase;
+import 
org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests for [EMAIL PROTECTED] QueryStringUrlCodingStrategy}
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jean-Baptiste Quenot</a>
+ */
+public class QueryStringUrlCodingStrategyTest extends WicketTestCase
+{
+       private static final Logger log = 
LoggerFactory.getLogger(QueryStringUrlCodingStrategyTest.class);
+
+       /**
+        * Tests mounting.
+        */
+       public void testQS()
+       {
+               IRequestTargetUrlCodingStrategy ucs = new 
QueryStringUrlCodingStrategy("/mount/point", TestPage.class);
+               PageParameters params = new PageParameters();
+               params.add("a", "1");
+               params.add("a", "2");
+               params.add("b", "1");
+               IRequestTarget rt = new 
BookmarkablePageRequestTarget(TestPage.class, params);
+               String path = ucs.encode(rt).toString();
+               log.debug(path);
+               assertEquals("mount/point?a=1&a=2&b=1", path);
+       }
+}

Propchange: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/QueryStringUrlCodingStrategyTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/UrlMountingTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/UrlMountingTest.java?view=diff&rev=535637&r1=535636&r2=535637
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/UrlMountingTest.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/request/target/coding/UrlMountingTest.java
 Sun May  6 10:33:33 2007
@@ -29,6 +29,8 @@
 
 /**
  * Tests package resources.
+ * 
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jean-Baptiste Quenot</a>
  */
 public class UrlMountingTest extends TestCase
 {


Reply via email to