You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/02/26 03:47:44 UTC

[1/2] git commit: CAMEL-7207 Removed the unused import in CxfRsSpringEndpointTest

Repository: camel
Updated Branches:
  refs/heads/master 18c23fa82 -> a6c4057a7


CAMEL-7207 Removed the unused import in CxfRsSpringEndpointTest


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b2b9626d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b2b9626d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b2b9626d

Branch: refs/heads/master
Commit: b2b9626d524da1df6a063ac2dbe982ec9f0a056e
Parents: 18c23fa
Author: Willem Jiang <wi...@gmail.com>
Authored: Wed Feb 26 10:46:29 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Feb 26 10:46:29 2014 +0800

----------------------------------------------------------------------
 .../apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java  | 2 --
 1 file changed, 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b2b9626d/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
index d80441e..a84fc20 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsSpringEndpointTest.java
@@ -19,12 +19,10 @@ package org.apache.camel.component.cxf.jaxrs;
 import java.util.Map;
 
 import org.apache.camel.component.cxf.jaxrs.testbean.CustomerService;
-import org.apache.camel.component.cxf.spring.AbstractCxfBeanDefinitionParser;
 import org.apache.camel.component.cxf.spring.SpringJAXRSClientFactoryBean;
 import org.apache.camel.component.cxf.spring.SpringJAXRSServerFactoryBean;
 import org.apache.camel.test.spring.CamelSpringTestSupport;
 import org.apache.cxf.version.Version;
-
 import org.junit.Test;
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;


[2/2] git commit: CAMEL-7178 Create JAX-RS MessageBodyWriter and Reader which will work with Camel data format with thanks to Sergey

Posted by ni...@apache.org.
CAMEL-7178 Create JAX-RS MessageBodyWriter and Reader which will work with Camel data format with thanks to Sergey


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a6c4057a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a6c4057a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a6c4057a

Branch: refs/heads/master
Commit: a6c4057a7ba3d8d2e2024081a75646a42730dc52
Parents: b2b9626
Author: Willem Jiang <wi...@gmail.com>
Authored: Wed Feb 26 10:47:34 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Feb 26 10:47:34 2014 +0800

----------------------------------------------------------------------
 .../component/cxf/jaxrs/DataFormatProvider.java | 121 +++++++++++++++++
 .../cxf/jaxrs/DataFormatProviderTest.java       | 130 +++++++++++++++++++
 2 files changed, 251 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a6c4057a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DataFormatProvider.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DataFormatProvider.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DataFormatProvider.java
