[camel-quarkus] branch master updated: Fix #382 Support path parameters in platform-http

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

[camel-quarkus] branch master updated: Fix #382 Support path parameters in platform-http

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ddd0648  Fix #382 Support path parameters in platform-http
ddd0648 is described below

commit ddd0648c4fb079d4bfe001fa4988a8b719a13663
Author: Peter Palaga <[hidden email]>
AuthorDate: Wed Nov 13 14:05:10 2019 +0100

    Fix #382 Support path parameters in platform-http
---
 .../http/runtime/QuarkusPlatformHttpConsumer.java  | 22 ++++++++++------------
 .../platform/http/it/PlatformHttpRouteBuilder.java |  7 +++++++
 .../component/http/server/it/PlatformHttpTest.java |  9 +++++++++
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java b/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
index da61b34..46726b1 100644
--- a/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
+++ b/extensions/platform-http/runtime/src/main/java/org/apache/camel/quarkus/component/platform/http/runtime/QuarkusPlatformHttpConsumer.java
@@ -29,7 +29,9 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import io.vertx.core.Handler;
 import io.vertx.core.MultiMap;
@@ -73,6 +75,7 @@ public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
     private Route route;
     private final String fileNameExtWhitelist;
     private final UploadAttacher uploadAttacher;
+    private final Pattern PATH_PARAMETER_PATTERN = Pattern.compile("\\{([^/}]+)\\}");
 
     public QuarkusPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor, Router router,
             List<Handler<RoutingContext>> handlers, UploadAttacher uploadAttacher) {
@@ -95,7 +98,9 @@ public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
 
         final PlatformHttpEndpoint endpoint = getEndpoint();
         final String path = endpoint.getPath();
-        final Route newRoute = router.route(path);
+        /* Transform from the Camel path param syntax /path/{key} to vert.x web's /path/:key */
+        final String vertxPathParamPath = PATH_PARAMETER_PATTERN.matcher(path).replaceAll(":$1");
+        final Route newRoute = router.route(vertxPathParamPath);
 
         final Set<Method> methods = Method.parseList(endpoint.getHttpMethodRestrict());
         if (!methods.equals(Method.getAll())) {
@@ -338,17 +343,10 @@ public class QuarkusPlatformHttpConsumer extends DefaultConsumer {
             }
         }
 
-        // TODO: figure out whether we need this or remove
-        // // Create headers for REST path placeholder variables
-        // Map<String, Object> predicateContextParams = httpExchange.getAttachment(Predicate.PREDICATE_CONTEXT);
-        // if (predicateContextParams != null) {
-        // // Remove this as it's an unwanted artifact of our Undertow predicate chain
-        // predicateContextParams.remove("remaining");
-        //
-        // for (String paramName : predicateContextParams.keySet()) {
-        // headersMap.put(paramName, predicateContextParams.get(paramName));
-        // }
-        // }
+        /* Path parameters */
+        for (Entry<String, String> en : ctx.pathParams().entrySet()) {
+            appendHeader(headersMap, en.getKey(), en.getValue());
+        }
 
         // NOTE: these headers is applied using the same logic as camel-http/camel-jetty to be consistent
         headersMap.put(Exchange.HTTP_METHOD, request.method().toString());
diff --git a/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java b/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
index b3ada44..0f7b9a3 100644
--- a/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
+++ b/integration-tests/platform-http/src/main/java/org/apache/camel/quarkus/component/platform/http/it/PlatformHttpRouteBuilder.java
@@ -96,5 +96,12 @@ public class PlatformHttpRouteBuilder extends RouteBuilder {
                 .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
                 .setBody().constant("");
 
+        /* Path parameters */
+        rest()
+                .get("/platform-http/hello-by-name/{name}")
+                .produces("text/plain")
+                .route()
+                .setBody(e -> "Hello " + e.getIn().getHeader("name", String.class))
+                .endRest();
     }
 }
diff --git a/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java b/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
index f36a0f0..af1c29a 100644
--- a/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
+++ b/integration-tests/platform-http/src/test/java/org/apache/camel/quarkus/component/http/server/it/PlatformHttpTest.java
@@ -232,4 +232,13 @@ class PlatformHttpTest {
                 .body(equalTo(""));
     }
 
+    @Test
+    public void pathParam() throws Exception {
+        RestAssured.given()
+                .get("/platform-http/hello-by-name/Kermit")
+                .then()
+                .statusCode(200)
+                .body(equalTo("Hello Kermit"));
+    }
+
 }