Skip to content

Commit

Permalink
BUG 35047999: Show Server Error in faultString instead of error detai…
Browse files Browse the repository at this point in the history
…ls. (eclipse-ee4j#648)

Signed-off-by: Himanshu Ranjan <himanshu.ranjan@oracle.com>
(cherry picked from commit 9203195)
  • Loading branch information
himanshuatgit authored and lukasj committed Oct 17, 2023
1 parent 67b6e9b commit bdd9528
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019 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 @@ -83,7 +83,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 @@ -100,7 +100,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, 2021 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
Expand Up @@ -37,6 +37,7 @@
import javax.xml.soap.Detail;
import javax.xml.soap.DetailEntry;
import javax.xml.transform.dom.DOMResult;
import javax.xml.soap.SOAPException;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.SOAPFaultException;
Expand All @@ -56,6 +57,13 @@
*/
public abstract class SOAPFaultBuilder {

private static final String SERVER_ERROR = "Server Error";

/**
* Default constructor.
*/
protected SOAPFaultBuilder() {}

/**
* Gives the {@link DetailType} for a Soap 1.1 or Soap 1.2 message that can be used to create either a checked exception or
* a protocol specific exception
Expand Down Expand Up @@ -374,14 +382,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 @@ -478,7 +481,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 @@ -496,6 +498,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 @@ -575,4 +579,12 @@ 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
Expand Up @@ -21,10 +21,12 @@
import javax.xml.namespace.QName;
import javax.xml.soap.Detail;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;

import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
Expand Down Expand Up @@ -123,24 +125,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,31 @@ private void verifyDetail(Message message) throws Exception {
fail("There is no detail element in the fault");
}
}

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");
}
}

0 comments on commit bdd9528

Please sign in to comment.