Author: sergeyb
Date: Mon Nov 7 19:23:51 2011
New Revision: 1198884
URL: http://svn.apache.org/viewvc?rev=1198884&view=rev
Log:
[CXF-3380] Adding DataHandlerProvider too
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
(with props)
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
(with props)
Added:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java?rev=1198884&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
Mon Nov 7 19:23:51 2011
@@ -0,0 +1,71 @@
+/**
+ * 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.DataHandler;
+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 DataHandlerProvider implements MessageBodyReader<DataHandler>,
+ MessageBodyWriter<DataHandler> {
+
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mt) {
+ return isSupported(type);
+ }
+
+ public DataHandler readFrom(Class<DataHandler> clazz, Type genericType,
Annotation[] annotations,
+ MediaType type, MultivaluedMap<String, String>
headers, InputStream is)
+ throws IOException {
+ return new DataHandler(new InputStreamDataSource(is, type.toString()));
+ }
+ public boolean isWriteable(Class<?> type, Type genericType, Annotation[]
annotations, MediaType mt) {
+ return isSupported(type);
+ }
+
+
+ public long getSize(DataHandler t, Class<?> type, Type genericType,
Annotation[] annotations,
+ MediaType mt) {
+ return -1;
+ }
+
+ private boolean isSupported(Class<?> type) {
+ return DataHandler.class.isAssignableFrom(type);
+ }
+
+ public void writeTo(DataHandler 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/DataHandlerProvider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataHandlerProvider.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java?rev=1198884&view=auto
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
(added)
+++
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
Mon Nov 7 19:23:51 2011
@@ -0,0 +1,62 @@
+/**
+ * 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.DataHandler;
+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 DataHandlerProviderTest extends Assert {
+
+ @Test
+ public void testReadFrom() throws Exception {
+ DataHandlerProvider p = new DataHandlerProvider();
+ DataHandler ds = p.readFrom(DataHandler.class, DataHandler.class, new
Annotation[]{},
+ MediaType.valueOf("image/png"), new MetadataMap<String,
String>(),
+ new ByteArrayInputStream("image".getBytes()));
+
+ assertEquals("image",
IOUtils.readStringFromStream(ds.getDataSource().getInputStream()));
+
+ }
+
+ @Test
+ public void testWriteFrom() throws Exception {
+ DataHandlerProvider p = new DataHandlerProvider();
+ 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());
+
+ }
+
+
+}
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/DataHandlerProviderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date