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 f335b58667a6 CAMEL-27372: camel-cxf - a consumer in PAYLOAD mode sends 
a soap:Fault String or DOM body as a fault (#30583)
f335b58667a6 is described below

commit f335b58667a6e82293f8829a4ef19d90bfe91223
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 11:32:16 2026 +0100

    CAMEL-27372: camel-cxf - a consumer in PAYLOAD mode sends a soap:Fault 
String or DOM body as a fault (#30583)
    
    A route behind a cxf: consumer in PAYLOAD mode that replied with a 
<soap:Fault>
    element as a String or DOM body sent it to the client inside a normal 
response
    with HTTP 200. Only a CxfPayload body was checked for a Fault element: the
    binding converts other bodies into a CxfPayload for the response it writes,
    but does not set it on the message, so the fault check after the conversion
    never saw it.
    
    The consumer now also checks a String, a DOM Node or a DOMSource body for a
    SOAP 1.1 or 1.2 Fault element. A String is only parsed when it mentions 
Fault
    and a SOAP envelope namespace, and a stream body is never read for the 
check.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../apache/camel/catalog/docs/cxf-component.adoc   |  18 +++
 .../src/main/docs/cxf-component.adoc               |  18 +++
 .../camel/component/cxf/jaxws/CxfConsumer.java     |  51 +++++--
 .../jaxws/CxfConsumerPayLoadStringFaultTest.java   | 163 +++++++++++++++++++++
 4 files changed, 241 insertions(+), 9 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc
index 94d386e0b242..84e1d51ac698 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cxf-component.adoc
@@ -926,6 +926,24 @@ Then throw it as you like:
 from(routerEndpointURI).process(exchange -> 
exchange.getMessage().setBody(SOAP_FAULT));
 ----
 
+In the `PAYLOAD` data format, the route can also reply with the SOAP Fault 
element itself, as a `String`, a DOM
+`Document` or `Element`, or a `CxfPayload`. A `Fault` element in the SOAP 1.1 
or SOAP 1.2 envelope namespace is sent
+to the client as a SOAP fault (HTTP status 500), with its fault code, fault 
string (or reason) and detail:
+
+[source,yaml]
+----
+- route:
+    from:
+      uri: cxf://http://localhost:9000/orders
+      parameters:
+        wsdlURL: classpath:wsdl/orders.wsdl
+        dataFormat: PAYLOAD
+      steps:
+        - setBody:
+            constant:
+              expression: '<soap:Fault 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";><faultcode>soap:Client</faultcode><faultstring>No
 such order</faultstring></soap:Fault>'
+----
+
 
 If your CXF endpoint is working in the `RAW` data format, you could
 set the SOAP Fault message in the message body and set the response
diff --git 
a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc 
b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
index 94d386e0b242..84e1d51ac698 100644
--- a/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
+++ b/components/camel-cxf/camel-cxf-soap/src/main/docs/cxf-component.adoc
@@ -926,6 +926,24 @@ Then throw it as you like:
 from(routerEndpointURI).process(exchange -> 
exchange.getMessage().setBody(SOAP_FAULT));
 ----
 
+In the `PAYLOAD` data format, the route can also reply with the SOAP Fault 
element itself, as a `String`, a DOM
+`Document` or `Element`, or a `CxfPayload`. A `Fault` element in the SOAP 1.1 
or SOAP 1.2 envelope namespace is sent
+to the client as a SOAP fault (HTTP status 500), with its fault code, fault 
string (or reason) and detail:
+
+[source,yaml]
+----
+- route:
+    from:
+      uri: cxf://http://localhost:9000/orders
+      parameters:
+        wsdlURL: classpath:wsdl/orders.wsdl
+        dataFormat: PAYLOAD
+      steps:
+        - setBody:
+            constant:
+              expression: '<soap:Fault 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";><faultcode>soap:Client</faultcode><faultstring>No
 such order</faultstring></soap:Fault>'
+----
+
 
 If your CXF endpoint is working in the `RAW` data format, you could
 set the SOAP Fault message in the message body and set the response
diff --git 
a/components/camel-cxf/camel-cxf-soap/src/main/java/org/apache/camel/component/cxf/jaxws/CxfConsumer.java
 
b/components/camel-cxf/camel-cxf-soap/src/main/java/org/apache/camel/component/cxf/jaxws/CxfConsumer.java
index 1423dde3dafe..698f2a69640e 100644
--- 
a/components/camel-cxf/camel-cxf-soap/src/main/java/org/apache/camel/component/cxf/jaxws/CxfConsumer.java
+++ 
b/components/camel-cxf/camel-cxf-soap/src/main/java/org/apache/camel/component/cxf/jaxws/CxfConsumer.java
@@ -25,7 +25,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import jakarta.xml.ws.WebFault;
 
 import javax.xml.namespace.QName;
+import javax.xml.transform.dom.DOMSource;
 
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -157,6 +159,8 @@ public class CxfConsumer extends DefaultConsumer implements 
Suspendable {
 
         private static final String COMPLETED = 
"org.apache.camel.component.cxf.jaxws.completed";
         private static final String MUTED_FAULT_MESSAGE = "Exchange processing 
failed";
+        private static final String SOAP11_ENVELOPE_NS = 
"http://schemas.xmlsoap.org/soap/envelope/";;
+        private static final String SOAP12_ENVELOPE_NS = 
"http://www.w3.org/2003/05/soap-envelope";;
 
         private final CxfEndpoint endpoint;
 
@@ -389,7 +393,7 @@ public class CxfConsumer extends DefaultConsumer implements 
Suspendable {
             // an exception set on the exchange is a route failure; one 
carried in the body is a fault the route
             // chose to return, and stays part of the contract either way
             final Throwable routeFailure = camelExchange.getException();
-            final Throwable t = routeFailure != null ? routeFailure : 
extractFromBody(camelExchange, null);
+            final Throwable t = routeFailure != null ? routeFailure : 
extractFromBody(camelExchange);
 
             if (t != null) {
                 cxfExchange.getInMessage().put(FaultMode.class, 
FaultMode.UNCHECKED_APPLICATION_FAULT);
@@ -465,15 +469,39 @@ public class CxfConsumer extends DefaultConsumer 
implements Suspendable {
         /**
          * SOAP faults can be stored as exceptions as message body (to be 
backwards compatible).
          */
-        private static Throwable extractFromBody(org.apache.camel.Exchange 
camelExchange, Throwable t) {
+        private static Throwable extractFromBody(org.apache.camel.Exchange 
camelExchange) {
             Object body = camelExchange.getMessage().getBody();
             if (body instanceof Throwable throwable) {
-                t = throwable;
+                return throwable;
             } else if (body instanceof CxfPayload<?> payload) {
                 // Check if the CxfPayload contains a SOAP Fault element set 
directly as body
-                t = extractFaultFromPayload(payload);
+                return extractFaultFromPayload(payload);
+            } else if 
(camelExchange.getProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.class) 
== DataFormat.PAYLOAD) {
+                // the binding converts the body into a CxfPayload only for 
the response it writes, so a SOAP Fault
+                // element in a String or DOM body is detected here
+                return extractFault(faultElementCandidate(camelExchange, 
body));
             }
-            return t;
+            return null;
+        }
+
+        /**
+         * The root element of a String or DOM body that may be a SOAP Fault. 
A String is only parsed when it mentions a
+         * Fault in a SOAP envelope namespace, and a stream is never read 
here, as that would consume it.
+         */
+        private static Element faultElementCandidate(org.apache.camel.Exchange 
camelExchange, Object body) {
+            Node node = null;
+            if (body instanceof Node n) {
+                node = n;
+            } else if (body instanceof DOMSource source) {
+                node = source.getNode();
+            } else if (body instanceof String text && text.contains("Fault")
+                    && (text.contains(SOAP11_ENVELOPE_NS) || 
text.contains(SOAP12_ENVELOPE_NS))) {
+                node = 
camelExchange.getContext().getTypeConverter().tryConvertTo(Document.class, 
camelExchange, text);
+            }
+            if (node instanceof Document document) {
+                return document.getDocumentElement();
+            }
+            return node instanceof Element element ? element : null;
         }
 
         /**
@@ -487,13 +515,18 @@ public class CxfConsumer extends DefaultConsumer 
implements Suspendable {
             if (elements == null || elements.size() != 1) {
                 return null;
             }
-            Element element = elements.get(0);
-            if (!"Fault".equals(element.getLocalName())) {
+            return extractFault(elements.get(0));
+        }
+
+        /**
+         * Converts a SOAP 1.1 or 1.2 Fault element to a SoapFault, or returns 
null when the element is no SOAP Fault.
+         */
+        private static SoapFault extractFault(Element element) {
+            if (element == null || !"Fault".equals(element.getLocalName())) {
                 return null;
             }
             String nsUri = element.getNamespaceURI();
-            if (!"http://schemas.xmlsoap.org/soap/envelope/".equals(nsUri)
-                    && 
!"http://www.w3.org/2003/05/soap-envelope".equals(nsUri)) {
+            if (!SOAP11_ENVELOPE_NS.equals(nsUri) && 
!SOAP12_ENVELOPE_NS.equals(nsUri)) {
                 return null;
             }
 
diff --git 
a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayLoadStringFaultTest.java
 
b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayLoadStringFaultTest.java
new file mode 100644
index 000000000000..fba7ca987e91
--- /dev/null
+++ 
b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayLoadStringFaultTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.cxf.jaxws;
+
+import java.io.StringReader;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.time.Duration;
+
+import org.w3c.dom.Document;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A route of a consumer in PAYLOAD mode replies with a SOAP Fault element 
that is not a CxfPayload (a String or a DOM
+ * body): the client gets it as a SOAP fault (HTTP 500), not as a normal 
response.
+ */
+public class CxfConsumerPayLoadStringFaultTest extends CamelTestSupport {
+
+    private static final String SOAP11_FAULT
+            = "<soap:Fault 
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\";>"
+              + "<faultcode>soap:Client</faultcode><faultstring>No such 
person</faultstring></soap:Fault>";
+
+    private static final String SOAP12_FAULT
+            = "<env:Fault 
xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\";>"
+              + "<env:Code><env:Value>env:Sender</env:Value></env:Code>"
+              + "<env:Reason><env:Text xml:lang=\"en\">No such 
person</env:Text></env:Reason></env:Fault>";
+
+    private static final String RESPONSE
+            = "<GetPersonResponse 
xmlns=\"http://camel.apache.org/wsdl-first/types\";>"
+              + 
"<personId>1</personId><ssn>000-000-0000</ssn><name>Fault</name></GetPersonResponse>";
+
+    private static final String REQUEST_BODY
+            = "<GetPerson 
xmlns=\"http://camel.apache.org/wsdl-first/types\";><personId>1</personId></GetPerson>";
+
+    private static final String SOAP11_REQUEST
+            = "<soap:Envelope 
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\";><soap:Body>" + 
REQUEST_BODY
+              + "</soap:Body></soap:Envelope>";
+
+    private static final String SOAP12_REQUEST
+            = "<soap:Envelope 
xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\";><soap:Body>" + 
REQUEST_BODY
+              + "</soap:Body></soap:Envelope>";
+
+    private static AvailablePortFinder.Port port;
+
+    private final HttpClient client = 
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
+
+    @BeforeAll
+    static void allocatePorts() {
+        port = AvailablePortFinder.find();
+    }
+
+    @AfterAll
+    static void releasePorts() {
+        if (port != null) {
+            port.release();
+        }
+    }
+
+    private static String address(String path) {
+        return "http://localhost:"; + port.getPort() + 
"/CxfConsumerPayLoadStringFaultTest/" + path;
+    }
+
+    private static String consumer(String path, String serviceName) {
+        return "cxf://" + address(path) + 
"?wsdlURL=classpath:person.wsdl&dataFormat=PAYLOAD"
+               + "&serviceName={http://camel.apache.org/wsdl-first}"; + 
serviceName
+               + "&portName={http://camel.apache.org/wsdl-first}soap";;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from(consumer("string11", 
"PersonService")).setBody(constant(SOAP11_FAULT));
+
+                from(consumer("validated11", "PersonService") + 
"&schemaValidationEnabled=true")
+                        .setBody(constant(SOAP11_FAULT));
+
+                from(consumer("dom11", "PersonService")).process(exchange -> {
+                    Document doc = StaxUtils.read(new 
StringReader(SOAP11_FAULT));
+                    exchange.getMessage().setBody(doc);
+                });
+
+                from(consumer("string12", 
"PersonService12")).setBody(constant(SOAP12_FAULT));
+
+                from(consumer("response", 
"PersonService")).setBody(constant(RESPONSE));
+            }
+        };
+    }
+
+    @Test
+    public void testSoap11FaultAsString() throws Exception {
+        HttpResponse<String> response = post("string11", SOAP11_REQUEST, 
"text/xml; charset=UTF-8");
+        assertEquals(500, response.statusCode(), response.body());
+        assertTrue(response.body().contains("<faultstring>No such 
person</faultstring>"), response.body());
+        assertTrue(response.body().contains(":Client</faultcode>"), 
response.body());
+    }
+
+    @Test
+    public void testSoap11FaultAsStringWithSchemaValidation() throws Exception 
{
+        HttpResponse<String> response = post("validated11", SOAP11_REQUEST, 
"text/xml; charset=UTF-8");
+        assertEquals(500, response.statusCode(), response.body());
+        assertTrue(response.body().contains("<faultstring>No such 
person</faultstring>"), response.body());
+    }
+
+    @Test
+    public void testSoap11FaultAsDocument() throws Exception {
+        HttpResponse<String> response = post("dom11", SOAP11_REQUEST, 
"text/xml; charset=UTF-8");
+        assertEquals(500, response.statusCode(), response.body());
+        assertTrue(response.body().contains("<faultstring>No such 
person</faultstring>"), response.body());
+    }
+
+    @Test
+    public void testSoap12FaultAsString() throws Exception {
+        HttpResponse<String> response = post("string12", SOAP12_REQUEST, 
"application/soap+xml; charset=UTF-8");
+        assertEquals(500, response.statusCode(), response.body());
+        assertTrue(response.body().contains("No such person</soap:Text>"), 
response.body());
+        assertTrue(response.body().contains(":Sender</soap:Value>"), 
response.body());
+    }
+
+    @Test
+    public void testResponseAsString() throws Exception {
+        HttpResponse<String> response = post("response", SOAP11_REQUEST, 
"text/xml; charset=UTF-8");
+        assertEquals(200, response.statusCode(), response.body());
+        assertTrue(response.body().contains("<name>Fault</name>"), 
response.body());
+        assertFalse(response.body().contains("faultstring"), response.body());
+    }
+
+    private HttpResponse<String> post(String path, String body, String 
contentType) throws Exception {
+        HttpRequest request = HttpRequest.newBuilder(URI.create(address(path)))
+                .timeout(Duration.ofSeconds(30))
+                .header("Content-Type", contentType)
+                .POST(HttpRequest.BodyPublishers.ofString(body))
+                .build();
+        return client.send(request, HttpResponse.BodyHandlers.ofString());
+    }
+}

Reply via email to