tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thiag...@apache.org
Subject git commit: TAP5-1403: Add support for Arrays in request parameters
Date Fri, 10 Jan 2014 20:45:00 GMT
Updated Branches:
  refs/heads/master 66a0a9bd5 -> fd693291d


TAP5-1403: Add support for Arrays in request parameters


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/fd693291
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/fd693291
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/fd693291

Branch: refs/heads/master
Commit: fd693291d4aba488df41eefb443545954f3b272f
Parents: 66a0a9b
Author: Thiago H. de Paula Figueiredo <thiagohp@apache.org>
Authored: Fri Jan 10 18:44:43 2014 -0200
Committer: Thiago H. de Paula Figueiredo <thiagohp@apache.org>
Committed: Fri Jan 10 18:44:43 2014 -0200

----------------------------------------------------------------------
 .../internal/transform/OnEventWorker.java       | 52 +++++++++++++++-----
 .../src/test/app1/RequestParameterDemo.tml      | 10 ++++
 .../integration/app1/RequestParameterTests.java | 10 ++++
 .../app1/pages/RequestParameterDemo.java        | 25 ++++++++++
 4 files changed, 85 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/fd693291/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java
index 06f3a1d..ca25276 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/OnEventWorker.java
@@ -40,6 +40,7 @@ import org.apache.tapestry5.services.ValueEncoderSource;
 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
 import org.apache.tapestry5.services.transform.TransformationSupport;
 
+import java.lang.reflect.Array;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -431,24 +432,38 @@ public class OnEventWorker implements ComponentClassTransformWorker2
             {
                 try
                 {
+                    
+                    Class parameterType = classCache.forName(parameterTypeName);
+                    boolean isArray = parameterType.isArray();
+                    
+                    if (isArray)
+                    {
+                        parameterType = parameterType.getComponentType();
+                    }
+                    
+                    ValueEncoder valueEncoder = valueEncoderSource.getValueEncoder(parameterType);
+
                     String parameterValue = request.getParameter(parameterName);
 
-                    if (!allowBlank && InternalUtils.isBlank(parameterValue))
+                    if (!allowBlank && parameterValue == null)
                         throw new RuntimeException(String.format(
                                 "The value for query parameter '%s' was blank, but a non-blank
value is needed.",
                                 parameterName));
+                    
+                    Object value;
 
-                    Class parameterType = classCache.forName(parameterTypeName);
-
-                    ValueEncoder valueEncoder = valueEncoderSource.getValueEncoder(parameterType);
-
-                    Object value = valueEncoder.toValue(parameterValue);
-
-                    if (parameterType.isPrimitive() && value == null)
-                        throw new RuntimeException(
-                                String.format(
-                                        "Query parameter '%s' evaluates to null, but the
event method parameter is type %s, a primitive.",
-                                        parameterName, parameterType.getName()));
+                    if (!isArray) {
+                        value = coerce(parameterName, parameterType, parameterValue, valueEncoder);
+                    }
+                    else {
+                        String[] parameterValues = request.getParameters(parameterName);
+                        Object[] array = (Object[]) Array.newInstance(parameterType, parameterValues.length);
+                        for (int i = 0; i < parameterValues.length; i++)
+                        {
+                            array[i] = coerce(parameterName, parameterType, parameterValues[i],
valueEncoder);
+                        }
+                        value = array;
+                    }
 
                     return value;
                 } catch (Exception ex)
@@ -460,6 +475,19 @@ public class OnEventWorker implements ComponentClassTransformWorker2
                                     ExceptionUtils.toMessage(ex)), ex);
                 }
             }
