cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1387178 - 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/ rt/rs/extensions/providers/src/test/java/org/ap...
Date Tue, 18 Sep 2012 14:17:41 GMT
Author: sergeyb
Date: Tue Sep 18 14:17:40 2012
New Revision: 1387178

URL: http://svn.apache.org/viewvc?rev=1387178&view=rev
Log:
Merged revisions 1384254,1387145,1387148 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1384254 | sergeyb | 2012-09-13 10:40:02 +0100 (Thu, 13 Sep 2012) | 1 line
  
  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
........
  r1387145 | sergeyb | 2012-09-18 14:28:05 +0100 (Tue, 18 Sep 2012) | 1 line
  
  Making it simpler customize Abdera writers
........
  r1387148 | sergeyb | 2012-09-18 14:33:43 +0100 (Tue, 18 Sep 2012) | 1 line
  
  Making it simpler customize Abdera writers, actual change missed
........

Added:
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java
      - copied unchanged from r1384254, cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryAllTest.java
Removed:
    cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/json/ProviderFactoryJsonTest.java
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/AbstractAtomProvider.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

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1384254,1387145-1387148

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/AbstractAtomProvider.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/AbstractAtomProvider.java?rev=1387178&r1=1387177&r2=1387178&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractAtomProvider.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/atom/AbstractAtomProvider.java
Tue Sep 18 14:17:40 2012
@@ -24,7 +24,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.util.logging.Logger;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -34,10 +36,12 @@ import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
 import org.apache.abdera.writer.Writer;
+import org.apache.cxf.common.logging.LogUtils;
 
 public abstract class AbstractAtomProvider<T extends Element> 
     implements MessageBodyWriter<T>, MessageBodyReader<T> {
 
+    private static final Logger LOG = LogUtils.getL7dLogger(AbstractAtomProvider.class);
     private static final Abdera ATOM_ENGINE = new Abdera();
         
     private boolean formattedOutput;
@@ -50,16 +54,31 @@ public abstract class AbstractAtomProvid
                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream
os) 
         throws IOException {
         if (MediaType.APPLICATION_JSON_TYPE.isCompatible(mt)) {
-            Writer w = ATOM_ENGINE.getWriterFactory().getWriter("json");
+            Writer w = createWriter("json");
+            if (w == null) {
+                throw new WebApplicationException(415);
+            }
             element.writeTo(w, os);   
         } else if (formattedOutput) {
-            Writer w = ATOM_ENGINE.getWriterFactory().getWriter("prettyxml");
-            element.writeTo(w, os);
+            Writer w = createWriter("prettyxml");
+            if (w != null) {
+                element.writeTo(w, os);
+            } else {
+                element.writeTo(os);
+            }
         } else {
             element.writeTo(os);
         }
     }
 
+    protected Writer createWriter(String writerName) {
+        Writer w = ATOM_ENGINE.getWriterFactory().getWriter(writerName);
+        if (w == null) {
+            LOG.fine("Atom writer \"" + writerName + "\" is not available");
+        }
+        return w;
+    }
+    
     public T readFrom(Class<T> clazz, Type t, Annotation[] a, MediaType mt, 
                          MultivaluedMap<String, String> headers, InputStream is) 
         throws IOException {

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=1387178&r1=1387177&r2=1387178&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 18 14:17:40 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 = 

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=1387178&r1=1387177&r2=1387178&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 18 14:17:40 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