cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1384254 - in /cxf/trunk/rt/rs/extensions/providers/src: main/java/org/apache/cxf/jaxrs/provider/atom/ test/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/atom/ test/java/org/apache/cxf/jaxrs/provider/json/
Date Thu, 13 Sep 2012 09:40:03 GMT
Author: sergeyb
Date: Thu Sep 13 09:40:02 2012
New Revision: 1384254

URL: http://svn.apache.org/viewvc?rev=1384254&view=rev
Log:
Relaxing the type safety of AtomPojoProvider as a single provider needs to handle both feed
and entry Pojos, registering a provider typed on a feed Pojo from Eclipse prevents the runtime
from selecting it when a Pojo representing an entry is returned from the resource

Added:
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java
      - copied, changed from r1381984, cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java
Removed:
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java
Modified:
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java?rev=1384254&r1=1384253&r2=1384254&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProvider.java
Thu Sep 13 09:40:02 2012
@@ -65,14 +65,14 @@ import org.apache.cxf.jaxrs.utils.Inject
 @Produces({"application/atom+xml", "application/atom+xml;type=feed", "application/atom+xml;type=entry"
})
 @Consumes({"application/atom+xml", "application/atom+xml;type=feed", "application/atom+xml;type=entry"
})
 @Provider
-public class AtomPojoProvider<T> extends AbstractConfigurableProvider
-    implements MessageBodyWriter<T>, MessageBodyReader<T> {
+public class AtomPojoProvider extends AbstractConfigurableProvider
+    implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
     
     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 JAXBElementProvider<Object> jaxbProvider = new JAXBElementProvider<Object>();
     private Map<String, String> collectionGetters = Collections.emptyMap();
     private Map<String, String> collectionSetters = Collections.emptyMap();
     private Map<String, AtomElementWriter<?, ?>> atomWriters = Collections.emptyMap();
@@ -117,7 +117,7 @@ public class AtomPojoProvider<T> extends
         }
     }
     
-    public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
+    public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mt) {
         return -1;
     }
     
@@ -133,11 +133,11 @@ public class AtomPojoProvider<T> extends
         return !Feed.class.isAssignableFrom(type) && !Entry.class.isAssignableFrom(type);
     }
 
