cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r759890 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/test/java/org/ap...
Date Mon, 30 Mar 2009 09:28:32 GMT
Author: sergeyb
Date: Mon Mar 30 09:28:29 2009
New Revision: 759890

URL: http://svn.apache.org/viewvc?rev=759890&view=rev
Log:
JAXRS: support for arrays

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    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
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=759890&r1=759889&r2=759890&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Mon Mar 30 09:28:29 2009
@@ -206,6 +206,7 @@
     
     private static void handleMapper(List<Object> candidates, ProviderInfo em, 
                                      Class<?> expectedType, Message m) {
+        
         Type[] types = em.getProvider().getClass().getGenericInterfaces();
         for (Type t : types) {
             if (t instanceof ParameterizedType) {
@@ -218,6 +219,7 @@
                             InjectionUtils.injectContextMethods(em.getProvider(), em, m);
                         }
                         candidates.add(em.getProvider());
+                        break;
                     }
                 }
             }

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=759890&r1=759889&r2=759890&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
Mon Mar 30 09:28:29 2009
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.jaxrs.utils;
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
@@ -134,10 +135,14 @@
     }
     
     public static Class<?> getActualType(Type genericType) {
-        if (genericType == null 
-            || !ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
+        
+        if (genericType == null) {
             return null;
         }
+        if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
+            Class<?> cls =  (Class<?>)genericType;
+            return cls.isArray() ? cls.getComponentType() : null;
+        }
         ParameterizedType paramType = (ParameterizedType)genericType;
         return (Class<?>)paramType.getActualTypeArguments()[0];
     }
@@ -320,6 +325,21 @@
         return theValues;
     }
     
+    @SuppressWarnings("unchecked")
+    public static Object injectIntoArray(Type genericType, List<String> values,
+                                         boolean decoded, ParameterType pathParam, Message
message) {
+        Class<?> realType = InjectionUtils.getActualType(genericType);
+        values = checkPathSegment(values, realType, pathParam);
+        Object[] array = (Object[])Array.newInstance(realType, values.size());
+        for (int i = 0; i < values.size(); i++) {
+            String value = decodeValue(values.get(i), decoded, pathParam);
+            Object o = InjectionUtils.handleParameter(value, realType, pathParam, message);
+            if (o != null) {
+                array[i] = o;
+            }
+        }
+        return array;
+    }
     
     
     @SuppressWarnings("unchecked")
@@ -382,16 +402,19 @@
                 }
             }
         }
-        
+
+        Object value = null;
         if (List.class.isAssignableFrom(paramType)) {
-            return InjectionUtils.injectIntoList(genericType, paramValues, decoded, pathParam,
+            value = InjectionUtils.injectIntoList(genericType, paramValues, decoded, pathParam,
                                                  message);
         } else if (Set.class.isAssignableFrom(paramType)) {
-            return InjectionUtils.injectIntoSet(genericType, paramValues, false, decoded,
pathParam,
+            value = InjectionUtils.injectIntoSet(genericType, paramValues, false, decoded,
pathParam,
                                                 message);
         } else if (SortedSet.class.isAssignableFrom(paramType)) {
-            return InjectionUtils.injectIntoSet(genericType, paramValues, true, decoded,
pathParam,
+            value = InjectionUtils.injectIntoSet(genericType, paramValues, true, decoded,
pathParam,
                                                 message);
+        } else if (paramType.isArray()) {
+            value = InjectionUtils.injectIntoArray(genericType, paramValues, decoded, pathParam,
message);
         } else {
             String result = null;
             if (paramValues.size() > 0) {
@@ -401,11 +424,10 @@
             }
             if (result != null) {
                 result = decodeValue(result, decoded, pathParam);
-                return InjectionUtils.handleParameter(result, paramType, pathParam, message);
-            } else {
-                return null;
+                value = InjectionUtils.handleParameter(result, paramType, pathParam, message);
             }
         }
+        return value;
     }
     
     public static ThreadLocalProxy createThreadLocalProxy(Class<?> type) {

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=759890&r1=759889&r2=759890&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 Mon Mar 30
09:28:29 2009
@@ -239,7 +239,11 @@
         // complete
     }
     
-    public void testCustomerParam(@QueryParam("p1") Customer c) {
+    public void testCustomerParam(@QueryParam("p1") Customer c, @QueryParam("p2") Customer[]
c2) {
+        // complete
+    }
+    
+    public void testCustomerParam2(@QueryParam("p1") String[] p) {
         // complete
     }
     

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=759890&r1=759889&r2=759890&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
Mon Mar 30 09:28:29 2009
@@ -557,16 +557,34 @@
         Message messageImpl = createMessage();
         ProviderFactory.getInstance(messageImpl).registerUserProvider(
             new CustomerParameterHandler());
-        Class[] argType = {Customer.class};
+        Class[] argType = {Customer.class, Customer[].class};
         Method m = Customer.class.getMethod("testCustomerParam", argType);
         
-        messageImpl.put(Message.QUERY_STRING, "p1=Fred");
+        messageImpl.put(Message.QUERY_STRING, "p1=Fred&p2=Barry");
         List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
                                                            null, 
                                                            messageImpl);
-        assertEquals(1, params.size());
+        assertEquals(2, params.size());
         Customer c = (Customer)params.get(0);
         assertEquals("Fred", c.getName());
+        Customer c2 = ((Customer[])params.get(1))[0];
+        assertEquals("Barry", c2.getName());
+    }
+    
+    @Test
+    public void testArrayParamNoProvider() throws Exception {
+        Message messageImpl = createMessage();
+        Class[] argType = {String[].class};
+        Method m = Customer.class.getMethod("testCustomerParam2", argType);
+        
+        messageImpl.put(Message.QUERY_STRING, "p1=Fred&p1=Barry");
+        List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m,
null),
+                                                           null, 
+                                                           messageImpl);
+        assertEquals(1, params.size());
+        String[] values = (String[])params.get(0);
+        assertEquals("Fred", values[0]);
+        assertEquals("Barry", values[1]);
     }
     
     @Test

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=759890&r1=759889&r2=759890&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Mon Mar 30
09:28:29 2009
@@ -191,7 +191,16 @@
         String url2 = new URL(urlValue).toString();
         int index = url2.lastIndexOf('/');
         return doGetBook(url2.substring(index + 1));
-    } 
+    }
+    
+    @GET
+    @Path("/bookidarray")
+    public Book getBookByURLQuery(@QueryParam("id") String[] ids) throws Exception {
+        if (ids == null || ids.length != 3) {
+            throw new WebApplicationException(); 
+        }
+        return doGetBook(ids[0] + ids[1] + ids[2]);
+    }
 
     @GET
     @Path("/securebooks/{bookId}/")

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=759890&r1=759889&r2=759890&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Mon Mar 30 09:28:29 2009
@@ -72,6 +72,14 @@
                                "resources/expected_get_book123.txt",
                                "application/xml", 200);
     }
+    
+    @Test
+    public void testGetBookByArrayQuery() throws Exception {
+        getAndCompareAsStrings("http://localhost:9080/bookstore/bookidarray?"
+                               + "id=1&id=2&id=3",
+                               "resources/expected_get_book123.txt",
+                               "application/xml", 200);
+    }
         
     @Test
     public void testNoRootResourceException() throws Exception {



Mime
View raw message