cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6941] Optional support for parsing query values as collections
Date Wed, 29 Jun 2016 13:00:41 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 91bb2a18f -> 653653964


[CXF-6941] Optional support for parsing query values as collections


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

Branch: refs/heads/master
Commit: 6536539644a8ceac3b260776e8cf34a3f2fa7624
Parents: 91bb2a1
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Wed Jun 29 14:00:25 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Wed Jun 29 14:00:25 2016 +0100

----------------------------------------------------------------------
 .../org/apache/cxf/jaxrs/impl/UriInfoImpl.java  | 15 ++++----
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java  | 40 ++++++++++++++++----
 .../apache/cxf/jaxrs/utils/JAXRSUtilsTest.java  | 21 ++++++++++
 .../jaxrs/provider/json/JSONProviderTest.java   | 29 ++++++++++++++
 .../org/apache/cxf/jaxrs/resources/TagVO.java   | 12 +++++-
 .../org/apache/cxf/jaxrs/resources/TagVO2.java  |  1 +
 6 files changed, 102 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
index 46238cf..b090927 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
@@ -45,11 +45,13 @@ import org.apache.cxf.message.MessageUtils;
 public class UriInfoImpl implements UriInfo {
     private static final Logger LOG = LogUtils.getL7dLogger(UriInfoImpl.class);
     private static final String CASE_INSENSITIVE_QUERIES = "org.apache.cxf.http.case_insensitive_queries";
+    private static final String PARSE_QUERY_VALUE_AS_COLLECTION = "parse.query.value.as.collection";
     
     private MultivaluedMap<String, String> templateParams;
     private Message message;
     private OperationResourceInfoStack stack;
     private boolean caseInsensitiveQueries;
+    private boolean queryValueIsCollection;
 
     @SuppressWarnings("unchecked")
     public UriInfoImpl(Message m) {
@@ -63,6 +65,8 @@ public class UriInfoImpl implements UriInfo {
             this.stack = m.get(OperationResourceInfoStack.class);
             this.caseInsensitiveQueries = 
                 MessageUtils.isTrue(m.getContextualProperty(CASE_INSENSITIVE_QUERIES));
+            this.queryValueIsCollection = 
+                MessageUtils.isTrue(m.getContextualProperty(PARSE_QUERY_VALUE_AS_COLLECTION));
         }
     }
 
@@ -110,15 +114,10 @@ public class UriInfoImpl implements UriInfo {
     }
 
     public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
-        
-        if (!caseInsensitiveQueries) {
-            return JAXRSUtils.getStructuredParams((String)message.get(Message.QUERY_STRING),

-                              "&", decode, decode);
-        }
-        
-        MultivaluedMap<String, String> queries = new MetadataMap<String, String>(false,
true);
+        MultivaluedMap<String, String> queries = !caseInsensitiveQueries 
+            ? new MetadataMap<String, String>() : new MetadataMap<String, String>(false,
true);
         JAXRSUtils.getStructuredParams(queries, (String)message.get(Message.QUERY_STRING),

-                                      "&", decode, decode);
+                                      "&", decode, decode, queryValueIsCollection);
         return queries;
         
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index 4ffc16d..2bea835 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -1241,6 +1241,15 @@ public final class JAXRSUtils {
                                            String sep, 
                                            boolean decode,
                                            boolean decodePlus) {
+        getStructuredParams(queries, query, sep, decode, decodePlus, false);
+    }
+        
+    public static void getStructuredParams(MultivaluedMap<String, String> queries,
+                                           String query, 
+                                           String sep, 
+                                           boolean decode,
+                                           boolean decodePlus,
+                                           boolean valueIsCollection) {    
         if (!StringUtils.isEmpty(query)) {            
             List<String> parts = Arrays.asList(StringUtils.split(query, sep));
             for (String part : parts) {
@@ -1253,18 +1262,35 @@ public final class JAXRSUtils {
                 } else {
                     name = part.substring(0, index);
                     value =  index < part.length() ? part.substring(index + 1) : "";
-                    if (decodePlus && value.contains("+")) {
-                        value = value.replace('+', ' ');
-                    }
-                    if (decode) {
-                        value = (";".equals(sep))
-                            ? HttpUtils.pathDecode(value) : HttpUtils.urlDecode(value); 
+                }
+                if (valueIsCollection) {
+                    for (String s : value.split(",")) {
+                        addStructuredPartToMap(queries, sep, name, s, decode, decodePlus);
                     }
+                } else {
+                    addStructuredPartToMap(queries, sep, name, value, decode, decodePlus);
                 }
-                queries.add(HttpUtils.urlDecode(name), value);
             }
         }
     }
+    
+    private static void addStructuredPartToMap(MultivaluedMap<String, String> queries,
+                                               String sep, 
+                                               String name,
+                                               String value,
+                                               boolean decode,
+                                               boolean decodePlus) {    
+        
+        if (decodePlus && value.contains("+")) {
+            value = value.replace('+', ' ');
+        }
+        if (decode) {
+            value = (";".equals(sep))
+                ? HttpUtils.pathDecode(value) : HttpUtils.urlDecode(value); 
+        }
+        
+        queries.add(HttpUtils.urlDecode(name), value);
+    }
 
     private static Object readFromMessageBody(Class<?> targetTypeClass,
                                                   Type parameterType,

http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
index 1b1ad92..4d747bb 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
@@ -848,6 +848,7 @@ public class JAXRSUtilsTest extends Assert {
                                                            messageImpl);
         assertEquals(1, params.size());
         Integer[] intValues = (Integer[])params.get(0);
+        assertEquals(2, intValues.length);
         assertEquals(1, (int)intValues[0]);
         assertEquals(2, (int)intValues[1]);
     }
@@ -865,10 +866,30 @@ public class JAXRSUtilsTest extends Assert {
                                                            messageImpl);
         assertEquals(1, params.size());
         int[] intValues = (int[])params.get(0);
+        assertEquals(2, intValues.length);
         assertEquals(1, intValues[0]);
         assertEquals(2, intValues[1]);
     }
     
