cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r803958 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ rt/fro...
Date Thu, 13 Aug 2009 17:45:56 GMT
Author: dkulp
Date: Thu Aug 13 17:45:53 2009
New Revision: 803958

URL: http://svn.apache.org/viewvc?rev=803958&view=rev
Log:
Merged revisions 801962 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r801962 | sergeyb | 2009-08-07 08:11:17 -0400 (Fri, 07 Aug 2009) | 1 line
  
  JAXRS : updating JAXB provider to optionally handle JAXB classes without XMLRootElement
........

Added:
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/SuperBook.java
      - copied unchanged from r801962, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/SuperBook.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/SuperBook2.java
      - copied unchanged from r801962, cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/SuperBook2.java
Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/Book.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ObjectFactory.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Aug 13 17:45:53 2009
@@ -1 +1 @@
-/cxf/trunk:782728-782730,783097,783294,783396,784059,784181-784184,784893,784895,785279-785282,785468,785621,785624,785651,785734,785866,786142,786271-786272,786395,786512,786514,786582-786583,786638,786647,786850,787200,787269,787277-787279,787290-787291,787305,787323,787366,787849,788030,788060,788187,788444,788451,788703,788752,788774,788819-788820,789013,789371,789387,789420,789527-789530,789704-789705,789788,789811,789896-789901,790074,790094,790134,790188,790294,790553,790637-790644,790868,791301,791354,791538,791753,791947,792007,792096,792183,792261-792265,792271,792604,792683-792685,792975,792985,793059,793570,794297,794396,794680,794728,794771,794778-794780,794892,795044,795104,795160,795583,795907,796022-796023,796352,796593,796741,796780,796994-796997,797117,797159,797192,797194,797231-797233,797442,797505,797517,797534,797581-797583,797587,797640,797651,797699,797882-797883,798344-798346,798363,798461,798479,798533,798551,798557,798561-798562,798570,798573,79858
 4,798654,798748-798749,798891,798929-798930,799245,799267,799439,799448,799637,799723-799724,799792,800453,800497-800498,801380-801381,801447
+/cxf/trunk:782728-782730,783097,783294,783396,784059,784181-784184,784893,784895,785279-785282,785468,785621,785624,785651,785734,785866,786142,786271-786272,786395,786512,786514,786582-786583,786638,786647,786850,787200,787269,787277-787279,787290-787291,787305,787323,787366,787849,788030,788060,788187,788444,788451,788703,788752,788774,788819-788820,789013,789371,789387,789420,789527-789530,789704-789705,789788,789811,789896-789901,790074,790094,790134,790188,790294,790553,790637-790644,790868,791301,791354,791538,791753,791947,792007,792096,792183,792261-792265,792271,792604,792683-792685,792975,792985,793059,793570,794297,794396,794680,794728,794771,794778-794780,794892,795044,795104,795160,795583,795907,796022-796023,796352,796593,796741,796780,796994-796997,797117,797159,797192,797194,797231-797233,797442,797505,797517,797534,797581-797583,797587,797640,797651,797699,797882-797883,798344-798346,798363,798461,798479,798533,798551,798557,798561-798562,798570,798573,79858
 4,798654,798748-798749,798891,798929-798930,799245,799267,799439,799448,799637,799723-799724,799792,800453,800497-800498,801380-801381,801447,801962

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
Thu Aug 13 17:45:53 2009
@@ -304,9 +304,13 @@
             //result in a call to the server when we have already decided not to.
             //Throw an exception if we have one
             Exception ex = exchange.getOutMessage().getContent(Exception.class);
-            throw ex;
+            if (ex != null) {
+                throw ex; 
+            } else {
+                throw new RuntimeException("Unknown client side exception");
+            }
         } 
