Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.xml.soap.SOAPException: InputStream does not represent a valid SOAP 1.1 Message

I am consuming a SOAP API. The XML response I receive is surrounded with a 'soap envelope' - so I need to remove or parse past that wrapper before I can process the XML. I've taken the below approach with other endpoints (so the code is sane, at least) but with this particular endpoint, I get errors.

The error I'm encountering is:

SEVERE: SAAJ0304: InputStream does not represent a valid SOAP 1.1 Message

This is the code I am using to remove the Soap Wrapper:

String soapResponse = getSoapResponseFromApi();
ByteArrayInputStream inputStream = new ByteArrayInputStream(soapResponse.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);
Document doc = message.getSOAPBody().extractContentAsDocument();   // <-- error thrown here


//unmarhsall the XML in 'doc' into an object
//do useful stuff with that object

here is the XML I am receiving back (the contents of soapResponse in the code above)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <XMLContentIActuallyWant xmlns="http://my-url.com/webservices/">
            <!-- Useful stuff here -->
        </XMLContentIActuallyWant >
    </soap:Body>
</soap:Envelope>
like image 244
Paul Avatar asked Mar 19 '18 17:03

Paul


1 Answers

I discovered the solution as I was preparing this question.

Soap versions have different formatting. The SoapMessage library was defaulting to soap 1.1 - but the contents of the response I was receiving was soap 1.2.

I could see this when I inspected the full request that was being sent, in order to receive the response noted above - it looked like this:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body> 
        <!-- xml content here --> 
  </soap12:Body>
</soap12:Envelope>

the soap12 part highlights that it is requesting soap 1.2.

so although the response doesn't contain the '12' - the response is also in 1.2.

So we need to tell SoapMessage to use 1.2 instead of the default (1.1 in my case).

I did this by modifying my code above like so:

Before:

SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);

After:

SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, inputStream);

It's worth noting that other endpoints of the same API served SOAP 1.1 - which is why this error was so confusing for me. I was doing the same thing and getting different results.

like image 118
Paul Avatar answered Nov 07 '22 05:11

Paul