Prashant Bhate
Prashant Bhate

Reputation: 11087

How to unmarshal SoapFault from raw xml payload from a file?

Suppose I have following xml payload in a file

<?xml version="1.0" encoding="UTF-8"?>
<ns2:Fault 
   xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/"   
   xmlns:ns3="http://www.w3.org/2003/05/soap-envelope">
  <faultcode>ns2:Server</faultcode>
  <faultstring>some Error</faultstring>
  <detail>
    <ns4:ApplicationSOAPFault xmlns:ns4="http://application.exception">
      <code>04</code>
      <message>SimiError</message>
    </ns4:ApplicationSOAPFault>
  </detail>
</ns2:Fault>

How to unmarshall this into a SoapFault (and CheckedException if available) and throw it as exception?

Background

We are trying to build a simulator that during record/proxy mode dumps the WebService Request and Response payload xml to FileSystem using LogicalHandler.(i.e After making webservice call to target server and get valid response back)

In case target server returns Soap Fault, SoapFault like above is dumped to File System.

When simulator is switched to playback mode Matching Response unmarshalled from the dump is server back (done with JAXB.unmarshall())

This works fine however simulator is currently not able to unmarshall SoapFault ( and corresponding CheckedException ) and throw it as Exception.

Upvotes: 0

Views: 2142

Answers (1)

Lyju I Edwinson
Lyju I Edwinson

Reputation: 1834

Try this please,

   final SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
                                .createMessage(new MimeHeaders(), new ByteArrayInputStream(yourSoapMessageWithFault.getBytes()));
     if (msg != null) {
                            SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
                            SOAPBody body = env.getBody();
                            if (body != null && body.hasFault()) {
                                final SOAPFault fault = body.getFault();                                
                                final DetailEntry entry = (DetailEntry)fault.getDetail().getDetailEntries().next();
    //now UNMarshall this entry to your custom exception class.

}

Upvotes: 1

Related Questions