Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a SOAPMessage from String XML of Entire SOAP Message

Hoh can I create a SOAPMessage from a String representation of an entire SOAP message? The reason I'm trying to do this is that I have a SOAP handler for a web service where I capture the SOAP message. I need to preserve the entire SOAP message in the web service to send off to another component. Right now, the web service strips off the SOAP envelope information. So in the handler I made a copy of the SOAP message, base64 encoded it, removed the original Body contents, and added the encoded string. In the web service I'm trying to decode the body (encoded SOAP message) and reconstruct it as a SOAPMessage to send off to another component.

like image 788
user994165 Avatar asked Nov 01 '12 15:11

user994165


2 Answers

As per Javadoc, javax.xml.soap.MessageFactory create methods pre-populate SOAP message with necessary objects like envelope, body, header ensuring that message is fomed correctly. However, the only variant of createMessage method that accepts message data, accepts it as an InputStream. Hence a String to InputStream conversion is needed, namely creating a new byte stream from string bytes.

This is a simplified example for the sake of brevity. In the application code one may avoid creating a factory on each method call by extracting it to a field/constant, specify required SOAP protocol version, different charset, etc.

private SOAPMessage getSoapMessageFromString(String xml) throws SOAPException, IOException {
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
    return message;
}
like image 112
tiurin Avatar answered Oct 21 '22 12:10

tiurin


I did this in two steps. First created a DOM Document, and then create the SOAPMessage from the Document.

like image 27
user994165 Avatar answered Oct 21 '22 11:10

user994165