Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1369D7FF2 for ; Mon, 7 Nov 2011 18:56:26 +0000 (UTC) Received: (qmail 78017 invoked by uid 500); 7 Nov 2011 18:56:26 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 77909 invoked by uid 500); 7 Nov 2011 18:56:25 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 77902 invoked by uid 99); 7 Nov 2011 18:56:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Nov 2011 18:56:25 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Nov 2011 18:56:24 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7A86523889D7 for ; Mon, 7 Nov 2011 18:56:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1198875 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java test/java/org/apache/cxf/jaxrs/provider/DataSourceProviderTest.java Date: Mon, 07 Nov 2011 18:56:04 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111107185604.7A86523889D7@eris.apache.org> 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, + MessageBodyWriter { + + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mt) { + return DataSource.class.isAssignableFrom(type); + } + + public DataSource readFrom(Class clazz, Type genericType, Annotation[] annotations, + MediaType type, MultivaluedMap 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 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(), + 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(), 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