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 @@ -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 {

public static final String SERVER_ERROR = "Server Error";
himanshuatgit marked this conversation as resolved.
Show resolved Hide resolved

/**
* Default constructor.
*/
Expand Down Expand Up @@ -214,7 +218,10 @@ public static Message createSOAPFaultMessage(SOAPVersion soapVersion, String fau
return createSOAPFaultMessage(soapVersion, faultString, faultCode, null);
}

public static Message createSOAPFaultMessage(SOAPVersion soapVersion, SOAPFault fault) {
public static Message createSOAPFaultMessage(SOAPVersion soapVersion, SOAPFault fault) throws SOAPException {
if (!isCaptureExceptionMessage()) {
fault.setFaultString(SERVER_ERROR);
}
switch (soapVersion) {
case SOAP_11:
return JAXBMessage.create(JAXB_CONTEXT, new SOAP11Fault(fault), soapVersion);
Expand All @@ -226,6 +233,9 @@ public static Message createSOAPFaultMessage(SOAPVersion soapVersion, SOAPFault
}

private static Message createSOAPFaultMessage(SOAPVersion soapVersion, String faultString, QName faultCode, Element detail) {
if (!isCaptureExceptionMessage()) {
faultString = SERVER_ERROR;
}
switch (soapVersion) {
case SOAP_11:
return JAXBMessage.create(JAXB_CONTEXT, new SOAP11Fault(faultCode, faultString, null, detail), soapVersion);
Expand Down Expand Up @@ -379,14 +389,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 All @@ -406,6 +411,11 @@ private static Message createSOAP11Fault(SOAPVersion soapVersion, Throwable e, O
faultCode = getDefaultFaultCode(soapVersion);
}
}

if (!isCaptureExceptionMessage()) {
faultString = SERVER_ERROR;
}

SOAP11Fault soap11Fault = new SOAP11Fault(faultCode, faultString, faultActor, detailNode);

//Don't fill the stacktrace for Service specific exceptions.
Expand Down Expand Up @@ -483,7 +493,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 +510,11 @@ private static Message createSOAP12Fault(SOAPVersion soapVersion, Throwable e, O
}
}

if (!isCaptureExceptionMessage()) {
faultString = SERVER_ERROR;
}
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
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 @@ -125,16 +127,7 @@ public void testCreate11FaultFromRE() {
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());
Expand All @@ -143,6 +136,82 @@ public void testCreate11FaultFromRE() {
}
}

public void testCreate12FaultFromRE() {
RuntimeException re = new RuntimeException(
"XML reader error: com.ctc.wstx.exc.WstxParsingException: Unexpected < character in element");
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(true);
}
}

public void testCreateSOAPFaultMessageWithSOAPFaultArgumentForSOAP11() {
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(true);
}
}

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 '>'?)";
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(true);
}
}

public void testCreateSOAPFaultMessageWithSOAPFaultArgumentForSOAP12() {
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(true);
}
}

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 '>'?)";
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(true);
}
}

public void testCreateException_14504957() throws Exception {
MessageFactory f = MessageFactory.newInstance();
SOAPMessage soapMsg = f.createMessage(null, new ByteArrayInputStream(NPE_FAULT.getBytes()));
Expand Down Expand Up @@ -177,4 +246,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(SOAPFaultBuilder.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(),SOAPFaultBuilder.SERVER_ERROR);
}
}