cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1383468 - in /cxf/branches/2.6.x-fixes: ./ rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/ rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/
Date Tue, 11 Sep 2012 16:21:27 GMT
Author: sergeyb
Date: Tue Sep 11 16:21:26 2012
New Revision: 1383468

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

........
  r1383465 | sergeyb | 2012-09-11 17:10:46 +0100 (Tue, 11 Sep 2012) | 1 line
  
  [CXF-4501] Update to AtomPojoProvider to avoid using JAXB to generate inlined content if
POJO itself or builder has a ready one
........

Modified:
    cxf/branches/2.6.x-fixes/   (props changed)
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractEntryBuilder.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/servers.xml

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1383465

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

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractEntryBuilder.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractEntryBuilder.java?rev=1383468&r1=1383467&r2=1383468&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractEntryBuilder.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractEntryBuilder.java
Tue Sep 11 16:21:26 2012
@@ -42,4 +42,13 @@ public abstract class AbstractEntryBuild
     public String getSummary(T pojo) {
         return null;
     }
+    
+    /**
+     * 
+     * @param pojo Object which is being mapped
+     * @return entry content
+     */
+    public String getContent(T pojo) {
+        return null;
+    }
 }

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java?rev=1383468&r1=1383467&r2=1383468&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
Tue Sep 11 16:21:26 2012
@@ -70,6 +70,7 @@ public class AtomPojoProvider<T> extends
     
     private static final Logger LOG = LogUtils.getL7dLogger(AtomPojoProvider.class);
     private static final Abdera ATOM_ENGINE = new Abdera();
+    private static final String DEFAULT_ENTRY_CONTENT_METHOD = "getContent";
     
     private JAXBElementProvider<T> jaxbProvider = new JAXBElementProvider<T>();
     private Map<String, String> collectionGetters = Collections.emptyMap();
@@ -80,6 +81,16 @@ public class AtomPojoProvider<T> extends
     
     private MessageContext mc;   
     private boolean formattedOutput;
+    private boolean useJaxbForContent = true;
+    private String entryContentMethodName = DEFAULT_ENTRY_CONTENT_METHOD;
+    
+    public void setUseJaxbForContent(boolean use) {
+        this.useJaxbForContent = use;
+    }
+    
+    public void setEntryContentMethodName(String name) {
+        this.entryContentMethodName = name;
+    }
     
     @Context
     public void setMessageContext(MessageContext context) {
@@ -343,16 +354,27 @@ public class AtomPojoProvider<T> extends
     
     protected void createEntryContent(Entry e, Object o, Class<?> cls) throws Exception
{
     
-        Factory factory = Abdera.getNewFactory();
-        JAXBContext jc = jaxbProvider.getJAXBContext(cls, cls);
+        String content = null;
+        
+        if (useJaxbForContent) {
+            JAXBContext jc = jaxbProvider.getJAXBContext(cls, cls);
+            StringWriter writer = new StringWriter();
+            jc.createMarshaller().marshal(o, writer);
+            content = writer.toString();
+        } else {
+            Method m = cls.getMethod(entryContentMethodName, new Class[]{});
+            content = (String)m.invoke(o, new Object[]{});
+        }
         
-        StringWriter writer = new StringWriter();
-        jc.createMarshaller().marshal(o, writer);
+        setEntryContent(e, content);
         
+    }
+    
+    protected void setEntryContent(Entry e, String content) {
+        Factory factory = Abdera.getNewFactory();
         e.setContentElement(factory.newContent());
         e.getContentElement().setContentType(Content.Type.XML);
-        e.getContentElement().setValue(writer.toString());
-        
+        e.getContentElement().setValue(content);
     }
     
     protected <X> void setEntryProperties(Factory factory, Entry entry, 
@@ -419,6 +441,11 @@ public class AtomPojoProvider<T> extends
             }
         }
         
+        String content = theBuilder.getContent(o);
+        if (content != null) {
+            setEntryContent(entry, content);    
+        }
+        
     }
 
     private <X> void setCommonElementProperties(Factory factory, ExtensibleElement
element, 

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java?rev=1383468&r1=1383467&r2=1383468&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
Tue Sep 11 16:21:26 2012
@@ -75,6 +75,40 @@ public class AtomPojoProviderTest extend
     }
     
     @Test
+    public void testWriteFeedWithBuildersNoJaxb() throws Exception {
+        @SuppressWarnings("unchecked")
+        AtomPojoProvider<Books> provider = (AtomPojoProvider<Books>)ctx.getBean("atomNoJaxb");
+        assertNotNull(provider);
+        provider.setFormattedOutput(true);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        
+        Books books = new Books();
+        List<Book> bs = new ArrayList<Book>();
+        bs.add(new Book("a"));
+        bs.add(new Book("b"));
+        books.setBooks(bs);
+        provider.writeTo(books, Books.class, Books.class, new Annotation[]{},
+                         MediaType.valueOf("application/atom+xml"), null, bos);
+        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+        Feed feed = new AtomFeedProvider().readFrom(Feed.class, null, null, null, null, bis);
+        assertEquals("Books", feed.getTitle()); 
+        List<Entry> entries = feed.getEntries();
+        assertEquals(2, entries.size());
+        
+        Entry entryA = getEntry(entries, "a");
+        verifyEntry(entryA, "a");
+        String entryAContent = entryA.getContent();
+        assertTrue("<a/>".equals(entryAContent) || "<a><a/>".equals(entryAContent)
+                   || "<a xmlns=\"\"/>".equals(entryAContent));
+        
+        Entry entryB = getEntry(entries, "b");
+        verifyEntry(entryB, "b");
+        String entryBContent = entryB.getContent();
+        assertTrue("<b/>".equals(entryBContent) || "<b><b/>".equals(entryBContent)
+                   || "<b xmlns=\"\"/>".equals(entryBContent));
+    }
+    
+    @Test
     public void testWriteEntryWithBuilders() throws Exception {
         @SuppressWarnings("unchecked")
         AtomPojoProvider<Book> provider = (AtomPojoProvider<Book>)ctx.getBean("atom2");
@@ -160,7 +194,7 @@ public class AtomPojoProviderTest extend
         assertNotNull(e);
         assertEquals(title, e.getTitle());
     }
- 
+    
     public static class CustomFeedWriter implements AtomElementWriter<Feed, Books>
{
 
         public void writeTo(Feed feed, Books pojoFeed) {
@@ -245,6 +279,9 @@ public class AtomPojoProviderTest extend
             return name;
         }
         
+        public String getXMLContent() {
+            return "<" + name + "/>";
+        }
         
     }
     

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/servers.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/servers.xml?rev=1383468&r1=1383467&r2=1383468&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/servers.xml
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/servers.xml
Tue Sep 11 16:21:26 2012
@@ -31,6 +31,13 @@ http://www.springframework.org/schema/ut
     <property name="atomBuilders" ref="atomBuilders"/>
   </bean>
   
+  <bean id="atomNoJaxb" class="org.apache.cxf.jaxrs.provider.atom.AtomPojoProvider">
+    <property name="atomWriters" ref="atomWriters"/>
+    <property name="atomBuilders" ref="atomBuilders"/>
+    <property name="useJaxbForContent" value="false"/>
+    <property name="entryContentMethodName" value="getXMLContent"/>
+  </bean>
+  
   <bean id="atom2" class="org.apache.cxf.jaxrs.provider.atom.AtomPojoProvider">
     <property name="atomWriters" ref="atomWriters2"/>
     <property name="atomBuilders" ref="atomBuilders2"/>



Mime
View raw message