-    public void writeTo(T o, Class<?> clazz, Type genericType, Annotation[] annotations,

+    public void writeTo(Object o, Class<?> cls, Type genericType, Annotation[] annotations,

                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream
os)
         throws IOException {
         boolean isFeed = isFeedRequested(mt);        
-        boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(clazz);
+        boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(cls);
         
         
         if (isFeed && isCollection) {
@@ -151,8 +151,6 @@ public class AtomPojoProvider<T> extends
             if (isFeed && !isCollection) {
                 atomElement = createFeedFromCollectionWrapper(o);
             } else if (!isFeed && !isCollection) {
-                @SuppressWarnings("unchecked")
-                Class<? extends T> cls = (Class<? extends T>)clazz;
                 atomElement = createEntryFromObject(o, cls);
             }
         } catch (Exception ex) {
@@ -233,15 +231,14 @@ public class AtomPojoProvider<T> extends
         return false;
     }
     
-    protected <X> void setFeedFromCollection(Factory factory, Feed feed, X wrapper,

+    protected void setFeedFromCollection(Factory factory, Feed feed, Object wrapper, 
                                              Object collection,
                                              Class<?> collectionCls, 
                                              Type collectionType, 
                                              boolean writerUsed) throws Exception {
         
         Object[] arr = collectionCls.isArray() ? (Object[])collection : ((Collection<?>)collection).toArray();
-        @SuppressWarnings("unchecked")
-        Class<? extends X> memberClass = (Class<? extends X>)InjectionUtils.getActualType(collectionType);
+        Class<?> memberClass = InjectionUtils.getActualType(collectionType);
         
         for (Object o : arr) {
             Entry entry = createEntryFromObject(o, memberClass);
@@ -253,17 +250,17 @@ public class AtomPojoProvider<T> extends
     }
     
     @SuppressWarnings("unchecked")
-    protected <X> void setFeedProperties(Factory factory, Feed feed, X wrapper, Object
collection, 
+    protected void setFeedProperties(Factory factory, Feed feed, Object wrapper, Object collection,

                                          Class<?> collectionCls, Type collectionType)
{
         
-        AbstractAtomElementBuilder<X> builder 
-            = (AbstractAtomElementBuilder<X>)atomBuilders.get(wrapper.getClass().getName());
+        AbstractAtomElementBuilder<Object> builder 
+            = (AbstractAtomElementBuilder<Object>)atomBuilders.get(wrapper.getClass().getName());
         if (builder == null) {
             return;
         }
         setCommonElementProperties(factory, feed, builder, wrapper);
         
-        AbstractFeedBuilder<X> theBuilder = (AbstractFeedBuilder<X>)builder;
+        AbstractFeedBuilder<Object> theBuilder = (AbstractFeedBuilder<Object>)builder;
         
         // the hierarchy is a bit broken in that we can not set author/title.etc on some
         // common Feed/Entry super type
@@ -325,7 +322,7 @@ public class AtomPojoProvider<T> extends
     
     
     
-    protected <X> Entry createEntryFromObject(X o, Class<? extends X> cls) throws
Exception {
+    protected Entry createEntryFromObject(Object o, Class<?> cls) throws Exception
{
         
         Factory factory = Abdera.getNewFactory();
         Entry entry = factory.getAbdera().newEntry();
@@ -377,18 +374,18 @@ public class AtomPojoProvider<T> extends
         e.getContentElement().setValue(content);
     }
     
-    protected <X> void setEntryProperties(Factory factory, Entry entry, 
-                                          X o, Class<? extends X> cls) {
+    protected void setEntryProperties(Factory factory, Entry entry, 
+                                          Object o, Class<?> cls) {
         @SuppressWarnings("unchecked")
-            AbstractAtomElementBuilder<X> builder 
-            = (AbstractAtomElementBuilder<X>)atomBuilders.get(o.getClass().getName());
+        AbstractAtomElementBuilder<Object> builder 
+            = (AbstractAtomElementBuilder<Object>)atomBuilders.get(o.getClass().getName());
         if (builder == null) {
             return;
         }
         
         setCommonElementProperties(factory, entry, builder, o);
         
-        AbstractEntryBuilder<X> theBuilder = (AbstractEntryBuilder<X>)builder;
+        AbstractEntryBuilder<Object> theBuilder = (AbstractEntryBuilder<Object>)builder;
         String author = theBuilder.getAuthor(o);
         if (author != null) {
             entry.addAuthor(author);
@@ -448,9 +445,9 @@ public class AtomPojoProvider<T> extends
         
     }
 
-    private <X> void setCommonElementProperties(Factory factory, ExtensibleElement
element, 
-                                            AbstractAtomElementBuilder<X> builder,
-                                            X o) {
+    private void setCommonElementProperties(Factory factory, ExtensibleElement element, 
+                                            AbstractAtomElementBuilder<Object> builder,
+                                            Object o) {
         String baseUri = builder.getBaseUri(o);
         if (baseUri != null) {
             element.setBaseUri(baseUri);
@@ -492,7 +489,7 @@ public class AtomPojoProvider<T> extends
         return true;
     }
 
-    public T readFrom(Class<T> cls, Type type, Annotation[] anns, MediaType mt, 
+    public Object readFrom(Class<Object> cls, Type type, Annotation[] anns, MediaType
mt, 
                       MultivaluedMap<String, String> headers, InputStream is) 
         throws IOException, WebApplicationException {
         boolean isFeed = isFeedRequested(mt);
@@ -507,7 +504,7 @@ public class AtomPojoProvider<T> extends
     }
     
     @SuppressWarnings("unchecked")
-    private T readFromFeed(Class<T> cls, MediaType mt, 
+    private Object readFromFeed(Class<Object> cls, MediaType mt, 
                            MultivaluedMap<String, String> headers, InputStream is)

         throws IOException {
         
@@ -516,9 +513,9 @@ public class AtomPojoProvider<T> extends
         
         AtomElementReader<?, ?> reader = atomReaders.get(cls.getName());
         if (reader != null) {
-            return ((AtomElementReader<Feed, T>)reader).readFrom(feed);
+            return ((AtomElementReader<Feed, Object>)reader).readFrom(feed);
         }
-        T instance = null;
+        Object instance = null;
         try {
             String methodName = getCollectionMethod(cls, false);
             Method m = cls.getMethod(methodName, new Class[]{List.class});
@@ -538,13 +535,13 @@ public class AtomPojoProvider<T> extends
     }
     
     @SuppressWarnings("unchecked")
-    private <X> X readFromEntry(Entry entry, Class<X> cls, MediaType mt, 
+    private Object readFromEntry(Entry entry, Class<Object> cls, MediaType mt, 
                             MultivaluedMap<String, String> headers, InputStream is)

         throws IOException {
         
         AtomElementReader<?, ?> reader = atomReaders.get(cls.getName());
         if (reader != null) {
-            return ((AtomElementReader<Entry, X>)reader).readFrom(entry);
+            return ((AtomElementReader<Entry, Object>)reader).readFrom(entry);
         }
         try {
             Unmarshaller um = 

Copied: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java
(from r1381984, cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java)
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java?p2=cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java&p1=cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java&r1=1381984&r2=1384254&rev=1384254&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java
Thu Sep 13 09:40:02 2012
@@ -17,21 +17,23 @@
  * under the License.
  */
 
-package org.apache.cxf.jaxrs.provider.json;
+package org.apache.cxf.jaxrs.provider;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 
-import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.provider.atom.AtomPojoProvider;
+import org.apache.cxf.jaxrs.provider.json.JSONProvider;
 import org.apache.cxf.jaxrs.resources.Book;
+import org.apache.cxf.jaxrs.resources.Chapter;
 import org.apache.cxf.message.MessageImpl;
 
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
-public class ProviderFactoryJsonTest extends Assert {
+public class ProviderFactoryAllTest extends Assert {
 
     @Before
     public void setUp() {
@@ -39,6 +41,25 @@ public class ProviderFactoryJsonTest ext
     }
     
     @Test
+    public void testAtomPojoProvider() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        AtomPojoProvider provider = new AtomPojoProvider();
+        pf.registerUserProvider(provider);
+        MessageBodyReader<?> feedReader = pf.createMessageBodyReader(Book.class,
+                                               Book.class, null, 
+                                               MediaType.valueOf("application/atom+xml"),

+                                               new MessageImpl());
+        assertSame(feedReader, provider);
+        
+        MessageBodyReader<?> entryReader = pf.createMessageBodyReader(Chapter.class,

+                                               Chapter.class, null, 
+                                               MediaType.valueOf("application/atom+xml;type=entry"),

+                                               new MessageImpl());
+        assertSame(entryReader, provider);
+    }
+    
+    
+    @Test
     public void testCustomJsonProvider() {
         ProviderFactory pf = ProviderFactory.getInstance();
         JSONProvider<Book> provider = new JSONProvider<Book>();

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java?rev=1384254&r1=1384253&r2=1384254&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/atom/AtomPojoProviderTest.java
Thu Sep 13 09:40:02 2012
@@ -39,6 +39,7 @@ import org.junit.Test;
 
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
+
 public class AtomPojoProviderTest extends Assert {
 
     private ClassPathXmlApplicationContext ctx;
@@ -52,8 +53,7 @@ public class AtomPojoProviderTest extend
     
     @Test
     public void testWriteFeedWithBuilders() throws Exception {
-        @SuppressWarnings("unchecked")
-        AtomPojoProvider<Books> provider = (AtomPojoProvider<Books>)ctx.getBean("atom");
+        AtomPojoProvider provider = (AtomPojoProvider)ctx.getBean("atom");
         assertNotNull(provider);
         provider.setFormattedOutput(true);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -76,8 +76,7 @@ public class AtomPojoProviderTest extend
     
     @Test
     public void testWriteFeedWithBuildersNoJaxb() throws Exception {
-        @SuppressWarnings("unchecked")
-        AtomPojoProvider<Books> provider = (AtomPojoProvider<Books>)ctx.getBean("atomNoJaxb");
+        AtomPojoProvider provider = (AtomPojoProvider)ctx.getBean("atomNoJaxb");
         assertNotNull(provider);
         provider.setFormattedOutput(true);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -110,8 +109,7 @@ public class AtomPojoProviderTest extend
     
     @Test
     public void testWriteEntryWithBuilders() throws Exception {
-        @SuppressWarnings("unchecked")
-        AtomPojoProvider<Book> provider = (AtomPojoProvider<Book>)ctx.getBean("atom2");
+        AtomPojoProvider provider = (AtomPojoProvider)ctx.getBean("atom2");
         assertNotNull(provider);
         provider.setFormattedOutput(true);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
@@ -125,25 +123,24 @@ public class AtomPojoProviderTest extend
     
     @Test
     public void testReadEntryWithBuilders() throws Exception {
-        @SuppressWarnings("unchecked")
-        AtomPojoProvider<Book> provider = (AtomPojoProvider<Book>)ctx.getBean("atom3");
+        AtomPojoProvider provider = (AtomPojoProvider)ctx.getBean("atom3");
         assertNotNull(provider);
         doTestReadEntry(provider);
     }
     
     @Test
     public void testReadEntryWithoutBuilders() throws Exception {
-        doTestReadEntry(new AtomPojoProvider<Book>());
+        doTestReadEntry(new AtomPojoProvider());
     }
     
-    private void doTestReadEntry(AtomPojoProvider<Book> provider) throws Exception
{
+    private void doTestReadEntry(AtomPojoProvider provider) throws Exception {
         provider.setFormattedOutput(true);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         MediaType mt = MediaType.valueOf("application/atom+xml;type=entry");
         provider.writeTo(new Book("a"), Book.class, Book.class, new Annotation[]{}, mt, null,
bos);
-        //System.out.println(bos.toString());
         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
-        Book book = (Book)provider.readFrom(Book.class, Book.class, 
+        @SuppressWarnings({"unchecked", "rawtypes" })
+        Book book = (Book)provider.readFrom((Class)Book.class, Book.class, 
                                             new Annotation[]{}, mt, null, bis);
         assertEquals("a", book.getName());
     }
@@ -151,19 +148,18 @@ public class AtomPojoProviderTest extend
     
     @Test
     public void testReadFeedWithBuilders() throws Exception {
-        @SuppressWarnings("unchecked")
-        AtomPojoProvider<Books> provider = (AtomPojoProvider<Books>)ctx.getBean("atom4");
+        AtomPojoProvider provider = (AtomPojoProvider)ctx.getBean("atom4");
         assertNotNull(provider);
         doTestReadFeed(provider);
     }
     
     @Test
     public void testReadFeedWithoutBuilders() throws Exception {
-        AtomPojoProvider<Books> provider = new AtomPojoProvider<Books>();
+        AtomPojoProvider provider = new AtomPojoProvider();
         doTestReadFeed(provider);
     }
 
-    private void doTestReadFeed(AtomPojoProvider<Books> provider) throws Exception
{
+    private void doTestReadFeed(AtomPojoProvider provider) throws Exception {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         MediaType mt = MediaType.valueOf("application/atom+xml;type=feed");
         Books books = new Books();
@@ -173,7 +169,8 @@ public class AtomPojoProviderTest extend
         books.setBooks(bs);
         provider.writeTo(books, Books.class, Books.class, new Annotation[]{}, mt, null, bos);
         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
-        Books books2 = provider.readFrom(Books.class, Books.class, 
+        @SuppressWarnings({"unchecked", "rawtypes" })
+        Books books2 = (Books)provider.readFrom((Class)Books.class, Books.class, 
                                             new Annotation[]{}, mt, null, bis);
         List<Book> list = books2.getBooks();
         assertEquals(2, list.size());



Mime
View raw message