This is an automated email from the ASF dual-hosted git repository. bvahdat pushed a commit to branch camel-3.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.18.x by this push: new b76a40b81c3 CAMEL-18809: RouteDefinitionHelper should resolve the intercepted from URI which is configured with property placeholder (#8888) b76a40b81c3 is described below commit b76a40b81c35c154181d574a18c21f32eaf7b225 Author: Babak Vahdat <bvah...@apache.org> AuthorDate: Tue Dec 13 17:13:27 2022 +0100 CAMEL-18809: RouteDefinitionHelper should resolve the intercepted from URI which is configured with property placeholder (#8888) --- .../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 0e27dc6af8a..44cd6ac99e1 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; @@ -594,8 +595,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..8a37545d6bc --- /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 java.util.Arrays; + +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.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 : Arrays.asList("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(); + } + +}