Author: sergeyb
Date: Mon Nov  7 18:56:04 2011
New Revision: 1198875

URL: http://svn.apache.org/viewvc?rev=1198875&view=rev
Log:
[CXF-3380] Adding DataSourceProvider

Added:
    
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
   (with props)
    
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
   (with props)

Added: 
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=1198875&view=auto
==============================================================================
--- 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
 (added)
+++ 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
 Mon Nov  7 18:56:04 2011
@@ -0,0 +1,67 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+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.helpers.IOUtils;
+import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
+
+public class DataSourceProvider implements MessageBodyReader<DataSource>, 
+    MessageBodyWriter<DataSource> {
+    
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] 
annotations, MediaType mt) {
+        return DataSource.class.isAssignableFrom(type);
+    }
+
+    public DataSource readFrom(Class<DataSource> clazz, Type genericType, 
Annotation[] annotations, 
+                               MediaType type, MultivaluedMap<String, String> 
headers, InputStream is)
+        throws IOException {
+        return new InputStreamDataSource(is, type.toString());
+    }
+
+    public long getSize(DataSource 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 void writeTo(DataSource src, Class<?> clazz, Type genericType, 
Annotation[] annotations, 
+                        MediaType type, MultivaluedMap<String, Object> 
headers, OutputStream os)
+        throws IOException {
+        IOUtils.copy(src.getInputStream(), os);
+    }
+    
+    
+
+}

Propchange: 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
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=1198875&view=auto
==============================================================================
--- 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
 (added)
+++ 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
 Mon Nov  7 18:56:04 2011
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.lang.annotation.Annotation;
+
+import javax.activation.DataSource;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DataSourceProviderTest extends Assert {
+
+    @Test
+    public void testReadFrom() throws Exception {
+        DataSourceProvider p = new DataSourceProvider();
+        DataSource ds = p.readFrom(DataSource.class, DataSource.class, new 
Annotation[]{}, 
+                   MediaType.valueOf("image/png"), new MetadataMap<String, 
String>(), 
+                   new ByteArrayInputStream("image".getBytes()));
+        
+        assertEquals("image", 
IOUtils.readStringFromStream(ds.getInputStream()));
+        
+    }
+    
+    @Test
+    public void testWriteFrom() throws Exception {
+        DataSourceProvider p = new DataSourceProvider();
+        DataSource ds = new InputStreamDataSource(new 
ByteArrayInputStream("image".getBytes()), 
+                                                  "image/png"); 
+        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
+        p.writeTo(ds, DataSource.class, DataSource.class, new Annotation[]{}, 
+                   MediaType.valueOf("image/png"), new MetadataMap<String, 
Object>(), os);
+        assertEquals("image", os.toString());
+        
+    }
+    
+    
+}

Propchange: 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to