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

coheigea pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit f6b516475838753e81671b9922ec0c7e1b2c00b6
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Thu Nov 19 13:41:18 2026 +0000

    follow-up: Use FaultUtils in JAX-RS default fault interceptor (#3742)
    
    (cherry picked from commit cf1610f4979853be8f24b8ac7d3a5a414dd21d4f)
---
 .../JAXRSDefaultFaultOutInterceptor.java           |  4 +-
 .../JAXRSDefaultFaultOutInterceptorTest.java       | 92 ++++++++++++++++++++++
 .../ws/addressing/impl/InternalContextUtils.java   |  3 +-
 .../systest/jaxrs/JAXRSClientServerBookTest.java   |  2 +-
 4 files changed, 97 insertions(+), 4 deletions(-)

diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptor.java
 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptor.java
index 3e6cac8e7af..57858dcca04 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptor.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptor.java
@@ -33,6 +33,7 @@ import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.NSStack;
 import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.FaultUtils;
 import org.apache.cxf.interceptor.StaxOutInterceptor;
 import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
@@ -87,8 +88,7 @@ public class JAXRSDefaultFaultOutInterceptor extends 
AbstractOutDatabindingInter
                                         
"http://cxf.apache.org/bindings/xformat";);
             StaxUtils.writeStartElement(writer, prefix, "faultstring",
                                         
"http://cxf.apache.org/bindings/xformat";);
-            Throwable t = f.getCause();
-            writer.writeCharacters(t == null ? f.getMessage() : t.toString());
+            writer.writeCharacters(FaultUtils.getFaultString(message, f));
             // fault string
             writer.writeEndElement();
             // call StaxUtils to write Fault detail.
diff --git 
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptorTest.java
 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptorTest.java
new file mode 100644
index 00000000000..ebafa464c3f
--- /dev/null
+++ 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/interceptor/JAXRSDefaultFaultOutInterceptorTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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.cxf.jaxrs.interceptor;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.ResourceBundle;
+
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.staxutils.StaxUtils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class JAXRSDefaultFaultOutInterceptorTest {
+
+    @Test
+    public void testFaultCauseNotLeakedByDefault() throws Exception {
+        String out = writeFault(false);
+        assertTrue(out, out.contains(">Application fault</"));
+        assertFalse(out, out.contains(IllegalStateException.class.getName()));
+        assertFalse(out, out.contains("internal detail"));
+    }
+
+    @Test
+    public void testFaultCauseIncludedWhenEnabled() throws Exception {
+        String out = writeFault(true);
+        assertTrue(out, out.contains(">Application fault Caused by: internal 
detail</"));
+        assertFalse(out, out.contains(IllegalStateException.class.getName()));
+    }
+
+    private static String writeFault(boolean causeEnabled) throws Exception {
+        Endpoint endpoint = mock(Endpoint.class);
+        when(endpoint.get(ServerProviderFactory.class.getName()))
+            .thenReturn(ServerProviderFactory.getInstance());
+
+        Message inMessage = new MessageImpl();
+        Exchange exchange = new ExchangeImpl();
+        exchange.put(Endpoint.class, endpoint);
+        exchange.setInMessage(inMessage);
+        inMessage.setExchange(exchange);
+
+        Message outMessage = new MessageImpl();
+        outMessage.setExchange(exchange);
+        exchange.setOutFaultMessage(outMessage);
+        if (causeEnabled) {
+            outMessage.put(Message.EXCEPTION_MESSAGE_CAUSE_ENABLED, 
Boolean.TRUE);
+        }
+
+        Fault fault = new Fault(new 
org.apache.cxf.common.i18n.Message("Application fault",
+                                    (ResourceBundle) null),
+                                new IllegalStateException("internal detail"));
+        outMessage.setContent(Exception.class, fault);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos);
+        outMessage.setContent(XMLStreamWriter.class, writer);
+
+        new JAXRSDefaultFaultOutInterceptor().handleMessage(outMessage);
+        writer.flush();
+        return new String(bos.toByteArray(), StandardCharsets.UTF_8);
+    }
+}
diff --git 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/InternalContextUtils.java
 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/InternalContextUtils.java
index 23b38da631e..646bba15d84 100644
--- 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/InternalContextUtils.java
+++ 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/InternalContextUtils.java
@@ -40,6 +40,7 @@ import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.endpoint.NullConduitSelector;
 import org.apache.cxf.endpoint.PreexistingConduitSelector;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.FaultUtils;
 import org.apache.cxf.interceptor.InterceptorChain;
 import org.apache.cxf.interceptor.OutgoingChainInterceptor;
 import org.apache.cxf.io.DelegatingInputStream;
@@ -212,7 +213,7 @@ final class InternalContextUtils {
                     }
 
                     // send the partial response to requester
-                    partialResponse.put("forced.faultstring",
+                    partialResponse.put(FaultUtils.FORCED_FAULT_STRING,
                                         "The server sent HTTP status code :"
                                             + 
inMessage.getExchange().get(Message.RESPONSE_CODE));
                     partialResponse.setContent(Exception.class, exception);
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 1ee2636e47f..fabd6d653b0 100644
--- 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -1214,7 +1214,7 @@ public class JAXRSClientServerBookTest extends 
AbstractBusClientServerTestBase {
     public void testPropogateException2() throws Exception {
         String data = "<ns1:XMLFault 
xmlns:ns1=\"http://cxf.apache.org/bindings/xformat\";>"
             + "<ns1:faultstring 
xmlns:ns1=\"http://cxf.apache.org/bindings/xformat\";>"
-            + "org.apache.cxf.systest.jaxrs.BookNotFoundFault: Book 
Exception</ns1:faultstring>"
+            + "Book Exception</ns1:faultstring>"
             + "</ns1:XMLFault>";
         getAndCompare("http://localhost:"; + PORT + 
"/bookstore/propagate-exception2",
                       data, "application/xml", null, 500);

Reply via email to