cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1133545 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java test/java/org/apache/cxf/jaxrs/Customer.java test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Date Wed, 08 Jun 2011 20:17:57 GMT
Author: sergeyb
Date: Wed Jun  8 20:17:57 2011
New Revision: 1133545

URL: http://svn.apache.org/viewvc?rev=1133545&view=rev
Log:
[CXF-3529] Applying a slightly modified patch on behalf of Biju Nair, thanks

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1133545&r1=1133544&r2=1133545&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Wed Jun  8 20:17:57 2011
@@ -518,6 +518,11 @@ public final class InjectionUtils {
                                                             pType, message);
                             paramValue = InjectionUtils.mergeCollectionsOrArrays(paramValue,
appendValue,
                                                             genericType);
+                        } else if (isSupportedMap(genericType)) {
+                            Object appendValue = InjectionUtils.injectIntoMap(
+                                type, genericType, paramAnns, processedValues, true, pType,
message);
+                            paramValue = InjectionUtils.mergeMap(paramValue, appendValue,
genericType);
+
                         } else if (isbean) {
                             paramValue = InjectionUtils.handleBean(type, paramAnns, processedValues,
                                                             pType, message, decoded);
@@ -542,6 +547,65 @@ public final class InjectionUtils {
         return bean;
     }
 
+    @SuppressWarnings("unchecked")
+    private static Object mergeMap(Object first, Object second, Type genericType) {
+        if (first == null) {
+            return second;
+        } else if (first instanceof Map) {
+            Map.class.cast(first).putAll((Map) second);
+            return first;
+        }
+        return null;
+    }
+    
+    // CHECKSTYLE:OFF
+    private static Object injectIntoMap(Class<?> rawType, Type genericType,
+                                        Annotation[] paramAnns,
+                                        MultivaluedMap<String, String> processedValues,

+                                        boolean decoded,
+                                        ParameterType pathParam, Message message) {
+    // CHECKSTYLE:ON
+        ParameterizedType paramType = (ParameterizedType) genericType;
+        ParameterizedType valueParamType = (ParameterizedType) InjectionUtils
+                                   .getType(paramType.getActualTypeArguments(), 1);
+        Class<?> valueType = (Class<?>) InjectionUtils.getType(valueParamType
+                           .getActualTypeArguments(), 0);
+
+        MultivaluedMap<String, Object> theValues = new MetadataMap<String, Object>();
+           
+        Set<Map.Entry<String, List<String>>> processedValuesEntrySet =
processedValues.entrySet();
+        for (Map.Entry<String, List<String>> processedValuesEntry : processedValuesEntrySet)
{
+            List<String> valuesList = processedValuesEntry.getValue();
+            for (String value : valuesList) {
+                Object o = InjectionUtils.handleParameter(value,
+                                   decoded, valueType, paramAnns, pathParam, message);
+                theValues.add(processedValuesEntry.getKey(), o);
+            }
+        }
+        return theValues;
+    }    
+
+    
+    private static boolean isSupportedMap(Type genericType) {
+        Class<?> rawType = getRawType(genericType);
+        if (Map.class.isAssignableFrom(rawType) && genericType instanceof ParameterizedType)
{
+            ParameterizedType paramType = (ParameterizedType) genericType;
+            if (paramType.getActualTypeArguments().length == 2) {
+                Class<?> firstType = getRawType(getType(paramType.getActualTypeArguments(),
0));
+                Type secondType = getType(paramType.getActualTypeArguments(), 1);
+                if (secondType instanceof ParameterizedType) {
+                    Class<?> secondRawType = getRawType(secondType);
+                    if (String.class == firstType && List.class.isAssignableFrom(secondRawType))
{
+                        Class<?> listtype = getRawType(
+                            getType(((ParameterizedType)secondType).getActualTypeArguments(),
0));
+                        return InjectionUtils.isPrimitive(listtype);
+                    }
+                }
+            } 
+        }
+        return false;
+    }
+    
     private static List<MultivaluedMap<String, String>> processValues(Class<?>
type, Type genericType,
                                         MultivaluedMap<String, String> values,
                                         boolean isbean) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java?rev=1133545&r1=1133544&r2=1133545&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/Customer.java Wed Jun 
8 20:17:57 2011
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs;
 
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.UUID;
 
 import javax.annotation.PostConstruct;
@@ -72,9 +73,11 @@ public class Customer extends AbstractCu
         private Long b;
         private List<String> c;
         private CustomerBean d;
+        private Map<String, List<String>> g;
         //CHECKSTYLE:OFF
         public List<CustomerBean> e;
         //CHECKSTYLE:ON
+        
         public void setA(String aString) {
             this.a = aString;
         }
@@ -99,6 +102,12 @@ public class Customer extends AbstractCu
         public CustomerBean getD() {
             return d;
         }
+        public void setG(Map<String, List<String>> g) {
+            this.g = g;
+        }
+        public Map<String, List<String>> getG() {
+            return g;
+        }
         
     }
     

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java?rev=1133545&r1=1133544&r2=1133545&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/JAXRSUtilsTest.java
Wed Jun  8 20:17:57 2011
@@ -999,6 +999,31 @@ public class JAXRSUtilsTest extends Asse
 
         verifyParametersBean(m, null, messageImpl, null, complexMessageImpl);
     }
+    
+    @Test
+    public void testFormParametersBeanWithMap() throws Exception {
+        Class[] argType = {Customer.CustomerBean.class};
+        Method m = Customer.class.getMethod("testFormBean", argType);
+        Message messageImpl = createMessage();
+        messageImpl.put(Message.REQUEST_URI, "/bar");
+        MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
+        headers.putSingle("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
+        messageImpl.put(Message.PROTOCOL_HEADERS, headers);
+        String body = "g.b=1&g.b=2";
+        messageImpl.setContent(InputStream.class, new ByteArrayInputStream(body.getBytes()));
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
+                                                           null, 
+                                                           messageImpl);
+        assertEquals("Bean should be created", 1, params.size());
+        Customer.CustomerBean cb = (Customer.CustomerBean)params.get(0);
+        assertNotNull(cb);
+        assertNotNull(cb.getG());
+        List<String> values = cb.getG().get("b");
+        assertEquals(2, values.size());
+        assertEquals("1", values.get(0));
+        assertEquals("2", values.get(1));
+        
+    }
 
     private void verifyParametersBean(Method m,
                                       MultivaluedMap<String, String> simpleValues,



Mime
View raw message