Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG 35047999: Show Server Error in faultString instead of error details. #648

Merged
merged 8 commits into from
Jun 23, 2023
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 ||
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -56,6 +58,8 @@
*/
public abstract class SOAPFaultBuilder {

private static final String SERVER_ERROR = "Server Error";

/**
* Default constructor.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 '&lt;' 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 '&lt;' 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);
}
}

Expand Down Expand Up @@ -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");
}
}