cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r812919 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/
Date Wed, 09 Sep 2009 12:15:25 GMT
Author: sergeyb
Date: Wed Sep  9 12:15:25 2009
New Revision: 812919

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

........
  r812899 | sergeyb | 2009-09-09 12:45:13 +0100 (Wed, 09 Sep 2009) | 1 line
  
  JAXRS : support for writing explicit collections with members having no XmlRootElement
........

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/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/provider/JAXBElementProviderTest.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Sep  9 12:15:25 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,798816,798891,798929-798930,799245,799267,799439,799448,799637,799723-799724,799792,800453,800497-800498,801380-801381,801447,801962,802892,803056,803129,803174,803419,803460,803493,803689,804002,804175,804276,805784,805907,805909,806020-806021,806023,806405-806406,806576,806602-806604,806620,806627,806631,806633,806638,806687,806876,806922,806979-806982,807181,807205,807295,807748,807807,808035,808069,808085,808107,808464,808488,808731,808885,808925,809082-809083,809162,809190,809417-809626,809631,809663,809706,809738,809962,810090,810143,810349,810376,810513,810526,810927,811013,811148-811149,811175,811183,811387,811467-811468,811680,811696,812232,812542
+/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,798816,798891,798929-798930,799245,799267,799439,799448,799637,799723-799724,799792,800453,800497-800498,801380-801381,801447,801962,802892,803056,803129,803174,803419,803460,803493,803689,804002,804175,804276,805784,805907,805909,806020-806021,806023,806405-806406,806576,806602-806604,806620,806627,806631,806633,806638,806687,806876,806922,806979-806982,807181,807205,807295,807748,807807,808035,808069,808085,808107,808464,808488,808731,808885,808925,809082-809083,809162,809190,809417-809626,809631,809663,809706,809738,809962,810090,810143,810349,810376,810513,810526,810927,811013,811148-811149,811175,811183,811387,811467-811468,811680,811696,812232,812542,812899

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/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=812919&r1=812918&r2=812919&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
Wed Sep  9 12:15:25 2009
@@ -389,15 +389,13 @@
         return unmarshaller;        
     }
     
