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

zhfeng 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 2efc889  CAMEL-17865: camel-platform-http-vertx - Fix CORS conflict in 
Camel REST (#7278)
2efc889 is described below

commit 2efc8895dd65118dbb2c819fdffe63d459e27d8e
Author: Amos Feng <[email protected]>
AuthorDate: Tue Mar 29 08:44:58 2022 +0800

    CAMEL-17865: camel-platform-http-vertx - Fix CORS conflict in Camel REST 
(#7278)
---
 .../http/vertx/VertxPlatformHttpConsumer.java      |  5 +++
 .../http/vertx/VertxPlatformHttpEngineTest.java    | 41 ++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
index becd940..74b12db 100644
--- 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
+++ 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpConsumer.java
@@ -35,6 +35,7 @@ import io.vertx.ext.auth.User;
 import io.vertx.ext.web.FileUpload;
 import io.vertx.ext.web.Route;
 import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.impl.RouteImpl;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.ExchangePropertyKey;
@@ -100,6 +101,10 @@ public class VertxPlatformHttpConsumer extends 
DefaultConsumer {
 
         final Route newRoute = router.route(path);
 
+        if 
(getEndpoint().getCamelContext().getRestConfiguration().isEnableCORS() && 
getEndpoint().getConsumes() != null) {
+            ((RouteImpl) newRoute).setEmptyBodyPermittedWithConsumes(true);
+        }
+
         if (!methods.equals(Method.getAll())) {
             methods.forEach(m -> 
newRoute.method(HttpMethod.valueOf(m.name())));
         }
diff --git 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index fbb5472..66ad222 100644
--- 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++ 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -41,6 +41,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.platform.http.PlatformHttpComponent;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.model.rest.RestParamType;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.support.jsse.KeyManagersParameters;
 import org.apache.camel.support.jsse.KeyStoreParameters;
 import org.apache.camel.support.jsse.SSLContextParameters;
@@ -454,6 +455,46 @@ public class VertxPlatformHttpEngineTest {
     }
 
     @Test
+    public void testRestCORSWitchConsumes() throws Exception {
+        final CamelContext context = createCamelContext();
+
+        try {
+            context.addRoutes(new RouteBuilder() {
+
+                @Override
+                public void configure() {
+                    
restConfiguration().component("platform-http").enableCORS(true);
+
+                    rest("/rest")
+                            .post()
+                            .consumes("application/json")
+                            .to("direct:rest");
+
+                    from("direct:rest")
+                            .setBody(simple("Hello ${body}"));
+                }
+            });
+
+            context.start();
+
+            final String origin = "http://custom.origin.quarkus";;
+
+            given()
+                    .header("Origin", origin)
+                    .when()
+                    .options("/rest")
+                    .then()
+                    .statusCode(204)
+                    .header("Access-Control-Allow-Origin", 
RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_ORIGIN)
+                    .header("Access-Control-Allow-Methods", 
RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_METHODS)
+                    .header("Access-Control-Allow-Headers", 
RestConfiguration.CORS_ACCESS_CONTROL_ALLOW_HEADERS)
+                    .header("Access-Control-Max-Age", 
RestConfiguration.CORS_ACCESS_CONTROL_MAX_AGE);
+        } finally {
+            context.stop();
+        }
+    }
+
+    @Test
     public void testBodyClientRequestValidation() throws Exception {
         final CamelContext context = createCamelContext();
 

Reply via email to