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 2018/08/28 08:02:05 UTC

[camel] branch master updated: CAMEL-12486: Placeholders are not resolved in Simple language while using resource: prefix. Thanks to Jan Bednar for unit test.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9930191  CAMEL-12486: Placeholders are not resolved in Simple language while using resource: prefix. Thanks to Jan Bednar for unit test.
9930191 is described below

commit 993019172b39b5c38fc8f8d3eef8b8e30ff2ae20
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 28 10:01:49 2018 +0200

    CAMEL-12486: Placeholders are not resolved in Simple language while using resource: prefix. Thanks to Jan Bednar for unit test.
---
 .../org/apache/camel/builder/SimpleBuilder.java    |  5 ++-
 .../java/org/apache/camel/util/ResourceHelper.java | 21 ++++++++++
 .../language/simple/SimpleResourceDynamicTest.java | 48 ++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java
index 678b63c..c1d5dd1 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/SimpleBuilder.java
@@ -19,6 +19,7 @@ package org.apache.camel.builder;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.language.Simple;
 import org.apache.camel.language.simple.SimpleLanguage;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ResourceHelper;
@@ -98,7 +99,7 @@ public class SimpleBuilder implements Predicate, Expression {
             // resolve property placeholders
             String resolve = exchange.getContext().resolvePropertyPlaceholders(text);
             // and optional it be refer to an external script on the file/classpath
-            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), resolve);
+            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), exchange, resolve);
             return simple.createPredicate(resolve);
         } catch (Exception e) {
             throw ObjectHelper.wrapCamelExecutionException(exchange, e);
@@ -111,7 +112,7 @@ public class SimpleBuilder implements Predicate, Expression {
             // resolve property placeholders
             String resolve = exchange.getContext().resolvePropertyPlaceholders(text);
             // and optional it be refer to an external script on the file/classpath
-            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), resolve);
+            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), exchange, resolve);
             return simple.createExpression(resolve, resultType);
         } catch (Exception e) {
             throw ObjectHelper.wrapCamelExecutionException(exchange, e);
diff --git a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
index 152f7db..1c9da1cc 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ResourceHelper.java
@@ -35,6 +35,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.language.simple.SimpleLanguage;
 import org.apache.camel.spi.ClassResolver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -58,6 +59,20 @@ public final class ResourceHelper {
      * If not then the returned value is returned as-is.
      */
     public static String resolveOptionalExternalScript(CamelContext camelContext, String expression) {
+        return resolveOptionalExternalScript(camelContext, null, expression);
+    }
+
+    /**
+     * Resolves the expression/predicate whether it refers to an external script on the file/classpath etc.
+     * This requires to use the prefix <tt>resource:</tt> such as <tt>resource:classpath:com/foo/myscript.groovy</tt>,
+     * <tt>resource:file:/var/myscript.groovy</tt>.
+     * <p/>
+     * If not then the returned value is returned as-is.
+     * <p/>
+     * If the exchange is provided (not null), then the external script can be referred via simple language for dynamic values, etc.
+     * <tt>resource:classpath:${header.myFileName}</tt>
+     */
+    public static String resolveOptionalExternalScript(CamelContext camelContext, Exchange exchange, String expression) {
         if (expression == null) {
             return null;
         }
@@ -75,6 +90,12 @@ public final class ResourceHelper {
             external = external.substring(9);
 
             if (hasScheme(external)) {
+
+                if (exchange != null && SimpleLanguage.hasSimpleFunction(external)) {
+                    SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple");
+                    external = simple.createExpression(external).evaluate(exchange, String.class);
+                }
+
                 InputStream is = null;
                 try {
                     is = resolveMandatoryResourceAsInputStream(camelContext, external);
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleResourceDynamicTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleResourceDynamicTest.java
new file mode 100644
index 0000000..29b074f
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleResourceDynamicTest.java
@@ -0,0 +1,48 @@
+/**
+ * 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.language.simple;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class SimpleResourceDynamicTest extends ContextTestSupport {
+
+    @Test
+    public void testSimpleResource() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("The name is Camel");
+
+        template.sendBodyAndHeader("direct:start", "Camel", "myFileName", "mysimple.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .transform().simple("resource:classpath:${header.myFileName}")
+                    .to("mock:result");
+            }
+        };
+    }
+}