You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/07/05 13:06:47 UTC

svn commit: r1499962 - 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/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/

Author: sergeyb
Date: Fri Jul  5 11:06:46 2013
New Revision: 1499962

URL: http://svn.apache.org/r1499962
Log:
[CXF-5111] Support for GenericTypeArray

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/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/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=1499962&r1=1499961&r2=1499962&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 Fri Jul  5 11:06:46 2013
@@ -307,6 +307,9 @@ public abstract class ProviderFactory {
                     if (actualClass == null) {
                         continue;
                     }
+                    if (expectedType.isArray()) {
+                        expectedType = expectedType.getComponentType();
+                    }
                     if (actualClass.isAssignableFrom(expectedType)) {
                         if (injectContext) {
                             injectContextValues(em, m);

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=1499962&r1=1499961&r2=1499962&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 Fri Jul  5 11:06:46 2013
@@ -23,6 +23,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
+import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -218,6 +219,8 @@ public final class InjectionUtils {
                     bounds = wildcardType.getUpperBounds();
                 }
                 genericType = getType(bounds, pos);
+            } else if (genericType instanceof GenericArrayType) {
+                genericType = ((GenericArrayType)genericType).getGenericComponentType();
             }
 
             Class<?> cls = (Class<?>)genericType;
@@ -237,9 +240,7 @@ public final class InjectionUtils {
     
     public static Class<?> getRawType(Type genericType) {
         
-        if (genericType == null) {
-            return null;
-        } else if (genericType instanceof Class) {
+        if (genericType instanceof Class) {
             return (Class<?>) genericType;
         } else if (genericType instanceof ParameterizedType) {
             ParameterizedType paramType = (ParameterizedType)genericType;
@@ -247,8 +248,9 @@ public final class InjectionUtils {
             if (t instanceof Class) {
                 return (Class<?>)t;
             }
+        } else if (genericType instanceof GenericArrayType) {
+            return getRawType(((GenericArrayType)genericType).getGenericComponentType());
         }
-        // it might be a TypeVariable, or a GenericArray.
         return null;
     }
     

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1499962&r1=1499961&r2=1499962&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java Fri Jul  5 11:06:46 2013
@@ -64,6 +64,8 @@ public class BookServer extends Abstract
         p.setEnableBuffering(true);
         p.setReportByteArraySize(true);
         providers.add(p);
+        providers.add(new BookStore.StringArrayBodyReaderWriter());
+        providers.add(new BookStore.StringListBodyReaderWriter());
         providers.add(new ContentTypeModifyingMBW());
         JAXBElementProvider<?> jaxbProvider = new JAXBElementProvider<Object>();
         Map<String, String> jaxbElementClassMap = new HashMap<String, String>(); 

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1499962&r1=1499961&r2=1499962&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Fri Jul  5 11:06:46 2013
@@ -26,10 +26,12 @@ import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
+import java.lang.reflect.Type;
 import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.List;
@@ -71,6 +73,8 @@ import javax.ws.rs.core.SecurityContext;
 import javax.ws.rs.core.StreamingOutput;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
 import javax.xml.bind.JAXBElement;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@@ -79,6 +83,7 @@ import javax.xml.transform.dom.DOMSource
 
 import org.apache.cxf.annotations.GZIP;
 import org.apache.cxf.common.util.ProxyHelper;
+import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.Oneway;
@@ -88,6 +93,7 @@ import org.apache.cxf.jaxrs.ext.search.S
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XSISchemaLocation;
 import org.apache.cxf.jaxrs.impl.ResourceContextImpl;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.systest.jaxrs.BookServer20.CustomHeaderAdded;
 import org.apache.cxf.systest.jaxrs.BookServer20.CustomHeaderAddedAsync;
@@ -137,6 +143,19 @@ public class BookStore {
     }
 
     @GET
+    @Path("/bookarray")
+    public String[] getBookStringArray() {
+        return new String[]{"Good book"};
+    }
+    
+    @SuppressWarnings("unchecked")
+    @GET
+    @Path("/booklist")
+    public List<String> getBookListArray() {
+        return Collections.singletonList("Good book");
+    }
+    
+    @GET
     @Path("/customtext")
     @Produces("text/custom")
     public String getCustomBookTest() {
@@ -1471,6 +1490,66 @@ public class BookStore {
             throw new RuntimeException(ex);
         }
     }
+    
+    public static class StringArrayBodyReaderWriter 
+        implements MessageBodyReader<String[]>, MessageBodyWriter<String[]> {
+        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return String[].class.isAssignableFrom(arg0);
+        }
+
+        public String[] readFrom(Class<String[]> arg0, Type arg1,
+            Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
+            throws IOException, WebApplicationException {
+            return new String[] {IOUtils.readStringFromStream(arg5)};
+        }
+
+        public long getSize(String[] arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+            return -1;
+        }
+
+        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return String[].class.isAssignableFrom(arg0);
+        }
+
+        @Override
+        public void writeTo(String[] arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
+                            MultivaluedMap<String, Object> arg5, OutputStream arg6) throws IOException,
+            WebApplicationException {
+            arg6.write(arg0[0].getBytes());
+        }
+
+    }
+        
+    public static class StringListBodyReaderWriter 
+        implements MessageBodyReader<List<String>>, MessageBodyWriter<List<String>> {
+        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return List.class.isAssignableFrom(arg0) 
+                && String.class == InjectionUtils.getActualType(arg1);
+        }
+
+        public List<String> readFrom(Class<List<String>> arg0, Type arg1,
+            Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
+            throws IOException, WebApplicationException {
+            return Collections.singletonList(IOUtils.readStringFromStream(arg5));
+        }
+
+        public long getSize(List<String> arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
+            return -1;
+        }
+
+        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
+            return List.class.isAssignableFrom(arg0) 
+                && String.class == InjectionUtils.getActualType(arg1);
+        }
+
+        @Override
+        public void writeTo(List<String> arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
+                            MultivaluedMap<String, Object> arg5, OutputStream arg6) throws IOException,
+            WebApplicationException {
+            arg6.write(arg0.get(0).getBytes());
+        }
+
+    }    
 }
 
 

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1499962&r1=1499961&r2=1499962&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Fri Jul  5 11:06:46 2013
@@ -942,6 +942,30 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testGetStringArray() throws Exception {
+        String address = "http://localhost:" + PORT;
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); 
+        bean.setProvider(new BookStore.StringArrayBodyReaderWriter());
+        bean.setAddress(address);
+        bean.setResourceClass(BookStore.class);
+        BookStore store = bean.create(BookStore.class);
+        String[] str = store.getBookStringArray();
+        assertEquals("Good book", str[0]);
+    }
+    
+    @Test
+    public void testGetStringList() throws Exception {
+        String address = "http://localhost:" + PORT;
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean(); 
+        bean.setProvider(new BookStore.StringListBodyReaderWriter());
+        bean.setAddress(address);
+        bean.setResourceClass(BookStore.class);
+        BookStore store = bean.create(BookStore.class);
+        List<String> str = store.getBookListArray();
+        assertEquals("Good book", str.get(0));
+    }
+    
+    @Test
     public void testEmptyPostProxy2() throws Exception {
         String address = "http://localhost:" + PORT;
         JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();