You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bv...@apache.org on 2022/12/13 10:25:22 UTC

[camel] branch CAMEL-18809 updated: CAMEL-18809: RouteDefinitionHelper should resolve the intercepted from URI which is configured with property placeholder

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

bvahdat pushed a commit to branch CAMEL-18809
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/CAMEL-18809 by this push:
     new 66a441afe86 CAMEL-18809: RouteDefinitionHelper should resolve the intercepted from URI which is configured with property placeholder
66a441afe86 is described below

commit 66a441afe8683508d07af5e5497636c6a512651e
Author: Babak Vahdat <bv...@apache.org>
AuthorDate: Tue Dec 13 11:24:33 2022 +0100

    CAMEL-18809: RouteDefinitionHelper should resolve the intercepted from URI which is configured with property placeholder
---
 .../apache/camel/model/RouteDefinitionHelper.java  | 21 +++++-
 ...InterceptSendToEndpointWithPlaceholderTest.java | 86 ++++++++++++++++++++++
 2 files changed, 105 insertions(+), 2 deletions(-)

diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index f85ddaf10b4..90e21b986c0 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -25,6 +25,7 @@ import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
@@ -609,8 +610,24 @@ public final class RouteDefinitionHelper {
                             uri = CamelContextHelper.getMandatoryEndpoint(context, ref).getEndpointUri();
                         }
                     }
-                    if (EndpointHelper.matchEndpoint(context, uri, pattern)) {
-                        match = true;
+
+                    // the route input uri can have property placeholders, so set them
+                    // as local properties on PropertiesComponent to have them resolved
+                    Properties properties = null;
+                    if (route.getTemplateParameters() != null && !route.getTemplateParameters().isEmpty()) {
+                        properties = context.getTypeConverter().tryConvertTo(Properties.class, route.getTemplateParameters());
+                    }
+                    try {
+                        if (properties != null) {
+                            context.getPropertiesComponent().setLocalProperties(properties);
+                        }
+                        if (EndpointHelper.matchEndpoint(context, uri, pattern)) {
+                            match = true;
+                        }
+                    } finally {
+                        if (properties != null) {
+                            context.getPropertiesComponent().setLocalProperties(null);
+                        }
                     }
                 }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateInterceptFromAndInterceptSendToEndpointWithPlaceholderTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateInterceptFromAndInterceptSendToEndpointWithPlaceholderTest.java
new file mode 100644
index 00000000000..a7743892c41
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateInterceptFromAndInterceptSendToEndpointWithPlaceholderTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.builder;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Route;
+import org.apache.camel.model.RouteConfigurationDefinition;
+import org.apache.camel.model.RouteTemplateDefinition;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RouteTemplateInterceptFromAndInterceptSendToEndpointWithPlaceholderTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testCreateRouteFromRouteTemplate() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("myTemplate").templateParameter("foo").templateParameter("bar")
+                        .from("direct:{{foo}}")
+                        .to("mock:{{bar}}");
+
+                RouteConfigurationDefinition routeConfigurationDefinition = new RouteConfigurationDefinition();
+                routeConfigurationDefinition.setCamelContext(context);
+
+                routeConfigurationDefinition.interceptFrom("direct:intercepted-from").to("mock:intercepted-from");
+
+                routeConfigurationDefinition.interceptSendToEndpoint("mock:intercepted-send").to("mock:intercepted-send-to-before")
+                        .afterUri("mock:intercepted-send-to-after");
+
+                context.addRouteConfiguration(routeConfigurationDefinition);
+            }
+        });
+
+        assertEquals(1, context.getRouteTemplateDefinitions().size());
+
+        RouteTemplateDefinition routeTemplate = context.getRouteTemplateDefinition("myTemplate");
+        assertEquals("foo", routeTemplate.getTemplateParameters().get(0).getName());
+        assertEquals("bar", routeTemplate.getTemplateParameters().get(1).getName());
+
+        for (String uriSuffix : new String[] { "from", "send-to-before", "send-to-after" }) {
+            getMockEndpoint("mock:intercepted-" + uriSuffix).expectedBodiesReceived("Hello Intercepted");
+        }
+
+        TemplatedRouteBuilder.builder(context, "myTemplate")
+                .routeId("intercepted")
+                .parameter("foo", "intercepted-from")
+                .parameter("bar", "intercepted-send")
+                .add();
+
+        // now start camel
+        context.start();
+
+        assertEquals(1, context.getRouteDefinitions().size());
+        assertEquals(1, context.getRoutes().size());
+        assertEquals("Started", context.getRouteController().getRouteStatus("intercepted").name());
+        assertEquals("true", context.getRoute("intercepted").getProperties().get(Route.TEMPLATE_PROPERTY));
+
+        template.sendBody("direct:intercepted-from", "Hello Intercepted");
+
+        assertMockEndpointsSatisfied();
+
+        context.stop();
+    }
+
+}