This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch df in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2c3c1762486b109fd0ad76ea85a4ef13076c1364 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Oct 28 16:33:19 2024 +0100 CAMEL-21391: Add support for <dataFormats> in xml-io DSL --- .../META-INF/org/apache/camel/model/app/beans.json | 13 ++++---- .../META-INF/org/apache/camel/model/app/camel.json | 13 ++++---- .../apache/camel/model/DataFormatDefinition.java | 16 +++++++++- .../apache/camel/model/app/BeansDefinition.java | 21 +++++++++++++ .../model/dataformat/DataFormatsDefinition.java | 17 ++++++++++- .../java/org/apache/camel/xml/in/ModelParser.java | 29 +++++++++++++++++- .../java/org/apache/camel/xml/out/ModelWriter.java | 3 +- .../java/org/apache/camel/xml/in/BaseParser.java | 4 +++ .../org/apache/camel/yaml/out/ModelWriter.java | 3 +- dsl/camel-xml-io-dsl/pom.xml | 5 ++++ .../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 16 ++++++++++ .../apache/camel/dsl/xml/io/XmlLoadAppTest.java | 23 ++++++++++++++ .../org/apache/camel/dsl/xml/io/camel-app14.xml | 35 ++++++++++++++++++++++ .../src/main/resources/velocity/model-parser.vm | 2 +- 14 files changed, 182 insertions(+), 18 deletions(-) diff --git a/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/beans.json b/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/beans.json index dea9b5905d1..142431336a4 100644 --- a/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/beans.json +++ b/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/beans.json @@ -14,11 +14,12 @@ "properties": { "component-scan": { "index": 0, "kind": "element", "displayName": "Component-scan", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.app.ComponentScanDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Component scanning that can auto-discover Camel route builders from the classpath." }, "bean": { "index": 1, "kind": "element", "displayName": "Bean", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.BeanFactoryDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "List of bean" }, - "restConfiguration": { "index": 2, "kind": "element", "displayName": "Rest Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL Configuration" }, - "rest": { "index": 3, "kind": "element", "displayName": "Rest", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL" }, - "routeConfiguration": { "index": 4, "kind": "element", "displayName": "Route Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route configurations" }, - "routeTemplate": { "index": 5, "kind": "element", "displayName": "Route Template", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteTemplateDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route templates" }, - "templatedRoute": { "index": 6, "kind": "element", "displayName": "Templated Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.TemplatedRouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes to be created from template" }, - "route": { "index": 7, "kind": "element", "displayName": "Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes" } + "dataFormat": { "index": 2, "kind": "element", "displayName": "Data Format", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.DataFormatDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel data formats" }, + "restConfiguration": { "index": 3, "kind": "element", "displayName": "Rest Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL Configuration" }, + "rest": { "index": 4, "kind": "element", "displayName": "Rest", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL" }, + "routeConfiguration": { "index": 5, "kind": "element", "displayName": "Route Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route configurations" }, + "routeTemplate": { "index": 6, "kind": "element", "displayName": "Route Template", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteTemplateDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route templates" }, + "templatedRoute": { "index": 7, "kind": "element", "displayName": "Templated Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.TemplatedRouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes to be created from template" }, + "route": { "index": 8, "kind": "element", "displayName": "Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes" } } } diff --git a/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/camel.json b/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/camel.json index 0e9631c8ed8..6d14ff17da6 100644 --- a/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/camel.json +++ b/core/camel-core-model/src/generated/resources/META-INF/org/apache/camel/model/app/camel.json @@ -14,11 +14,12 @@ "properties": { "component-scan": { "index": 0, "kind": "element", "displayName": "Component-scan", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.app.ComponentScanDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Component scanning that can auto-discover Camel route builders from the classpath." }, "bean": { "index": 1, "kind": "element", "displayName": "Bean", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.BeanFactoryDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "List of bean" }, - "restConfiguration": { "index": 2, "kind": "element", "displayName": "Rest Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL Configuration" }, - "rest": { "index": 3, "kind": "element", "displayName": "Rest", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL" }, - "routeConfiguration": { "index": 4, "kind": "element", "displayName": "Route Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route configurations" }, - "routeTemplate": { "index": 5, "kind": "element", "displayName": "Route Template", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteTemplateDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route templates" }, - "templatedRoute": { "index": 6, "kind": "element", "displayName": "Templated Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.TemplatedRouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes to be created from template" }, - "route": { "index": 7, "kind": "element", "displayName": "Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes" } + "dataFormat": { "index": 2, "kind": "element", "displayName": "Data Format", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.DataFormatDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel data formats" }, + "restConfiguration": { "index": 3, "kind": "element", "displayName": "Rest Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL Configuration" }, + "rest": { "index": 4, "kind": "element", "displayName": "Rest", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.rest.RestDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel Rest DSL" }, + "routeConfiguration": { "index": 5, "kind": "element", "displayName": "Route Configuration", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteConfigurationDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route configurations" }, + "routeTemplate": { "index": 6, "kind": "element", "displayName": "Route Template", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteTemplateDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel route templates" }, + "templatedRoute": { "index": 7, "kind": "element", "displayName": "Templated Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.TemplatedRouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes to be created from template" }, + "route": { "index": 8, "kind": "element", "displayName": "Route", "group": "common", "required": false, "type": "array", "javaType": "java.util.List<org.apache.camel.model.RouteDefinition>", "deprecated": false, "autowired": false, "secret": false, "description": "Camel routes" } } } diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/DataFormatDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/DataFormatDefinition.java index 503de71cf7e..7fe33797441 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/DataFormatDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/DataFormatDefinition.java @@ -23,6 +23,8 @@ import jakarta.xml.bind.annotation.XmlType; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.Resource; +import org.apache.camel.spi.ResourceAware; /** * Represents a Camel data format @@ -30,12 +32,14 @@ import org.apache.camel.spi.Metadata; @Metadata(label = "dataformat,transformation") @XmlType(name = "dataFormat") @XmlAccessorType(XmlAccessType.FIELD) -public class DataFormatDefinition extends IdentifiedType implements CopyableDefinition<DataFormatDefinition> { +public class DataFormatDefinition extends IdentifiedType implements CopyableDefinition<DataFormatDefinition>, ResourceAware { @XmlTransient private DataFormat dataFormat; @XmlTransient private String dataFormatName; + @XmlTransient + private Resource resource; public DataFormatDefinition() { } @@ -73,6 +77,16 @@ public class DataFormatDefinition extends IdentifiedType implements CopyableDefi this.dataFormat = dataFormat; } + @Override + public Resource getResource() { + return resource; + } + + @Override + public void setResource(Resource resource) { + this.resource = resource; + } + public String getShortName() { String name = getClass().getSimpleName(); if (name.endsWith("DataFormat")) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/app/BeansDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/app/BeansDefinition.java index 3c5688b08a7..ae415575df7 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/app/BeansDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/app/BeansDefinition.java @@ -23,9 +23,14 @@ import jakarta.xml.bind.annotation.XmlAccessType; import jakarta.xml.bind.annotation.XmlAccessorType; import jakarta.xml.bind.annotation.XmlAnyElement; import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlElementRef; +import jakarta.xml.bind.annotation.XmlElementWrapper; import jakarta.xml.bind.annotation.XmlRootElement; import jakarta.xml.bind.annotation.XmlType; +import org.apache.camel.model.DataFormatDefinition; +import org.apache.camel.model.dataformat.DataFormatsDefinition; +import org.apache.camel.spi.annotations.DslProperty; import org.w3c.dom.Element; import org.apache.camel.model.BeanFactoryDefinition; @@ -51,6 +56,7 @@ import org.apache.camel.spi.annotations.ExternalSchemaElement; "beans", "springBeans", "blueprintBeans", + "dataFormats", "restConfigurations", "rests", "routeConfigurations", @@ -96,6 +102,10 @@ public class BeansDefinition { // initially we'll be supporting only these elements which are parsed by // org.apache.camel.dsl.xml.io.XmlRoutesBuilderLoader in camel-xml-io-dsl + @XmlElementWrapper(name = "dataFormats") + @XmlElement(name = "dataFormat") + @DslProperty(name = "dataFormats") // yaml-dsl + private List<DataFormatDefinition> dataFormats; @XmlElement(name = "restConfiguration") private List<RestConfigurationDefinition> restConfigurations = new ArrayList<>(); @XmlElement(name = "rest") @@ -219,4 +229,15 @@ public class BeansDefinition { this.routes = routes; } + public List<DataFormatDefinition> getDataFormats() { + return dataFormats; + } + + /** + * Camel data formats + */ + public void setDataFormats(List<DataFormatDefinition> dataFormats) { + this.dataFormats = dataFormats; + } + } diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java index e129a74a6a9..a250787d0a3 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java @@ -26,10 +26,13 @@ import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlElements; import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlTransient; import org.apache.camel.model.CopyableDefinition; import org.apache.camel.model.DataFormatDefinition; import org.apache.camel.model.ProcessorDefinitionHelper; import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.Resource; +import org.apache.camel.spi.ResourceAware; /** * Configure data formats. @@ -37,7 +40,7 @@ import org.apache.camel.spi.Metadata; @Metadata(label = "dataformat,transformation", title = "Data formats") @XmlRootElement(name = "dataFormats") @XmlAccessorType(XmlAccessType.FIELD) -public class DataFormatsDefinition implements CopyableDefinition<DataFormatsDefinition> { +public class DataFormatsDefinition implements CopyableDefinition<DataFormatsDefinition>, ResourceAware { // cannot use @XmlElementRef as it doesn't allow optional properties @XmlElements({ @@ -83,6 +86,8 @@ public class DataFormatsDefinition implements CopyableDefinition<DataFormatsDefi @XmlElement(name = "zipDeflater", type = ZipDeflaterDataFormat.class), @XmlElement(name = "zipFile", type = ZipFileDataFormat.class) }) private List<DataFormatDefinition> dataFormats; + @XmlTransient + private Resource resource; public DataFormatsDefinition() { } @@ -117,4 +122,14 @@ public class DataFormatsDefinition implements CopyableDefinition<DataFormatsDefi } return dataFormatsAsMap; } + + @Override + public Resource getResource() { + return resource; + } + + @Override + public void setResource(Resource resource) { + this.resource = resource; + } } diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java index 479084a27a3..e6287cec5ba 100644 --- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java +++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/in/ModelParser.java @@ -1340,7 +1340,13 @@ public class ModelParser extends BaseParser { case "routeTemplate": doAdd(doParseRouteTemplateDefinition(), def.getRouteTemplates(), def::setRouteTemplates); break; case "route": doAdd(doParseRouteDefinition(), def.getRoutes(), def::setRoutes); break; case "templatedRoute": doAdd(doParseTemplatedRouteDefinition(), def.getTemplatedRoutes(), def::setTemplatedRoutes); break; - default: return false; + default: + DataFormatDefinition v = doParseDataFormatDefinitionRef(key); + if (v != null) { + doAdd(v, def.getDataFormats(), def::setDataFormats); + return true; + } + return false; } return true; }; @@ -1798,6 +1804,27 @@ public class ModelParser extends BaseParser { return false; }, noValueHandler()); } + public Optional<DataFormatsDefinition> parseDataFormatsDefinition() throws IOException, XmlPullParserException { + String tag = getNextTag("dataFormats", "dataFormat"); + if (tag != null) { + switch (tag) { + case "dataFormats" : return Optional.of(doParseDataFormatsDefinition()); + case "dataFormat" : return parseSingleDataFormatsDefinition(); + } + } + return Optional.empty(); + } + private Optional<DataFormatsDefinition> parseSingleDataFormatsDefinition() throws IOException, XmlPullParserException { + Optional<DataFormatDefinition> single = Optional.of(doParseDataFormatDefinition()); + if (single.isPresent()) { + List<DataFormatDefinition> list = new ArrayList<>(); + list.add(single.get()); + DataFormatsDefinition def = new DataFormatsDefinition(); + def.setDataFormats(list); + return Optional.of(def); + } + return Optional.empty(); + } protected FhirJsonDataFormat doParseFhirJsonDataFormat() throws IOException, XmlPullParserException { return doParse(new FhirJsonDataFormat(), fhirDataformatAttributeHandler(), noElementHandler(), noValueHandler()); } diff --git a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java index ce5841617db..33d5cd85f14 100644 --- a/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java +++ b/core/camel-xml-io/src/generated/java/org/apache/camel/xml/out/ModelWriter.java @@ -1996,9 +1996,10 @@ public class ModelWriter extends BaseWriter { protected void doWriteBeansDefinitionElements(BeansDefinition def) throws IOException { doWriteList(null, "route", def.getRoutes(), this::doWriteRouteDefinition); domElements(def.getSpringBeans()); + doWriteList("dataFormats", "dataFormat", def.getDataFormats(), this::doWriteDataFormatDefinition); domElements(def.getBlueprintBeans()); - doWriteList(null, "component-scan", def.getComponentScanning(), this::doWriteComponentScanDefinition); doWriteList(null, "bean", def.getBeans(), this::doWriteBeanFactoryDefinition); + doWriteList(null, "component-scan", def.getComponentScanning(), this::doWriteComponentScanDefinition); doWriteList(null, "restConfiguration", def.getRestConfigurations(), this::doWriteRestConfigurationDefinition); doWriteList(null, "rest", def.getRests(), this::doWriteRestDefinition); doWriteList(null, "routeConfiguration", def.getRouteConfigurations(), this::doWriteRouteConfigurationDefinition); diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java index 5bd3053732d..4b338253f06 100644 --- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java +++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java @@ -359,6 +359,10 @@ public class BaseParser { } protected boolean ignoreUnexpectedElement(String namespace, String name) throws XmlPullParserException { + // special for dataFormats (wrapper) + if ("dataFormats".equals(name)) { + return true; + } return false; } diff --git a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java index 75466431c09..0f85e80d355 100644 --- a/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java +++ b/core/camel-yaml-io/src/generated/java/org/apache/camel/yaml/out/ModelWriter.java @@ -1996,9 +1996,10 @@ public class ModelWriter extends BaseWriter { protected void doWriteBeansDefinitionElements(BeansDefinition def) throws IOException { doWriteList(null, "route", def.getRoutes(), this::doWriteRouteDefinition); domElements(def.getSpringBeans()); + doWriteList("dataFormats", "dataFormat", def.getDataFormats(), this::doWriteDataFormatDefinition); domElements(def.getBlueprintBeans()); - doWriteList(null, "component-scan", def.getComponentScanning(), this::doWriteComponentScanDefinition); doWriteList(null, "bean", def.getBeans(), this::doWriteBeanFactoryDefinition); + doWriteList(null, "component-scan", def.getComponentScanning(), this::doWriteComponentScanDefinition); doWriteList(null, "restConfiguration", def.getRestConfigurations(), this::doWriteRestConfigurationDefinition); doWriteList(null, "rest", def.getRests(), this::doWriteRestDefinition); doWriteList(null, "routeConfiguration", def.getRouteConfigurations(), this::doWriteRouteConfigurationDefinition); diff --git a/dsl/camel-xml-io-dsl/pom.xml b/dsl/camel-xml-io-dsl/pom.xml index 0ec4e0f9a8b..56cc9fd0ffc 100644 --- a/dsl/camel-xml-io-dsl/pom.xml +++ b/dsl/camel-xml-io-dsl/pom.xml @@ -114,6 +114,11 @@ <artifactId>camel-groovy</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-base64</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.assertj</groupId> diff --git a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java index 95a5292a31f..7085bdaf928 100644 --- a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java +++ b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java @@ -44,6 +44,7 @@ import org.apache.camel.model.RoutesDefinition; import org.apache.camel.model.TemplatedRouteDefinition; import org.apache.camel.model.TemplatedRoutesDefinition; import org.apache.camel.model.app.BeansDefinition; +import org.apache.camel.model.dataformat.DataFormatsDefinition; import org.apache.camel.model.rest.RestConfigurationDefinition; import org.apache.camel.model.rest.RestDefinition; import org.apache.camel.model.rest.RestsDefinition; @@ -128,6 +129,9 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport { .ifPresent(this::configureCamel); } } + case "dataFormats", "dataFormat" -> new XmlModelParser(resource, xmlInfo.getRootElementNamespace()) + .parseDataFormatsDefinition() + .ifPresent(this::addDataFormats); case "routeTemplate", "routeTemplates" -> new XmlModelParser(resource, xmlInfo.getRootElementNamespace()) .parseRouteTemplatesDefinition() @@ -208,6 +212,12 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport { } } + if (app.getDataFormats() != null) { + DataFormatsDefinition list = new DataFormatsDefinition(); + list.setDataFormats(app.getDataFormats()); + addDataFormats(list); + } + app.getRests().forEach(r -> { r.setResource(getResource()); List<RestDefinition> list = new ArrayList<>(); @@ -278,6 +288,12 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport { getRouteConfigurationCollection().routeConfiguration(config); } } + + private void addDataFormats(DataFormatsDefinition dataFormats) { + Model model = getCamelContext().getCamelContextExtension().getContextPlugin(Model.class); + dataFormats.getDataFormats().forEach(d -> d.setResource(getResource())); + model.setDataFormats(dataFormats.asMap()); + } }; } diff --git a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java index 114f538344b..1065980179e 100644 --- a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java +++ b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java @@ -29,6 +29,7 @@ import org.apache.camel.spi.Resource; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RoutesLoader; import org.apache.camel.support.PluginHelper; +import org.apache.commons.codec.binary.Base64; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -361,4 +362,26 @@ public class XmlLoadAppTest { bar.assertIsSatisfied(); } } + + @Test + public void testLoadAppWithDataFormat() throws Exception { + try (DefaultCamelContext context = new DefaultCamelContext()) { + context.start(); + + // camel-app13 has a route configuration and a route using the configuration + Resource resource = PluginHelper.getResourceLoader(context).resolveResource( + "/org/apache/camel/dsl/xml/io/camel-app14.xml"); + + PluginHelper.getRoutesLoader(context).loadRoutes(resource); + + // test that loaded route works + MockEndpoint bar = context.getEndpoint("mock:result", MockEndpoint.class); + + Base64 codec = new Base64(40, new byte[] { '\r', '\n' }, true); + byte[] encoded = codec.encode("Hi World".getBytes()); + bar.expectedBodiesReceived(encoded); + context.createProducerTemplate().sendBody("direct:start", "Hi World"); + bar.assertIsSatisfied(); + } + } } diff --git a/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app14.xml b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app14.xml new file mode 100644 index 00000000000..55d0c5aa986 --- /dev/null +++ b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app14.xml @@ -0,0 +1,35 @@ +<?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. + +--> +<camel xmlns:s="http://www.springframework.org/schema/beans"> + + <!-- embed Camel with routes --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <dataFormats> + <base64 id="myBase" lineLength="40" urlSafe="true"/> + </dataFormats> + + <route> + <from uri="direct:start"/> + <marshal><custom ref="myBase"/></marshal> + <to uri="mock:result"/> + </route> + </camelContext> + +</camel> \ No newline at end of file diff --git a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-parser.vm b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-parser.vm index f37eab1ae47..381e05b05b2 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-parser.vm +++ b/tooling/maven/camel-package-maven-plugin/src/main/resources/velocity/model-parser.vm @@ -447,7 +447,7 @@ ${indent} }## return Optional.empty(); } #elseif( $name == "RoutesDefinition" || $name == "RouteTemplatesDefinition" || $name == "TemplatedRoutesDefinition" - || $name == "RestsDefinition" || $name == "RouteConfigurationsDefinition" ) + || $name == "RestsDefinition" || $name == "RouteConfigurationsDefinition" || $name == "DataFormatsDefinition" ) #set( $element = $clazz.xmlRootElement.name() ) #set( $capitalElement = $mojo.uppercase($element) ) #if( $element.endsWith("s") )