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 2023/05/23 07:21:31 UTC
[camel] branch camel-3.x updated: CAMEL-19377: camel-platform-http-vertx - When suspended then return 503 like the other http components
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.x by this push:
new 28b2cd38dd5 CAMEL-19377: camel-platform-http-vertx - When suspended then return 503 like the other http components
28b2cd38dd5 is described below
commit 28b2cd38dd5f0b74c1066c856bff704b31075408
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 23 09:19:07 2023 +0200
CAMEL-19377: camel-platform-http-vertx - When suspended then return 503 like the other http components
---
.../http/vertx/VertxPlatformHttpConsumer.java | 26 +++-----
.../http/vertx/VertxPlatformHttpEngineTest.java | 36 ++++++++++
.../platform/http/PlatformHttpConsumer.java | 76 ++++++++++++++++++++++
.../platform/http/PlatformHttpEndpoint.java | 50 ++------------
4 files changed, 128 insertions(+), 60 deletions(-)
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 60876f552a1..f1a0fb61ae9 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
@@ -43,6 +43,8 @@ import org.apache.camel.ExchangePropertyKey;
import org.apache.camel.ExtendedExchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
+import org.apache.camel.Suspendable;
+import org.apache.camel.SuspendableService;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.camel.attachment.CamelFileDataSource;
import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
@@ -61,7 +63,7 @@ import static org.apache.camel.component.platform.http.vertx.VertxPlatformHttpSu
* A {@link org.apache.camel.Consumer} for the {@link org.apache.camel.component.platform.http.spi.PlatformHttpEngine}
* based on Vert.x Web.
*/
-public class VertxPlatformHttpConsumer extends DefaultConsumer {
+public class VertxPlatformHttpConsumer extends DefaultConsumer implements Suspendable, SuspendableService {
private static final Logger LOGGER = LoggerFactory.getLogger(VertxPlatformHttpConsumer.class);
private static final Pattern PATH_PARAMETER_PATTERN = Pattern.compile("\\{([^/}]+)\\}");
@@ -143,22 +145,6 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
super.doStop();
}
- @Override
- protected void doSuspend() throws Exception {
- if (route != null) {
- route.disable();
- }
- super.doSuspend();
- }
-
- @Override
- protected void doResume() throws Exception {
- if (route != null) {
- route.enable();
- }
- super.doResume();
- }
-
private String configureEndpointPath(PlatformHttpEndpoint endpoint) {
String path = endpoint.getPath();
if (endpoint.isMatchOnUriPrefix() && !path.endsWith("*")) {
@@ -169,6 +155,12 @@ public class VertxPlatformHttpConsumer extends DefaultConsumer {
}
protected void handleRequest(RoutingContext ctx) {
+ if (isSuspended()) {
+ ctx.response().setStatusCode(503);
+ ctx.end();
+ return;
+ }
+
final Vertx vertx = ctx.vertx();
final Exchange exchange = toExchange(ctx);
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 4e15f971341..c114379646f 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
@@ -714,6 +714,42 @@ public class VertxPlatformHttpEngineTest {
}
}
+ @Test
+ public void testConsumerSuspended() throws Exception {
+ final CamelContext context = createCamelContext();
+
+ try {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("platform-http:/get")
+ .routeId("get")
+ .setBody().constant("get");
+ }
+ });
+
+ context.start();
+
+ given()
+ .when()
+ .get("/get")
+ .then()
+ .statusCode(200)
+ .body(equalTo("get"));
+
+ context.getRouteController().suspendRoute("get");
+
+ given()
+ .when()
+ .get("/get")
+ .then()
+ .statusCode(503);
+
+ } finally {
+ context.stop();
+ }
+ }
+
static CamelContext createCamelContext() throws Exception {
return createCamelContext(null);
}
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsumer.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsumer.java
new file mode 100644
index 00000000000..3cd204e19be
--- /dev/null
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpConsumer.java
@@ -0,0 +1,76 @@
+/*
+ * 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.platform.http;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.Suspendable;
+import org.apache.camel.SuspendableService;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.support.service.ServiceHelper;
+
+public class PlatformHttpConsumer extends DefaultConsumer implements Suspendable, SuspendableService {
+
+ private Consumer delegatedConsumer;
+
+ public PlatformHttpConsumer(Endpoint endpoint, Processor processor) {
+ super(endpoint, processor);
+ }
+
+ @Override
+ public PlatformHttpEndpoint getEndpoint() {
+ return (PlatformHttpEndpoint) super.getEndpoint();
+ }
+
+ public PlatformHttpComponent getComponent() {
+ return getEndpoint().getComponent();
+ }
+
+ @Override
+ protected void doInit() throws Exception {
+ super.doInit();
+ delegatedConsumer = getEndpoint().createDelegateConsumer(getProcessor());
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ super.doStart();
+ ServiceHelper.startService(delegatedConsumer);
+ getComponent().addHttpEndpoint(getEndpoint().getPath(), getEndpoint().getHttpMethodRestrict(), delegatedConsumer);
+ }
+
+ @Override
+ protected void doStop() throws Exception {
+ super.doStop();
+ getComponent().removeHttpEndpoint(getEndpoint().getPath());
+ ServiceHelper.stopAndShutdownServices(delegatedConsumer);
+ }
+
+ @Override
+ protected void doResume() throws Exception {
+ ServiceHelper.resumeService(delegatedConsumer);
+ super.doResume();
+ }
+
+ @Override
+ protected void doSuspend() throws Exception {
+ ServiceHelper.suspendService(delegatedConsumer);
+ super.doSuspend();
+ }
+
+}
diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
index 7c958ce7e09..6c3a3e81de1 100644
--- a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
+++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpEndpoint.java
@@ -29,9 +29,7 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
-import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.DefaultEndpoint;
-import org.apache.camel.support.service.ServiceHelper;
/**
* Expose HTTP endpoints using the HTTP server available in the current platform.
@@ -89,47 +87,13 @@ public class PlatformHttpEndpoint extends DefaultEndpoint implements AsyncEndpoi
@Override
public Consumer createConsumer(Processor processor) throws Exception {
- Consumer consumer = new DefaultConsumer(this, processor) {
- private Consumer delegatedConsumer;
-
- @Override
- public PlatformHttpEndpoint getEndpoint() {
- return (PlatformHttpEndpoint) super.getEndpoint();
- }
-
- @Override
- protected void doInit() throws Exception {
- super.doInit();
- delegatedConsumer = getEndpoint().getOrCreateEngine().createConsumer(getEndpoint(), getProcessor());
- configureConsumer(delegatedConsumer);
- }
-
- @Override
- protected void doStart() throws Exception {
- super.doStart();
- ServiceHelper.startService(delegatedConsumer);
- getComponent().addHttpEndpoint(getPath(), httpMethodRestrict, delegatedConsumer);
- }
-
- @Override
- protected void doStop() throws Exception {
- super.doStop();
- getComponent().removeHttpEndpoint(getPath());
- ServiceHelper.stopAndShutdownServices(delegatedConsumer);
- }
-
- @Override
- protected void doResume() throws Exception {
- ServiceHelper.resumeService(delegatedConsumer);
- super.doResume();
- }
-
- @Override
- protected void doSuspend() throws Exception {
- ServiceHelper.suspendService(delegatedConsumer);
- super.doSuspend();
- }
- };
+ Consumer consumer = new PlatformHttpConsumer(this, processor);
+ configureConsumer(consumer);
+ return consumer;
+ }
+
+ protected Consumer createDelegateConsumer(Processor processor) throws Exception {
+ Consumer consumer = getOrCreateEngine().createConsumer(this, processor);
configureConsumer(consumer);
return consumer;
}