CAMEL-6766: Fixed interceptFrom to support property placeholders.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f8dd754a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f8dd754a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f8dd754a Branch: refs/heads/camel-2.12.x Commit: f8dd754a3285b11bb7bf21deb6b9053612a3768d Parents: edde4da Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Sep 18 17:15:35 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Sep 18 17:15:49 2013 +0200 ---------------------------------------------------------------------- .../camel/model/RouteDefinitionHelper.java | 11 +++- .../org/apache/camel/util/EndpointHelper.java | 2 +- .../PropertiesComponentInterceptFromTest.java | 67 ++++++++++++++++++++ ...ingPropertiesComponentInterceptFromTest.java | 32 ++++++++++ ...ringPropertiesComponentInterceptFromTest.xml | 49 ++++++++++++++ 5 files changed, 159 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/f8dd754a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java index 5df829d..e669911 100644 --- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java +++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java @@ -348,6 +348,15 @@ public final class RouteDefinitionHelper { // should we only apply interceptor for a given endpoint uri boolean match = true; if (intercept.getUri() != null) { + + // the uri can have property placeholders so resolve them first + String pattern; + try { + pattern = context.resolvePropertyPlaceholders(intercept.getUri()); + } catch (Exception e) { + throw ObjectHelper.wrapRuntimeCamelException(e); + } + match = false; for (FromDefinition input : route.getInputs()) { // a bit more logic to lookup the endpoint as it can be uri/ref based @@ -359,7 +368,7 @@ public final class RouteDefinitionHelper { // lookup the endpoint to get its url uri = CamelContextHelper.getMandatoryEndpoint(context, "ref:" + input.getRef()).getEndpointUri(); } - if (EndpointHelper.matchEndpoint(context, uri, intercept.getUri())) { + if (EndpointHelper.matchEndpoint(context, uri, pattern)) { match = true; break; } http://git-wip-us.apache.org/repos/asf/camel/blob/f8dd754a/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java b/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java index effa70c..1151cdf 100644 --- a/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/EndpointHelper.java @@ -123,7 +123,7 @@ public final class EndpointHelper { } // we need to test with and without scheme separators (//) - if (uri.indexOf("://") != -1) { + if (uri.contains("://")) { // try without :// also String scheme = ObjectHelper.before(uri, "://"); String path = ObjectHelper.after(uri, "://"); http://git-wip-us.apache.org/repos/asf/camel/blob/f8dd754a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInterceptFromTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInterceptFromTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInterceptFromTest.java new file mode 100644 index 0000000..a49be89 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentInterceptFromTest.java @@ -0,0 +1,67 @@ +/** + * 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.properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.ResolveEndpointFailedException; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version + */ +public class PropertiesComponentInterceptFromTest extends ContextTestSupport { + + public void testPropertiesComponentInterceptFrom() throws Exception { + getMockEndpoint("mock:bar").expectedBodiesReceived("World"); + getMockEndpoint("mock:cool").expectedBodiesReceived("Bye Camel"); + + template.sendBody("direct:bar", "World"); + template.sendBody("direct:cool", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + PropertiesComponent pc = new PropertiesComponent(); + pc.setLocation("classpath:org/apache/camel/component/properties/myproperties.properties"); + context.addComponent("properties", pc); + + return context; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + interceptFrom("{{cool.start}}") + .transform().simple("Bye ${body}"); + + from("direct:cool") + .to("mock:cool"); + + from("direct:bar") + .to("mock:bar"); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/f8dd754a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.java new file mode 100644 index 0000000..f657817 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.java @@ -0,0 +1,32 @@ +/** + * 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.properties; + +import org.apache.camel.CamelContext; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +/** + * @version + */ +public class SpringPropertiesComponentInterceptFromTest extends PropertiesComponentInterceptFromTest { + + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.xml"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/f8dd754a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.xml new file mode 100644 index 0000000..ba334f1 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringPropertiesComponentInterceptFromTest.xml @@ -0,0 +1,49 @@ +<?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 + "> + + <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"> + <property name="location" value="classpath:org/apache/camel/component/properties/myproperties.properties"/> + </bean> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <interceptFrom uri="{{cool.start}}"> + <transform> + <simple>Bye ${body}</simple> + </transform> + </interceptFrom> + + <route> + <from uri="direct:cool"/> + <to uri="mock:cool"/> + </route> + + <route> + <from uri="direct:bar"/> + <to uri="mock:bar"/> + </route> + + </camelContext> + +</beans>