new file mode 100644
index 0000000..73ef59a
--- /dev/null
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DataFormatProvider.java
@@ -0,0 +1,121 @@
+/**
+ * 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.camel.component.cxf.jaxrs;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.WebApplicationException;
+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.camel.spi.DataFormat;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+
+public class DataFormatProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> {
+
+    private Map<String, DataFormat> formats = new HashMap<String, DataFormat>();
+
+    @Override
+    public boolean isReadable(Class<?> cls, Type type, Annotation[] anns, MediaType mt) {
+        return getDataFormat(mt) != null;
+    }
+
+    @Override
+    public boolean isWriteable(Class<?> cls, Type type, Annotation[] anns, MediaType mt) {
+        return getDataFormat(mt) != null;
+    }
+
+    @Override
+    public T readFrom(Class<T> cls, Type type, Annotation[] anns, MediaType mt,
+                      MultivaluedMap<String, String> headers, InputStream is) throws IOException,
+        WebApplicationException {
+        DataFormat format = getValidDataFormat(mt);
+        try {
+            @SuppressWarnings("unchecked")
+            T result = (T)format.unmarshal(null, is);
+            return result;
+        } catch (Exception ex) {
+            throw new BadRequestException(ex);
+        }
+    }
+
+    @Override
+    public long getSize(T obj, Class<?> cls, Type type, Annotation[] anns, MediaType mt) {
+        return -1;
+    }
+
+    @Override
+    public void writeTo(T obj, Class<?> cls, Type type, Annotation[] anns, MediaType mt,
+                        MultivaluedMap<String, Object> headers, OutputStream os) throws IOException,
+        WebApplicationException {
+        DataFormat format = getValidDataFormat(mt);
+        try {
+            format.marshal(null, obj, os);
+        } catch (Exception ex) {
+            throw new InternalServerErrorException(ex);
+        }
+
+    }
+
+    public void setFormat(DataFormat format) {
+        setFormat(MediaType.WILDCARD, format);
+    }
+
+    public void setFormat(String mediaType, DataFormat format) {
+        formats.put(mediaType, format);
+    }
+
+    public void setFormats(Map<String, DataFormat> formats) {
+        this.formats.putAll(formats);
+    }
+
+    private DataFormat getValidDataFormat(MediaType mt) {
+        DataFormat format = getDataFormat(mt);
+        if (format == null) {
+            throw new InternalServerErrorException();
+        }
+        return format;
+    }
+
+    private DataFormat getDataFormat(MediaType mt) {
+        String type = JAXRSUtils.mediaTypeToString(mt);
+        DataFormat format = formats.get(type);
+        if (format != null) {
+            return format;
+        }
+        int subtypeIndex = type.lastIndexOf('+');
+        if (subtypeIndex != -1) {
+            // example, application/json+v1, should still be handled by JSON
+            // handler, etc
+            format = formats.get(type.substring(0, subtypeIndex));
+        }
+        if (format == null && formats.containsKey(MediaType.WILDCARD)) {
+            format = formats.get(MediaType.WILDCARD);
+        }
+        return format;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a6c4057a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DataFormatProviderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DataFormatProviderTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DataFormatProviderTest.java
new file mode 100644
index 0000000..b369448
--- /dev/null
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DataFormatProviderTest.java
@@ -0,0 +1,130 @@
+/**
+ * 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.camel.component.cxf.jaxrs;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.DataFormat;
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DataFormatProviderTest extends Assert {
+    
+    @Test
+    public void testIsReadableWriteableSpecificMatch() {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat("text/plain", new TestDataFormat());
+        assertTrue(p.isReadable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+        assertTrue(p.isWriteable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+    }
+
+    @Test
+    public void testIsReadableWriteableComplexSubMatch() {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat("text/plain", new TestDataFormat());
+        assertTrue(p.isReadable(Book.class, Book.class, new Annotation[] {},
+                                MediaType.valueOf("text/plain+v2")));
+        assertTrue(p.isWriteable(Book.class, Book.class, new Annotation[] {},
+                                 MediaType.valueOf("text/plain+v2")));
+    }
+
+    @Test
+    public void testIsReadableWriteableStarMatch() {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat(new TestDataFormat());
+        assertTrue(p.isReadable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+        assertTrue(p.isWriteable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+    }
+
+    @Test
+    public void testNotReadableWriteable() {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat("application/json", new TestDataFormat());
+        assertFalse(p.isReadable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+        assertFalse(p.isWriteable(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE));
+    }
+
+    @Test
+    public void testReadFrom() throws Exception {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat("text/plain", new TestDataFormat());
+
+        ByteArrayInputStream bis = new ByteArrayInputStream("dataformat".getBytes());
+        Book b = p.readFrom(Book.class, Book.class, new Annotation[] {}, MediaType.TEXT_PLAIN_TYPE,
+                            new MetadataMap<String, String>(), bis);
+        assertEquals("dataformat", b.getName());
+    }
+
+    @Test
+    public void testWriteTo() throws Exception {
+        DataFormatProvider<Book> p = new DataFormatProvider<Book>();
+        p.setFormat("text/plain", new TestDataFormat());
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        p.writeTo(new Book("dataformat"), Book.class, Book.class, new Annotation[] {},
+                  MediaType.TEXT_PLAIN_TYPE, new MetadataMap<String, Object>(), bos);
+        assertEquals("dataformat", bos.toString());
+    }
+
+    private static class Book {
+        private String name;
+
+        @SuppressWarnings("unused")
+        public Book() {
+
+        }
+
+        public Book(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        @SuppressWarnings("unused")
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    private static class TestDataFormat implements DataFormat {
+
+        @Override
+        public void marshal(Exchange ex, Object obj, OutputStream os) throws Exception {
+            os.write(((Book)obj).getName().getBytes());
+            os.flush();
+        }
+
+        @Override
+        public Object unmarshal(Exchange ex, InputStream is) throws Exception {
+            return new Book(IOUtils.readStringFromStream(is));
+        }
+
+    }
+
+}