This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 7b98a09ed87 CAMEL-18953: camel-jetty - Should throw 504 error if
continuation timeout was hit
7b98a09ed87 is described below
commit 7b98a09ed87a47d0f2cb957cc926c2bf3b58b4f9
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Jan 18 22:35:03 2023 +0100
CAMEL-18953: camel-jetty - Should throw 504 error if continuation timeout
was hit
---
.../component/jetty/CamelContinuationServlet.java | 7 +-
.../camel/component/jetty/JettyHttpComponent.java | 5 ++
.../async/JettyAsyncContinuationTimeoutTest.java | 2 +-
.../JettyAsyncDefaultContinuationTimeoutTest.java | 76 ----------------------
4 files changed, 9 insertions(+), 81 deletions(-)
diff --git
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
index 99694863d47..c4fece46b23 100644
---
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
+++
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
@@ -20,7 +20,6 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import jakarta.servlet.AsyncContext;
@@ -51,6 +50,8 @@ import org.apache.camel.util.UnsafeUriCharactersEncoder;
*/
public class CamelContinuationServlet extends CamelServlet {
+ static final String TIMEOUT_ERROR = "CamelTimeoutException";
+
static final String EXCHANGE_ATTRIBUTE_NAME = "CamelExchange";
static final String EXCHANGE_ATTRIBUTE_ID = "CamelExchangeId";
@@ -318,13 +319,11 @@ public class CamelContinuationServlet extends
CamelServlet {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
HttpServletRequest request = (HttpServletRequest)
event.getSuppliedRequest();
- HttpServletResponse response = (HttpServletResponse)
event.getSuppliedResponse();
String id = (String) request.getAttribute(EXCHANGE_ATTRIBUTE_ID);
// remember this id as expired
expiredExchanges.put(id, id);
log.warn("Continuation expired of exchangeId: {}", id);
- consumer.getBinding().doWriteExceptionResponse(new
TimeoutException(), response);
- return;
+ request.setAttribute(TIMEOUT_ERROR, Boolean.TRUE);
}
@Override
diff --git
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
index d3d9d518bd1..55f7ef42b40 100644
---
a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
+++
b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
@@ -1268,6 +1268,11 @@ public abstract class JettyHttpComponent extends
HttpCommonComponent
HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException {
String msg = HttpStatus.getMessage(response.getStatus());
+ Object timeout =
request.getAttribute(CamelContinuationServlet.TIMEOUT_ERROR);
+ if (Boolean.TRUE.equals(timeout)) {
+
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 504);
+ response.setStatus(504);
+ }
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
super.handle(target, baseRequest, request, response);
}
diff --git
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
index 5afab1f976b..4b09b772b9b 100644
---
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
+++
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
@@ -49,7 +49,7 @@ public class JettyAsyncContinuationTimeoutTest extends
BaseJettyTest {
long taken = watch.taken();
HttpOperationFailedException cause =
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
- assertEquals(500, cause.getStatusCode());
+ assertEquals(504, cause.getStatusCode());
// should be approx 3-4 sec.
assertTrue(taken < 4500, "Timeout should occur faster than " +
taken);
diff --git
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncDefaultContinuationTimeoutTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncDefaultContinuationTimeoutTest.java
deleted file mode 100644
index 98ceee96be6..00000000000
---
a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncDefaultContinuationTimeoutTest.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.jetty.async;
-
-import java.util.concurrent.TimeUnit;
-
-import org.apache.camel.CamelExecutionException;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.jetty.BaseJettyTest;
-import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.http.base.HttpOperationFailedException;
-import org.apache.camel.util.StopWatch;
-import org.junit.jupiter.api.Disabled;
-import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
-
-@Disabled("This test takes a long time to run, so run it manually")
-public class JettyAsyncDefaultContinuationTimeoutTest extends BaseJettyTest {
-
- private static final Logger LOG =
LoggerFactory.getLogger(JettyAsyncDefaultContinuationTimeoutTest.class);
-
- @Test
- public void testJettyAsyncTimeout() throws Exception {
- getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
- StopWatch watch = new StopWatch();
- try {
- template.requestBody("http://localhost:{{port}}/myservice", null,
String.class);
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- LOG.info("Timeout hit and client got reply with failure status
code");
-
- long taken = watch.taken();
-
- HttpOperationFailedException cause =
assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
- assertEquals(504, cause.getStatusCode());
-
- // should be approx 30-34 sec.
- assertTrue(taken < 34000, "Timeout should occur faster than " +
taken);
- }
-
- MockEndpoint.assertIsSatisfied(context, 2, TimeUnit.MINUTES);
- }
-
- @Override
- protected RouteBuilder createRouteBuilder() {
- return new RouteBuilder() {
- @Override
- public void configure() {
- context.addComponent("async", new MyAsyncComponent());
-
-
from("jetty:http://localhost:{{port}}/myservice").to("async:bye:world?delay=45s").to("mock:result");
- }
- };
- }
-}