+
+            private Object coerce(final String parameterName, Class parameterType,
+                    String parameterValue, ValueEncoder valueEncoder)
+            {
+                Object value = valueEncoder.toValue(parameterValue);
+
+                if (parameterType.isPrimitive() && value == null)
+                    throw new RuntimeException(
+                            String.format(
+                                    "Query parameter '%s' evaluates to null, but the event
method parameter is type %s, a primitive.",
+                                    parameterName, parameterType.getName()));
+                return value;
+            }
         };
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/fd693291/tapestry-core/src/test/app1/RequestParameterDemo.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/RequestParameterDemo.tml b/tapestry-core/src/test/app1/RequestParameterDemo.tml
index 17e64c7..6d13f2e 100644
--- a/tapestry-core/src/test/app1/RequestParameterDemo.tml
+++ b/tapestry-core/src/test/app1/RequestParameterDemo.tml
@@ -8,11 +8,21 @@
     .
   </p>
 
+  <p>
+    The current list of values is:
+    <ul id="values">
+      <li t:type="Loop" t:source="values" t:value="var:v">${var:v}</li>
+    </ul>
+  </p>
+
   <ul>
     <li>
       <a href="${workingLink}">Working Link</a>
     </li>
     <li>
+      <a href="${multivaluedQueryParameterLink}">Link With Multivalued Query Parameter</a>
+    </li>
+    <li>
       <a href="${brokenLink}">Broken Link</a>
     </li>
     <li>

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/fd693291/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/RequestParameterTests.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/RequestParameterTests.java
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/RequestParameterTests.java
index 487ec8c..0ac0193 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/RequestParameterTests.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/RequestParameterTests.java
@@ -27,6 +27,16 @@ public class RequestParameterTests extends App1TestCase
     }
 
     @Test
+    public void successful_use_of_query_parameter_annotation_with_array_parameter()
+    {
+        openLinks("RequestParameter Annotation Demo", "Link With Multivalued Query Parameter");
+
+        assertText("//ul[@id='values']/li[1]", "97");
+        assertText("//ul[@id='values']/li[2]", "98");
+        assertText("//ul[@id='values']/li[3]", "99");
+    }
+
+    @Test
     public void null_value_when_not_allowed()
     {
         openLinks("RequestParameter Annotation Demo", "Null Link");

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/fd693291/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/RequestParameterDemo.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/RequestParameterDemo.java
b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/RequestParameterDemo.java
index 69c235c..f7bd799 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/RequestParameterDemo.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/RequestParameterDemo.java
@@ -14,6 +14,9 @@
 
 package org.apache.tapestry5.integration.app1.pages;
 
+import java.util.Arrays;
+import java.util.List;
+
 import org.apache.tapestry5.ComponentResources;
 import org.apache.tapestry5.Link;
 import org.apache.tapestry5.annotations.Persist;
@@ -26,11 +29,17 @@ public class RequestParameterDemo
     private static final String PARAMETER_NAME = "gnip";
 
     private static final String EVENT_NAME = "frob";
+    
+    private static final String MULTIVALUED_PARAMETER_EVENT_NAME = "frobFrob";
 
     @Property
     @Persist
     private int value;
 
+    @Property
+    @Persist
+    private List<Integer> values;
+
     @Inject
     private ComponentResources resources;
 
@@ -41,6 +50,16 @@ public class RequestParameterDemo
 
         return link;
     }
+    
+    public Link getMultivaluedQueryParameterLink()
+    {
+        Link link = resources.createEventLink(MULTIVALUED_PARAMETER_EVENT_NAME);
+        link.addParameter(PARAMETER_NAME, "97");
+        link.addParameter(PARAMETER_NAME, "98");
+        link.addParameter(PARAMETER_NAME, "99");
+
+        return link;
+    }
 
     public Link getBrokenLink()
     {
@@ -65,6 +84,12 @@ public class RequestParameterDemo
     {
         this.value = value;
     }
+    
+    void onFrobFrob(@RequestParameter(PARAMETER_NAME)
+    Integer[] values)
+    {
+        this.values = Arrays.asList(values);
+    }
 
     void onFrobNullAllowed(@RequestParameter(value = PARAMETER_NAME, allowBlank = true)
     int value)


Mime
View raw message