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 2021/12/09 10:51:40 UTC

[camel] branch main updated: CAMEL-17298: camel-vertx-http should be similar to camel-http for toD (dynamic send)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 0f5691e  CAMEL-17298: camel-vertx-http should be similar to camel-http for toD (dynamic send)
0f5691e is described below

commit 0f5691ed0e7218d880b57fea5b2009484b1c2a41
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Dec 9 11:50:55 2021 +0100

    CAMEL-17298: camel-vertx-http should be similar to camel-http for toD (dynamic send)
---
 .../camel/http/base/HttpSendDynamicAware.java      |  17 +++-
 .../apache/camel/component/http/HttpProducer.java  |  10 +-
 components/camel-vertx/camel-vertx-http/pom.xml    |   5 +
 .../vertx/http/DefaultVertxHttpBinding.java        |  10 +-
 .../component/vertx/http/VertxHttpHelper.java      |   6 +-
 .../vertx/http/VertxHttpProducerToDTest.java       | 107 +++++++++++++++++++++
 6 files changed, 136 insertions(+), 19 deletions(-)

diff --git a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
index d2bd5e0..123f5a9 100644
--- a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
+++ b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.support.component.SendDynamicAwareSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
@@ -118,16 +119,17 @@ public class HttpSendDynamicAware extends SendDynamicAwareSupport {
     }
 
     /**
-     * Parses the uri into an string array with 3 elements.
+     * Parses the uri into a string array with 3 elements.
      *
      * 0 = host:port 1 = path 2 = authority
      */
     public String[] parseUri(DynamicAwareEntry entry) {
         String u = entry.getUri();
 
-        // remove scheme prefix (unless its camel-http or camel-http)
+        // remove scheme prefix (unless its camel-http or camel-vertx-http)
         boolean httpComponent = "http".equals(getScheme()) || "https".equals(getScheme());
-        if (!httpComponent) {
+        boolean vertxHttpComponent = "vertx-http".equals(getScheme());
+        if (!httpComponent && !vertxHttpComponent) {
             String prefix = getScheme() + "://";
             String prefix2 = getScheme() + ":";
             if (u.startsWith(prefix)) {
@@ -142,6 +144,15 @@ public class HttpSendDynamicAware extends SendDynamicAwareSupport {
             u = StringHelper.before(u, "?");
         }
 
+        if (vertxHttpComponent && u.startsWith("vertx-http:")) {
+            u = u.substring(11);
+            // must include http prefix
+            String scheme = ResourceHelper.getScheme(u);
+            if (scheme == null) {
+                u = "http://" + u;
+            }
+        }
+
         // favour using java.net.URI for parsing into host, context-path and authority
         try {
             URI parse = new URI(u);
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 680d271..8b9c5b5 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -592,17 +592,17 @@ public class HttpProducer extends DefaultProducer {
         // these checks are checks that is done in HttpHelper.createURL and HttpHelper.createURI methods
         boolean create = false;
         Message in = exchange.getIn();
-        if (in.getHeader("CamelRestHttpUri") != null) {
+        if (in.getHeader(Exchange.REST_HTTP_URI) != null) {
             create = true;
-        } else if (in.getHeader("CamelHttpUri") != null && !getEndpoint().isBridgeEndpoint()) {
+        } else if (in.getHeader(Exchange.HTTP_URI) != null && !getEndpoint().isBridgeEndpoint()) {
             create = true;
-        } else if (in.getHeader("CamelHttpPath") != null) {
+        } else if (in.getHeader(Exchange.HTTP_PATH) != null) {
             create = true;
-        } else if (in.getHeader("CamelRestHttpQuery") != null) {
+        } else if (in.getHeader(Exchange.REST_HTTP_QUERY) != null) {
             create = true;
         } else if (in.getHeader("CamelHttpRawQuery") != null) {
             create = true;
-        } else if (in.getHeader("CamelHttpQuery") != null) {
+        } else if (in.getHeader(Exchange.HTTP_QUERY) != null) {
             create = true;
         }
 
diff --git a/components/camel-vertx/camel-vertx-http/pom.xml b/components/camel-vertx/camel-vertx-http/pom.xml
index d83b233..d3c423e 100644
--- a/components/camel-vertx/camel-vertx-http/pom.xml
+++ b/components/camel-vertx/camel-vertx-http/pom.xml
@@ -62,6 +62,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
+            <artifactId>camel-http</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
             <artifactId>camel-undertow</artifactId>
             <scope>test</scope>
         </dependency>
diff --git a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/DefaultVertxHttpBinding.java b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/DefaultVertxHttpBinding.java
index 4cfe8be..1f5660f 100644
--- a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/DefaultVertxHttpBinding.java
+++ b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/DefaultVertxHttpBinding.java
@@ -78,10 +78,7 @@ public class DefaultVertxHttpBinding implements VertxHttpBinding {
         }
 
         // Resolve the URI to use which is either a combination of headers HTTP_URI & HTTP_PATH or the HTTP URI configured on the endpoint
-        URI uri = VertxHttpHelper.resolveHttpURI(exchange);
-        if (uri == null) {
-            uri = configuration.getHttpUri();
-        }
+        URI uri = VertxHttpHelper.resolveHttpURI(exchange, endpoint);
 
         WebClient webClient = endpoint.getWebClient();
         HttpRequest<Buffer> request;
@@ -251,10 +248,7 @@ public class DefaultVertxHttpBinding implements VertxHttpBinding {
             Map<String, String> headers = new HashMap<>();
             result.headers().names().forEach(header -> headers.put(header, result.getHeader(header)));
 
-            URI httpURI = VertxHttpHelper.resolveHttpURI(exchange);
-            if (httpURI == null) {
-                httpURI = configuration.getHttpUri();
-            }
+            URI httpURI = VertxHttpHelper.resolveHttpURI(exchange, endpoint);
             exception = new HttpOperationFailedException(
                     httpURI.toString(), result.statusCode(), result.statusMessage(), location, headers, result.bodyAsString());
         }
diff --git a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpHelper.java b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpHelper.java
index a691f64..dec6cc1 100644
--- a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpHelper.java
+++ b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpHelper.java
@@ -63,7 +63,7 @@ public final class VertxHttpHelper {
     /**
      * Resolves a HTTP URI and path string from the given exchange message headers
      */
-    public static URI resolveHttpURI(Exchange exchange) throws URISyntaxException {
+    public static URI resolveHttpURI(Exchange exchange, VertxHttpEndpoint endpoint) throws URISyntaxException {
         Message message = exchange.getMessage();
         String uri = (String) message.removeHeader(Exchange.REST_HTTP_URI);
 
@@ -71,8 +71,8 @@ public final class VertxHttpHelper {
             uri = message.getHeader(Exchange.HTTP_URI, String.class);
         }
 
-        if (ObjectHelper.isEmpty(uri)) {
-            return null;
+        if (uri == null) {
+            uri = endpoint.getConfiguration().getHttpUri().toASCIIString();
         }
 
         // Resolve property placeholders that may be present in the URI
diff --git a/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpProducerToDTest.java b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpProducerToDTest.java
new file mode 100644
index 0000000..6e4feb9
--- /dev/null
+++ b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpProducerToDTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.vertx.http;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class VertxHttpProducerToDTest extends VertxHttpTestSupport {
+
+    @Test
+    public void testStatic() {
+        String out = template.requestBody("direct:static", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testStatic2() {
+        String out = template.requestBody("direct:static2", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testDynamic() {
+        String out = template.requestBody("direct:dynamic", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testDynamic2() {
+        String out = template.requestBody("direct:dynamic2", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testDynamic3() {
+        String out = template.requestBody("direct:dynamic3", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testDynamic4() {
+        String out = template.requestBody("direct:dynamic4", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Test
+    public void testDynamic5() {
+        String out = template.requestBody("direct:dynamic5", null, String.class);
+        Assertions.assertEquals("Hello World", out);
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:static")
+                        .toD(getProducerUri() + "/hello");
+
+                from("direct:static2")
+                        .setHeader("CamelHttpUri", constant(getTestServerUrl() + "/hello"))
+                        .to("vertx-http:does-not-exist");
+
+                from("direct:dynamic")
+                        .setHeader("foo", constant("/hello"))
+                        .toD(getProducerUri() + "${header.foo}");
+
+                from("direct:dynamic2")
+                        .setHeader("bar", constant(getTestServerUrl() + "/hello"))
+                        .toD("vertx-http:${header.bar}");
+
+                from("direct:dynamic3")
+                        .setHeader("CamelHttpUri", constant(getTestServerUrl() + "/hello"))
+                        // will use regular http component
+                        .toD("${header.CamelHttpUri}");
+
+                from("direct:dynamic4")
+                        .setHeader("CamelHttpUri", constant(getTestServerUrl() + "/hello"))
+                        // compare with regular http component
+                        .toD("http:does-not-exist");
+
+                from("direct:dynamic5")
+                        .setHeader("CamelHttpUri", constant(getTestServerUrl() + "/hello"))
+                        .toD("vertx-http:does-not-exist");
+
+                from(getTestServerUri() + "/hello")
+                        .setBody(constant("Hello World"));
+            }
+        };
+    }
+}