How is a SOAP response treated different in Java 8 vs Java 6?

885 views Asked by At

Have some legacy code that works with Java 6 but not fully with Java 8. It sends a SOAP request and reads the response. When using Java 8, the response is not correctly parsed and the code returns a failed result.

Here is a simplified version of the code, I have cut out most of the request building code and changed server names and the like. Please pardon any typos.

try {
    // Init the factories
    SOAPConnectionFactory SOAPConnFac = SOAPConnectionFactory.newInstance();
    SOAPFactory SOAPFac = SOAPFactory.newInstance();
    MessageFactory MessageFac = MessageFactory.newInstance();

    // Create the SOAP Connection
    SOAPConnection SOAPConn = SOAPConnFac.createConnection();

    // Build the SOAP request message
    SOAPMessage SOAPRequestMsg = MessageFac.createMessage();

    // Add info to the MIME header for .NET compatibility
    MimeHeaders hd = SOAPRequestMsg.getMimeHeaders();
    hd.addHeader("SOAPAction", "http://www.mysever.com/respond");

    // Remove the SOAP message header, it is not needed in this case
    SOAPHeader SOAPRequestHeader = SOAPRequestMsg.getSOAPHeader();
    SOAPRequestHeader.detachNode();

    // Build the SOAP body main element CheckIt
    SOAPBody SOAPRequestBody = SOAPRequestMsg.getSOAPBody();
    javax.xml.soap.Name nameRequestBody = SOAPFac.createName("CheckIt", "", "http://www.myserver.com/");
    SOAPBodyElement sbeRequestBodyEle = SOAPRequestBody.addBodyElement(nameRequestBody);

    // Add all child elements to the CheckIt element
    SOAPElement seNameNode = sbeRequestBodyEle.addChildElement("Name");
    seNameNode.addChildElement("FirstName").addTextNode("mary");
    seNameNode.addChildElement("LastName").addTextNode("smith");

    sbeRequestBodyEle.addChildElement("ID").addTextNode("3423");

    URL urlWebService = new URL ("http://webserver/BGC/");
    // Send the SOAP request message and get back the SOAP response message
    SOAPMessage SOAPResponseMsg = SOAPConn.call(SOAPRequestMsg, urlWebService);

    SOAPConn.close();

    // Pull out the SOAP response body
    SOAPBody SOAPResponseBody = SOAPResponseMsg.getSOAPBody();

    // Check for SOAP faults
    if (SOAPResponseBody.hasFault()) {
        SOAPFault newFault = SOAPResponseBody.getFault();
        strStatus = "SOAP FAULT:<br>";
        strStatus += "<br>code = " + newFault.getFaultCodeAsName();
        strStatus += "<br>message = " + newFault.getFaultString();
        strStatus += "<br>actor = " + newFault.getFaultActor()+"<br>";
        boolOK = false;
    } else {
        // Pull the CheckProspectResponse element out of the SOAP response body
        javax.xml.soap.Name nameResponseBody = SOAPFac.createName("CheckResponse", "", "http://www.mysever.com/");
        Iterator iterElement = SOAPResponseBody.getChildElements(nameResponseBody);
        if (iterElement.hasNext()){
            SOAPBodyElement sbeResponseBodyEle = (SOAPBodyElement) iterElement.next();
            // Check for success or failure
            String strResultMsg = ((sbeResponseBodyEle.getValue() == null) ? sbeResponseBodyEle.toString() : sbeResponseBodyEle.getValue());
            strStatus = "Check response came back from webserver as *" + strResultMsg + "*<br>";
            boolOK = (strResultMsg.trim().toUpperCase().indexOf("SUCCESS") > 0);
        } // End if (iterElement.hasNext())
    } // End if (SOAPResponseBody.hasFault())
} catch(Exception e) {
    strStatus = "Error : "+((e.getMessage() != null) ? e.getMessage() : e.toString());
    boolOK = false;
} // End try

It seems that when pulling elements out of the response, it gets an empty set.

Iterator iterElement = SOAPResponseBody.getChildElements(nameResponseBody);

Returns a set of null elements. Not a null itself, the set has at least one member but that member is null. It should have a value of "SUCCESS" but it's null. I have looked at the full response and it looks fine, everything is there and it works with Java 6, just not with Java 8. A typical, successful response looks like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <CheckResponse xmlns="http://www.mysever.com/">
            <CheckResult>SUCCESS</CheckResult>
        </CheckResponse>
    </soap:Body>
</soap:Envelope>

UPDATE

I have found a way around my problem, using getTextContent I get the correct data, or at least the data I expect to get. Given the above SOAP response, Java 8 returns the following for the given method call:

getValue = null
getNodeValue = null
getLocalName = CheckResponse
getNodeName = CheckResponse
getNamespaceURI = http://www.mysever.com/
getPrefix = null
getTextContent = SUCCESS

Acording to both Java 6 and Java 8 docs, the returned value for getValue should be "SUCCESS" in this case. Which is true for Java 6 but false for Java 8.

Any ideas on why this method works with Java 6 but not with Java 8? Is this a bug?

0

There are 0 answers