[camel] branch master updated: CAMEL-15087: Fixed toD to resolve property placeholders in dynamic uris.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-15087: Fixed toD to resolve property placeholders in dynamic uris.

davsclaus-2
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 0e7553b  CAMEL-15087: Fixed toD to resolve property placeholders in dynamic uris.
0e7553b is described below

commit 0e7553bd74186b7926c0193589835799a70afd30
Author: Claus Ibsen <[hidden email]>
AuthorDate: Fri May 22 20:32:59 2020 +0200

    CAMEL-15087: Fixed toD to resolve property placeholders in dynamic uris.
---
 .../camel/processor/SendDynamicProcessor.java      |  7 +++
 .../org/apache/camel/reifier/ToDynamicReifier.java |  5 ++
 .../ToDynamicPropertyPlaceholderTest.java          | 71 ++++++++++++++++++++++
 3 files changed, 83 insertions(+)

diff --git a/core/camel-base/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java b/core/camel-base/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
index cd0137e..74a70cf 100644
--- a/core/camel-base/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
+++ b/core/camel-base/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.processor;
 
+import java.net.URISyntaxException;
+
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
@@ -260,6 +262,11 @@ public class SendDynamicProcessor extends AsyncProcessorSupport implements IdAwa
                 // convert to a string type we can work with
                 uri = ecc.getTypeConverter().mandatoryConvertTo(String.class, exchange, recipient);
             }
+            // make sure the uri has a scheme and a path
+            int colon = uri.indexOf(':');
+            if (colon == -1 || colon == uri.length() - 1) {
+                throw new ResolveEndpointFailedException(uri, "Endpoint should include scheme:path");
+            }
             // optimize and normalize endpoint
             return ecc.normalizeUri(uri);
         }
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java
index 9b115b5..310d019 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java
@@ -29,6 +29,7 @@ import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.model.ToDynamicDefinition;
 import org.apache.camel.processor.SendDynamicProcessor;
 import org.apache.camel.spi.Language;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.util.Pair;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
@@ -72,6 +73,9 @@ public class ToDynamicReifier<T extends ToDynamicDefinition> extends ProcessorRe
     protected Expression createExpression(String uri) {
         List<Expression> list = new ArrayList<>();
 
+        // make sure to parse property placeholders
+        uri = camelContext.resolvePropertyPlaceholders(uri);
+
         String[] parts = safeSplitRaw(uri);
         for (String part : parts) {
             // the part may have optional language to use, so you can mix
@@ -94,6 +98,7 @@ public class ToDynamicReifier<T extends ToDynamicDefinition> extends ProcessorRe
                     }
                 }
             }
+
             // fallback and use simple language
             Language lan = camelContext.resolveLanguage("simple");
             Expression exp = lan.createExpression(part);
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicPropertyPlaceholderTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicPropertyPlaceholderTest.java
new file mode 100644
index 0000000..7b9a3e8
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicPropertyPlaceholderTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.processor;
+
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class ToDynamicPropertyPlaceholderTest extends ContextTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        Properties prop = new Properties();
+        prop.setProperty("foo", "${header.myHeader}");
+        context.getPropertiesComponent().setInitialProperties(prop);
+
+        return context;
+    }
+
+    @Test
+    public void testToDynamic() throws Exception {
+        getMockEndpoint("mock:cheese").expectedBodiesReceived("Hello Camel");
+        getMockEndpoint("mock:cake").expectedBodiesReceived("Hello World");
+
+        template.sendBodyAndHeader("direct:start", "Hello Camel", "myHeader", "cheese");
+        template.sendBodyAndHeader("direct:start", "Hello World", "myHeader", "cake");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testToDynamicNoHeader() throws Exception {
+        try {
+            template.sendBody("direct:start", "Hello Camel");
+            fail("Should throw exception");
+        } catch (Exception e) {
+            ResolveEndpointFailedException ref = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            assertEquals("mock:", ref.getUri());
+        }
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").toD("mock:{{foo}}");
+            }
+        };
+    }
+}