This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch fix/CAMEL-24176
in repository https://gitbox.apache.org/repos/asf/camel.git

commit c474fae4b8a3da986cef2975c799d73d9494cdc5
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jul 20 09:34:29 2026 +0200

    CAMEL-24176: camel-cxfrs - Fix async producer never mapping HTTP errors to 
CxfOperationException
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/component/cxf/jaxrs/CxfRsProducer.java   | 28 ++++++++++++----------
 .../cxf/jaxrs/CxfRsAsyncProducerTest.java          | 20 ++++++++++++++++
 2 files changed, 36 insertions(+), 12 deletions(-)

diff --git 
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
 
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
index 84c5e7eb7b48..f4abc3f0d2e3 100644
--- 
a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
+++ 
b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
@@ -746,17 +746,19 @@ public class CxfRsProducer extends DefaultAsyncProducer {
         }
 
         private void fail(Throwable throwable) {
-            if 
(throwable.getClass().isInstance(WebApplicationException.class)) {
-                final WebApplicationException cast = 
WebApplicationException.class.cast(throwable);
-                final Response response = cast.getResponse();
+            if (throwable instanceof WebApplicationException wae) {
+                Response response = wae.getResponse();
                 if (shouldHandleError(response)) {
                     handleError(response);
+                } else {
+                    exchange.setException(throwable);
                 }
-            } else if 
(throwable.getClass().isInstance(ResponseProcessingException.class)) {
-                final ResponseProcessingException cast = 
ResponseProcessingException.class.cast(throwable);
-                final Response response = cast.getResponse();
+            } else if (throwable instanceof ResponseProcessingException rpe) {
+                Response response = rpe.getResponse();
                 if (shouldHandleError(response)) {
                     handleError(response);
+                } else {
+                    exchange.setException(throwable);
                 }
             } else {
                 exchange.setException(throwable);
@@ -839,17 +841,19 @@ public class CxfRsProducer extends DefaultAsyncProducer {
         }
 
         private void fail(Throwable throwable) {
-            if 
(throwable.getClass().isInstance(WebApplicationException.class)) {
-                final WebApplicationException cast = 
WebApplicationException.class.cast(throwable);
-                final Response response = cast.getResponse();
+            if (throwable instanceof WebApplicationException wae) {
+                Response response = wae.getResponse();
                 if (shouldHandleError(response)) {
                     handleError(response);
+                } else {
+                    exchange.setException(throwable);
                 }
-            } else if 
(throwable.getClass().isInstance(ResponseProcessingException.class)) {
-                final ResponseProcessingException cast = 
ResponseProcessingException.class.cast(throwable);
-                final Response response = cast.getResponse();
+            } else if (throwable instanceof ResponseProcessingException rpe) {
+                Response response = rpe.getResponse();
                 if (shouldHandleError(response)) {
                     handleError(response);
+                } else {
+                    exchange.setException(throwable);
                 }
             } else {
                 exchange.setException(throwable);
diff --git 
a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsAsyncProducerTest.java
 
b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsAsyncProducerTest.java
index f5c4fe0c8f59..90ffd0c329a2 100644
--- 
a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsAsyncProducerTest.java
+++ 
b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsAsyncProducerTest.java
@@ -44,6 +44,7 @@ import 
org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -506,4 +507,23 @@ public class CxfRsAsyncProducerTest extends 
CamelSpringTestSupport {
 
         assertEquals(422, cxfOperationException.getStatusCode(), "CXF 
operation exception has correct response code");
     }
+
+    @Test
+    public void 
testAsyncProxyProducerServerErrorMappedToCxfOperationException() {
+        Exchange exchange = template.send("direct://proxy", newExchange -> {
+            newExchange.setPattern(ExchangePattern.InOut);
+            Message inMessage = newExchange.getIn();
+            inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer");
+            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, 
Boolean.FALSE);
+            // pass an invalid id that causes NumberFormatException on the 
server (500 error),
+            // which the proxy client receives as a WebApplicationException 
through failed()
+            inMessage.setBody("invalid");
+        });
+
+        assertNotNull(exchange.getException(), "Expect an exception on the 
exchange");
+        assertInstanceOf(CxfOperationException.class, exchange.getException(),
+                "Async proxy producer should map WebApplicationException to 
CxfOperationException");
+        CxfOperationException coe = (CxfOperationException) 
exchange.getException();
+        assertEquals(500, coe.getStatusCode(), "Status code should be 500");
+    }
 }

Reply via email to