-        int status = responseCode.intValue(); //conn.getResponseCode();
+        int status = responseCode.intValue();
         responseBuilder = Response.status(status);
         for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet())
{
             if (null == entry.getKey()) {

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Thu Aug 13 17:45:53 2009
@@ -83,21 +83,38 @@
     private static Set<Class<?>> collectionContextClasses = new HashSet<Class<?>>();
     private static JAXBContext collectionContext; 
     
+    protected Map<String, String> jaxbElementClassMap;
+    protected boolean unmarshalAsJaxbElement;
+    
     private MessageContext mc;
     private Schema schema;
     private String collectionWrapperName;
     private Map<String, String> collectionWrapperMap;
     private List<String> jaxbElementClassNames;
     
+    public void setUnmarshallAsJaxbElement(boolean value) {
+        unmarshalAsJaxbElement = value;
+    }
+    
     public void setJaxbElementClassNames(List<String> names) {
         jaxbElementClassNames = names;
     }
+    
+    public void setJaxbElementClassMap(Map<String, String> map) {
+        jaxbElementClassMap = map;
+    }
 
     @SuppressWarnings("unchecked")
     protected Object convertToJaxbElementIfNeeded(Object obj, Class<?> cls, Type genericType)

         throws Exception {
-        if (jaxbElementClassNames != null && jaxbElementClassNames.contains(cls.getName()))
{
-            QName name = getJaxbQName(cls, genericType, obj, false);
+        if (jaxbElementClassNames != null && jaxbElementClassNames.contains(cls.getName())

+            || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(cls.getName()))
{
+            QName name = null;
+            if (jaxbElementClassMap != null) {
+                name = convertStringToQName(jaxbElementClassMap.get(cls.getName()));
+            } else {
+                name = getJaxbQName(cls, genericType, obj, false);
+            }
             if (name != null) {
                 return new JAXBElement(name, cls, null, obj);
             }
@@ -147,6 +164,10 @@
             return getJaxbQName(cls, type, object, pluralName);
         }
             
+        return convertStringToQName(name);
+    }
+    
+    private QName convertStringToQName(String name) {
         int ind1 = name.indexOf('{');
         if (ind1 != 0) {
             return new QName(name);

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
Thu Aug 13 17:45:53 2009
@@ -144,11 +144,16 @@
                 unmarshaller.setEventHandler(eventHandler);
             }
             Object response = null;
-            if (JAXBElement.class.isAssignableFrom(type)) {
+            if (JAXBElement.class.isAssignableFrom(type) 
+                || unmarshalAsJaxbElement 
+                || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(theType.getName()))
{
                 response = unmarshaller.unmarshal(new StreamSource(is), theType);
             } else {
                 response = doUnmarshal(unmarshaller, type, is, mt);
             }
+            if (response instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type))
{
+                response = ((JAXBElement)response).getValue();    
+            }
             if (isCollection) {
                 response = ((CollectionWrapper)response).getCollectionOrArray(theType, type);

             }

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JSONProvider.java
Thu Aug 13 17:45:53 2009
@@ -150,11 +150,16 @@
             XMLStreamReader xsw = createReader(type, realStream);
             
             Object response = null;
-            if (JAXBElement.class.isAssignableFrom(type)) {
+            if (JAXBElement.class.isAssignableFrom(type) 
+                || unmarshalAsJaxbElement
+                || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(theType.getName()))
{
                 response = unmarshaller.unmarshal(xsw, theType);
             } else {
                 response = unmarshaller.unmarshal(xsw);
             }
+            if (response instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type))
{
+                response = ((JAXBElement)response).getValue();    
+            }
             response = checkAdapter(response, anns, false);
             return response;
             

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/Book.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/Book.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/Book.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/Book.java
Thu Aug 13 17:45:53 2009
@@ -19,11 +19,18 @@
 
 package org.apache.cxf.jaxrs.fortest.jaxb;
 
+import javax.xml.bind.annotation.XmlSeeAlso;
 
+@XmlSeeAlso({SuperBook.class })
 public class Book {
     private String name;
     private long id;
     
+    public Book(String name, long id) {
+        this.name = name;
+        this.id = id;
+    }
+    
     public Book() {
     }
     

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ObjectFactory.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ObjectFactory.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ObjectFactory.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/jaxb/ObjectFactory.java
Thu Aug 13 17:45:53 2009
@@ -18,9 +18,29 @@
  */
 package org.apache.cxf.jaxrs.fortest.jaxb;
 
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.annotation.XmlElementDecl;
+import javax.xml.bind.annotation.XmlRegistry;
+import javax.xml.namespace.QName;
 
+@XmlRegistry
 public class ObjectFactory {
+    private static final QName SUPERBOOK2_QNAME = new QName("http://books", "SuperBook2");
+    
     public Book createBook() {
         return new Book();
     }
+    
+    public SuperBook createSuperBook() {
+        return new SuperBook();
+    }
+    
+    public SuperBook2 createSuperBook2() {
+        return new SuperBook2();
+    }
+    
+    @XmlElementDecl(namespace = "http://books", name = "SuperBook2")
+    public JAXBElement<SuperBook2> createExactlyOne(SuperBook2 value) {
+        return new JAXBElement<SuperBook2>(SUPERBOOK2_QNAME, SuperBook2.class, null,
value);
+    }
 }

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java?rev=803958&r1=803957&r2=803958&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java
Thu Aug 13 17:45:53 2009
@@ -159,7 +159,7 @@
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         provider.writeTo(b, Book.class, Book.class,
                        new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
Object>(), bos);
-        readSuperBook(bos.toString());
+        readSuperBook(bos.toString(), true);
     }
     
     @Test
@@ -171,11 +171,87 @@
         provider.writeTo(b, Book.class, Book.class,
                        new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
Object>(), bos);
         
-        readSuperBook(bos.toString());
+        readSuperBook(bos.toString(), false);
+    }
+    
+    @Test
+    public void testWriteWithoutXmlRootElement() throws Exception {
+        doTestWriteWithoutXmlRootElement("SuperBook", false);
+    }
+    
+    @Test
+    public void testWriteWithoutXmlRootElement2() throws Exception {
+        doTestWriteWithoutXmlRootElement("SuperBook", true);
+    }
+    
+    @Test
+    public void testWriteWithoutXmlRootElement3() throws Exception {
+        doTestWriteWithoutXmlRootElement("{http://books}SuperBook", false);
+    }
+    
+    public void doTestWriteWithoutXmlRootElement(String name, boolean unmarshalAsJaxbElement)

+        throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setJaxbElementClassMap(Collections.singletonMap(
+            org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), 
+            name));
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        readSuperBook2(bos.toString(), unmarshalAsJaxbElement);
+    }
+    
+    @Test
+    public void testWriteWithoutXmlRootElementDerived() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setJaxbElementClassMap(Collections.singletonMap(
+            org.apache.cxf.jaxrs.fortest.jaxb.Book.class.getName(), "Book"));
+        org.apache.cxf.jaxrs.fortest.jaxb.Book b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.Book.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        readSuperBook2(bos.toString(), false);
+    }
+    
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testWriteWithoutXmlRootElementObjectFactory() throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setJaxbElementClassMap(Collections.singletonMap(
+            org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2.class.getName(), 
+            "{http://books}SuperBook2"));
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2 b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2("CXF in Action", 123L, 124L);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        JAXBElementProvider provider2 = new JAXBElementProvider();
+        ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2 book = 
+            (org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2)provider2.readFrom(
+                       (Class)org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2.class, 
+                       org.apache.cxf.jaxrs.fortest.jaxb.SuperBook2.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
String>(), is);
+        assertEquals(124L, book.getSuperId());
     }
     
     @SuppressWarnings("unchecked")
