This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/jaxrs-fault in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 81a1f7ea9b5857c60972a274e34ce997a8347986 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Thu Nov 19 07:07:04 2026 +0000 follow-up: Use FaultUtils in JAX-RS default fault interceptor --- .../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 8b1348683be..e3378f9d71f 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 704bb0c58ea..d61471744c0 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 4d6cb3eaad0..5dda99f212c 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 @@ -1217,7 +1217,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);
