cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1351626 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Date Tue, 19 Jun 2012 09:49:55 GMT
Author: sergeyb
Date: Tue Jun 19 09:49:55 2012
New Revision: 1351626

URL: http://svn.apache.org/viewvc?rev=1351626&view=rev
Log:
[CXF-4359] Adding Types to a single JAXBContext

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java?rev=1351626&r1=1351625&r2=1351626&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
Tue Jun 19 09:49:55 2012
@@ -490,25 +490,28 @@ public final class ResourceUtils {
     private static void getAllTypesForResource(ClassResourceInfo resource, Map<Class<?>,
Type> types,
                                                boolean jaxbOnly) {
         for (OperationResourceInfo ori : resource.getMethodDispatcher().getOperationResourceInfos())
{
-            Class<?> cls = ori.getMethodToInvoke().getReturnType();
+            Class<?> realReturnType = ori.getMethodToInvoke().getReturnType();
+            Class<?> cls = realReturnType;
             if (cls == Response.class) {
                 cls = getActualJaxbType(cls, ori.getMethodToInvoke(), false);
             }
+            Type type = ori.getMethodToInvoke().getGenericReturnType();
             if (jaxbOnly) {
-                checkJaxbType(cls, types);
+                checkJaxbType(cls, realReturnType == Response.class ? cls : type, types);
             } else {
-                Type type = ori.getMethodToInvoke().getGenericReturnType();
                 types.put(cls, type);
             }
+            
             for (Parameter pm : ori.getParameters()) {
                 if (pm.getType() == ParameterType.REQUEST_BODY) {
                     Class<?> inType = ori.getMethodToInvoke().getParameterTypes()[pm.getIndex()];
+                    Type paramType = ori.getMethodToInvoke().getGenericParameterTypes()[pm.getIndex()];
                     if (jaxbOnly) {
-                        checkJaxbType(inType, types);
+                        checkJaxbType(inType, paramType, types);
                     } else {
-                        Type type = ori.getMethodToInvoke().getGenericParameterTypes()[pm.getIndex()];
-                        types.put(inType, type);
+                        types.put(inType, paramType);
                     }
+                    
                 }
             }
             
@@ -521,13 +524,20 @@ public final class ResourceUtils {
         }
     }
     
-    private static void checkJaxbType(Class<?> type, Map<Class<?>, Type>
types) {
+    private static void checkJaxbType(Class<?> type, Type genericType, Map<Class<?>,
Type> types) {
         JAXBElementProvider<?> provider = new JAXBElementProvider<Object>();
-        if (!InjectionUtils.isPrimitive(type) 
+        if (type != null 
+            && !InjectionUtils.isPrimitive(type) 
             && !JAXBElement.class.isAssignableFrom(type)
             && provider.isReadable(type, type, new Annotation[0], MediaType.APPLICATION_XML_TYPE))
{
             types.put(type, type);
-        }        
+            
+            Class<?> genCls = InjectionUtils.getActualType(genericType);
+            if (genCls != type && genCls instanceof Class && genCls != Object.class

+                && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
+                types.put(genCls, genCls);
+            }
+        }
     }
     
     private static UserResource getResourceFromElement(Element e) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=1351626&r1=1351625&r2=1351626&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Tue Jun 19 09:49:55 2012
@@ -186,12 +186,23 @@ public class JAXBElementProviderTest ext
         ClassResourceInfo cri = 
             ResourceUtils.createClassResourceInfo(XmlListResource.class, XmlListResource.class,
true, true);
         JAXBElementProvider<?> provider = new JAXBElementProvider<Object>();
+        provider.setSingleJaxbContext(true);
         provider.setExtraClass(new Class[]{XmlObject.class});
         provider.init(Collections.singletonList(cri));
         testXmlList(provider);
         
     }
     
+    @Test
+    public void testGenericsAndSingleContext() throws Exception {
+        ClassResourceInfo cri = 
+            ResourceUtils.createClassResourceInfo(XmlListResource.class, XmlListResource.class,
true, true);
+        JAXBElementProvider<?> provider = new JAXBElementProvider<Object>();
+        provider.setSingleJaxbContext(true);
+        provider.init(Collections.singletonList(cri));
+        testXmlList(provider);
+    }
+    
     @SuppressWarnings("unchecked")
     private void testXmlList(JAXBElementProvider<?> provider) throws Exception {
         
@@ -590,6 +601,34 @@ public class JAXBElementProviderTest ext
         doReadUnqualifiedCollection(bos.toString(), "setBooks", List.class);
     }
     
+    @SuppressWarnings({"rawtypes", "unchecked" })
+    @Test
+    public void testReadJAXBElement() throws Exception {
+        String xml = "<Book><id>123</id><name>CXF in Action</name></Book>";
+        JAXBElementProvider<JAXBElement> provider = new JAXBElementProvider<JAXBElement>();
+        JAXBElement<Book> jaxbElement = provider.readFrom(JAXBElement.class, Book.class,
+             new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(),
+             new ByteArrayInputStream(xml.getBytes("UTF-8")));
+        Book book = jaxbElement.getValue();
+        assertEquals(123L, book.getId());
+        assertEquals("CXF in Action", book.getName());
+        
+    }
+    
+    @Test
+    @Ignore
+    public void testReadBookJAXBElement() throws Exception {
+        String xml = "<Book><id>123</id><name>CXF in Action</name></Book>";
+        JAXBElementProvider<BookJAXBElement> provider = new JAXBElementProvider<BookJAXBElement>();
+        BookJAXBElement jaxbElement = provider.readFrom(BookJAXBElement.class, BookJAXBElement.class,
+             new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(),
+             new ByteArrayInputStream(xml.getBytes("UTF-8")));
+        Book book = jaxbElement.getValue();
+        assertEquals(123L, book.getId());
+        assertEquals("CXF in Action", book.getName());
+        
+    }
+    
     @Test
     public void testWriteQualifiedCollection() throws Exception {
         JAXBElementProvider<List<TagVO2>> provider = new JAXBElementProvider<List<TagVO2>>();
@@ -1494,6 +1533,20 @@ public class JAXBElementProviderTest ext
         }
     }
     
+    public static class BookJAXBElement extends JAXBElement<Book> {
+        protected static final QName NAME = new QName("Book");
+
+        private static final long serialVersionUID = -7388721095437704766L;
+        
+        public BookJAXBElement(Book value) {
+            super(NAME, Book.class, null, value);
+        }
+
+        public BookJAXBElement() {
+            super(NAME, Book.class, null, null);
+        }
+    }
+    
     private Message createMessage() {
         ProviderFactory factory = ProviderFactory.getInstance();
         Message m = new MessageImpl();



Mime
View raw message