-    private void readSuperBook(String data) throws Exception {
+    private void readSuperBook(String data, boolean xsiTypeExpected) throws Exception {
+        if (xsiTypeExpected) {
+            assertTrue(data.contains("xsi:type"));
+        }
         JAXBElementProvider provider = new JAXBElementProvider();
         ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
         SuperBook book = (SuperBook)provider.readFrom(
@@ -184,6 +260,24 @@
         assertEquals(124L, book.getSuperId());
     }
     
+    @SuppressWarnings("unchecked")
+    private void readSuperBook2(String data, boolean unmarshalAsJaxbElement) throws Exception
{
+        JAXBElementProvider provider = new JAXBElementProvider();
+        if (!unmarshalAsJaxbElement) {
+            provider.setJaxbElementClassMap(Collections.singletonMap(
+                org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), "SuperBook"));
+        } else {
+            provider.setUnmarshallAsJaxbElement(true);
+        }
+        ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook book = 
+            (org.apache.cxf.jaxrs.fortest.jaxb.SuperBook)provider.readFrom(
+                       (Class)org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class, 
+                       org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class,
+                       new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String,
String>(), is);
+        assertEquals(124L, book.getSuperId());
+    }
+    
     private void doTestWriteJAXBCollection(String mName) throws Exception {
         JAXBElementProvider provider = new JAXBElementProvider();
         List<JAXBElement<Book>> books = new ArrayList<JAXBElement<Book>>();



Mime
View raw message