CAMEL-9189: Jackson XML Mapper Data format
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0936806d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0936806d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0936806d Branch: refs/heads/master Commit: 0936806dca8d5cb37e9c84e44de8e00b7b274384 Parents: 5fc96ef Author: Antoine Toulme <anto...@lunar-ocean.com> Authored: Sun Oct 4 02:03:51 2015 -0700 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Oct 5 12:23:26 2015 +0200 ---------------------------------------------------------------------- .../src/main/descriptors/common-bin.xml | 1 + .../apache/camel/builder/DataFormatClause.java | 120 +++++ .../apache/camel/model/MarshalDefinition.java | 2 + .../apache/camel/model/UnmarshalDefinition.java | 2 + .../model/dataformat/DataFormatsDefinition.java | 1 + .../model/dataformat/JacksonXMLDataFormat.java | 313 ++++++++++++ components/camel-jacksonxml/pom.xml | 81 +++ .../jacksonxml/JacksonXMLConstants.java | 29 ++ .../jacksonxml/JacksonXMLDataFormat.java | 503 +++++++++++++++++++ .../jacksonxml/ListJacksonXMLDataFormat.java | 55 ++ .../converter/JacksonXMLTypeConverters.java | 88 ++++ .../src/main/resources/META-INF/LICENSE.txt | 203 ++++++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + .../services/org/apache/camel/TypeConverter | 17 + .../org/apache/camel/dataformat/jacksonxml | 18 + .../jacksonxml/JacksonBindingTest.java | 64 +++ .../jacksonxml/JacksonConcurrentTest.java | 80 +++ .../JacksonConversionsSimpleTest.java | 70 +++ .../jacksonxml/JacksonConversionsTest.java | 59 +++ .../jacksonxml/JacksonIncludeDefaultTest.java | 53 ++ .../jacksonxml/JacksonIncludeNotNulllTest.java | 54 ++ .../jacksonxml/JacksonJAXBAnnotationTest.java | 65 +++ .../jacksonxml/JacksonJsonDataFormatTest.java | 41 ++ .../JacksonMarshalAllowJMSTypeTest.java | 56 +++ .../jacksonxml/JacksonMarshalTest.java | 112 +++++ .../JacksonMarshalUnmarshalListTest.java | 83 +++ .../JacksonMarshalUnmarshalTypeHeaderTest.java | 57 +++ .../jacksonxml/JacksonMarshalViewTest.java | 83 +++ .../jacksonxml/JacksonModuleRefTest.java | 46 ++ .../component/jacksonxml/JacksonModuleTest.java | 56 +++ .../jacksonxml/JacksonNameBindingTest.java | 69 +++ .../jacksonxml/JacksonObjectListSplitTest.java | 63 +++ .../jacksonxml/ListJacksonUnmarshalDTest.java | 34 ++ .../camel/component/jacksonxml/MyModule.java | 45 ++ .../SpringJacksonEnableFeatureTest.java | 44 ++ .../SpringJacksonJsonDataFormatTest.java | 114 +++++ .../SpringJacksonMarshalUnmarshalListTest.java | 77 +++ .../component/jacksonxml/TestJAXBPojo.java | 50 ++ .../component/jacksonxml/TestOtherPojo.java | 40 ++ .../camel/component/jacksonxml/TestPojo.java | 45 ++ .../component/jacksonxml/TestPojoView.java | 56 +++ .../camel/component/jacksonxml/Views.java | 25 + .../src/test/resources/log4j.properties | 36 ++ .../jackson/SpringJacksonEnableFeatureTest.xml | 39 ++ .../jackson/SpringJacksonJsonDataFormatTest.xml | 80 +++ .../SpringJacksonMarshalUnmarshalListTest.xml | 43 ++ components/pom.xml | 1 + parent/pom.xml | 5 + .../features/src/main/resources/features.xml | 8 + 49 files changed, 3297 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/apache-camel/src/main/descriptors/common-bin.xml ---------------------------------------------------------------------- diff --git a/apache-camel/src/main/descriptors/common-bin.xml b/apache-camel/src/main/descriptors/common-bin.xml index 857a801..3b7aa4b 100644 --- a/apache-camel/src/main/descriptors/common-bin.xml +++ b/apache-camel/src/main/descriptors/common-bin.xml @@ -102,6 +102,7 @@ <include>org.apache.camel:camel-infinispan</include> <include>org.apache.camel:camel-irc</include> <include>org.apache.camel:camel-jackson</include> + <include>org.apache.camel:camel-jacksonxml</include> <include>org.apache.camel:camel-jasypt</include> <include>org.apache.camel:camel-javaspace</include> <include>org.apache.camel:camel-jaxb</include> http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java index a82a6c9..e87195c 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java +++ b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java @@ -36,6 +36,7 @@ import org.apache.camel.model.dataformat.CustomDataFormat; import org.apache.camel.model.dataformat.GzipDataFormat; import org.apache.camel.model.dataformat.HL7DataFormat; import org.apache.camel.model.dataformat.IcalDataFormat; +import org.apache.camel.model.dataformat.JacksonXMLDataFormat; import org.apache.camel.model.dataformat.JaxbDataFormat; import org.apache.camel.model.dataformat.JibxDataFormat; import org.apache.camel.model.dataformat.JsonDataFormat; @@ -302,6 +303,125 @@ public class DataFormatClause<T extends ProcessorDefinition<?>> { pgp.setIntegrity(integrity); return dataFormat(pgp); } + + /** + * Uses the Jackson XML data format + */ + public T jacksonxml() { + return dataFormat(new JacksonXMLDataFormat()); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + */ + public T jacksonxml(Class<?> unmarshalType) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + * @param jsonView + * the view type for xml jackson type + */ + public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + jacksonXMLDataFormat.setJsonView(jsonView); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format using the Jackson library turning pretty + * printing on or off + * + * @param prettyPrint + * turn pretty printing on or off + */ + public T jacksonxml(boolean prettyPrint) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setPrettyPrint(prettyPrint); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + * @param prettyPrint + * turn pretty printing on or off + */ + public T jacksonxml(Class<?> unmarshalType, boolean prettyPrint) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + jacksonXMLDataFormat.setPrettyPrint(prettyPrint); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + * @param jsonView + * the view type for xml jackson type + * @param prettyPrint + * turn pretty printing on or off + */ + public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, boolean prettyPrint) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + jacksonXMLDataFormat.setJsonView(jsonView); + jacksonXMLDataFormat.setPrettyPrint(prettyPrint); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + * @param jsonView + * the view type for xml jackson type + * @param include + * include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. + */ + public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + jacksonXMLDataFormat.setJsonView(jsonView); + jacksonXMLDataFormat.setInclude(include); + return dataFormat(jacksonXMLDataFormat); + } + + /** + * Uses the Jackson XML data format + * + * @param unmarshalType + * unmarshal type for xml jackson type + * @param jsonView + * the view type for xml jackson type + * @param include + * include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. + * @param prettyPrint + * turn pretty printing on or off + */ + public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include, boolean prettyPrint) { + JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); + jacksonXMLDataFormat.setUnmarshalType(unmarshalType); + jacksonXMLDataFormat.setJsonView(jsonView); + jacksonXMLDataFormat.setInclude(include); + jacksonXMLDataFormat.setPrettyPrint(prettyPrint); + return dataFormat(jacksonXMLDataFormat); + } /** * Uses the JAXB data format http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java index 4453ef5..706f6e3 100644 --- a/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java @@ -37,6 +37,7 @@ import org.apache.camel.model.dataformat.FlatpackDataFormat; import org.apache.camel.model.dataformat.GzipDataFormat; import org.apache.camel.model.dataformat.HL7DataFormat; import org.apache.camel.model.dataformat.IcalDataFormat; +import org.apache.camel.model.dataformat.JacksonXMLDataFormat; import org.apache.camel.model.dataformat.JaxbDataFormat; import org.apache.camel.model.dataformat.JibxDataFormat; import org.apache.camel.model.dataformat.JsonDataFormat; @@ -90,6 +91,7 @@ public class MarshalDefinition extends NoOutputDefinition<MarshalDefinition> { @XmlElement(required = false, name = "gzip", type = GzipDataFormat.class), @XmlElement(required = false, name = "hl7", type = HL7DataFormat.class), @XmlElement(required = false, name = "ical", type = IcalDataFormat.class), + @XmlElement(required = false, name = "jacksonxml", type = JacksonXMLDataFormat.class), @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class), @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class), @XmlElement(required = false, name = "json", type = JsonDataFormat.class), http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java index 1914a49..9b95f23 100644 --- a/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java @@ -37,6 +37,7 @@ import org.apache.camel.model.dataformat.FlatpackDataFormat; import org.apache.camel.model.dataformat.GzipDataFormat; import org.apache.camel.model.dataformat.HL7DataFormat; import org.apache.camel.model.dataformat.IcalDataFormat; +import org.apache.camel.model.dataformat.JacksonXMLDataFormat; import org.apache.camel.model.dataformat.JaxbDataFormat; import org.apache.camel.model.dataformat.JibxDataFormat; import org.apache.camel.model.dataformat.JsonDataFormat; @@ -90,6 +91,7 @@ public class UnmarshalDefinition extends NoOutputDefinition<UnmarshalDefinition> @XmlElement(required = false, name = "gzip", type = GzipDataFormat.class), @XmlElement(required = false, name = "hl7", type = HL7DataFormat.class), @XmlElement(required = false, name = "ical", type = IcalDataFormat.class), + @XmlElement(required = false, name = "jacksonxml", type = JacksonXMLDataFormat.class), @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class), @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class), @XmlElement(required = false, name = "json", type = JsonDataFormat.class), http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java index f04ce4b..49d079a 100644 --- a/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java @@ -52,6 +52,7 @@ public class DataFormatsDefinition { @XmlElement(required = false, name = "gzip", type = GzipDataFormat.class), @XmlElement(required = false, name = "hl7", type = HL7DataFormat.class), @XmlElement(required = false, name = "ical", type = IcalDataFormat.class), + @XmlElement(required = false, name = "jacksonxml", type = JacksonXMLDataFormat.class), @XmlElement(required = false, name = "jaxb", type = JaxbDataFormat.class), @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class), @XmlElement(required = false, name = "json", type = JsonDataFormat.class), http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java new file mode 100644 index 0000000..4201dc2 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/JacksonXMLDataFormat.java @@ -0,0 +1,313 @@ +/** + * 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.model.dataformat; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; + +import org.apache.camel.CamelContext; +import org.apache.camel.model.DataFormatDefinition; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RouteContext; +import org.apache.camel.util.ObjectHelper; + +/** + * Jackson XML data format + * + * @version + */ +@Metadata(label = "dataformat,transformation,jacksonxml", title = "JacksonXML") +@XmlRootElement(name = "jacksonxml") +@XmlAccessorType(XmlAccessType.FIELD) +public class JacksonXMLDataFormat extends DataFormatDefinition { + @XmlAttribute + private Boolean prettyPrint; + @XmlAttribute + private String unmarshalTypeName; + @XmlTransient + private Class<?> unmarshalType; + @XmlAttribute + private Class<?> jsonView; + @XmlAttribute + private String include; + @XmlAttribute + private Boolean allowJmsType; + @XmlAttribute + private String collectionTypeName; + @XmlTransient + private Class<?> collectionType; + @XmlAttribute + private Boolean useList; + @XmlAttribute + private Boolean enableJaxbAnnotationModule; + @XmlAttribute + private String moduleClassNames; + @XmlAttribute + private String moduleRefs; + @XmlAttribute + private String enableFeatures; + @XmlAttribute + private String disableFeatures; + + public JacksonXMLDataFormat() { + super("jacksonxml"); + } + + public Boolean getPrettyPrint() { + return prettyPrint; + } + + /** + * To enable pretty printing output nicely formatted. + * <p/> + * Is by default false. + */ + public void setPrettyPrint(Boolean prettyPrint) { + this.prettyPrint = prettyPrint; + } + + public String getUnmarshalTypeName() { + return unmarshalTypeName; + } + + /** + * Class name of the java type to use when unarmshalling + */ + public void setUnmarshalTypeName(String unmarshalTypeName) { + this.unmarshalTypeName = unmarshalTypeName; + } + + public Class<?> getUnmarshalType() { + return unmarshalType; + } + + /** + * Class of the java type to use when unarmshalling + */ + public void setUnmarshalType(Class<?> unmarshalType) { + this.unmarshalType = unmarshalType; + } + + public Class<?> getJsonView() { + return jsonView; + } + + /** + * When marshalling a POJO to JSON you might want to exclude certain fields + * from the JSON output. With Jackson you can use JSON views to accomplish + * this. This option is to refer to the class which has @JsonView + * annotations + */ + public void setJsonView(Class<?> jsonView) { + this.jsonView = jsonView; + } + + public String getInclude() { + return include; + } + + /** + * If you want to marshal a pojo to JSON, and the pojo has some fields with + * null values. And you want to skip these null values, you can set this + * option to <tt>NOT_NULL</tt> + */ + public void setInclude(String include) { + this.include = include; + } + + public Boolean getAllowJmsType() { + return allowJmsType; + } + + /** + * Used for JMS users to allow the JMSType header from the JMS spec to + * specify a FQN classname to use to unmarshal to. + */ + public void setAllowJmsType(Boolean allowJmsType) { + this.allowJmsType = allowJmsType; + } + + public String getCollectionTypeName() { + return collectionTypeName; + } + + /** + * Refers to a custom collection type to lookup in the registry to use. This + * option should rarely be used, but allows to use different collection + * types than java.util.Collection based as default. + */ + public void setCollectionTypeName(String collectionTypeName) { + this.collectionTypeName = collectionTypeName; + } + + public Boolean getUseList() { + return useList; + } + + /** + * To unarmshal to a List of Map or a List of Pojo. + */ + public void setUseList(Boolean useList) { + this.useList = useList; + } + + public Boolean getEnableJaxbAnnotationModule() { + return enableJaxbAnnotationModule; + } + + /** + * Whether to enable the JAXB annotations module when using jackson. When + * enabled then JAXB annotations can be used by Jackson. + */ + public void setEnableJaxbAnnotationModule(Boolean enableJaxbAnnotationModule) { + this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; + } + + public String getModuleClassNames() { + return moduleClassNames; + } + + /** + * To use custom Jackson modules com.fasterxml.jackson.databind.Module + * specified as a String with FQN class names. Multiple classes can be + * separated by comma. + */ + public void setModuleClassNames(String moduleClassNames) { + this.moduleClassNames = moduleClassNames; + } + + public String getModuleRefs() { + return moduleRefs; + } + + /** + * To use custom Jackson modules referred from the Camel registry. Multiple + * modules can be separated by comma. + */ + public void setModuleRefs(String moduleRefs) { + this.moduleRefs = moduleRefs; + } + + public String getEnableFeatures() { + return enableFeatures; + } + + /** + * Set of features to enable on the Jackson + * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. + * <p/> + * The features should be a name that matches a enum from + * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, + * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or + * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> + * <p/> + * Multiple features can be separated by comma + */ + public void setEnableFeatures(String enableFeatures) { + this.enableFeatures = enableFeatures; + } + + public String getDisableFeatures() { + return disableFeatures; + } + + /** + * Set of features to disable on the Jackson + * <tt>com.fasterxml.jackson.databind.ObjectMapper</tt>. + * <p/> + * The features should be a name that matches a enum from + * <tt>com.fasterxml.jackson.databind.SerializationFeature</tt>, + * <tt>com.fasterxml.jackson.databind.DeserializationFeature</tt>, or + * <tt>com.fasterxml.jackson.databind.MapperFeature</tt> + * <p/> + * Multiple features can be separated by comma + */ + public void setDisableFeatures(String disableFeatures) { + this.disableFeatures = disableFeatures; + } + + @Override + public String getDataFormatName() { + return "jacksonxml"; + } + + @Override + protected DataFormat createDataFormat(RouteContext routeContext) { + + if (unmarshalType == null && unmarshalTypeName != null) { + try { + unmarshalType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(unmarshalTypeName); + } catch (ClassNotFoundException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + if (collectionType == null && collectionTypeName != null) { + try { + collectionType = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(collectionTypeName); + } catch (ClassNotFoundException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + + return super.createDataFormat(routeContext); + } + + @Override + protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { + if (unmarshalType != null) { + setProperty(camelContext, dataFormat, "unmarshalType", unmarshalType); + } + if (prettyPrint != null) { + setProperty(camelContext, dataFormat, "prettyPrint", prettyPrint); + } + if (jsonView != null) { + setProperty(camelContext, dataFormat, "jsonView", jsonView); + } + if (include != null) { + setProperty(camelContext, dataFormat, "include", include); + } + if (allowJmsType != null) { + setProperty(camelContext, dataFormat, "allowJmsType", allowJmsType); + } + if (collectionType != null) { + setProperty(camelContext, dataFormat, "collectionType", collectionType); + } + if (useList != null) { + setProperty(camelContext, dataFormat, "useList", useList); + } + if (enableJaxbAnnotationModule != null) { + setProperty(camelContext, dataFormat, "enableJaxbAnnotationModule", enableJaxbAnnotationModule); + } + if (moduleClassNames != null) { + setProperty(camelContext, dataFormat, "modulesClassNames", moduleClassNames); + } + if (moduleRefs != null) { + setProperty(camelContext, dataFormat, "moduleRefs", moduleRefs); + } + if (enableFeatures != null) { + setProperty(camelContext, dataFormat, "enableFeatures", enableFeatures); + } + if (disableFeatures != null) { + setProperty(camelContext, dataFormat, "disableFeatures", disableFeatures); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/pom.xml b/components/camel-jacksonxml/pom.xml new file mode 100644 index 0000000..f2e93b3 --- /dev/null +++ b/components/camel-jacksonxml/pom.xml @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.16.1-SNAPSHOT</version> + </parent> + + <artifactId>camel-jacksonxml</artifactId> + <packaging>bundle</packaging> + <name>Camel :: Jackson XML</name> + <description>Camel Jackson XML support</description> + + <properties> + <camel.osgi.import.before.defaults> + com.fasterxml.jackson.*;version="[2.3,3)" + </camel.osgi.import.before.defaults> + <camel.osgi.export.pkg>org.apache.camel.component.jacksonxml.*</camel.osgi.export.pkg> + <camel.osgi.export.service>org.apache.camel.spi.DataFormatResolver;dataformat=jacksonxml</camel.osgi.export.service> + </properties> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>${jackson2-version}</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-xml</artifactId> + <version>${jackson2-version}</version> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.module</groupId> + <artifactId>jackson-module-jaxb-annotations</artifactId> + <version>${jackson2-version}</version> + </dependency> + <dependency> + <groupId>org.codehaus.woodstox</groupId> + <artifactId>woodstox-core-asl</artifactId> + <version>4.1.4</version> + </dependency> + + <!-- testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLConstants.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLConstants.java new file mode 100644 index 0000000..1e8c3db --- /dev/null +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLConstants.java @@ -0,0 +1,29 @@ +/** + * 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.jacksonxml; + +public final class JacksonXMLConstants { + + public static final String ENABLE_TYPE_CONVERTER = "CamelJacksonXmlEnableTypeConverter"; + + public static final String ENABLE_XML_TYPE_CONVERTER = "CamelJacksonXmlEnableTypeConverter"; + + public static final String UNMARSHAL_TYPE = "CamelJacksonXmlUnmarshalType"; + + private JacksonXMLConstants() { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java new file mode 100644 index 0000000..bb6131c --- /dev/null +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/JacksonXMLDataFormat.java @@ -0,0 +1,503 @@ +/** + * 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.jacksonxml; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.Exchange; +import org.apache.camel.spi.DataFormat; +import org.apache.camel.spi.DataFormatName; +import org.apache.camel.support.ServiceSupport; +import org.apache.camel.util.CamelContextHelper; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.type.CollectionType; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; + +/** + * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat}) + * using <a href="http://jackson.codehaus.org/">Jackson</a> to marshal to and from XML. + */ +public class JacksonXMLDataFormat extends ServiceSupport implements DataFormat, DataFormatName, CamelContextAware { + + private static final Logger LOG = LoggerFactory.getLogger(JacksonXMLDataFormat.class); + + private final XmlMapper xmlMapper; + private CamelContext camelContext; + private Class<? extends Collection> collectionType; + private List<Module> modules; + private String moduleClassNames; + private String moduleRefs; + private Class<?> unmarshalType; + private Class<?> jsonView; + private String include; + private boolean prettyPrint; + private boolean allowJmsType; + private boolean useList; + private boolean enableJaxbAnnotationModule; + private String enableFeatures; + private String disableFeatures; + private boolean enableJacksonTypeConverter; + + /** + * Use the default Jackson {@link XmlMapper} and {@link Map} + */ + public JacksonXMLDataFormat() { + this(HashMap.class); + } + + /** + * Use the default Jackson {@link XmlMapper} and with a custom + * unmarshal type + * + * @param unmarshalType the custom unmarshal type + */ + public JacksonXMLDataFormat(Class<?> unmarshalType) { + this(unmarshalType, null); + } + + /** + * Use the default Jackson {@link XmlMapper} and with a custom + * unmarshal type and JSON view + * + * @param unmarshalType the custom unmarshal type + * @param jsonView marker class to specify properties to be included during marshalling. + * See also http://wiki.fasterxml.com/JacksonJsonViews + */ + public JacksonXMLDataFormat(Class<?> unmarshalType, Class<?> jsonView) { + this(unmarshalType, jsonView, true); + } + + /** + * Use the default Jackson {@link XmlMapper} and with a custom + * unmarshal type and JSON view + * + * @param unmarshalType the custom unmarshal type + * @param jsonView marker class to specify properties to be included during marshalling. + * See also http://wiki.fasterxml.com/JacksonJsonViews + * @param enableJaxbAnnotationModule if it is true, will enable the JaxbAnnotationModule. + */ + public JacksonXMLDataFormat(Class<?> unmarshalType, Class<?> jsonView, boolean enableJaxbAnnotationModule) { + this.xmlMapper = new XmlMapper(); + this.unmarshalType = unmarshalType; + this.jsonView = jsonView; + this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; + } + + /** + * Use a custom Jackson mapper and and unmarshal type + * + * @param mapper the custom mapper + * @param unmarshalType the custom unmarshal type + */ + public JacksonXMLDataFormat(XmlMapper mapper, Class<?> unmarshalType) { + this(mapper, unmarshalType, null); + } + + /** + * Use a custom Jackson mapper, unmarshal type and JSON view + * + * @param mapper the custom mapper + * @param unmarshalType the custom unmarshal type + * @param jsonView marker class to specify properties to be included during marshalling. + * See also http://wiki.fasterxml.com/JacksonJsonViews + */ + public JacksonXMLDataFormat(XmlMapper mapper, Class<?> unmarshalType, Class<?> jsonView) { + this.xmlMapper = mapper; + this.unmarshalType = unmarshalType; + this.jsonView = jsonView; + } + + @Override + public String getDataFormatName() { + return "xml-jackson"; + } + + public CamelContext getCamelContext() { + return camelContext; + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { + this.xmlMapper.writerWithView(jsonView).writeValue(stream, graph); + } + + public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { + + // is there a header with the unmarshal type? + Class<?> clazz = unmarshalType; + String type = exchange.getIn().getHeader(JacksonXMLConstants.UNMARSHAL_TYPE, String.class); + if (type == null && isAllowJmsType()) { + type = exchange.getIn().getHeader("JMSType", String.class); + } + if (type != null) { + clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(type); + } + if (collectionType != null) { + CollectionType collType = xmlMapper.getTypeFactory().constructCollectionType(collectionType, clazz); + return this.xmlMapper.readValue(stream, collType); + } else { + return this.xmlMapper.readValue(stream, clazz); + } + } + + // Properties + // ------------------------------------------------------------------------- + + public Class<?> getUnmarshalType() { + return this.unmarshalType; + } + + public void setUnmarshalType(Class<?> unmarshalType) { + this.unmarshalType = unmarshalType; + } + + public Class<? extends Collection> getCollectionType() { + return collectionType; + } + + public void setCollectionType(Class<? extends Collection> collectionType) { + this.collectionType = collectionType; + } + + public Class<?> getJsonView() { + return jsonView; + } + + public void setJsonView(Class<?> jsonView) { + this.jsonView = jsonView; + } + + public XmlMapper getXmlMapper() { + return this.xmlMapper; + } + + public String getInclude() { + return include; + } + + public void setInclude(String include) { + this.include = include; + } + + public boolean isAllowJmsType() { + return allowJmsType; + } + + public boolean isPrettyPrint() { + return prettyPrint; + } + + public void setPrettyPrint(boolean prettyPrint) { + this.prettyPrint = prettyPrint; + } + + public boolean isUseList() { + return useList; + } + + public void setUseList(boolean useList) { + this.useList = useList; + } + + public boolean isEnableJaxbAnnotationModule() { + return enableJaxbAnnotationModule; + } + + public void setEnableJaxbAnnotationModule(boolean enableJaxbAnnotationModule) { + this.enableJaxbAnnotationModule = enableJaxbAnnotationModule; + } + + public List<Module> getModules() { + return modules; + } + + /** + * To use custom Jackson {@link Module}s + */ + public void setModules(List<Module> modules) { + this.modules = modules; + } + + public String getModuleClassNames() { + return moduleClassNames; + } + + /** + * To use the custom Jackson module + */ + public void addModule(Module module) { + if (this.modules == null) { + this.modules = new ArrayList<Module>(); + } + this.modules.add(module); + } + + /** + * To use custom Jackson {@link Module}s specified as a String with FQN class names. + * Multiple classes can be separated by comma. + */ + public void setModuleClassNames(String moduleClassNames) { + this.moduleClassNames = moduleClassNames; + } + + public String getModuleRefs() { + return moduleRefs; + } + + /** + * To use custom Jackson modules referred from the Camel registry. + * Multiple modules can be separated by comma. + */ + public void setModuleRefs(String moduleRefs) { + this.moduleRefs = moduleRefs; + } + + /** + * Uses {@link java.util.ArrayList} when unmarshalling. + */ + public void useList() { + setCollectionType(ArrayList.class); + } + + /** + * Uses {@link java.util.HashMap} when unmarshalling. + */ + public void useMap() { + setCollectionType(null); + setUnmarshalType(HashMap.class); + } + + /** + * Allows jackson to use the <tt>JMSType</tt> header as an indicator what the classname is for unmarshaling XML content to POJO + * <p/> + * By default this option is <tt>false</tt>. + */ + public void setAllowJmsType(boolean allowJmsType) { + this.allowJmsType = allowJmsType; + } + + public boolean isEnableJacksonTypeConverter() { + return enableJacksonTypeConverter; + } + + /** + * If enabled then Jackson is allowed to attempt to be used during Camels <a href="https://camel.apache.org/type-converter.html">type converter</a> + * as a {@link org.apache.camel.FallbackConverter} that attempts to convert POJOs to/from {@link Map}/{@link List} types. + * <p/> + * This should only be enabled when desired to be used. + */ + public void setEnableJacksonTypeConverter(boolean enableJacksonTypeConverter) { + this.enableJacksonTypeConverter = enableJacksonTypeConverter; + } + + public String getEnableFeatures() { + return enableFeatures; + } + + /** + * Set of features to enable on the Jackson {@link XmlMapper}. + * The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}. + */ + public void setEnableFeatures(String enableFeatures) { + this.enableFeatures = enableFeatures; + } + + public String getDisableFeatures() { + return disableFeatures; + } + + /** + * Set of features to disable on the Jackson {@link XmlMapper}. + * The features should be a name that matches a enum from {@link SerializationFeature}, {@link DeserializationFeature}, or {@link MapperFeature}. + */ + public void setDisableFeatures(String disableFeatures) { + this.disableFeatures = disableFeatures; + } + + public void enableFeature(SerializationFeature feature) { + if (enableFeatures == null) { + enableFeatures = feature.name(); + } else { + enableFeatures += "," + feature.name(); + } + } + + public void enableFeature(DeserializationFeature feature) { + if (enableFeatures == null) { + enableFeatures = feature.name(); + } else { + enableFeatures += "," + feature.name(); + } + } + + public void enableFeature(MapperFeature feature) { + if (enableFeatures == null) { + enableFeatures = feature.name(); + } else { + enableFeatures += "," + feature.name(); + } + } + + public void disableFeature(SerializationFeature feature) { + if (disableFeatures == null) { + disableFeatures = feature.name(); + } else { + disableFeatures += "," + feature.name(); + } + } + + public void disableFeature(DeserializationFeature feature) { + if (disableFeatures == null) { + disableFeatures = feature.name(); + } else { + disableFeatures += "," + feature.name(); + } + } + + public void disableFeature(MapperFeature feature) { + if (disableFeatures == null) { + disableFeatures = feature.name(); + } else { + disableFeatures += "," + feature.name(); + } + } + + @Override + protected void doStart() throws Exception { + + if (enableJaxbAnnotationModule) { + // Enables JAXB processing + JaxbAnnotationModule module = new JaxbAnnotationModule(); + LOG.info("Registering module: {}", module); + xmlMapper.registerModule(module); + } + + if (useList) { + setCollectionType(ArrayList.class); + } + if (include != null) { + JsonInclude.Include inc = getCamelContext().getTypeConverter().mandatoryConvertTo(JsonInclude.Include.class, include); + xmlMapper.setSerializationInclusion(inc); + } + if (prettyPrint) { + xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); + } + + if (enableFeatures != null) { + Iterator<Object> it = ObjectHelper.createIterator(enableFeatures); + while (it.hasNext()) { + String enable = it.next().toString(); + // it can be different kind + SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable); + if (sf != null) { + xmlMapper.enable(sf); + continue; + } + DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, enable); + if (df != null) { + xmlMapper.enable(df); + continue; + } + MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, enable); + if (mf != null) { + xmlMapper.enable(mf); + continue; + } + throw new IllegalArgumentException("Enable feature: " + enable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]"); + } + } + if (disableFeatures != null) { + Iterator<Object> it = ObjectHelper.createIterator(disableFeatures); + while (it.hasNext()) { + String disable = it.next().toString(); + // it can be different kind + SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable); + if (sf != null) { + xmlMapper.disable(sf); + continue; + } + DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, disable); + if (df != null) { + xmlMapper.disable(df); + continue; + } + MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, disable); + if (mf != null) { + xmlMapper.disable(mf); + continue; + } + throw new IllegalArgumentException("Disable feature: " + disable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]"); + } + } + + if (modules != null) { + for (Module module : modules) { + LOG.info("Registering module: {}", module); + xmlMapper.registerModules(module); + } + } + if (moduleClassNames != null) { + Iterable<Object> it = ObjectHelper.createIterable(moduleClassNames); + for (Object o : it) { + String name = o.toString(); + Class<Module> clazz = camelContext.getClassResolver().resolveMandatoryClass(name, Module.class); + Module module = camelContext.getInjector().newInstance(clazz); + LOG.info("Registering module: {} -> {}", name, module); + xmlMapper.registerModule(module); + } + } + if (moduleRefs != null) { + Iterable<Object> it = ObjectHelper.createIterable(moduleRefs); + for (Object o : it) { + String name = o.toString(); + if (name.startsWith("#")) { + name = name.substring(1); + } + Module module = CamelContextHelper.mandatoryLookup(camelContext, name, Module.class); + LOG.info("Registering module: {} -> {}", name, module); + xmlMapper.registerModule(module); + } + } + } + + @Override + protected void doStop() throws Exception { + // noop + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/ListJacksonXMLDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/ListJacksonXMLDataFormat.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/ListJacksonXMLDataFormat.java new file mode 100644 index 0000000..f88cdf9 --- /dev/null +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/ListJacksonXMLDataFormat.java @@ -0,0 +1,55 @@ +/** + * 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.jacksonxml; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +/** + * A {@link org.apache.camel.component.jacksonxml.JacksonXMLDataFormat} that is using a list + */ +public class ListJacksonXMLDataFormat extends JacksonXMLDataFormat { + + public ListJacksonXMLDataFormat() { + useList(); + } + + public ListJacksonXMLDataFormat(Class<?> unmarshalType) { + super(unmarshalType); + useList(); + } + + public ListJacksonXMLDataFormat(Class<?> unmarshalType, Class<?> jsonView) { + super(unmarshalType, jsonView); + useList(); + } + + public ListJacksonXMLDataFormat(Class<?> unmarshalType, Class<?> jsonView, boolean enableJaxbAnnotationModule) { + super(unmarshalType, jsonView, enableJaxbAnnotationModule); + useList(); + } + + public ListJacksonXMLDataFormat(XmlMapper mapper, Class<?> unmarshalType) { + super(mapper, unmarshalType); + useList(); + } + + public ListJacksonXMLDataFormat(XmlMapper mapper, Class<?> unmarshalType, Class<?> jsonView) { + super(mapper, unmarshalType, jsonView); + useList(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/converter/JacksonXMLTypeConverters.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/converter/JacksonXMLTypeConverters.java b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/converter/JacksonXMLTypeConverters.java new file mode 100644 index 0000000..922e69e --- /dev/null +++ b/components/camel-jacksonxml/src/main/java/org/apache/camel/component/jacksonxml/converter/JacksonXMLTypeConverters.java @@ -0,0 +1,88 @@ +/** + * 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.jacksonxml.converter; + +import java.util.Map; +import java.util.Set; + +import org.apache.camel.Exchange; +import org.apache.camel.FallbackConverter; +import org.apache.camel.component.jacksonxml.JacksonXMLConstants; +import org.apache.camel.spi.Registry; +import org.apache.camel.spi.TypeConverterRegistry; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +public final class JacksonXMLTypeConverters { + + private final XmlMapper defaultMapper = new XmlMapper(); + private boolean init; + private Boolean enabled; + + public JacksonXMLTypeConverters() { + } + + @FallbackConverter + public <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) { + + // only do this if enabled + if (!init && exchange != null) { + // init to see if this is enabled + String text = exchange.getContext().getProperties().get(JacksonXMLConstants.ENABLE_XML_TYPE_CONVERTER); + enabled = "true".equalsIgnoreCase(text); + init = true; + } + + if (enabled == null || !enabled) { + return null; + } + + + if (isNotPojoType(type)) { + return null; + } + + if (exchange != null && value instanceof Map) { + ObjectMapper mapper = resolveObjectMapper(exchange.getContext().getRegistry()); + if (mapper.canSerialize(type)) { + return mapper.convertValue(value, type); + } + } + + // Just return null to let other fallback converter to do the job + return null; + } + + private static boolean isNotPojoType(Class<?> type) { + boolean isString = String.class.isAssignableFrom(type); + boolean isNumber = Number.class.isAssignableFrom(type) + || int.class.isAssignableFrom(type) || long.class.isAssignableFrom(type) + || short.class.isAssignableFrom(type) || char.class.isAssignableFrom(type) + || float.class.isAssignableFrom(type) || double.class.isAssignableFrom(type); + return isString || isNumber; + } + + private ObjectMapper resolveObjectMapper(Registry registry) { + Set<XmlMapper> mappers = registry.findByType(XmlMapper.class); + if (mappers.size() == 1) { + return mappers.iterator().next(); + } + return defaultMapper; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/resources/META-INF/LICENSE.txt ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/resources/META-INF/LICENSE.txt b/components/camel-jacksonxml/src/main/resources/META-INF/LICENSE.txt new file mode 100755 index 0000000..6b0b127 --- /dev/null +++ b/components/camel-jacksonxml/src/main/resources/META-INF/LICENSE.txt @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. + http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/resources/META-INF/NOTICE.txt ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/resources/META-INF/NOTICE.txt b/components/camel-jacksonxml/src/main/resources/META-INF/NOTICE.txt new file mode 100644 index 0000000..2e215bf --- /dev/null +++ b/components/camel-jacksonxml/src/main/resources/META-INF/NOTICE.txt @@ -0,0 +1,11 @@ + ========================================================================= + == NOTICE file corresponding to the section 4 d of == + == the Apache License, Version 2.0, == + == in this case for the Apache Camel distribution. == + ========================================================================= + + This product includes software developed by + The Apache Software Foundation (http://www.apache.org/). + + Please read the different LICENSE files present in the licenses directory of + this distribution. http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/TypeConverter ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/TypeConverter b/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/TypeConverter new file mode 100644 index 0000000..227d9c3 --- /dev/null +++ b/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/TypeConverter @@ -0,0 +1,17 @@ +# +# 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. +# +org.apache.camel.component.jacksonxml.converter.JacksonXMLTypeConverters \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/dataformat/jacksonxml ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/dataformat/jacksonxml b/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/dataformat/jacksonxml new file mode 100644 index 0000000..08249cd --- /dev/null +++ b/components/camel-jacksonxml/src/main/resources/META-INF/services/org/apache/camel/dataformat/jacksonxml @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.component.jacksonxml.JacksonXMLDataFormat http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonBindingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonBindingTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonBindingTest.java new file mode 100644 index 0000000..92d8ba0 --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonBindingTest.java @@ -0,0 +1,64 @@ +/** + * 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.jacksonxml; + +import javax.naming.Context; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.binding.BindingComponent; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.processor.binding.DataFormatBinding; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonBindingTest extends CamelTestSupport { + protected MockEndpoint results; + + @Test + public void testMarshalAndUnmarshalPojo() throws Exception { + TestPojo in = new TestPojo(); + in.setName("Camel"); + + results.expectedMessageCount(1); + results.message(0).body().isInstanceOf(TestPojo.class); + results.message(0).body().equals(in); + + template.sendBody("direct:start", in); + results.assertIsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + results = getMockEndpoint("mock:results"); + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("direct:start").to("jsonmq:orders").to("file:target/copyOfMessages"); + from("jsonmq:orders").to(results); + } + }; + } + + @Override + protected Context createJndiContext() throws Exception { + Context context = super.createJndiContext(); + JacksonXMLDataFormat format = new JacksonXMLDataFormat(TestPojo.class); + context.bind("jsonmq", new BindingComponent(new DataFormatBinding(format), "file:target/")); + return context; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConcurrentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConcurrentTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConcurrentTest.java new file mode 100644 index 0000000..90d5530 --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConcurrentTest.java @@ -0,0 +1,80 @@ +/** + * 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.jacksonxml; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * @version + */ +public class JacksonConcurrentTest extends CamelTestSupport { + + @Test + public void testNoConcurrentProducers() throws Exception { + doSendMessages(1, 1); + } + + @Test + public void testConcurrentProducers() throws Exception { + doSendMessages(10, 5); + } + + private void doSendMessages(int files, int poolSize) throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(files); + getMockEndpoint("mock:result").assertNoDuplicates(body()); + + ExecutorService executor = Executors.newFixedThreadPool(poolSize); + for (int i = 0; i < files; i++) { + final int index = i; + executor.submit(new Callable<Object>() { + public Object call() throws Exception { + TestPojo pojo = new TestPojo(); + pojo.setName("Hi " + index); + + template.sendBody("direct:start", pojo); + return null; + } + }); + } + + assertMockEndpointsSatisfied(); + executor.shutdownNow(); + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("direct:start"). + marshal().jacksonxml(). + to("log:marshalled"). + to("direct:marshalled"); + + from("direct:marshalled"). + unmarshal().jacksonxml(TestPojo.class). + to("mock:result"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsSimpleTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsSimpleTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsSimpleTest.java new file mode 100644 index 0000000..53f71f7 --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsSimpleTest.java @@ -0,0 +1,70 @@ +/** + * 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.jacksonxml; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.component.jacksonxml.JacksonXMLConstants; +import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonConversionsSimpleTest extends CamelTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + // enable jackson type converter by setting this property on CamelContext + context.getProperties().put(JacksonXMLConstants.ENABLE_TYPE_CONVERTER, "true"); + return context; + } + + @Test + public void shouldNotConvertMapToString() { + Exchange exchange = new DefaultExchange(context); + + Map<String, String> body = new HashMap<String, String>(); + Object convertedObject = context.getTypeConverter().convertTo(String.class, exchange, body); + // will do a toString which is an empty map + assertEquals(body.toString(), convertedObject); + } + + @Test + public void shouldNotConvertMapToNumber() { + Exchange exchange = new DefaultExchange(context); + + Object convertedObject = context.getTypeConverter().convertTo(Long.class, exchange, new HashMap<String, String>()); + assertNull(convertedObject); + } + + @Test + public void shouldNotConvertMapToPrimitive() { + Exchange exchange = new DefaultExchange(context); + Object convertedObject = context.getTypeConverter().convertTo(long.class, exchange, new HashMap<String, String>()); + + assertNull(convertedObject); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsTest.java new file mode 100644 index 0000000..ffbdeb3 --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonConversionsTest.java @@ -0,0 +1,59 @@ +/** + * 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.jacksonxml; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jacksonxml.JacksonXMLConstants; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class JacksonConversionsTest extends CamelTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + // enable jackson type converter by setting this property on CamelContext + context.getProperties().put(JacksonXMLConstants.ENABLE_TYPE_CONVERTER, "true"); + return context; + } + + @Test + public void shouldConvertMapToPojo() { + String name = "someName"; + Map<String, String> pojoAsMap = new HashMap<String, String>(); + pojoAsMap.put("name", name); + + TestPojo testPojo = (TestPojo) template.requestBody("direct:test", pojoAsMap); + + assertEquals(name, testPojo.getName()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").convertBodyTo(TestPojo.class); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0936806d/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonIncludeDefaultTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonIncludeDefaultTest.java b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonIncludeDefaultTest.java new file mode 100644 index 0000000..095ae45 --- /dev/null +++ b/components/camel-jacksonxml/src/test/java/org/apache/camel/component/jacksonxml/JacksonIncludeDefaultTest.java @@ -0,0 +1,53 @@ +/** + * 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.jacksonxml; + +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 JacksonIncludeDefaultTest extends CamelTestSupport { + + @Test + public void testMmarshalPojo() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:marshal"); + mock.expectedMessageCount(1); + mock.message(0).body(String.class).isEqualTo("<TestOtherPojo><name>Camel</name><country/></TestOtherPojo>"); + + TestOtherPojo pojo = new TestOtherPojo(); + pojo.setName("Camel"); + + template.sendBody("direct:marshal", pojo); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + JacksonXMLDataFormat format = new JacksonXMLDataFormat(); + + from("direct:marshal").marshal(format).to("mock:marshal"); + } + }; + } + +}