tags:

views:

1272

answers:

4

I've set up a SOAP WebServiceProvider in JAX-WS, but I'm having trouble figuring out how to get the raw XML from a SOAPMessage (or any Node) object. Here's a sample of the code I've got right now, and where I'm trying to grab the XML:

@WebServiceProvider(wsdlLocation="SoapService.wsdl")
@ServiceMode(value=Service.Mode.MESSAGE)
public class SoapProvider implements Provider<SOAPMessage>
{
    public SOAPMessage invoke(SOAPMessage msg)
    {
     // How do I get the raw XML here?
    }
}

Is there a simple way to get the XML of the original request? If there's a way to get the raw XML by setting up a different type of Provider (such as Source), I'd be willing to do that, too.

+2  A: 

It turns out that one can get the raw XML by using Provider<Source>, in this way:

@ServiceMode(value=Service.Mode.PAYLOAD)
@WebServiceProvider()
public class SoapProvider implements Provider<Source>
{
    public Source invoke(Source msg)
    {
        StreamResult sr = new StreamResult();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sr.setOutputStream(out);

        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(source, sr);       

        // Use out to your heart's desire.
    }
}

I've ended up not needing this solution, so I haven't actually tried this code myself - it might need some tweaking to get right. But I know this is the right path to go down to get the raw XML from a web service.

(I'm not sure how to make this work if you absolutely must have a SOAPMessage object, but then again, if you're going to be handling the raw XML anyways, why would you use a higher-level object?)

Daniel Lew
A: 

You could try in this way.

        SOAPMessage msg = messageContext.getMessage();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        msg.writeTo(out);
        String strMsg = new String(out.toByteArray());
Smith Torsahakul
A: 

Hallo both of these solution create a string with the soap message, ok,but the formattation are lost. I mean the line breaks of my original message are lost. I need them to be the same because of digital signature validations. Any ideas? I tried everything and I can't find a solution

Lally
A: 

Tried that already before...Line breaks are lost. Any other ideas? or maybe a totally different approach? thanks

Lally