diff --git a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP11Fault.java b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP11Fault.java index 829abbb25..5c1746a0f 100644 --- a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP11Fault.java +++ b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP11Fault.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -76,7 +76,7 @@ class SOAP11Fault extends SOAPFaultBuilder { */ SOAP11Fault(QName code, String reason, String actor, Element detailObject) { this.faultcode = code; - this.faultstring = reason; + this.faultstring = createFaultString(reason); this.faultactor = actor; if (detailObject != null) { if ((detailObject.getNamespaceURI() == null || @@ -93,7 +93,7 @@ class SOAP11Fault extends SOAPFaultBuilder { SOAP11Fault(SOAPFault fault) { this.faultcode = fault.getFaultCodeAsQName(); - this.faultstring = fault.getFaultString(); + this.faultstring = createFaultString(fault.getFaultString()); this.faultactor = fault.getFaultActor(); if (fault.getDetail() != null) { detail = new DetailType(); diff --git a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP12Fault.java b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP12Fault.java index ecc7a8b1c..fd67e96ce 100644 --- a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP12Fault.java +++ b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAP12Fault.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -99,7 +99,7 @@ class SOAP12Fault extends SOAPFaultBuilder { SOAP12Fault(CodeType code, ReasonType reason, String node, String role, Element detailObject) { this.code = code; - this.reason = reason; + this.reason = isCaptureExceptionMessage() ? reason : new ReasonType(createFaultString()); this.node = node; this.role = role; if (detailObject != null) { @@ -122,7 +122,7 @@ class SOAP12Fault extends SOAPFaultBuilder { throw new WebServiceException(e); } - reason = new ReasonType(fault.getFaultString()); + reason = new ReasonType(createFaultString(fault.getFaultString())); role = fault.getFaultRole(); node = fault.getFaultNode(); if (fault.getDetail() != null) { diff --git a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAPFaultBuilder.java b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAPFaultBuilder.java index 754707532..3ff096fc8 100644 --- a/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAPFaultBuilder.java +++ b/jaxws-ri/runtime/rt/src/main/java/com/sun/xml/ws/fault/SOAPFaultBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -37,9 +37,11 @@ import jakarta.xml.soap.Detail; import jakarta.xml.soap.DetailEntry; import javax.xml.transform.dom.DOMResult; +import jakarta.xml.soap.SOAPException; import jakarta.xml.ws.ProtocolException; import jakarta.xml.ws.WebServiceException; import jakarta.xml.ws.soap.SOAPFaultException; + import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -56,6 +58,8 @@ */ public abstract class SOAPFaultBuilder { + private static final String SERVER_ERROR = "Server Error"; + /** * Default constructor. */ @@ -379,14 +383,9 @@ private static Message createSOAP11Fault(SOAPVersion soapVersion, Throwable e, O } if (faultString == null) { - if (!isCaptureExceptionMessage()) { - faultString = "Server Error"; - } - else { - faultString = e.getMessage(); - if (faultString == null) { - faultString = e.toString(); - } + faultString = e.getMessage(); + if (faultString == null) { + faultString = e.toString(); } } Element detailNode = null; @@ -483,7 +482,6 @@ private static Message createSOAP12Fault(SOAPVersion soapVersion, Throwable e, O } } - ReasonType reason = new ReasonType(faultString); Element detailNode = null; QName firstEntry = null; if (detail == null && soapFaultException != null) { @@ -501,6 +499,8 @@ private static Message createSOAP12Fault(SOAPVersion soapVersion, Throwable e, O } } + ReasonType reason = new ReasonType(faultString); + SOAP12Fault soap12Fault = new SOAP12Fault(code, reason, faultNode, faultRole, detailNode); //Don't fill the stacktrace for Service specific exceptions. @@ -579,4 +579,13 @@ private static JAXBContext createJAXBContext() { public static boolean isCaptureExceptionMessage() { return captureExceptionMessage; } + + + protected static String createFaultString(String faultString) { + return isCaptureExceptionMessage() ? faultString : SERVER_ERROR; + } + + protected static String createFaultString(){ + return SERVER_ERROR; + } } diff --git a/jaxws-ri/runtime/rt/src/test/java/com/sun/xml/ws/fault/SOAPFaultBuilderTest.java b/jaxws-ri/runtime/rt/src/test/java/com/sun/xml/ws/fault/SOAPFaultBuilderTest.java index e1b606c72..ddf9eb1a7 100644 --- a/jaxws-ri/runtime/rt/src/test/java/com/sun/xml/ws/fault/SOAPFaultBuilderTest.java +++ b/jaxws-ri/runtime/rt/src/test/java/com/sun/xml/ws/fault/SOAPFaultBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -21,10 +21,12 @@ import javax.xml.namespace.QName; import jakarta.xml.soap.Detail; import jakarta.xml.soap.MessageFactory; +import jakarta.xml.soap.SOAPConstants; import jakarta.xml.soap.SOAPElement; import jakarta.xml.soap.SOAPFactory; import jakarta.xml.soap.SOAPFault; import jakarta.xml.soap.SOAPMessage; + import javax.xml.stream.XMLStreamReader; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; @@ -122,24 +124,102 @@ public void testCreate12FaultFromFault() throws Exception { public void testCreate11FaultFromRE() { RuntimeException re = new RuntimeException("XML reader error: com.ctc.wstx.exc.WstxParsingException: Unexpected < character in element"); + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); try { SOAPFaultBuilder.setCaptureExceptionMessage(false); Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_11, null, re); - XMLStreamReader rdr = faultMsg.readPayload(); - while(rdr.hasNext()) { - int event = rdr.next(); - if (event == XMLStreamReader.START_ELEMENT) { - if (rdr.getName().getLocalPart().equals("faultstring")) { - event = rdr.next(); - assertEquals("Server Error", rdr.getText()); - } - } - } + verifyFaultStringForSoap11(faultMsg); } catch(Exception ex) { ex.printStackTrace(); fail(ex.getMessage()); } finally{ - SOAPFaultBuilder.setCaptureExceptionMessage(true); + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); + } + } + + public void testCreate12FaultFromRE() { + RuntimeException re = new RuntimeException( + "XML reader error: com.ctc.wstx.exc.WstxParsingException: Unexpected < character in element"); + + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); + try { + SOAPFaultBuilder.setCaptureExceptionMessage(false); + Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_12, null, re); + verifyFaultStringForSoap12(faultMsg); + } catch (Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } finally { + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); + } + } + + public void testCreateSOAPFaultMessageWithSOAPFaultArgumentForSOAP11() { + + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); + try { + SOAPFaultBuilder.setCaptureExceptionMessage(false); + Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_11, FAULT_11); + verifyFaultStringForSoap11(faultMsg); + } catch (Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } finally { + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); + } + } + + public void testCreateSOAPFaultMessageWithFaultStringArgumentForSOAP11() { + + String faultString = "Couldn't create SOAP message due to exception: XML reader error: " + + "com.ctc.wstx.exc.WstxParsingException: Unexpected '<' character in element " + + "(missing closing '>'?)"; + + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); + try { + SOAPFaultBuilder.setCaptureExceptionMessage(false); + Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_11, faultString, DETAIL1_QNAME); + verifyFaultStringForSoap11(faultMsg); + } catch (Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } finally { + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); + } + } + + public void testCreateSOAPFaultMessageWithSOAPFaultArgumentForSOAP12() { + + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); + try { + SOAPFaultBuilder.setCaptureExceptionMessage(false); + Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_12, FAULT_12); + verifyFaultStringForSoap12(faultMsg); + } catch (Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } finally { + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); + } + } + + public void testCreateSOAPFaultMessageWithFaultStringArgumentForSOAP12() { + + String faultString = "Couldn't create SOAP message due to exception: XML reader error: " + + "com.ctc.wstx.exc.WstxParsingException: Unexpected '<' character in element " + + "(missing closing '>'?)"; + + boolean captureExceptionMessageOldValue = SOAPFaultBuilder.isCaptureExceptionMessage(); + try { + SOAPFaultBuilder.setCaptureExceptionMessage(false); + Message faultMsg = SOAPFaultBuilder.createSOAPFaultMessage(SOAPVersion.SOAP_12, faultString, + SOAPConstants.SOAP_RECEIVER_FAULT); + verifyFaultStringForSoap12(faultMsg); + } catch (Exception ex) { + ex.printStackTrace(); + fail(ex.getMessage()); + } finally { + SOAPFaultBuilder.setCaptureExceptionMessage(captureExceptionMessageOldValue); } } @@ -177,4 +257,30 @@ private void verifyDetail(Message message) throws Exception { } } + private void verifyFaultStringForSoap11(Message faultMsg) throws Exception { + boolean isfaultStringPresent = false; + XMLStreamReader rdr = faultMsg.readPayload(); + while (rdr.hasNext()) { + int event = rdr.next(); + if (event == XMLStreamReader.START_ELEMENT) { + if (rdr.getName().getLocalPart().equals("faultstring")) { + isfaultStringPresent = true; + event = rdr.next(); + assertEquals("Server Error", rdr.getText()); + } + } + } + if(!isfaultStringPresent) { + fail("There is no faultstring in the response"); + } + } + + private void verifyFaultStringForSoap12(Message faultMsg) throws Exception { + SOAPFaultBuilder sfb = SOAPFaultBuilder.create(faultMsg); + Throwable ex = sfb.createException(null); + assertTrue(ex instanceof SOAPFaultException); + SOAPFaultException sfe = (SOAPFaultException) ex; + SOAPFault sf = sfe.getFault(); + assertEquals(sf.getFaultString(),"Server Error"); + } }