CAMEL-11755: toD should ignore when dynamic uri is empty

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0034b993
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0034b993
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0034b993

Branch: refs/heads/camel-2.19.x
Commit: 0034b993e1d4070da0b06e260b390bf5390e40f2
Parents: 0bd2b5d
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Sep 6 16:40:42 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Sep 6 16:41:49 2017 +0200

----------------------------------------------------------------------
 .../camel/processor/SendDynamicProcessor.java   | 16 ++++++-
 .../camel/processor/ToDynamicEmptyTest.java     | 49 ++++++++++++++++++++
 2 files changed, 63 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0034b993/camel-core/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java 
b/camel-core/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
index 8bf317d..a43f2bf 100644
--- 
a/camel-core/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
+++ 
b/camel-core/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
@@ -102,6 +102,14 @@ public class SendDynamicProcessor extends ServiceSupport 
implements AsyncProcess
         try {
             recipient = expression.evaluate(exchange, Object.class);
             endpoint = resolveEndpoint(exchange, recipient);
+            if (endpoint == null) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Send dynamic evaluated as null so cannot send 
to any endpoint");
+                }
+                // no endpoint to send to, so ignore
+                callback.done(true);
+                return true;
+            }
             destinationExchangePattern = 
EndpointHelper.resolveExchangePatternFromUrl(endpoint.getEndpointUri());
         } catch (Throwable e) {
             if (isIgnoreInvalidEndpoint()) {
@@ -139,12 +147,16 @@ public class SendDynamicProcessor extends ServiceSupport 
implements AsyncProcess
             recipient = ((String) recipient).trim();
         } else if (recipient instanceof Endpoint) {
             return (Endpoint) recipient;
-        } else {
+        } else if (recipient != null) {
             // convert to a string type we can work with
             recipient = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, 
exchange, recipient);
         }
 
-        return ExchangeHelper.resolveEndpoint(exchange, recipient);
+        if (recipient != null) {
+            return ExchangeHelper.resolveEndpoint(exchange, recipient);
+        } else {
+            return null;
+        }
     }
 
     protected Exchange configureExchange(Exchange exchange, ExchangePattern 
pattern, ExchangePattern destinationExchangePattern, Endpoint endpoint) {

http://git-wip-us.apache.org/repos/asf/camel/blob/0034b993/camel-core/src/test/java/org/apache/camel/processor/ToDynamicEmptyTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/processor/ToDynamicEmptyTest.java 
b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicEmptyTest.java
new file mode 100644
index 0000000..7651b19
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/processor/ToDynamicEmptyTest.java
@@ -0,0 +1,49 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class ToDynamicEmptyTest extends ContextTestSupport {
+
+    public void testToDynamic() throws Exception {
+        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello Camel");
+        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
+
+        template.sendBodyAndHeader("direct:start", "Hello Camel", "foo", 
"mock:foo");
+        template.sendBodyAndHeader("direct:start", "Hello World", "foo", 
"mock:bar");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testToDynamicEmpty() throws Exception {
+        template.sendBody("direct:start", "Hello Camel");
+        // its empty so not sending anywhere
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .toD("${header.foo}");
+            }
+        };
+    }
+}

Reply via email to