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 c785a83d40e CAMEL-22166: camel-http - Also set HTTP_RESPONSE_CODE and
HTTP_RESPONSE_TEXT when throw exception on failure
c785a83d40e is described below
commit c785a83d40ec97073964cd517b4dc2ee33a4d3f9
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jun 12 11:16:50 2025 +0200
CAMEL-22166: camel-http - Also set HTTP_RESPONSE_CODE and
HTTP_RESPONSE_TEXT when throw exception on failure
---
.../java/org/apache/camel/component/http/HttpProducer.java | 14 ++++++--------
.../component/http/HttpThrowExceptionOnFailureTest.java | 6 ++++++
2 files changed, 12 insertions(+), 8 deletions(-)
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 866d4c2e91a..672eb853f7e 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -261,6 +261,7 @@ public class HttpProducer extends DefaultProducer
implements LineNumberAware {
if (LOG.isDebugEnabled()) {
LOG.debug("Http responseCode: {}",
responseCode);
}
+ populateResponseCode(exchange.getOut(),
httpResponse, responseCode);
if (!throwException) {
// if we do not use failed exception then
populate response for all response codes
@@ -355,7 +356,7 @@ public class HttpProducer extends DefaultProducer
implements LineNumberAware {
throws IOException, ClassNotFoundException {
// We just make the out message is not create when extractResponseBody
throws exception
Object response = extractResponseBody(httpResponse, exchange,
getEndpoint().isIgnoreResponseBody());
- final Message answer = createResponseMessage(exchange, httpResponse,
responseCode);
+ Message answer = exchange.getOut();
answer.setBody(response);
if (!getEndpoint().isSkipResponseHeaders()) {
@@ -406,19 +407,16 @@ public class HttpProducer extends DefaultProducer
implements LineNumberAware {
}
}
- private static Message createResponseMessage(Exchange exchange,
ClassicHttpResponse httpResponse, int responseCode) {
- Message answer = exchange.getOut();
-
+ private static void populateResponseCode(Message message,
ClassicHttpResponse httpResponse, int responseCode) {
// optimize for 200 response code as the boxing is outside the cached
integers
if (responseCode == 200) {
- answer.setHeader(HttpConstants.HTTP_RESPONSE_CODE,
OK_RESPONSE_CODE);
+ message.setHeader(HttpConstants.HTTP_RESPONSE_CODE,
OK_RESPONSE_CODE);
} else {
- answer.setHeader(HttpConstants.HTTP_RESPONSE_CODE, responseCode);
+ message.setHeader(HttpConstants.HTTP_RESPONSE_CODE, responseCode);
}
if (httpResponse.getReasonPhrase() != null) {
- answer.setHeader(HttpConstants.HTTP_RESPONSE_TEXT,
httpResponse.getReasonPhrase());
+ message.setHeader(HttpConstants.HTTP_RESPONSE_TEXT,
httpResponse.getReasonPhrase());
}
- return answer;
}
protected Exception populateHttpOperationFailedException(
diff --git
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
index aa92fe82bcc..71a409a4fe1 100644
---
a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
+++
b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpThrowExceptionOnFailureTest.java
@@ -82,6 +82,12 @@ public class HttpThrowExceptionOnFailureTest extends
BaseHttpTest {
assertNotNull(e, "Should have thrown an exception");
HttpOperationFailedException cause =
assertIsInstanceOf(HttpOperationFailedException.class, e);
assertEquals(501, cause.getStatusCode());
+
+ Message out = reply.getMessage();
+ assertNotNull(out);
+ Map<String, Object> headers = out.getHeaders();
+ assertEquals(HttpStatus.SC_NOT_IMPLEMENTED,
headers.get(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals("Not Implemented",
headers.get(Exchange.HTTP_RESPONSE_TEXT));
}
@Test