cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1543802 - in /cxf/branches/2.7.x-fixes: ./ rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
Date Wed, 20 Nov 2013 12:22:58 GMT
Author: sergeyb
Date: Wed Nov 20 12:22:58 2013
New Revision: 1543802

URL: http://svn.apache.org/r1543802
Log:
Merged revisions 1543800 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1543800 | sergeyb | 2013-11-20 12:18:26 +0000 (Wed, 20 Nov 2013) | 1 line
  
  [CXF-5404,CXF-5406] Fixing NPE and ClassCastException when reading entries with no content
or without 'type' parameter
........

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

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1543800

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

Modified: cxf/branches/2.7.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.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java?rev=1543802&r1=1543801&r2=1543802&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
Wed Nov 20 12:22:58 2013
@@ -561,8 +561,8 @@ public class AtomPojoProvider extends Ab
         reportError(message, ex, 500);
     }
     
-    private boolean isFeedRequested(MediaType mt) {
-        if ("entry".equals(mt.getParameters().get("type"))) {
+    protected boolean isFeedRequested(MediaType mt) {
+        if ("entry".equalsIgnoreCase(mt.getParameters().get("type"))) {
             return false;
         }
         return true;
@@ -603,25 +603,29 @@ public class AtomPojoProvider extends Ab
         boolean isFeed = isFeedRequested(mt);
         
         if (isFeed) {
-            return readFromFeed(cls, mt, headers, is);
+            return readFromFeedOrEntry(cls, mt, headers, is);
         } else {
             AtomEntryProvider p = new AtomEntryProvider();
             p.setAutodetectCharset(autodetectCharset);
             Entry entry = p.readFrom(Entry.class, Entry.class, 
                                      new Annotation[]{}, mt, headers, is);
-            return readFromEntry(entry, cls, mt, headers, is);
+            return readFromEntry(entry, cls);
         }
     }
     
     @SuppressWarnings("unchecked")
-    private Object readFromFeed(Class<Object> cls, MediaType mt, 
+    private Object readFromFeedOrEntry(Class<Object> cls, MediaType mt, 
                            MultivaluedMap<String, String> headers, InputStream is)

         throws IOException {
         
         AtomFeedProvider p = new AtomFeedProvider();
         p.setAutodetectCharset(autodetectCharset);
-        Feed feed = p.readFrom(Feed.class, Feed.class, new Annotation[]{}, mt, headers, is);
-        
+        Object atomObject = p.readFrom(Feed.class, Feed.class, new Annotation[]{}, mt, headers,
is);
+        if (atomObject instanceof Entry) {
+            return this.readFromEntry((Entry)atomObject, cls);
+        }
+            
+        Feed feed = (Feed)atomObject;
         AtomElementReader<?, ?> reader = getAtomReader(cls);
         if (reader != null) {
             return ((AtomElementReader<Feed, Object>)reader).readFrom(feed);
@@ -634,7 +638,7 @@ public class AtomPojoProvider extends Ab
                 = (Class<Object>)InjectionUtils.getActualType(m.getGenericParameterTypes()[0]);
             List<Object> objects = new ArrayList<Object>();
             for (Entry e : feed.getEntries()) {
-                objects.add(readFromEntry(e, realCls, mt, headers, is));
+                objects.add(readFromEntry(e, realCls));
             }
             instance = cls.newInstance();
             m.invoke(instance, new Object[]{objects});
@@ -646,20 +650,22 @@ public class AtomPojoProvider extends Ab
     }
     
     @SuppressWarnings("unchecked")
-    private Object readFromEntry(Entry entry, Class<Object> cls, MediaType mt, 
-                            MultivaluedMap<String, String> headers, InputStream is)

+    private Object readFromEntry(Entry entry, Class<Object> cls) 
         throws IOException {
         
         AtomElementReader<?, ?> reader = getAtomReader(cls);
         if (reader != null) {
             return ((AtomElementReader<Entry, Object>)reader).readFrom(entry);
         }
-        try {
-            Unmarshaller um = 
-                jaxbProvider.getJAXBContext(cls, cls).createUnmarshaller();
-            return cls.cast(um.unmarshal(new StringReader(entry.getContent())));
-        } catch (Exception ex) {
-            reportError("Object of type " + cls.getName() + " can not be deserialized from
Entry", ex, 400);
+        String entryContent = entry.getContent();
+        if (entryContent != null) {
+            try {
+                Unmarshaller um = 
+                    jaxbProvider.getJAXBContext(cls, cls).createUnmarshaller();
+                return cls.cast(um.unmarshal(new StringReader(entryContent)));
+            } catch (Exception ex) {
+                reportError("Object of type " + cls.getName() + " can not be deserialized
from Entry", ex, 400);
+            }
         }
         return null;
     }

Modified: cxf/branches/2.7.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.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java?rev=1543802&r1=1543801&r2=1543802&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
(original)
+++ cxf/branches/2.7.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
Wed Nov 20 12:22:58 2013
@@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.StringReader;
 import java.lang.annotation.Annotation;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -31,6 +32,7 @@ import javax.xml.bind.annotation.XmlRoot
 
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 
 import org.junit.Assert;
@@ -39,7 +41,6 @@ import org.junit.Test;
 
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-
 public class AtomPojoProviderTest extends Assert {
 
     private ClassPathXmlApplicationContext ctx;
@@ -177,7 +178,71 @@ public class AtomPojoProviderTest extend
         assertTrue("a".equals(list.get(0).getName()) || "a".equals(list.get(1).getName()));
         assertTrue("b".equals(list.get(0).getName()) || "b".equals(list.get(1).getName()));
       
     }
-        
+     
+    @Test
+    public void testReadEntryNoContent() throws Exception {
+        /** A sample entry without content. */
+        final String entryNoContent =
+            "<?xml version='1.0' encoding='UTF-8'?>\n" 
+            + "<entry xmlns=\"http://www.w3.org/2005/Atom\">\n" 
+            + "  <id>84297856</id>\n" 
+            + "</entry>";
+
+        AtomPojoProvider atomPojoProvider = new AtomPojoProvider();
+        @SuppressWarnings({
+            "rawtypes", "unchecked"
+        })
+        JaxbDataType type = (JaxbDataType)atomPojoProvider.readFrom((Class)JaxbDataType.class,
+                                  JaxbDataType.class,
+                                  new Annotation[0],
+                                  MediaType.valueOf("application/atom+xml;type=entry"),
+                                  new MetadataMap<String, String>(),
+                                  new ByteArrayInputStream(entryNoContent.getBytes(Charset.forName("UTF-8"))));
+        assertNull(type);
+    }
+    
+    @Test
+    public void testReadEntryWithUpperCaseTypeParam() throws Exception {
+        doReadEntryWithContent("application/atom+xml;type=ENTRY");
+    }
+    
+    @Test
+    public void testReadEntryNoTypeParam() throws Exception {
+        doReadEntryWithContent("application/atom+xml");
+    }
+    
+    private void doReadEntryWithContent(String mediaType) throws Exception {
+        final String entryWithContent =
+            "<?xml version='1.0' encoding='UTF-8'?>\n" 
+            + "<entry xmlns=\"http://www.w3.org/2005/Atom\">\n" 
+            + "  <id>84297856</id>\n" 
+            + "  <content type=\"application/xml\">\n" 
+            + "    <jaxbDataType xmlns=\"\">\n" 
+            + "    </jaxbDataType>\n" 
+            + "  </content>\n" 
+            + "</entry>";
+
+        AtomPojoProvider atomPojoProvider = new AtomPojoProvider();
+        @SuppressWarnings({
+            "rawtypes", "unchecked"
+        })
+        JaxbDataType type = (JaxbDataType)atomPojoProvider.readFrom((Class)JaxbDataType.class,
+                                  JaxbDataType.class,
+                                  new Annotation[0],
+                                  MediaType.valueOf(mediaType),
+                                  new MetadataMap<String, String>(),
+                                  new ByteArrayInputStream(entryWithContent.getBytes(Charset.forName("UTF-8"))));
+        assertNotNull(type);
+    }
+    
+    /**
+     * A sample JAXB data-type to read data into.
+     */
+    @XmlRootElement
+    public static class JaxbDataType {
+        // no data
+    }
+    
     private Entry getEntry(List<Entry> entries, String title) {
         for (Entry e : entries) {
             if (title.equals(e.getTitle())) {



Mime
View raw message