Repository: camel Updated Branches: refs/heads/master 4e5599c6c -> 53600cfaf
CAMEL-9661 - YAML Data Format Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/53600cfa Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/53600cfa Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/53600cfa Branch: refs/heads/master Commit: 53600cfaf16966ed01b4e888993e7700106ed97b Parents: 4a3c464 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Wed Mar 2 16:17:55 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Mar 4 08:53:04 2016 +0100 ---------------------------------------------------------------------- apache-camel/pom.xml | 4 + .../src/main/descriptors/common-bin.xml | 1 + .../apache/camel/builder/DataFormatClause.java | 24 +- .../model/dataformat/DataFormatsDefinition.java | 1 + .../camel/model/dataformat/YAMLDataFormat.java | 241 +++++++++++++++ .../camel/model/dataformat/YAMLLibrary.java | 31 ++ .../apache/camel/model/dataformat/jaxb.index | 2 + components/camel-snakeyaml/pom.xml | 67 ++++ .../snakeyaml/SnakeYAMLDataFormat.java | 304 +++++++++++++++++++ .../src/main/resources/META-INF/LICENSE.txt | 203 +++++++++++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + .../org/apache/camel/dataformat/yaml-snakeyaml | 18 ++ .../snakeyaml/SnakeYAMLMarshalTest.java | 100 ++++++ .../snakeyaml/SnakeYAMLMarshalTestHelper.java | 84 +++++ .../snakeyaml/SnakeYAMLSpringMarshalTest.java | 73 +++++ .../component/snakeyaml/model/TestPojo.java | 52 ++++ .../src/test/resources/log4j.properties | 36 +++ .../snakeyaml/SnakeYAMLSpringMarshalTest.xml | 76 +++++ components/pom.xml | 1 + parent/pom.xml | 7 +- .../features/src/main/resources/features.xml | 5 + .../camel/itest/karaf/CamelSnakeyamlTest.java | 40 +++ .../maven/packaging/PackageDataFormatMojo.java | 4 + 23 files changed, 1382 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/apache-camel/pom.xml ---------------------------------------------------------------------- diff --git a/apache-camel/pom.xml b/apache-camel/pom.xml index ae628ec..0eb7c9d 100644 --- a/apache-camel/pom.xml +++ b/apache-camel/pom.xml @@ -736,6 +736,10 @@ </dependency> <dependency> <groupId>org.apache.camel</groupId> + <artifactId>camel-snakeyaml</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> <artifactId>camel-snmp</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/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 ad76d9b..b00bd97 100644 --- a/apache-camel/src/main/descriptors/common-bin.xml +++ b/apache-camel/src/main/descriptors/common-bin.xml @@ -195,6 +195,7 @@ <include>org.apache.camel:camel-sjms</include> <include>org.apache.camel:camel-slack</include> <include>org.apache.camel:camel-smpp</include> + <include>org.apache.camel:camel-snakeyaml</include> <include>org.apache.camel:camel-snmp</include> <include>org.apache.camel:camel-soap</include> <include>org.apache.camel:camel-solr</include> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/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 de2a7c1..33b5403 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 @@ -20,8 +20,6 @@ import java.nio.charset.Charset; import java.util.Map; import java.util.zip.Deflater; -import org.w3c.dom.Node; - import org.apache.camel.model.DataFormatDefinition; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.dataformat.AvroDataFormat; @@ -55,10 +53,13 @@ import org.apache.camel.model.dataformat.XMLBeansDataFormat; import org.apache.camel.model.dataformat.XMLSecurityDataFormat; import org.apache.camel.model.dataformat.XStreamDataFormat; import org.apache.camel.model.dataformat.XmlJsonDataFormat; +import org.apache.camel.model.dataformat.YAMLDataFormat; +import org.apache.camel.model.dataformat.YAMLLibrary; import org.apache.camel.model.dataformat.ZipDataFormat; import org.apache.camel.model.dataformat.ZipFileDataFormat; import org.apache.camel.util.CollectionStringBuffer; import org.apache.camel.util.jsse.KeyStoreParameters; +import org.w3c.dom.Node; /** * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat} @@ -871,6 +872,25 @@ public class DataFormatClause<T extends ProcessorDefinition<?>> { } /** + * Uses the YAML data format + * + * @param library the yaml library to use + */ + public T yaml(YAMLLibrary library) { + return dataFormat(new YAMLDataFormat(library)); + } + + /** + * Uses the YAML data format + * + * @param type the yaml type to use + * @param type the type for json snakeyaml type + */ + public T yaml(YAMLLibrary library, Class<?> type) { + return dataFormat(new YAMLDataFormat(library, type)); + } + + /** * Uses the XML Security data format */ public T secureXML() { http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/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 a887c04..81b09f9 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 @@ -75,6 +75,7 @@ public class DataFormatsDefinition { @XmlElement(required = false, name = "xmlrpc", type = XmlRpcDataFormat.class), @XmlElement(required = false, name = "xstream", type = XStreamDataFormat.class), @XmlElement(required = false, name = "pgp", type = PGPDataFormat.class), + @XmlElement(required = false, name = "yaml", type = YAMLDataFormat.class), @XmlElement(required = false, name = "zip", type = ZipDataFormat.class), @XmlElement(required = false, name = "zipFile", type = ZipFileDataFormat.class)} ) http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLDataFormat.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLDataFormat.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLDataFormat.java new file mode 100644 index 0000000..77be7a6 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLDataFormat.java @@ -0,0 +1,241 @@ +/* + * 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; + +/** + * YAML data format + * + * @version + */ +@Metadata(label = "dataformat,transformation,yaml", title = "YAML") +@XmlRootElement(name = "yaml") +@XmlAccessorType(XmlAccessType.FIELD) +public class YAMLDataFormat extends DataFormatDefinition { + @XmlAttribute @Metadata(defaultValue = "SnakeYAML") + private YAMLLibrary library = YAMLLibrary.SnakeYAML; + @XmlTransient + private ClassLoader classLoader; + @XmlTransient + private Class<?> unmarshalType; + @XmlAttribute + private String unmarshalTypeName; + @XmlAttribute + private String constructor; + @XmlAttribute + private String representer; + @XmlAttribute + private String dumperOptions; + @XmlAttribute + private String resolver; + @XmlAttribute @Metadata(defaultValue = "true") + private Boolean useApplicationContextClassLoader = true; + @XmlAttribute @Metadata(defaultValue = "false") + private Boolean prettyFlow = false; + + public YAMLDataFormat() { + this(YAMLLibrary.SnakeYAML); + } + + public YAMLDataFormat(YAMLLibrary library) { + super("yaml-" + library.name().toLowerCase()); + this.library = library; + } + + public YAMLDataFormat(YAMLLibrary library, Class<?> unmarshalType) { + super("yaml-" + library.name().toLowerCase()); + this.library = library; + this.unmarshalType = unmarshalType; + } + + public YAMLLibrary getLibrary() { + return library; + } + + /** + * Which yaml library to use such. + * <p/> + * Is by default SnakeYAML + */ + public void setLibrary(YAMLLibrary library) { + this.library = library; + setDataFormatName("yaml-" + library.name().toLowerCase()); + } + + public Class<?> getUnmarshalType() { + return unmarshalType; + } + + /** + * Class of the object to be created + */ + public void setUnmarshalType(Class<?> type) { + this.unmarshalType = type; + } + + public String getUnmarshalTypeName() { + return unmarshalTypeName; + } + + /** + * Class name of the java type to use when unarmshalling + */ + public void setUnmarshalTypeName(String unmarshalTypeName) { + this.unmarshalTypeName = unmarshalTypeName; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + /** + * Set a custom classloader + */ + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public String getConstructor() { + return constructor; + } + + /** + * BaseConstructor to construct incoming documents. + */ + public void setConstructor(String constructor) { + this.constructor = constructor; + } + + public String getRepresenter() { + return representer; + } + + /** + * Representer to emit outgoing objects. + */ + public void setRepresenter(String representer) { + this.representer = representer; + } + + public String getDumperOptions() { + return dumperOptions; + } + + /** + * DumperOptions to configure outgoing objects. + */ + public void setDumperOptions(String dumperOptions) { + this.dumperOptions = dumperOptions; + } + + public String getResolver() { + return resolver; + } + + /** + * Resolver to detect implicit type + */ + public void setResolver(String resolver) { + this.resolver = resolver; + } + + public boolean isUseApplicationContextClassLoader() { + return useApplicationContextClassLoader; + } + + /** + * Use ApplicationContextClassLoader as custom ClassLoader + */ + public void setUseApplicationContextClassLoader(boolean useApplicationContextClassLoader) { + this.useApplicationContextClassLoader = useApplicationContextClassLoader; + } + + public boolean isPrettyFlow() { + return prettyFlow; + } + + /** + * Force the emitter to produce a pretty YAML document when using the flow + * style. + */ + public void setPrettyFlow(boolean prettyFlow) { + this.prettyFlow = prettyFlow; + } + + @Override + protected DataFormat createDataFormat(RouteContext routeContext) { + if (library == YAMLLibrary.SnakeYAML) { + setProperty(routeContext.getCamelContext(), this, "dataFormatName", "yaml-snakeyaml"); + } + + return super.createDataFormat(routeContext); + } + + @Override + protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) { + if (library == YAMLLibrary.SnakeYAML) { + configureSnakeDataFormat(dataFormat, camelContext); + } + } + + protected void configureSnakeDataFormat(DataFormat dataFormat, CamelContext camelContext) { + Class<?> yamlUnmarshalType = unmarshalType; + if (yamlUnmarshalType == null && unmarshalTypeName != null) { + try { + yamlUnmarshalType = camelContext.getClassResolver().resolveMandatoryClass(unmarshalTypeName); + } catch (ClassNotFoundException e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + } + + setProperty(dataFormat, camelContext, "unmarshalType", yamlUnmarshalType); + setProperty(dataFormat, camelContext, "classLoader", classLoader); + setProperty(dataFormat, camelContext, "useApplicationContextClassLoader", useApplicationContextClassLoader); + setProperty(dataFormat, camelContext, "prettyFlow", prettyFlow); + + setPropertyRef(dataFormat, camelContext, "constructor", constructor); + setPropertyRef(dataFormat, camelContext, "representer", representer); + setPropertyRef(dataFormat, camelContext, "dumperOptions", dumperOptions); + setPropertyRef(dataFormat, camelContext, "resolver", resolver); + } + + protected void setProperty(DataFormat dataFormat, CamelContext camelContext, String propertyName, Object propertyValue) { + if (ObjectHelper.isNotEmpty(propertyValue)) { + setProperty(camelContext, dataFormat, propertyName, propertyValue); + } + } + + protected void setPropertyRef(DataFormat dataFormat, CamelContext camelContext, String propertyName, String propertyValue) { + if (ObjectHelper.isNotEmpty(propertyValue)) { + // must be a reference value + String ref = propertyValue.startsWith("#") ? propertyValue : "#" + propertyValue; + setProperty(camelContext, dataFormat, propertyName, ref); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLLibrary.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLLibrary.java b/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLLibrary.java new file mode 100644 index 0000000..4f47b3a --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/YAMLLibrary.java @@ -0,0 +1,31 @@ +/* + * 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.XmlEnum; +import javax.xml.bind.annotation.XmlType; + +/** + * Represents the concrete Yaml libraries Camel supports. + * + * @version + */ +@XmlType +@XmlEnum +public enum YAMLLibrary { + SnakeYAML +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index ---------------------------------------------------------------------- diff --git a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index index 6b78444..9328654 100644 --- a/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index +++ b/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index @@ -48,5 +48,7 @@ UniVocityTsvDataFormat XMLBeansDataFormat XMLSecurityDataFormat XStreamDataFormat +YAMLDataFormat +YAMLLibrary ZipDataFormat ZipFileDataFormat http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/pom.xml b/components/camel-snakeyaml/pom.xml new file mode 100644 index 0000000..1f36c80 --- /dev/null +++ b/components/camel-snakeyaml/pom.xml @@ -0,0 +1,67 @@ +<?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.17-SNAPSHOT</version> + </parent> + + <artifactId>camel-snakeyaml</artifactId> + <packaging>bundle</packaging> + <name>Camel :: SnakeYAML</name> + <description>Camel SnakeYAML support</description> + + <properties> + <camel.osgi.export.pkg>org.apache.camel.component.snakeyaml.*</camel.osgi.export.pkg> + <camel.osgi.export.service>org.apache.camel.spi.DataFormatResolver;dataformat=yaml-snakeyaml</camel.osgi.export.service> + </properties> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>${snakeyaml-version}</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/53600cfa/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java new file mode 100644 index 0000000..7e030bb --- /dev/null +++ b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java @@ -0,0 +1,304 @@ +/** + * 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.snakeyaml; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.ref.WeakReference; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelContext; +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.IOHelper; +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.TypeDescription; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.BaseConstructor; +import org.yaml.snakeyaml.constructor.Constructor; +import org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor; +import org.yaml.snakeyaml.nodes.Tag; +import org.yaml.snakeyaml.representer.Representer; +import org.yaml.snakeyaml.resolver.Resolver; + + +/** + * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat}) + * using <a href="http://www.snakeyaml.org">SnakeYAML</a> to marshal to and from YAML. + */ +public class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat, DataFormatName { + + private ThreadLocal<WeakReference<Yaml>> yamlCache; + private BaseConstructor constructor; + private Representer representer; + private DumperOptions dumperOptions; + private Resolver resolver; + private ClassLoader classLoader; + private Class<?> unmarshalType; + private List<TypeDescription> typeDescriptions; + private Map<Class<?>, Tag> classTags; + private boolean useApplicationContextClassLoader; + private boolean prettyFlow; + + public SnakeYAMLDataFormat() { + this(Object.class); + } + + public SnakeYAMLDataFormat(Class<?> type) { + this.unmarshalType = type; + this.yamlCache = new ThreadLocal<>(); + this.useApplicationContextClassLoader = true; + this.prettyFlow = false; + } + + @Override + public String getDataFormatName() { + return "yaml-snakeyaml"; + } + + @Override + public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception { + try (final OutputStreamWriter osw = new OutputStreamWriter(stream, IOHelper.getCharsetName(exchange))) { + getYaml(exchange.getContext()).dump(graph, osw); + } + } + + @Override + public Object unmarshal(final Exchange exchange, final InputStream stream) throws Exception { + try (final InputStreamReader isr = new InputStreamReader(stream, IOHelper.getCharsetName(exchange))) { + return getYaml(exchange.getContext()).loadAs(isr, unmarshalType); + } + } + + protected Yaml getYaml(CamelContext context) { + Yaml yaml = null; + WeakReference<Yaml> ref = yamlCache.get(); + + if (ref != null) { + yaml = ref.get(); + } + + if (yaml == null) { + BaseConstructor yamlConstructor = this.constructor; + Representer yamlRepresenter = this.representer; + DumperOptions yamlDumperOptions = this.dumperOptions; + Resolver yamlResolver = this.resolver; + ClassLoader yamlClassLoader = this.classLoader; + + if (yamlClassLoader == null && useApplicationContextClassLoader) { + yamlClassLoader = context.getApplicationContextClassLoader(); + } + + if (yamlConstructor == null) { + yamlConstructor = yamlClassLoader == null + ? new Constructor() + : new CustomClassLoaderConstructor(yamlClassLoader); + + if (typeDescriptions != null) { + for (TypeDescription typeDescription : typeDescriptions) { + ((Constructor)yamlConstructor).addTypeDescription(typeDescription); + } + } + } + if (yamlRepresenter == null) { + yamlRepresenter = new Representer(); + + if (classTags != null) { + for (Map.Entry<Class<?>, Tag> entry : classTags.entrySet()) { + yamlRepresenter.addClassTag(entry.getKey(), entry.getValue()); + } + } + } + if (yamlDumperOptions == null) { + yamlDumperOptions = new DumperOptions(); + yamlDumperOptions.setPrettyFlow(prettyFlow); + } + if (yamlResolver == null) { + yamlResolver = new Resolver(); + } + + yaml = new Yaml(yamlConstructor, yamlRepresenter, yamlDumperOptions, yamlResolver); + yamlCache.set(new WeakReference<>(yaml)); + } + + return yaml; + } + + @Override + protected void doStart() throws Exception { + // noop + } + + @Override + protected void doStop() throws Exception { + // noop + } + + public BaseConstructor getConstructor() { + return constructor; + } + + /** + * BaseConstructor to construct incoming documents. + */ + public void setConstructor(BaseConstructor constructor) { + this.constructor = constructor; + } + + public Representer getRepresenter() { + return representer; + } + + /** + * Representer to emit outgoing objects. + */ + public void setRepresenter(Representer representer) { + this.representer = representer; + } + + public DumperOptions getDumperOptions() { + return dumperOptions; + } + + /** + * DumperOptions to configure outgoing objects. + */ + public void setDumperOptions(DumperOptions dumperOptions) { + this.dumperOptions = dumperOptions; + } + + public Resolver getResolver() { + return resolver; + } + + /** + * Resolver to detect implicit type + */ + public void setResolver(Resolver resolver) { + this.resolver = resolver; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + /** + * Set a custom classloader + */ + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + public Class<?> getUnmarshalType() { + return this.unmarshalType; + } + + /** + * Class of the object to be created + */ + public void setUnmarshalType(Class<?> unmarshalType) { + this.unmarshalType = unmarshalType; + } + + public List<TypeDescription> getTypeDescriptions() { + return typeDescriptions; + } + + /** + * Make YAML aware how to parse a custom Class. + */ + public void setTypeDescriptions(List<TypeDescription> typeDescriptions) { + this.typeDescriptions = typeDescriptions; + } + + public void addTypeDescriptions(TypeDescription... typeDescriptions) { + if (this.typeDescriptions == null) { + this.typeDescriptions = new LinkedList<>(); + } + + for (TypeDescription typeDescription : typeDescriptions) { + this.typeDescriptions.add(typeDescription); + } + } + + public void addTypeDescription(Class<?> type, Tag tag) { + if (this.typeDescriptions == null) { + this.typeDescriptions = new LinkedList<>(); + } + + this.typeDescriptions.add(new TypeDescription(type, tag)); + } + + public Map<Class<?>, Tag> getClassTags() { + return classTags; + } + + /** + * Define a tag for the <code>Class</code> to serialize. + */ + public void setClassTags(Map<Class<?>, Tag> classTags) { + this.classTags = classTags; + } + + public void addClassTags(Class<?> type, Tag tag) { + if (this.classTags == null) { + this.classTags = new LinkedHashMap<>(); + } + + this.classTags.put(type, tag); + } + + + public boolean isUseApplicationContextClassLoader() { + return useApplicationContextClassLoader; + } + + /** + * Use ApplicationContextClassLoader as custom ClassLoader + */ + public void setUseApplicationContextClassLoader(boolean useApplicationContextClassLoader) { + this.useApplicationContextClassLoader = useApplicationContextClassLoader; + } + + public boolean isPrettyFlow() { + return prettyFlow; + } + + /** + * Force the emitter to produce a pretty YAML document when using the flow + * style. + */ + public void setPrettyFlow(boolean prettyFlow) { + this.prettyFlow = prettyFlow; + } + + /** + * Convenience method to set class tag for bot <code>Constructor</code> and + * <code>Representer</code> + */ + public void addTag(Class<?> type, Tag tag) { + addClassTags(type, tag); + addTypeDescription(type, tag); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/main/resources/META-INF/LICENSE.txt ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/main/resources/META-INF/LICENSE.txt b/components/camel-snakeyaml/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000..6b0b127 --- /dev/null +++ b/components/camel-snakeyaml/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/53600cfa/components/camel-snakeyaml/src/main/resources/META-INF/NOTICE.txt ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/main/resources/META-INF/NOTICE.txt b/components/camel-snakeyaml/src/main/resources/META-INF/NOTICE.txt new file mode 100644 index 0000000..2e215bf --- /dev/null +++ b/components/camel-snakeyaml/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/53600cfa/components/camel-snakeyaml/src/main/resources/META-INF/services/org/apache/camel/dataformat/yaml-snakeyaml ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/main/resources/META-INF/services/org/apache/camel/dataformat/yaml-snakeyaml b/components/camel-snakeyaml/src/main/resources/META-INF/services/org/apache/camel/dataformat/yaml-snakeyaml new file mode 100644 index 0000000..c503c8a --- /dev/null +++ b/components/camel-snakeyaml/src/main/resources/META-INF/services/org/apache/camel/dataformat/yaml-snakeyaml @@ -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.snakeyaml.SnakeYAMLDataFormat http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTest.java ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTest.java b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTest.java new file mode 100644 index 0000000..0284386 --- /dev/null +++ b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTest.java @@ -0,0 +1,100 @@ +/** + * 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.snakeyaml; + +import java.util.Arrays; +import java.util.Collection; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.snakeyaml.model.TestPojo; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.yaml.snakeyaml.nodes.Tag; + +import static org.apache.camel.component.snakeyaml.SnakeYAMLMarshalTestHelper.createClassTagDataFormat; +import static org.apache.camel.component.snakeyaml.SnakeYAMLMarshalTestHelper.createDataFormat; +import static org.apache.camel.component.snakeyaml.SnakeYAMLMarshalTestHelper.createPrettyFlowDataFormat; +import static org.apache.camel.component.snakeyaml.SnakeYAMLMarshalTestHelper.createTestMap; +import static org.apache.camel.component.snakeyaml.SnakeYAMLMarshalTestHelper.createTestPojo; + +@RunWith(Parameterized.class) +public class SnakeYAMLMarshalTest extends CamelTestSupport { + + private final SnakeYAMLDataFormat format; + private final Object body; + private final String expected; + + public SnakeYAMLMarshalTest(SnakeYAMLDataFormat format, Object body, String expected) { + this.format = format; + this.body = body; + this.expected = expected; + } + + @Parameterized.Parameters + public static Collection yamlCases() { + return Arrays.asList(new Object[][] { + { + createDataFormat(null), + createTestMap(), + "{name: Camel}" + }, + { + createDataFormat(TestPojo.class), + createTestPojo(), + "!!org.apache.camel.component.snakeyaml.model.TestPojo {name: Camel}" + }, + { + createPrettyFlowDataFormat(TestPojo.class, true), + createTestPojo(), + "!!org.apache.camel.component.snakeyaml.model.TestPojo {\n name: Camel\n}" + }, + { + createClassTagDataFormat(TestPojo.class, new Tag("!tpojo")), + createTestPojo(), + "!tpojo {name: Camel}" + } + }); + } + + @Test + public void testMarshalAndUnmarshal() throws Exception { + SnakeYAMLMarshalTestHelper.marshalAndUnmarshal( + context(), + body, + "mock:reverse", + "direct:in", + "direct:back", + expected + ); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:in") + .marshal(format); + from("direct:back") + .unmarshal(format) + .to("mock:reverse"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTestHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTestHelper.java b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTestHelper.java new file mode 100644 index 0000000..e004103 --- /dev/null +++ b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLMarshalTestHelper.java @@ -0,0 +1,84 @@ +/** + * 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.snakeyaml; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.snakeyaml.model.TestPojo; +import org.yaml.snakeyaml.nodes.Tag; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public final class SnakeYAMLMarshalTestHelper { + + protected SnakeYAMLMarshalTestHelper() { + } + + public static TestPojo createTestPojo() { + return new TestPojo("Camel"); + } + + public static Map<String, String> createTestMap() { + Map<String, String> map = new HashMap<>(); + map.put("name", "Camel"); + + return map; + } + + public static SnakeYAMLDataFormat createDataFormat(Class<?> type) { + return type == null ? new SnakeYAMLDataFormat() : new SnakeYAMLDataFormat(type); + } + + public static SnakeYAMLDataFormat createPrettyFlowDataFormat(Class<?> type, boolean prettyFlow) { + SnakeYAMLDataFormat df = type == null ? new SnakeYAMLDataFormat() : new SnakeYAMLDataFormat(type); + df.setPrettyFlow(prettyFlow); + + return df; + } + + public static SnakeYAMLDataFormat createClassTagDataFormat(Class<?> type, Tag tag) { + SnakeYAMLDataFormat df = new SnakeYAMLDataFormat(type); + df.addTag(type, tag); + + return df; + } + + public static void marshalAndUnmarshal( + CamelContext context, Object body, String mockName, String directIn, String directBack, String expected) throws Exception { + + MockEndpoint mock = context.getEndpoint(mockName, MockEndpoint.class); + assertNotNull(mock); + + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(body.getClass()); + mock.message(0).body().equals(body); + + ProducerTemplate template = context.createProducerTemplate(); + String result = template.requestBody(directIn, body, String.class); + assertNotNull(result); + assertEquals(expected, result.trim()); + + template.sendBody(directBack, result); + + mock.assertIsSatisfied(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.java ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.java b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.java new file mode 100644 index 0000000..a80b4fd --- /dev/null +++ b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.java @@ -0,0 +1,73 @@ +/** + * 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.snakeyaml; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.test.spring.CamelSpringTestSupport; +import org.junit.Test; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SnakeYAMLSpringMarshalTest extends CamelSpringTestSupport { + + @Test + public void testMarshalAndUnmarshalMap() throws Exception { + Map<String, String> in = new HashMap<>(); + in.put("name", "Camel"); + + SnakeYAMLMarshalTestHelper.marshalAndUnmarshal( + context(), + SnakeYAMLMarshalTestHelper.createTestMap(), + "mock:reverse", + "direct:in", + "direct:back", + "{name: Camel}" + ); + } + + @Test + public void testMarshalAndUnmarshalPojo() throws Exception { + SnakeYAMLMarshalTestHelper.marshalAndUnmarshal( + context(), + SnakeYAMLMarshalTestHelper.createTestPojo(), + "mock:reversePojo", + "direct:inPojo", + "direct:backPojo", + "!!org.apache.camel.component.snakeyaml.model.TestPojo {name: Camel}" + ); + } + + @Test + public void testMarshalAndUnmarshalPojoWithPrettyFlow() throws Exception { + SnakeYAMLMarshalTestHelper.marshalAndUnmarshal( + context(), + SnakeYAMLMarshalTestHelper.createTestPojo(), + "mock:reversePojoWithPrettyFlow", + "direct:inPojoWithPrettyFlow", + "direct:backPojoWithPrettyFlow", + "!!org.apache.camel.component.snakeyaml.model.TestPojo {\n name: Camel\n}" + ); + } + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/model/TestPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/model/TestPojo.java b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/model/TestPojo.java new file mode 100644 index 0000000..c62c929 --- /dev/null +++ b/components/camel-snakeyaml/src/test/java/org/apache/camel/component/snakeyaml/model/TestPojo.java @@ -0,0 +1,52 @@ +/** + * 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.snakeyaml.model; + +public class TestPojo { + private String name; + + public TestPojo() { + } + + public TestPojo(String name) { + this.name = 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 "TestPojo[" + name + "]"; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/resources/log4j.properties b/components/camel-snakeyaml/src/test/resources/log4j.properties new file mode 100644 index 0000000..52724cb --- /dev/null +++ b/components/camel-snakeyaml/src/test/resources/log4j.properties @@ -0,0 +1,36 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +# +# The logging properties used during tests.. +# +log4j.rootLogger=INFO, file + +# uncomment this to turn on debug of camel +#log4j.logger.org.apache.camel=DEBUG + +# CONSOLE appender not used by default +log4j.appender.out=org.apache.log4j.ConsoleAppender +log4j.appender.out.layout=org.apache.log4j.PatternLayout +log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + +# File appender +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n +log4j.appender.file.file=target/camel-snakeyaml-test.log +log4j.appender.file.append=true http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/camel-snakeyaml/src/test/resources/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-snakeyaml/src/test/resources/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.xml b/components/camel-snakeyaml/src/test/resources/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.xml new file mode 100644 index 0000000..78210ff --- /dev/null +++ b/components/camel-snakeyaml/src/test/resources/org/apache/camel/component/snakeyaml/SnakeYAMLSpringMarshalTest.xml @@ -0,0 +1,76 @@ +<?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. + --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + + <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> + + <!-- we define the yaml SnakeYAML data formats to be used --> + <dataFormats> + <yaml id="yaml" + library="SnakeYAML"/> + + <yaml id="yaml-pojo" + library="SnakeYAML" + unmarshalTypeName="org.apache.camel.component.snakeyaml.model.TestPojo"/> + + <yaml id="yaml-pojo-pretty-flow" + library="SnakeYAML" + unmarshalTypeName="org.apache.camel.component.snakeyaml.model.TestPojo" + prettyFlow="true"/> + </dataFormats> + + + <route> + <from uri="direct:in"/> + <marshal ref="yaml"/> + </route> + + <route> + <from uri="direct:back"/> + <unmarshal ref="yaml"/> + <to uri="mock:reverse"/> + </route> + + <route> + <from uri="direct:inPojo"/> + <marshal ref="yaml-pojo"/> + </route> + + <route> + <from uri="direct:backPojo"/> + <unmarshal ref="yaml-pojo"/> + <to uri="mock:reversePojo"/> + </route> + + <route> + <from uri="direct:inPojoWithPrettyFlow"/> + <marshal ref="yaml-pojo-pretty-flow"/> + </route> + + <route> + <from uri="direct:backPojoWithPrettyFlow"/> + <unmarshal ref="yaml-pojo-pretty-flow"/> + <to uri="mock:reversePojoWithPrettyFlow"/> + </route> + + </camelContext> +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/components/pom.xml ---------------------------------------------------------------------- diff --git a/components/pom.xml b/components/pom.xml index e14d134..80c11ca 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -213,6 +213,7 @@ <module>camel-shiro</module> <module>camel-sip</module> <module>camel-smpp</module> + <module>camel-snakeyaml</module> <module>camel-snmp</module> <module>camel-sjms</module> <module>camel-slack</module> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/parent/pom.xml ---------------------------------------------------------------------- diff --git a/parent/pom.xml b/parent/pom.xml index 330f0b2..3c42c1a 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -482,7 +482,7 @@ <slf4j-version>1.7.13</slf4j-version> <smack-bundle-version>4.0.6</smack-bundle-version> <smack-version>4.0.7</smack-version> - <snakeyaml-version>1.16</snakeyaml-version> + <snakeyaml-version>1.17</snakeyaml-version> <snappy-version>1.1.2.1</snappy-version> <snmp4j-version>2.3.4_1</snmp4j-version> <solr-bundle-version>5.3.1_1</solr-bundle-version> @@ -1540,6 +1540,11 @@ </dependency> <dependency> <groupId>org.apache.camel</groupId> + <artifactId>camel-snakeyaml</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> <artifactId>camel-snmp</artifactId> <version>${project.version}</version> </dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/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 573faa3..64651f2 100644 --- a/platforms/karaf/features/src/main/resources/features.xml +++ b/platforms/karaf/features/src/main/resources/features.xml @@ -1433,6 +1433,11 @@ <bundle dependency='true'>mvn:commons-codec/commons-codec/${commons-codec-version}</bundle> <bundle>mvn:org.apache.camel/camel-smpp/${project.version}</bundle> </feature> + <feature name='camel-snakeyaml' version='${project.version}' resolver='(obr)' start-level='50'> + <feature version='${project.version}'>camel-core</feature> + <bundle dependency='true'>mvn:org.yaml/snakeyaml/${snakeyaml-version}</bundle> + <bundle>mvn:org.apache.camel/camel-snakeyaml/${project.version}</bundle> + </feature> <feature name='camel-snmp' version='${project.version}' resolver='(obr)' start-level='50'> <feature version='${project.version}'>camel-core</feature> <bundle dependency='true'>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.snmp4j/${snmp4j-version}</bundle> http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/tests/camel-itest-karaf/src/test/java/org/apache/camel/itest/karaf/CamelSnakeyamlTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest-karaf/src/test/java/org/apache/camel/itest/karaf/CamelSnakeyamlTest.java b/tests/camel-itest-karaf/src/test/java/org/apache/camel/itest/karaf/CamelSnakeyamlTest.java new file mode 100644 index 0000000..2a8c416 --- /dev/null +++ b/tests/camel-itest-karaf/src/test/java/org/apache/camel/itest/karaf/CamelSnakeyamlTest.java @@ -0,0 +1,40 @@ +/** + * 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.itest.karaf; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Configuration; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; + +@RunWith(PaxExam.class) +public class CamelSnakeyamlTest extends AbstractFeatureTest { + + public static final String COMPONENT = extractName(CamelSnakeyamlTest.class); + + @Test + public void test() throws Exception { + testComponent(COMPONENT); + } + + @Configuration + public static Option[] configure() { + return configure(COMPONENT); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/53600cfa/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java ---------------------------------------------------------------------- diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java index b1301e7..9f88702 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageDataFormatMojo.java @@ -315,6 +315,8 @@ public class PackageDataFormatMojo extends AbstractMojo { } else if ("zipfile".equals(name)) { // darn should have been lower case return "zipFile"; + } else if ("yaml-snakeyaml".equals(name)) { + return "yaml"; } return name; } @@ -333,6 +335,8 @@ public class PackageDataFormatMojo extends AbstractMojo { return "Bindy Fixed Length"; } else if ("bindy-kvp".equals(name)) { return "Bindy Key Value Pair"; + } else if ("yaml-snakeyaml".equals(name)) { + return "YAML SnakeYAML"; } return title; }