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

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


The following commit(s) were added to refs/heads/camel-2.21.x by this push:
     new c3eeb98  CAMEL-12532: Content Based Router in Java DSL may not resolve 
property placeholders in when predicates. Thanks to Tom Donohue for sample test
c3eeb98 is described below

commit c3eeb98b2d6f684b8dda631b60a56576faae34e3
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed May 23 20:50:37 2018 +0200

    CAMEL-12532: Content Based Router in Java DSL may not resolve property 
placeholders in when predicates. Thanks to Tom Donohue for sample test
---
 .../org/apache/camel/builder/BuilderSupport.java   |  10 +-
 .../org/apache/camel/builder/xml/XPathBuilder.java |   4 +-
 .../org/apache/camel/model/ChoiceDefinition.java   |  12 +++
 .../spring/boot/XPathPropertyPlaceholderTest.java  | 114 +++++++++++++++++++++
 4 files changed, 137 insertions(+), 3 deletions(-)

diff --git 
a/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java 
b/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
index ea81d2c..8753641 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
@@ -231,7 +231,7 @@ public abstract class BuilderSupport {
      * @return the builder
      */
     public XPathBuilder xpath(String value) {
-        return XPathBuilder.xpath(value);
+        return xpath(value, null);
     }
 
     /**
@@ -241,7 +241,13 @@ public abstract class BuilderSupport {
      * @param resultType the result type that the XPath expression will return.
      * @return the builder
      */
-    public static XPathBuilder xpath(String value, Class<?> resultType) {
+    public XPathBuilder xpath(String value, Class<?> resultType) {
+        // the value may contain property placeholders as it may be used 
directly from Java DSL
+        try {
+            value = getContext().resolvePropertyPlaceholders(value);
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
         return XPathBuilder.xpath(value, resultType);
     }
 
diff --git 
a/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java 
b/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
index 4b61f8f..c2e5d2b 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/xml/XPathBuilder.java
@@ -148,7 +148,9 @@ public class XPathBuilder extends ServiceSupport implements 
CamelContextAware, E
      */
     public static XPathBuilder xpath(String text, Class<?> resultType) {
         XPathBuilder builder = new XPathBuilder(text);
-        builder.setResultType(resultType);
+        if (resultType != null) {
+            builder.setResultType(resultType);
+        }
         return builder;
     }
 
diff --git 
a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java 
b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java
index d37058a..1d79309 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java
@@ -28,6 +28,7 @@ import javax.xml.bind.annotation.XmlRootElement;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.model.language.ExpressionDefinition;
 import org.apache.camel.processor.ChoiceProcessor;
 import org.apache.camel.processor.FilterProcessor;
 import org.apache.camel.spi.AsPredicate;
@@ -133,6 +134,17 @@ public class ChoiceDefinition extends 
ProcessorDefinition<ChoiceDefinition> {
     public Processor createProcessor(RouteContext routeContext) throws 
Exception {
         List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
         for (WhenDefinition whenClause : whenClauses) {
+            // also resolve properties and constant fields on embedded 
expressions in the when clauses
+            ExpressionNode exp = whenClause;
+            ExpressionDefinition expressionDefinition = exp.getExpression();
+            if (expressionDefinition != null) {
+                // resolve properties before we create the processor
+                
ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(),
 expressionDefinition);
+
+                // resolve constant fields (eg Exchange.FILE_NAME)
+                
ProcessorDefinitionHelper.resolveKnownConstantFields(expressionDefinition);
+            }
+
             FilterProcessor filter = (FilterProcessor) 
createProcessor(routeContext, whenClause);
             filters.add(filter);
         }
diff --git 
a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/XPathPropertyPlaceholderTest.java
 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/XPathPropertyPlaceholderTest.java
new file mode 100644
index 0000000..533ebbe
--- /dev/null
+++ 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/XPathPropertyPlaceholderTest.java
@@ -0,0 +1,114 @@
+/**
+ * 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.spring.boot;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@DirtiesContext
+@RunWith(SpringRunner.class)
+@EnableAutoConfiguration
+@SpringBootTest(
+    classes = XPathPropertyPlaceholderTest.TestConfig.class,
+    properties = {"foo = //greeting/text = 'Hello, world!'", "bar = 
//greeting/text = 'Bye, world!'"}
+)
+public class XPathPropertyPlaceholderTest {
+
+    @Autowired
+    private CamelContext context;
+
+    @Autowired
+    private ProducerTemplate template;
+
+    @EndpointInject(uri = "mock:output-filter")
+    private MockEndpoint mockOutputFilter;
+
+    @EndpointInject(uri = "mock:output-choice")
+    private MockEndpoint mockOutputChoice;
+
+    @EndpointInject(uri = "mock:output-choice2")
+    private MockEndpoint mockOutputChoice2;
+
+    @Test
+    public void testFilter() throws Exception {
+        mockOutputFilter.expectedMessageCount(1);
+
+        template.sendBody("direct:filter", "<greeting><text>Hello, 
world!</text></greeting>");
+
+        mockOutputFilter.assertIsSatisfied();
+    }
+
+    @Test
+    public void testChoice() throws Exception {
+        mockOutputChoice.expectedMessageCount(1);
+
+        template.sendBody("direct:choice", "<greeting><text>Bye, 
world!</text></greeting>");
+
+        mockOutputChoice.assertIsSatisfied();
+    }
+
+    @Test
+    public void testChoice2() throws Exception {
+        mockOutputChoice2.expectedMessageCount(1);
+
+        template.sendBody("direct:choice2", "<greeting><text>Bye, 
world!</text></greeting>");
+
+        mockOutputChoice2.assertIsSatisfied();
+    }
+
+    @Configuration
+    public static class TestConfig {
+        @Bean
+        public RouteBuilder routeBuilder() {
+            return new RouteBuilder() {
+                @Override
+                public void configure() throws Exception {
+                    from("direct:filter")
+                        .filter().xpath("{{foo}}")
+                            .log("Passed filter!")
+                            .to("mock:output-filter");
+
+                    from("direct:choice")
+                        .choice()
+                            .when(xpath("{{bar}}"))
+                                .log("Passed choice!")
+                                .to("mock:output-choice");
+
+                    from("direct:choice2")
+                        .choice()
+                            .when().xpath("{{bar}}")
+                                .log("Passed choice2!")
+                                .to("mock:output-choice2");
+                }
+            };
+        }
+    }
+
+}
+

-- 
To stop receiving notification emails like this one, please contact
davscl...@apache.org.

Reply via email to