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.git


The following commit(s) were added to refs/heads/master by this push:
     new 991ace2  CAMEL-14954: Make undertow suspendable and return status 503 
if suspended just like other http component does.
991ace2 is described below

commit 991ace268566cfef5dc588689171c0c09246f00d
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Apr 23 09:32:17 2020 +0200

    CAMEL-14954: Make undertow suspendable and return status 503 if suspended 
just like other http component does.
---
 .../camel/component/undertow/UndertowConsumer.java | 25 +++++++-
 .../undertow/UndertowSuspendResumeTest.java        | 66 ++++++++++++++++++++++
 2 files changed, 90 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
index 5061ea8..ff12135 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
@@ -65,6 +65,7 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
     private static final Logger LOG = 
LoggerFactory.getLogger(UndertowConsumer.class);
     private CamelWebSocketHandler webSocketHandler;
     private boolean rest;
+    private volatile boolean suspended;
 
     public UndertowConsumer(UndertowEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -93,6 +94,7 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
 
     @Override
     protected void doStart() throws Exception {
+        this.suspended = false;
         super.doStart();
         final UndertowEndpoint endpoint = getEndpoint();
         if (endpoint.isWebSocket()) {
@@ -128,6 +130,7 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
 
     @Override
     protected void doStop() throws Exception {
+        this.suspended = false;
         super.doStop();
         if (this.webSocketHandler != null) {
             this.webSocketHandler.setConsumer(null);
@@ -136,10 +139,23 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
         endpoint.getComponent().unregisterEndpoint(this, 
endpoint.getHttpHandlerRegistrationInfo(), endpoint.getSslContext());
     }
 
+    protected void doSuspend() throws Exception {
+        this.suspended = true;
+        super.doSuspend();
+    }
+
+    protected void doResume() throws Exception {
+        this.suspended = false;
+        super.doResume();
+    }
+
+    public boolean isSuspended() {
+        return this.suspended;
+    }
+
     @Override
     public void handleRequest(HttpServerExchange httpExchange) throws 
Exception {
         HttpString requestMethod = httpExchange.getRequestMethod();
-
         if (Methods.OPTIONS.equals(requestMethod) && 
!getEndpoint().isOptionsEnabled()) {
             CollectionStringBuffer csb = new CollectionStringBuffer(",");
 
@@ -180,6 +196,13 @@ public class UndertowConsumer extends DefaultConsumer 
implements HttpHandler, Su
             return;
         }
 
+        // are we suspended
+        if (isSuspended()) {
+            httpExchange.setStatusCode(StatusCodes.SERVICE_UNAVAILABLE);
+            httpExchange.endExchange();
+            return;
+        }
+
         if (getEndpoint().getSecurityProvider() != null) {
             //security provider decides, whether endpoint is accessible
             int statusCode = 
getEndpoint().getSecurityProvider().authenticate(httpExchange, 
computeAllowedRoles());
diff --git 
a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSuspendResumeTest.java
 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSuspendResumeTest.java
new file mode 100644
index 0000000..b06e9ba
--- /dev/null
+++ 
b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSuspendResumeTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.undertow;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.http.base.HttpOperationFailedException;
+import org.junit.Test;
+
+public class UndertowSuspendResumeTest extends BaseUndertowTest {
+
+    private String serverUri = "http://localhost:{{port}}/foo";;
+
+    @Test
+    public void testSuspendResume() throws Exception {
+        context.getShutdownStrategy().setTimeout(50);
+
+        String reply = template.requestBody(serverUri, "World", String.class);
+        assertEquals("Bye World", reply);
+
+        // now suspend jetty
+        UndertowConsumer consumer = (UndertowConsumer) 
context.getRoute("route1").getConsumer();
+        assertNotNull(consumer);
+
+        // suspend
+        consumer.suspend();
+
+        try {
+            template.requestBody(serverUri, "Moon", String.class);
+            fail("Should throw exception");
+        } catch (Exception e) {
+            HttpOperationFailedException cause = 
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
+            assertEquals(503, cause.getStatusCode());
+        }
+
+        // resume
+        consumer.resume();
+
+        // and send request which should be processed
+        reply = template.requestBody(serverUri, "Moon", String.class);
+        assertEquals("Bye Moon", reply);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("undertow://" + 
serverUri).id("route1").transform(body().prepend("Bye "));
+            }
+        };
+    }
+}

Reply via email to