This is an automated email from the ASF dual-hosted git repository.
ffang pushed a commit to branch camel-4.14.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.14.x by this push:
new 8db5eeb69960 [CAMEL-22754]ensure we save response code when needing to
cache the inputstream (#20339)
8db5eeb69960 is described below
commit 8db5eeb69960bb77d3d4baa6e54b50c14e7e4f8a
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Wed Dec 10 11:20:47 2025 -0500
[CAMEL-22754]ensure we save response code when needing to cache the
inputstream (#20339)
(cherry picked from commit b0c5ae0869d9c76845c0310dfc2259b58b9562a1)
---
.../camel/component/cxf/jaxrs/CxfConverter.java | 16 +++++++
.../cxf/jaxrs/CxfRsConvertBodyToTest.java | 50 +++++++++++++++++++++-
2 files changed, 65 insertions(+), 1 deletion(-)
diff --git
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfConverter.java
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfConverter.java
index 3db5285c1cf3..60f57fcb5eb8 100644
---
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfConverter.java
+++
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfConverter.java
@@ -126,16 +126,32 @@ public final class CxfConverter {
@Converter(allowNull = true)
public static StreamCache toStreamCache(Response response, Exchange
exchange) {
+ if (response == null) {
+ return null;
+ }
+
+ // retrieve the HTTP status from the Response object
+ // and set it explicitly on the Exchange's IN message headers.
+ // This ensures the status code is saved before the conversion loses
the Response object context.
+ int status = response.getStatus();
+ if (status > 0) {
+ exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE,
status);
+ }
+
+ // Convert the body (entity) to an InputStream
InputStream is = toInputStream(response, exchange);
+ // Find the appropriate TypeConverter for StreamCache
TypeConverterRegistry registry =
exchange.getContext().getTypeConverterRegistry();
TypeConverter tc = registry.lookup(StreamCache.class, is.getClass());
if (tc != null) {
+ // Convert the InputStream payload into a StreamCache
return tc.convertTo(StreamCache.class, exchange, is);
}
return null;
+
}
/**
diff --git
a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
index 8f9814124fd9..c1d2f7bb1d71 100644
---
a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
+++
b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
@@ -33,6 +33,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
public class CxfRsConvertBodyToTest extends CamelTestSupport {
private static final String PUT_REQUEST =
"<Customer><name>Mary</name><id>123</id></Customer>";
@@ -41,6 +42,14 @@ public class CxfRsConvertBodyToTest extends CamelTestSupport
{
= "cxfrs://http://localhost:" + CXT
+
"/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
+ // Define a separate endpoint for the status code test to avoid
interference
+ private static final String CXF_RS_STATUS_TEST_URI
+ = "cxfrs://http://localhost:" + CXFTestSupport.getPort2()
+ +
"/reststatus?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
+
+ private static final String CXF_RS_STATUS_TEST_URL
+ = "http://localhost:" + CXFTestSupport.getPort2() +
"/reststatus/customerservice/customers";
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@@ -51,8 +60,21 @@ public class CxfRsConvertBodyToTest extends CamelTestSupport
{
// should be able to convert to Customer
.convertBodyTo(Customer.class)
.to("mock:result")
- // respond with OK
+ // respond with OK (status 200)
.transform(constant(ok));
+
+ // New Route: Intentionally uses an EIP (.log()) after setting
the JAX-RS Response
+ // to trigger the StreamCache conversion.
+ from(CXF_RS_STATUS_TEST_URI)
+ .to("mock:statusCheckResult")
+ // Set Response object with custom status 202 inside a
processor
+ .process(exchange -> {
+ // Simulate setting the Response object
+ Response resp =
Response.status(202).entity("").build();
+ exchange.getMessage().setBody(resp);
+ })
+ // The .log() EIP here is the one that triggered the
StreamCache
+ .log("Checking if status is still 202 after logging.");
}
};
}
@@ -73,6 +95,32 @@ public class CxfRsConvertBodyToTest extends CamelTestSupport
{
assertEquals(200, response.getCode());
assertEquals("", EntityUtils.toString(response.getEntity()));
}
+
+ mock.assertIsSatisfied();
}
+ @Test
+ public void testResponseStatusPreservedAfterConversion() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:statusCheckResult");
+ mock.expectedMessageCount(1);
+
+ HttpPut put = new HttpPut(CXF_RS_STATUS_TEST_URL);
+ StringEntity entity = new StringEntity(PUT_REQUEST,
ContentType.parse("text/xml; charset=ISO-8859-1"));
+ put.setEntity(entity);
+
+ int expectedStatus = 202;
+
+ try (CloseableHttpClient httpclient =
HttpClientBuilder.create().build();
+ CloseableHttpResponse response = httpclient.execute(put)) {
+
+ assertEquals(expectedStatus, response.getCode(),
+ "The HTTP status code should be 202");
+
+ String responseBody = EntityUtils.toString(response.getEntity());
+ assertNotNull(responseBody);
+ assertEquals("", responseBody);
+ }
+
+ mock.assertIsSatisfied();
+ }
}