You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2011/11/09 19:24:29 UTC

svn commit: r1199905 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/provider/

Author: sergeyb
Date: Wed Nov  9 18:24:28 2011
New Revision: 1199905

URL: http://svn.apache.org/viewvc?rev=1199905&view=rev
Log:
[CXF-3380] Collapsing DataHandlerProvider into DataSourceProvider to minimize duplication

Removed:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java?rev=1199905&r1=1199904&r2=1199905&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java Wed Nov  9 18:24:28 2011
@@ -25,43 +25,59 @@ import java.io.OutputStream;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
 
-public class DataSourceProvider implements MessageBodyReader<DataSource>, 
-    MessageBodyWriter<DataSource> {
+public class DataSourceProvider implements MessageBodyReader, MessageBodyWriter {
     
-    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-        return DataSource.class.isAssignableFrom(type);
+    public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mt) {
+        return isSupported(type, mt);
     }
 
-    public DataSource readFrom(Class<DataSource> clazz, Type genericType, Annotation[] annotations, 
-                               MediaType type, MultivaluedMap<String, String> headers, InputStream is)
+    public Object readFrom(Class cls, Type genericType, Annotation[] annotations, 
+                               MediaType type, MultivaluedMap headers, InputStream is)
         throws IOException {
-        return new InputStreamDataSource(is, type.toString());
+        DataSource ds = new InputStreamDataSource(is, type.toString());
+        return DataSource.class.isAssignableFrom(cls) ? ds : new DataHandler(ds);
     }
 
-    public long getSize(DataSource t, Class<?> type, Type genericType, Annotation[] annotations, 
+    public long getSize(Object t, Class type, Type genericType, Annotation[] annotations, 
                         MediaType mt) {
         return -1;
     }
 
-    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mt) {
-        return DataSource.class.isAssignableFrom(type);
+    public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mt) {
+        return isSupported(type, mt);
     }
 
-    public void writeTo(DataSource src, Class<?> clazz, Type genericType, Annotation[] annotations, 
-                        MediaType type, MultivaluedMap<String, Object> headers, OutputStream os)
+    private boolean isSupported(Class<?> type, MediaType mt) {
+        return  !mt.getType().equals("multipart")
+            && (DataSource.class.isAssignableFrom(type) || DataHandler.class.isAssignableFrom(type));
+    }
+    
+    public void writeTo(Object src, Class cls, Type genericType, Annotation[] annotations, 
+                        MediaType type, MultivaluedMap headers, OutputStream os)
         throws IOException {
-        IOUtils.copy(src.getInputStream(), os);
+        DataSource ds = DataSource.class.isAssignableFrom(cls) 
+            ? (DataSource)src : ((DataHandler)src).getDataSource();
+        setContentTypeIfNeeded(type, headers, ds.getContentType());
+        IOUtils.copy(ds.getInputStream(), os);
     }
     
+    @SuppressWarnings("unchecked")
+    private void setContentTypeIfNeeded(MediaType type, MultivaluedMap headers, String ct) {
+        if (!StringUtils.isEmpty(ct) && !type.equals(MediaType.valueOf(ct))) { 
+            headers.putSingle("Content-Type", ct);
+        }
+    }
     
 
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java?rev=1199905&r1=1199904&r2=1199905&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java Wed Nov  9 18:24:28 2011
@@ -22,8 +22,10 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.lang.annotation.Annotation;
 
+import javax.activation.DataHandler;
 import javax.activation.DataSource;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
@@ -35,9 +37,33 @@ import org.junit.Test;
 public class DataSourceProviderTest extends Assert {
 
     @Test
-    public void testReadFrom() throws Exception {
+    public void testReadDataHandler() throws Exception {
         DataSourceProvider p = new DataSourceProvider();
-        DataSource ds = p.readFrom(DataSource.class, DataSource.class, new Annotation[]{}, 
+        DataHandler ds = (DataHandler)p.readFrom(DataHandler.class, null, new Annotation[]{}, 
+                   MediaType.valueOf("image/png"), new MetadataMap<String, String>(), 
+                   new ByteArrayInputStream("image".getBytes()));
+        
+        assertEquals("image", IOUtils.readStringFromStream(ds.getDataSource().getInputStream()));
+        
+    }
+    
+    @Test
+    public void testWriteDataHandler() throws Exception {
+        DataSourceProvider p = new DataSourceProvider();
+        DataHandler ds = new DataHandler(new InputStreamDataSource(
+                             new ByteArrayInputStream("image".getBytes()), 
+                             "image/png")); 
+        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        p.writeTo(ds, DataHandler.class, DataHandler.class, new Annotation[]{}, 
+                   MediaType.valueOf("image/png"), new MetadataMap<String, Object>(), os);
+        assertEquals("image", os.toString());
+        
+    }
+    
+    @Test
+    public void testReadDataSource() throws Exception {
+        DataSourceProvider p = new DataSourceProvider();
+        DataSource ds = (DataSource)p.readFrom(DataSource.class, null, new Annotation[]{}, 
                    MediaType.valueOf("image/png"), new MetadataMap<String, String>(), 
                    new ByteArrayInputStream("image".getBytes()));
         
@@ -46,15 +72,30 @@ public class DataSourceProviderTest exte
     }
     
     @Test
-    public void testWriteFrom() throws Exception {
+    public void testWriteDataSource() throws Exception {
         DataSourceProvider p = new DataSourceProvider();
         DataSource ds = new InputStreamDataSource(new ByteArrayInputStream("image".getBytes()), 
                                                   "image/png"); 
         ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        MultivaluedMap<String, Object> outHeaders = new MetadataMap<String, Object>();
+        
         p.writeTo(ds, DataSource.class, DataSource.class, new Annotation[]{}, 
-                   MediaType.valueOf("image/png"), new MetadataMap<String, Object>(), os);
+                   MediaType.valueOf("image/png"), outHeaders, os);
         assertEquals("image", os.toString());
-        
+        assertEquals(0, outHeaders.size());
+    }
+    
+    @Test
+    public void testWriteDataSourceWithDiffCT() throws Exception {
+        DataSourceProvider p = new DataSourceProvider();
+        DataSource ds = new InputStreamDataSource(new ByteArrayInputStream("image".getBytes()), 
+                                                  "image/png"); 
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        MultivaluedMap<String, Object> outHeaders = new MetadataMap<String, Object>();
+        p.writeTo(ds, DataSource.class, DataSource.class, new Annotation[]{}, 
+                   MediaType.valueOf("image/bar"), outHeaders, os);
+        assertEquals("image", os.toString());
+        assertEquals("image/png", outHeaders.getFirst("Content-Type"));
     }
     
     

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java?rev=1199905&r1=1199904&r2=1199905&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java Wed Nov  9 18:24:28 2011
@@ -34,6 +34,8 @@ import java.util.concurrent.CountDownLat
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
@@ -265,6 +267,45 @@ public class ProviderFactoryTest extends
     }
     
     @Test
+    public void testDataSourceReader() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        pf.registerUserProvider(new DataSourceProvider());
+        MessageBodyReader reader = pf.createMessageBodyReader(
+              (Class<?>)DataSource.class, null, null, 
+              MediaType.valueOf("image/png"), new MessageImpl());
+        assertTrue(reader instanceof DataSourceProvider);
+        MessageBodyReader reader2 = pf.createMessageBodyReader(
+                          (Class<?>)DataHandler.class, null, null, 
+                          MediaType.valueOf("image/png"), new MessageImpl());
+        assertSame(reader, reader2);
+    }
+    
+    @Test
+    public void testDataSourceWriter() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        pf.registerUserProvider(new DataSourceProvider());
+        MessageBodyWriter writer = pf.createMessageBodyWriter(
+              (Class<?>)DataSource.class, null, null, 
+              MediaType.valueOf("image/png"), new MessageImpl());
+        assertTrue(writer instanceof DataSourceProvider);
+        MessageBodyWriter writer2 = pf.createMessageBodyWriter(
+                          (Class<?>)DataHandler.class, null, null, 
+                          MediaType.valueOf("image/png"), new MessageImpl());
+        assertSame(writer, writer2);
+    }
+    
+    @Test
+    public void testNoDataSourceWriter() {
+        ProviderFactory pf = ProviderFactory.getInstance();
+        pf.registerUserProvider(new DataSourceProvider());
+        MessageBodyWriter writer = pf.createMessageBodyWriter(
+              (Class<?>)DataSource.class, null, null, 
+              MediaType.valueOf("multipart/form-data"), new MessageImpl());
+        assertFalse(writer instanceof DataSourceProvider);
+    }
+    
+    
+    @Test
     public void testSchemaLocations() {
         ProviderFactory pf = ProviderFactory.getInstance();
         pf.setSchemaLocations(Collections.singletonList("classpath:/test.xsd"));