This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-spring-boot-4.4.x in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git
The following commit(s) were added to refs/heads/camel-spring-boot-4.4.x by this push: new 637fbd2c9fb CAMEL-21300: camel-platform-http - Consumer should have option to control if writing response failing should cause Exchange to fail 637fbd2c9fb is described below commit 637fbd2c9fbbe7e4deef9b348b84dfa4ed8b352e Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Oct 2 12:37:43 2024 +0200 CAMEL-21300: camel-platform-http - Consumer should have option to control if writing response failing should cause Exchange to fail --- .../catalog/components/platform-http.json | 28 ++--- .../src/main/docs/platform-http.json | 7 ++ .../PlatformHttpComponentConfiguration.java | 16 +++ .../http/springboot/PlatformHttpMessage.java | 6 +- .../springboot/SpringBootPlatformHttpConsumer.java | 56 ++++++---- .../platform/http/springboot/PlatformHttpBase.java | 33 +++--- ...SpringBootPlatformHttpHandleWriteErrorTest.java | 119 +++++++++++++++++++++ 7 files changed, 216 insertions(+), 49 deletions(-) diff --git a/catalog/camel-catalog-provider-springboot/src/main/resources/org/apache/camel/springboot/catalog/components/platform-http.json b/catalog/camel-catalog-provider-springboot/src/main/resources/org/apache/camel/springboot/catalog/components/platform-http.json index 7552aad0525..44fbeed14f9 100644 --- a/catalog/camel-catalog-provider-springboot/src/main/resources/org/apache/camel/springboot/catalog/components/platform-http.json +++ b/catalog/camel-catalog-provider-springboot/src/main/resources/org/apache/camel/springboot/catalog/components/platform-http.json @@ -24,22 +24,24 @@ }, "componentProperties": { "bridgeErrorHandler": { "index": 0, "kind": "property", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the like [...] - "autowiredEnabled": { "index": 1, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching t [...] - "engine": { "index": 2, "kind": "property", "displayName": "Engine", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.platform.http.spi.PlatformHttpEngine", "deprecated": false, "autowired": false, "secret": false, "description": "An HTTP Server engine implementation to serve the requests" } + "handleWriteResponseError": { "index": 1, "kind": "property", "displayName": "Handle Write Response Error", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "When Camel is complete processing the message, and the HTTP server is writing response. This option controls whether Camel should catch any failure during writing response [...] + "autowiredEnabled": { "index": 2, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching t [...] + "engine": { "index": 3, "kind": "property", "displayName": "Engine", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.platform.http.spi.PlatformHttpEngine", "deprecated": false, "autowired": false, "secret": false, "description": "An HTTP Server engine implementation to serve the requests" } }, "properties": { "path": { "index": 0, "kind": "path", "displayName": "Path", "group": "consumer", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The path under which this endpoint serves the HTTP requests, for proxy use 'proxy'" }, "consumes": { "index": 1, "kind": "parameter", "displayName": "Consumes", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The content type this endpoint accepts as an input, such as application\/xml or application\/json. null or *\/* mean no restriction." }, - "httpMethodRestrict": { "index": 2, "kind": "parameter", "displayName": "Http Method Restrict", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "A comma separated list of HTTP methods to serve, e.g. GET,POST . If no methods are specified, all methods will be served." }, - "matchOnUriPrefix": { "index": 3, "kind": "parameter", "displayName": "Match On Uri Prefix", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether or not the consumer should try to find a target consumer by matching the URI prefix if no exact match is found." }, - "muteException": { "index": 4, "kind": "parameter", "displayName": "Mute Exception", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "If enabled and an Exchange failed processing on the consumer side the response's body won't contain the exception's stack trace." }, - "produces": { "index": 5, "kind": "parameter", "displayName": "Produces", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The content type this endpoint produces, such as application\/xml or application\/json." }, - "useStreaming": { "index": 6, "kind": "parameter", "displayName": "Use Streaming", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to use streaming for large requests and responses (currently only supported by camel-platform-http-vertx)" }, - "bridgeErrorHandler": { "index": 7, "kind": "parameter", "displayName": "Bridge Error Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming [...] - "exceptionHandler": { "index": 8, "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "autowired": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By def [...] - "exchangePattern": { "index": 9, "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ], "deprecated": false, "autowired": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." }, - "fileNameExtWhitelist": { "index": 10, "kind": "parameter", "displayName": "File Name Ext Whitelist", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "A comma or whitespace separated list of file extensions. Uploads having these extensions will be stored locally. Null value or asterisk () will allow all files." }, - "headerFilterStrategy": { "index": 11, "kind": "parameter", "displayName": "Header Filter Strategy", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.HeaderFilterStrategy", "deprecated": false, "autowired": false, "secret": false, "description": "To use a custom HeaderFilterStrategy to filter headers to and from Camel message." }, - "platformHttpEngine": { "index": 12, "kind": "parameter", "displayName": "Platform Http Engine", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.platform.http.spi.PlatformHttpEngine", "deprecated": false, "autowired": false, "secret": false, "description": "An HTTP Server engine implementation to serve the requests of this endpoint." } + "handleWriteResponseError": { "index": 2, "kind": "parameter", "displayName": "Handle Write Response Error", "group": "consumer", "label": "advanced,consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "When Camel is complete processing the message, and the HTTP server is writing response. This option controls whether Camel should catch any failure during writing respons [...] + "httpMethodRestrict": { "index": 3, "kind": "parameter", "displayName": "Http Method Restrict", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "A comma separated list of HTTP methods to serve, e.g. GET,POST . If no methods are specified, all methods will be served." }, + "matchOnUriPrefix": { "index": 4, "kind": "parameter", "displayName": "Match On Uri Prefix", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether or not the consumer should try to find a target consumer by matching the URI prefix if no exact match is found." }, + "muteException": { "index": 5, "kind": "parameter", "displayName": "Mute Exception", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "If enabled and an Exchange failed processing on the consumer side the response's body won't contain the exception's stack trace." }, + "produces": { "index": 6, "kind": "parameter", "displayName": "Produces", "group": "consumer", "label": "consumer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The content type this endpoint produces, such as application\/xml or application\/json." }, + "useStreaming": { "index": 7, "kind": "parameter", "displayName": "Use Streaming", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to use streaming for large requests and responses (currently only supported by camel-platform-http-vertx)" }, + "bridgeErrorHandler": { "index": 8, "kind": "parameter", "displayName": "Bridge Error Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming [...] + "exceptionHandler": { "index": 9, "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "autowired": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By def [...] + "exchangePattern": { "index": 10, "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ], "deprecated": false, "autowired": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." }, + "fileNameExtWhitelist": { "index": 11, "kind": "parameter", "displayName": "File Name Ext Whitelist", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "A comma or whitespace separated list of file extensions. Uploads having these extensions will be stored locally. Null value or asterisk () will allow all files." }, + "headerFilterStrategy": { "index": 12, "kind": "parameter", "displayName": "Header Filter Strategy", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.HeaderFilterStrategy", "deprecated": false, "autowired": false, "secret": false, "description": "To use a custom HeaderFilterStrategy to filter headers to and from Camel message." }, + "platformHttpEngine": { "index": 13, "kind": "parameter", "displayName": "Platform Http Engine", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.camel.component.platform.http.spi.PlatformHttpEngine", "deprecated": false, "autowired": false, "secret": false, "description": "An HTTP Server engine implementation to serve the requests of this endpoint." } } } diff --git a/components-starter/camel-platform-http-starter/src/main/docs/platform-http.json b/components-starter/camel-platform-http-starter/src/main/docs/platform-http.json index c98c9e05d23..a4f0d21ea31 100644 --- a/components-starter/camel-platform-http-starter/src/main/docs/platform-http.json +++ b/components-starter/camel-platform-http-starter/src/main/docs/platform-http.json @@ -43,6 +43,13 @@ "type": "org.apache.camel.component.platform.http.spi.PlatformHttpEngine", "description": "An HTTP Server engine implementation to serve the requests. The option is a org.apache.camel.component.platform.http.spi.PlatformHttpEngine type.", "sourceType": "org.apache.camel.component.platform.http.springboot.PlatformHttpComponentConfiguration" + }, + { + "name": "camel.component.platform-http.handle-write-response-error", + "type": "java.lang.Boolean", + "description": "When Camel is complete processing the message, and the HTTP server is writing response. This option controls whether Camel should catch any failure during writing response and store this on the Exchange, which allows onCompletion\/UnitOfWork to regard the Exchange as failed and have access to the caused exception from the HTTP server.", + "sourceType": "org.apache.camel.component.platform.http.springboot.PlatformHttpComponentConfiguration", + "defaultValue": false } ], "hints": [] diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpComponentConfiguration.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpComponentConfiguration.java index 0567b8936b1..9b635d4892d 100644 --- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpComponentConfiguration.java +++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpComponentConfiguration.java @@ -50,6 +50,14 @@ public class PlatformHttpComponentConfiguration * with exceptions, that will be logged at WARN or ERROR level and ignored. */ private Boolean bridgeErrorHandler = false; + /** + * When Camel is complete processing the message, and the HTTP server is + * writing response. This option controls whether Camel should catch any + * failure during writing response and store this on the Exchange, which + * allows onCompletion/UnitOfWork to regard the Exchange as failed and have + * access to the caused exception from the HTTP server. + */ + private Boolean handleWriteResponseError = false; /** * Whether autowiring is enabled. This is used for automatic autowiring * options (the option must be marked as autowired) by looking up in the @@ -73,6 +81,14 @@ public class PlatformHttpComponentConfiguration this.bridgeErrorHandler = bridgeErrorHandler; } + public Boolean getHandleWriteResponseError() { + return handleWriteResponseError; + } + + public void setHandleWriteResponseError(Boolean handleWriteResponseError) { + this.handleWriteResponseError = handleWriteResponseError; + } + public Boolean getAutowiredEnabled() { return autowiredEnabled; } diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpMessage.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpMessage.java index 5818a70cad1..233f469e1c0 100644 --- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpMessage.java +++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/PlatformHttpMessage.java @@ -96,7 +96,11 @@ public class PlatformHttpMessage extends DefaultMessage { } public PlatformHttpMessage newInstance() { - return new PlatformHttpMessage(this.request, this.response, this.getExchange(), this.binding, this.requestRead); + PlatformHttpMessage answer = new PlatformHttpMessage(this.request, this.response, this.getExchange(), this.binding, this.requestRead); + if (answer.camelContext == null) { + answer.setCamelContext(this.camelContext); + } + return answer; } public String toString() { diff --git a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java index 6c07c2cd197..a4b01ad4a10 100644 --- a/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java +++ b/components-starter/camel-platform-http-starter/src/main/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpConsumer.java @@ -25,19 +25,18 @@ import org.apache.camel.Processor; import org.apache.camel.Suspendable; import org.apache.camel.SuspendableService; import org.apache.camel.component.platform.http.PlatformHttpEndpoint; -import org.apache.camel.http.common.DefaultHttpBinding; +import org.apache.camel.http.common.HttpBinding; import org.apache.camel.http.common.HttpHelper; import org.apache.camel.support.DefaultConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; - public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements Suspendable, SuspendableService { private static final Logger LOG = LoggerFactory.getLogger(SpringBootPlatformHttpConsumer.class); - private final DefaultHttpBinding binding; + private HttpBinding binding; + private final boolean handleWriteResponseError; public SpringBootPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor) { super(endpoint, processor); @@ -45,6 +44,14 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements S this.binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy()); this.binding.setMuteException(endpoint.isMuteException()); this.binding.setFileNameExtWhitelist(endpoint.getFileNameExtWhitelist()); + this.handleWriteResponseError = endpoint.isHandleWriteResponseError(); + } + + /** + * Used for testing purposes + */ + void setBinding(HttpBinding binding) { + this.binding = binding; } @Override @@ -111,35 +118,46 @@ public class SpringBootPlatformHttpConsumer extends DefaultConsumer implements S } catch (Exception e) { exchange.setException(e); } finally { - afterProcess(response, exchange, true); + afterProcess(response, exchange); } } - protected void afterProcess(HttpServletResponse response, Exchange exchange, boolean rethrow) throws IOException, ServletException { + protected void afterProcess(HttpServletResponse response, Exchange exchange) throws Exception { + boolean writeFailure = false; try { // now lets output to the res if (LOG.isTraceEnabled()) { LOG.trace("Writing res for exchangeId: {}", exchange.getExchangeId()); } binding.writeResponse(exchange, response); - } catch (IOException e) { - LOG.error("Error processing request", e); - if (rethrow) { - throw e; - } else { - exchange.setException(e); - } } catch (Exception e) { - LOG.error("Error processing request", e); - if (rethrow) { - throw new ServletException(e); - } else { - exchange.setException(e); - } + writeFailure = true; + handleFailure(exchange, e); } finally { doneUoW(exchange); releaseExchange(exchange, false); } + try { + if (writeFailure && !response.isCommitted()) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } catch (Exception e) { + // ignore + } + } + + private void handleFailure(Exchange exchange, Throwable failure) { + getExceptionHandler().handleException( + "Failed writing HTTP response url: " + getEndpoint().getPath() + " due to: " + failure.getMessage(), + failure); + if (handleWriteResponseError) { + Exception existing = exchange.getException(); + if (existing != null) { + failure.addSuppressed(existing); + } + exchange.setProperty(Exchange.EXCEPTION_CAUGHT, failure); + exchange.setException(failure); + } } } diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpBase.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpBase.java index f04ab743419..0b9cab6634d 100644 --- a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpBase.java +++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpBase.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.platform.http.springboot; +import org.apache.camel.CamelContext; import org.junit.jupiter.api.Test; import org.assertj.core.api.Assertions; @@ -24,20 +25,20 @@ import org.springframework.boot.test.web.client.TestRestTemplate; abstract class PlatformHttpBase { - @Autowired - private TestRestTemplate restTemplate; - - @Test - public void testGet() { - Assertions.assertThat( - restTemplate.getForEntity("/myget", String.class).getStatusCodeValue()) - .isEqualTo(200); - } - - @Test - public void testPost() { - Assertions.assertThat( - restTemplate.postForEntity("/mypost", "test", String.class).getBody()) - .isEqualTo("TEST"); - } + @Autowired + TestRestTemplate restTemplate; + + @Autowired + CamelContext camelContext; + + @Test + public void testGet() throws Exception { + Assertions.assertThat(restTemplate.getForEntity("/myget", String.class).getStatusCode().value()).isEqualTo(200); + } + + @Test + public void testPost() throws Exception { + Assertions.assertThat(restTemplate.postForEntity("/mypost", "test", String.class).getBody()).isEqualTo("TEST"); + } + } diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpHandleWriteErrorTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpHandleWriteErrorTest.java new file mode 100644 index 00000000000..5bf56dd58af --- /dev/null +++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/SpringBootPlatformHttpHandleWriteErrorTest.java @@ -0,0 +1,119 @@ +/* + * 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.springboot; + +import jakarta.servlet.http.HttpServletResponse; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.platform.http.PlatformHttpEndpoint; +import org.apache.camel.component.platform.http.spi.PlatformHttpEngine; +import org.apache.camel.http.common.DefaultHttpBinding; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; + +import java.io.IOException; + +@EnableAutoConfiguration +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) +@CamelSpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { CamelAutoConfiguration.class, + SpringBootPlatformHttpHandleWriteErrorTest.class, SpringBootPlatformHttpHandleWriteErrorTest.TestConfiguration.class, + PlatformHttpComponentAutoConfiguration.class, SpringBootPlatformHttpAutoConfiguration.class }) +public class SpringBootPlatformHttpHandleWriteErrorTest extends PlatformHttpBase { + + @Test + @Override + public void testGet() throws Exception { + MockEndpoint me = camelContext.getEndpoint("mock:failure", MockEndpoint.class); + me.expectedMessageCount(0); + + super.testGet(); + + me.assertIsSatisfied(); + } + + @Test + @Override + public void testPost() throws Exception { + MockEndpoint me = camelContext.getEndpoint("mock:failure", MockEndpoint.class); + me.expectedMessageCount(1); + + Assertions.assertThat(restTemplate.postForEntity("/mypost", "test", String.class).getStatusCode().value()).isEqualTo(500); + + me.assertIsSatisfied(); + } + + // ************************************* + // Config + // ************************************* + @Configuration + public static class TestConfiguration { + + @Bean(name = "platform-http-engine") + public PlatformHttpEngine myHttpEngine(Environment env) { + return new MyEngine(); + } + + @Bean + public RouteBuilder servletPlatformHttpRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + onCompletion().onFailureOnly().to("log:failure").to("mock:failure"); + + from("platform-http:/myget").id("myget").setBody().constant("get"); + from("platform-http:/mypost?handleWriteResponseError=true").id("mypost").transform().body(String.class, b -> b.toUpperCase()); + } + }; + } + } + + private static class MyErrorBinding extends DefaultHttpBinding { + + @Override + public void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException { + // force an exception during writing response to simulate error at that point + String uri = exchange.getMessage().getHeader(Exchange.HTTP_URI, String.class); + if ("/mypost".equals(uri)) { + throw new IOException("Forced error"); + } else { + super.writeResponse(exchange, response); + } + } + } + + private static class MyEngine extends SpringBootPlatformHttpEngine { + + @Override + public SpringBootPlatformHttpConsumer createConsumer(PlatformHttpEndpoint endpoint, Processor processor) { + SpringBootPlatformHttpConsumer answer = new SpringBootPlatformHttpConsumer(endpoint, processor); + answer.setBinding(new MyErrorBinding()); + return answer; + } + } +}