-    protected Marshaller createMarshaller(Object obj, Class<?> cls, Type genericType,
String enc, 
-                                          boolean isCollection)
+    protected Marshaller createMarshaller(Object obj, Class<?> cls, Type genericType,
String enc)
         throws JAXBException {
         
         Class<?> objClazz = JAXBElement.class.isAssignableFrom(cls) 
                             ? ((JAXBElement)obj).getDeclaredType() : cls;
                             
-        JAXBContext context = isCollection ? getCollectionContext(objClazz) 
-            : getJAXBContext(objClazz, genericType);
+        JAXBContext context = getJAXBContext(objClazz, genericType);
         Marshaller marshaller = context.createMarshaller();
         if (enc != null) {
             marshaller.setProperty(Marshaller.JAXB_ENCODING, enc);
@@ -405,12 +403,6 @@
         return marshaller;
     }
     
-    protected Marshaller createMarshaller(Object obj, Class<?> cls, Type genericType,
String enc)
-        throws JAXBException {
-        
-        return createMarshaller(obj, cls, genericType, enc, false);
-    }
-    
     protected String getEncoding(MediaType mt, MultivaluedMap<String, Object> headers)
{
         String enc = mt.getParameters().get(CHARSET_PARAMETER);
         if (enc == null) {
@@ -535,6 +527,16 @@
         @SuppressWarnings("unchecked")
         public <T> Object getCollectionOrArray(Class<T> type, Class<?>
origType) {
             List<?> theList = getList();
+            if (theList.size() > 0) {
+                Object first = theList.get(0);
+                if (first instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type))
{
+                    List<Object> newList = new ArrayList<Object>(theList.size());
+                    for (Object o : theList) {
+                        newList.add(((JAXBElement)o).getValue());
+                    }
+                    theList = newList;
+                }
+            }
             if (origType.isArray()) {
                 T[] values = (T[])Array.newInstance(type, theList.size());
                 for (int i = 0; i < theList.size(); i++) {

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=812919&r1=812918&r2=812919&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
Wed Sep  9 12:15:25 2009
@@ -266,6 +266,10 @@
     
     protected void marshalCollectionMember(Object obj, Class<?> cls, Type genericType,

                            String enc, OutputStream os, MediaType mt, String ns) throws Exception
{
+        obj = convertToJaxbElementIfNeeded(obj, cls, genericType);
+        if (obj instanceof JAXBElement && cls != JAXBElement.class) {
+            cls = JAXBElement.class;
+        }
         Marshaller ms = createMarshaller(obj, cls, genericType, enc);
         ms.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
         if (ns.length() > 0) {

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=812919&r1=812918&r2=812919&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
Wed Sep  9 12:15:25 2009
@@ -289,8 +289,13 @@
         os.write(startTag.getBytes());
         Object[] arr = originalCls.isArray() ? (Object[])actualObject : ((Collection)actualObject).toArray();
         for (int i = 0; i < arr.length; i++) {
-            Marshaller ms = createMarshaller(actualObject, actualClass, genericType, encoding);
-            marshal(ms, arr[i], actualClass, genericType, encoding, os, true);
+            Object obj = convertToJaxbElementIfNeeded(arr[i], actualClass, genericType);
+            Class<?> cls = actualClass;
+            if (obj instanceof JAXBElement && actualClass != JAXBElement.class) {
+                cls = JAXBElement.class;
+            }
+            Marshaller ms = createMarshaller(obj, cls, genericType, encoding);
+            marshal(ms, obj, cls, genericType, encoding, os, true);
             if (i + 1 < arr.length) {
                 os.write(",".getBytes());
             }

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=812919&r1=812918&r2=812919&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
Wed Sep  9 12:15:25 2009
@@ -217,6 +217,31 @@
     }
     
     @Test
+    public void testWriteCollectionWithoutXmlRootElement() 
+        throws Exception {
+        JAXBElementProvider provider = new JAXBElementProvider();
+        provider.setCollectionWrapperName("{http://superbooks}SuperBooks");
+        provider.setJaxbElementClassMap(Collections.singletonMap(
+                org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), 
+                "{http://superbooks}SuperBook"));
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
+        List<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook> books = 
+            Collections.singletonList(b);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(books, List.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class,
+                         new Annotation[0], MediaType.TEXT_XML_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        String expected = "<ns1:SuperBooks xmlns:ns1=\"http://superbooks\">"
+            + "<ns1:SuperBook xmlns:ns2=\"http://books\" xmlns:ns1=\"http://superbooks\"><id>123</id>"
+            + "<name>CXF in Action</name><superId>124</superId></ns1:SuperBook></ns1:SuperBooks>";
+        assertEquals(expected, bos.toString());
+    }
+    
+    
+    @Test
     public void testWriteWithoutXmlRootElementDerived() throws Exception {
         JAXBElementProvider provider = new JAXBElementProvider();
         provider.setJaxbElementClassMap(Collections.singletonMap(

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java?rev=812919&r1=812918&r2=812919&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
Wed Sep  9 12:15:25 2009
@@ -56,6 +56,27 @@
 
 public class JSONProviderTest extends Assert {
 
+    @Test
+    public void testWriteCollectionWithoutXmlRootElement() 
+        throws Exception {
+        JSONProvider provider = new JSONProvider();
+        provider.setCollectionWrapperName("{http://superbooks}SuperBooks");
+        provider.setJaxbElementClassMap(Collections.singletonMap(
+                org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class.getName(), 
+                "{http://superbooks}SuperBook"));
+        org.apache.cxf.jaxrs.fortest.jaxb.SuperBook b = 
+            new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
+        List<org.apache.cxf.jaxrs.fortest.jaxb.SuperBook> books = 
+            Collections.singletonList(b);
+        
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        provider.writeTo(books, List.class, 
+                         org.apache.cxf.jaxrs.fortest.jaxb.SuperBook.class,
+                         new Annotation[0], MediaType.APPLICATION_JSON_TYPE, 
+                         new MetadataMap<String, Object>(), bos);
+        String expected = "{\"ns1.SuperBooks\":[{\"id\":123,\"name\":\"CXF in Action\",\"superId\":124}]}";
+        assertEquals(expected, bos.toString());
+    }
     
     @SuppressWarnings("unchecked")
     @Test



Mime
View raw message