+    @Test
+    public void testQueryParametersIntegerArrayValueIsColection() throws Exception {
+        Class<?>[] argType = {Integer[].class};
+        Method m = Customer.class.getMethod("testQueryIntegerArray", argType);
+        Message messageImpl = createMessage();
+        messageImpl.put("parse.query.value.as.collection", true);
+        messageImpl.put(Message.QUERY_STRING, "query=1&query=2,3");
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,

+                                                               new ClassResourceInfo(Customer.class)),
+                                                           null, 
+                                                           messageImpl);
+        assertEquals(1, params.size());
+        Integer[] intValues = (Integer[])params.get(0);
+        assertEquals(3, intValues.length);
+        assertEquals(1, (int)intValues[0]);
+        assertEquals(2, (int)intValues[1]);
+        assertEquals(3, (int)intValues[2]);
+    }
+    
     @SuppressWarnings("unchecked")
     @Test
     public void testQueryParamAsListWithDefaultValue() throws Exception {

http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
index 1e7f689..1258e82 100644
--- a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
+++ b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/JSONProviderTest.java
@@ -77,6 +77,7 @@ import org.apache.cxf.jaxrs.resources.jaxb.Book2;
 import org.apache.cxf.jaxrs.resources.jaxb.Book2NoRootElement;
 import org.apache.cxf.staxutils.DelegatingXMLStreamWriter;
 import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.staxutils.transform.TransformUtils;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -1338,6 +1339,34 @@ public class JSONProviderTest extends Assert {
     }
     
     @Test
+    public void testAttributesAsElementsWithTransform() throws Exception {
+        JSONProvider<TagVO2Holder> provider = new JSONProvider<TagVO2Holder>()
{
+            protected XMLStreamWriter createTransformWriterIfNeeded(XMLStreamWriter writer,
+                                                                    OutputStream os,
+                                                                    boolean dropAtXmlLevel)
{
+                return TransformUtils.createTransformWriterIfNeeded(writer, os, 
+                                                                    Collections.emptyMap(),
+                                                                    null,
+                                                                    Collections.emptyMap(),
+                                                                    true,
+                                                                    null);
+            }    
+        };
+        provider.setIgnoreNamespaces(true);
+        TagVO2 tag = new TagVO2("A", "B");
+        tag.setAttrInt(123);
+        TagVO2Holder holder = new TagVO2Holder();
+        holder.setTag(tag);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(holder, TagVO2Holder.class, TagVO2Holder.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
Object>(), bos);
+        String expected = 
+            "{\"tagholder\":{\"attr\":\"attribute\",\"thetag\":{\"attrInt\":123,\"group\":\"B\",\"name\":\"A\"}}}";
+        assertEquals(expected, bos.toString());
+    }
+    
+    @Test
     public void testOutAttributesAsElementsForList() throws Exception {
 
         //Provider

http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
index 6d9c081..3187415 100644
--- a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
+++ b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO.java
@@ -19,13 +19,14 @@
 
 package org.apache.cxf.jaxrs.resources;
 
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
 @XmlRootElement
 public class TagVO {
     private String name;
     private String group;
-    
+    private Integer attrInt;
     public TagVO() { 
         
     }
@@ -50,5 +51,14 @@ public class TagVO {
     public String getGroup() {
         return group;
     }
+
+    @XmlAttribute
+    public Integer getAttrInt() {
+        return attrInt;
+    }
+
+    public void setAttrInt(Integer attrInt) {
+        this.attrInt = attrInt;
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/65365396/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO2.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO2.java
b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO2.java
index 8a0e623..b61aaab 100644
--- a/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO2.java
+++ b/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/resources/TagVO2.java
@@ -31,4 +31,5 @@ public class TagVO2 extends TagVO {
     public TagVO2(String name, String group) {
         super(name, group);
     }
+
 }


Mime
View raw message