camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/7] git commit: CAMEL-6446: camel-jackson support using JAXB annotations. Thanks to Chris Geer for the patch.
Date Tue, 25 Jun 2013 09:08:55 GMT
CAMEL-6446: camel-jackson support using JAXB annotations. Thanks to Chris Geer for the patch.


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

Branch: refs/heads/master
Commit: 55c66af89f4e711decd570fbc4ebd36169871806
Parents: afcecf7
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Jun 25 11:05:14 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Jun 25 11:05:14 2013 +0200

----------------------------------------------------------------------
 components/camel-jackson/pom.xml                |  5 ++
 .../component/jackson/JacksonDataFormat.java    | 13 +++-
 .../jackson/JacksonJAXBAnnotationTest.java      | 65 ++++++++++++++++++++
 .../camel/component/jackson/TestJAXBPojo.java   | 50 +++++++++++++++
 .../features/src/main/resources/features.xml    |  1 +
 5 files changed, 131 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/55c66af8/components/camel-jackson/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-jackson/pom.xml b/components/camel-jackson/pom.xml
index f6209b5..431849a 100644
--- a/components/camel-jackson/pom.xml
+++ b/components/camel-jackson/pom.xml
@@ -45,6 +45,11 @@
           <artifactId>jackson-databind</artifactId>
           <version>${jackson2-version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.module</groupId>
+            <artifactId>jackson-module-jaxb-annotations</artifactId>
+            <version>${jackson2-version}</version>
+        </dependency>
 
         <!-- testing -->
         <dependency>

http://git-wip-us.apache.org/repos/asf/camel/blob/55c66af8/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
index 2b8246f..231adf7 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
@@ -41,7 +42,7 @@ public class JacksonDataFormat implements DataFormat {
      * Use the default Jackson {@link ObjectMapper} and {@link Map}
      */
     public JacksonDataFormat() {
-        this(new ObjectMapper(), HashMap.class);
+        this(HashMap.class);
     }
 
     /**
@@ -51,7 +52,7 @@ public class JacksonDataFormat implements DataFormat {
      * @param unmarshalType the custom unmarshal type
      */
     public JacksonDataFormat(Class<?> unmarshalType) {
-        this(new ObjectMapper(), unmarshalType);
+        this(unmarshalType, null);
     }
 
     /**
@@ -63,7 +64,13 @@ public class JacksonDataFormat implements DataFormat {
      *                 See also http://wiki.fasterxml.com/JacksonJsonViews
      */
     public JacksonDataFormat(Class<?> unmarshalType, Class<?> jsonView) {
-        this(new ObjectMapper(), unmarshalType, jsonView);
+        this.objectMapper = new ObjectMapper();
+        this.unmarshalType = unmarshalType;
+        this.jsonView = jsonView;
+
+        // Enables JAXB processing
+        JaxbAnnotationModule module = new JaxbAnnotationModule();
+        this.objectMapper.registerModule(module);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/55c66af8/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java
new file mode 100644
index 0000000..91afad8
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java
@@ -0,0 +1,65 @@
+/**
+ * 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.jackson;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JacksonJAXBAnnotationTest extends CamelTestSupport {
+
+    @Test
+    public void testMarshalJAXBObject() throws Exception {
+        TestJAXBPojo in = new TestJAXBPojo();
+        in.setName("Camel");
+
+        MockEndpoint mock = getMockEndpoint("mock:reversePojo");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(TestJAXBPojo.class);
+        mock.message(0).body().equals(in);
+
+        Object marshalled = template.requestBody("direct:inPojo", in);
+        String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);
+        assertEquals("{\"PojoName\":\"Camel\"}", marshalledAsString);
+
+        template.sendBody("direct:backPojo", marshalled);
+
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+
+            @Override
+            public void configure() throws Exception {
+
+                JacksonDataFormat format = new JacksonDataFormat();
+
+                from("direct:in").marshal(format);
+                from("direct:back").unmarshal(format).to("mock:reverse");
+
+                JacksonDataFormat formatPojo = new JacksonDataFormat(TestJAXBPojo.class);
+
+                from("direct:inPojo").marshal(formatPojo);
+                from("direct:backPojo").unmarshal(formatPojo).to("mock:reversePojo");
+
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/55c66af8/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java
new file mode 100644
index 0000000..20cee37
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java
@@ -0,0 +1,50 @@
+/**
+ * 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.jackson;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "XMLPojo")
+public class TestJAXBPojo {
+
+    @XmlElement(name = "PojoName")
+    private String name;
+
+    public String getName() {
+        return this.name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return this.name.equals(((TestPojo) obj).getName());
+    }
+
+    @Override
+    public int hashCode() {
+        return name != null ? name.hashCode() : 0;
+    }
+
+    @Override
+    public String toString() {
+        return "TestJAXBPojo[" + name + "]";
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/55c66af8/platforms/karaf/features/src/main/resources/features.xml
----------------------------------------------------------------------
diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml
index cf30660..f15027e 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -476,6 +476,7 @@
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-core/${jackson2-version}</bundle>
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-databind/${jackson2-version}</bundle>
     <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-annotations/${jackson2-version}</bundle>
+    <bundle dependency='true'>mvn:com.fasterxml.jackson.module/jackson-module-jaxb-annotations/${jackson2-version}</bundle>
     <feature version='${project.version}'>camel-core</feature>
     <bundle>mvn:org.apache.camel/camel-jackson/${project.version}</bundle>
   </feature>


Mime
View raw message