This is an automated email from the ASF dual-hosted git repository.
ffang 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 46b6e88dfa8e [CAMEL-22823]Ensure Null Response Entity will be honored
when StreamCache kicks in (#20707)
46b6e88dfa8e is described below
commit 46b6e88dfa8e458c3cd30ced9c85cc7d6c80788b
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Wed Jan 7 11:54:21 2026 -0500
[CAMEL-22823]Ensure Null Response Entity will be honored when StreamCache
kicks in (#20707)
---
.../camel/component/cxf/jaxrs/CxfConverter.java | 6 +++
.../cxf/jaxrs/CxfRsConvertBodyToTest.java | 43 ++++++++++++++++++++++
2 files changed, 49 insertions(+)
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 60f57fcb5eb8..7815650c7748 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
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.cxf.jaxrs;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -140,6 +141,11 @@ public final class CxfConverter {
// Convert the body (entity) to an InputStream
InputStream is = toInputStream(response, exchange);
+ //If there is no entity body, provide an empty stream
+ // to ensure we return a valid StreamCache instead of null (which
would be wrapped as Void.class).
+ if (is == null) {
+ is = new ByteArrayInputStream(new byte[0]);
+ }
// Find the appropriate TypeConverter for StreamCache
TypeConverterRegistry registry =
exchange.getContext().getTypeConverterRegistry();
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 c1d2f7bb1d71..a6402f350c1c 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
@@ -50,6 +50,13 @@ public class CxfRsConvertBodyToTest extends CamelTestSupport
{
private static final String CXF_RS_STATUS_TEST_URL
= "http://localhost:" + CXFTestSupport.getPort2() +
"/reststatus/customerservice/customers";
+ private static final String CXF_RS_EMPTY_BODY_TEST_URI
+ = "cxfrs://http://localhost:" + CXFTestSupport.getPort3()
+ +
"/restempty?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
+
+ private static final String CXF_RS_EMPTY_BODY_TEST_URL
+ = "http://localhost:" + CXFTestSupport.getPort3() +
"/restempty/customerservice/customers";
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@@ -75,6 +82,17 @@ public class CxfRsConvertBodyToTest extends CamelTestSupport
{
})
// The .log() EIP here is the one that triggered the
StreamCache
.log("Checking if status is still 202 after logging.");
+
+ // Route for testing empty body scenario (No Entity)
+ from(CXF_RS_EMPTY_BODY_TEST_URI)
+ .to("mock:emptyBodyResult")
+ .process(exchange -> {
+ // Creating a response with NO entity
+ Response resp = Response.status(204).build();
+ exchange.getMessage().setBody(resp);
+ })
+ .log("Logging to trigger conversion with empty body");
+
}
};
}
@@ -123,4 +141,29 @@ public class CxfRsConvertBodyToTest extends
CamelTestSupport {
mock.assertIsSatisfied();
}
+
+ @Test
+ public void testEmptyResponseStatusPreservedAfterConversion() throws
Exception {
+ MockEndpoint mock = getMockEndpoint("mock:emptyBodyResult");
+ mock.expectedMessageCount(1);
+
+ HttpPut put = new HttpPut(CXF_RS_EMPTY_BODY_TEST_URL);
+ StringEntity entity = new StringEntity(PUT_REQUEST,
ContentType.parse("text/xml; charset=ISO-8859-1"));
+ put.setEntity(entity);
+
+ try (CloseableHttpClient httpclient =
HttpClientBuilder.create().build();
+ CloseableHttpResponse response = httpclient.execute(put)) {
+
+ // Verify status 204 is preserved even with no entity body
+ assertEquals(204, response.getCode(), "Status 204 should be
preserved for empty response");
+
+ // Entity should be null or empty
+ if (response.getEntity() != null) {
+ assertEquals("", EntityUtils.toString(response.getEntity()));
+ }
+ }
+
+ mock.assertIsSatisfied();
+ }
+
}