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-quarkus.git
The following commit(s) were added to refs/heads/master by this push: new 6c7ae13 Fix #217 Support rest dsl in platform-http component (#256) 6c7ae13 is described below commit 6c7ae13bae61817e5483d88302abb5b8d7adb0c7 Author: Peter Palaga <ppal...@redhat.com> AuthorDate: Sun Oct 13 09:53:28 2019 +0200 Fix #217 Support rest dsl in platform-http component (#256) --- .../platform/http/PlatformHttpComponent.java | 94 +++++++++++++++++++++- integration-tests/platform-http/pom.xml | 4 + .../platform/http/it/PlatformHttpRouteBuilder.java | 10 +++ .../component/http/server/it/PlatformHttpTest.java | 18 ++++- 4 files changed, 123 insertions(+), 3 deletions(-) diff --git a/extensions/platform-http/component/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java b/extensions/platform-http/component/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java index 6ecfc0a..c9764ba 100644 --- a/extensions/platform-http/component/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java +++ b/extensions/platform-http/component/src/main/java/org/apache/camel/component/platform/http/PlatformHttpComponent.java @@ -16,20 +16,29 @@ */ package org.apache.camel.component.platform.http; +import java.util.HashMap; +import java.util.Locale; import java.util.Map; import org.apache.camel.CamelContext; +import org.apache.camel.Consumer; import org.apache.camel.Endpoint; +import org.apache.camel.Processor; import org.apache.camel.component.platform.http.spi.PlatformHttpEngine; import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.RestApiConsumerFactory; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; +import org.apache.camel.util.FileUtil; +import org.apache.camel.util.URISupport; /** * Exposes HTTP endpoints leveraging the given platform's (SpringBoot, WildFly, Quarkus, ...) HTTP server. */ @Component("platform-http") -public class PlatformHttpComponent extends DefaultComponent { +public class PlatformHttpComponent extends DefaultComponent implements RestConsumerFactory, RestApiConsumerFactory { @Metadata(label = "advanced") private PlatformHttpEngine engine; @@ -49,6 +58,19 @@ public class PlatformHttpComponent extends DefaultComponent { return endpoint; } + @Override + public Consumer createApiConsumer(CamelContext camelContext, Processor processor, String contextPath, + RestConfiguration configuration, Map<String, Object> parameters) throws Exception { + // reuse the createConsumer method we already have. The api need to use GET and match on uri prefix + return doCreateConsumer(camelContext, processor, "GET", contextPath, null, null, null, configuration, parameters, true); + } + + @Override + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, + String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { + return doCreateConsumer(camelContext, processor, verb, basePath, uriTemplate, consumes, produces, configuration, parameters, false); + } + public PlatformHttpEngine getEngine() { return engine; } @@ -60,4 +82,74 @@ public class PlatformHttpComponent extends DefaultComponent { this.engine = engine; return this; } + + private Consumer doCreateConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, + String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters, boolean api) throws Exception { + + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } + path = FileUtil.stripLeadingSeparator(path); + + // if no explicit port/host configured, then use port from rest configuration + RestConfiguration config = configuration; + if (config == null) { + config = camelContext.getRestConfiguration(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME, true); + } + + Map<String, Object> map = new HashMap<>(); + // build query string, and append any endpoint configuration properties + if (config.getComponent() == null || config.getComponent().equals(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME)) { + // setup endpoint options + if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) { + map.putAll(config.getEndpointProperties()); + } + } + + boolean cors = config.isEnableCORS(); + if (cors) { + // allow HTTP Options as we want to handle CORS in rest-dsl + map.put("optionsEnabled", "true"); + } + + // do not append with context-path as the servlet path should be without context-path + + String query = URISupport.createQueryString(map); + + String url; + if (api) { + url = "platform-http:/%s?matchOnUriPrefix=true&httpMethodRestrict=%s"; + } else { + url = "platform-http:/%s?httpMethodRestrict=%s"; + } + + // must use upper case for restrict + String restrict = verb.toUpperCase(Locale.US); + if (cors) { + restrict += ",OPTIONS"; + } + // get the endpoint + url = String.format(url, path, restrict); + + if (!query.isEmpty()) { + url = url + "&" + query; + } + + PlatformHttpEndpoint endpoint = camelContext.getEndpoint(url, PlatformHttpEndpoint.class); + setProperties(camelContext, endpoint, parameters); + + // configure consumer properties + Consumer consumer = endpoint.createConsumer(processor); + if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) { + setProperties(camelContext, consumer, config.getConsumerProperties()); + } + + return consumer; + } } diff --git a/integration-tests/platform-http/pom.xml b/integration-tests/platform-http/pom.xml index 409ebbd..2b4b318 100644 --- a/integration-tests/platform-http/pom.xml +++ b/integration-tests/platform-http/pom.xml @@ -37,6 +37,10 @@ <artifactId>camel-quarkus-platform-http</artifactId> </dependency> <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-rest</artifactId> + </dependency> + <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-jsonb</artifactId> </dependency> 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 894deee..0c336fb 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 @@ -21,6 +21,16 @@ import org.apache.camel.builder.RouteBuilder; public class PlatformHttpRouteBuilder extends RouteBuilder { @Override public void configure() { + rest() + .get("/platform-http/rest-get") + .route() + .setBody(constant("GET: /rest-get")) + .endRest() + .post("/platform-http/rest-post") + .route() + .setBody(constant("POST: /rest-post")) + .endRest(); + from("platform-http:/platform-http/hello?httpMethodRestrict=GET").setBody(simple("Hello ${header.name}")); from("platform-http:/platform-http/get-post?httpMethodRestrict=GET,POST").setBody(simple("Hello ${body}")); from("platform-http:/platform-http/multipart?httpMethodRestrict=POST").setBody(simple("Hello ${body}")); 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 c08eaba..2b76bf6 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 @@ -30,8 +30,9 @@ import static org.hamcrest.CoreMatchers.equalTo; @QuarkusTest class PlatformHttpTest { + @Test - public void testRegistrySetUp() { + public void registrySetUp() { JsonPath p = RestAssured.given() .get("/test/registry/inspect") .then() @@ -67,10 +68,23 @@ class PlatformHttpTest { .body(equalTo("Hello ")); // there is no body for get } + @Test + public void rest() throws Throwable { + RestAssured.get("/platform-http/rest-get") + .then().body(equalTo("GET: /rest-get")); + RestAssured.post("/platform-http/rest-post") + .then().body(equalTo("POST: /rest-post")); + } + @Disabled("See https://github.com/quarkusio/quarkus/issues/4408") @Test - public void testInvalidMethod() { + public void invalidMethod() { RestAssured.post("/platform-http/hello") .then().statusCode(405); + RestAssured.post("/platform-http/rest-get") + .then().statusCode(405); + RestAssured.get("/platform-http/rest-post") + .then().statusCode(405); } + }