You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/08/21 10:26:49 UTC

[camel] branch camel-3.11.x updated: CAMEL-16873: dumpModelToXml now resolves template parameters with their real values when dumping

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-3.11.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.11.x by this push:
     new 24cc97e  CAMEL-16873: dumpModelToXml now resolves template parameters with their real values when dumping
24cc97e is described below

commit 24cc97e3db20fa340cb0e6bec64417d0d6061805
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Aug 20 09:10:27 2021 +0200

    CAMEL-16873: dumpModelToXml now resolves template parameters with their real values when dumping
---
 .../util/DumpModelAsXmlFromRouteTemplateTest.java  | 141 +++++++++++++++++++++
 .../camel/xml/jaxb/JaxbModelToXMLDumper.java       |  15 +++
 2 files changed, 156 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlFromRouteTemplateTest.java b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlFromRouteTemplateTest.java
new file mode 100644
index 0000000..e8d46e5
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/util/DumpModelAsXmlFromRouteTemplateTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.converter.jaxp.XmlConverter;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ *
+ */
+public class DumpModelAsXmlFromRouteTemplateTest extends ContextTestSupport {
+
+    @Test
+    public void testDumpModelAsXml() throws Exception {
+        Map<String, Object> map = new HashMap<>();
+        map.put("bar", "start");
+        map.put("greeting", "Hello");
+        map.put("whereto", "Moes");
+        context.addRouteFromTemplate("foo", "myTemplate", map);
+
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        String xml = ecc.getModelToXMLDumper().dumpModelAsXml(context, context.getRouteDefinition("foo"));
+        assertNotNull(xml);
+        log.info(xml);
+
+        Document doc = new XmlConverter().toDOMDocument(xml, null);
+        NodeList nodes = doc.getElementsByTagName("simple");
+        assertEquals(1, nodes.getLength());
+        Element node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <simple> expected to be instanceof Element");
+        assertEquals("{{greeting}}", node.getTextContent());
+
+        nodes = doc.getElementsByTagName("to");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <to> expected to be instanceof Element");
+        assertEquals("mock:{{whereto}}", node.getAttribute("uri"));
+
+        nodes = doc.getElementsByTagName("route");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertEquals("foo", node.getAttribute("id"));
+    }
+
+    @Test
+    public void testDumpModelAsXmlResolvePlaceholder() throws Exception {
+        Map<String, Object> map = new HashMap<>();
+        map.put("bar", "start");
+        map.put("greeting", "Hello");
+        map.put("whereto", "Moes");
+        context.addRouteFromTemplate("bar", "myTemplate", map);
+        map.clear();
+        map.put("bar", "start2");
+        map.put("greeting", "Bye");
+        map.put("whereto", "Jacks");
+        context.addRouteFromTemplate("bar2", "myTemplate", map);
+
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        String xml = ecc.getModelToXMLDumper().dumpModelAsXml(context, context.getRouteDefinition("bar"), true, false);
+        assertNotNull(xml);
+        log.info(xml);
+
+        Document doc = new XmlConverter().toDOMDocument(xml, null);
+        NodeList nodes = doc.getElementsByTagName("simple");
+        assertEquals(1, nodes.getLength());
+        Element node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <simple> expected to be instanceof Element");
+        assertEquals("Hello", node.getTextContent());
+
+        nodes = doc.getElementsByTagName("to");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <to> expected to be instanceof Element");
+        assertEquals("mock:Moes", node.getAttribute("uri"));
+
+        nodes = doc.getElementsByTagName("route");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertEquals("bar", node.getAttribute("id"));
+
+        xml = ecc.getModelToXMLDumper().dumpModelAsXml(context, context.getRouteDefinition("bar2"), true, false);
+        assertNotNull(xml);
+        log.info(xml);
+
+        doc = new XmlConverter().toDOMDocument(xml, null);
+        nodes = doc.getElementsByTagName("simple");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <simple> expected to be instanceof Element");
+        assertEquals("Bye", node.getTextContent());
+
+        nodes = doc.getElementsByTagName("to");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertNotNull(node, "Node <to> expected to be instanceof Element");
+        assertEquals("mock:Jacks", node.getAttribute("uri"));
+
+        nodes = doc.getElementsByTagName("route");
+        assertEquals(1, nodes.getLength());
+        node = (Element) nodes.item(0);
+        assertEquals("bar2", node.getAttribute("id"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("myTemplate").templateParameter("bar").templateParameter("greeting").templateParameter("whereto")
+                        .from("direct:{{bar}}").transform(simple("{{greeting}}")).to("mock:{{whereto}}");
+            }
+        };
+    }
+}
diff --git a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
index 9280a87..f21fc98 100644
--- a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
+++ b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
@@ -47,6 +47,7 @@ import org.apache.camel.model.RouteTemplateDefinition;
 import org.apache.camel.model.RouteTemplatesDefinition;
 import org.apache.camel.model.RoutesDefinition;
 import org.apache.camel.spi.ModelToXMLDumper;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.annotations.JdkService;
 import org.apache.camel.util.xml.XmlLineNumberParser;
 
@@ -156,10 +157,24 @@ public class JaxbModelToXMLDumper implements ModelToXMLDumper {
                     }
 
                     if (resolvePlaceholders) {
+                        PropertiesComponent pc = context.getPropertiesComponent();
+                        if (definition instanceof RouteDefinition) {
+                            RouteDefinition routeDefinition = (RouteDefinition) definition;
+                            // if the route definition was created via a route template then we need to prepare its parameters when the route is being created and started
+                            if (routeDefinition.isTemplate() != null && routeDefinition.isTemplate()
+                                    && routeDefinition.getTemplateParameters() != null) {
+                                Properties prop = new Properties();
+                                prop.putAll(routeDefinition.getTemplateParameters());
+                                pc.setLocalProperties(prop);
+                            }
+                        }
                         try {
                             after = context.resolvePropertyPlaceholders(after);
                         } catch (Exception e) {
                             // ignore
+                        } finally {
+                            // clear local after the route is dumped
+                            pc.setLocalProperties(null);
                         }
                     }