How to modify the generated SOAP request?
Asked Answered
C

3

10

I'm at the stage where I created an output interceptor and I get an OuputStream out of the SOAP message. But how could I modify the SOAP envelope right before sending it to the endpoint? I would like to delete some xml elements.

Camelopardus answered 2/8, 2011 at 1:50 Comment(0)
M
5

one way could be to get the document and run it through XSLT transform.

You can get at the document in the handleMessage of your interceptor by calling

@Override
public void handleMessage(SoapMessage message) throws Fault{
    SOAPMessage saaj = message.getContent(SOAPMessage.class);
    Document doc = saaj.getSOAPPart(); // This actually returns a SOAPPart instance but it does implement the w3c Document interface

    //play around with the document, doc is a reference so any changes made to that instance
    //will be forwarded to the rest of the chain
}

careful though that if you have security such as XML signature that must be performed on the soap content you must ensure that your interceptor occurs BEFORE the signature are applied otherwise you will invalidate them.

To play around with the timing of the interceptor you can specify the phase at which it will run. CXF should also honor the order in which you will configure them should they be performed at the same phase.

but don't take my word for it... check these for more info

debugging through the CXF source code also helped me a great deal in understanding how it worked

---- EDIT ----

(thanks Daniel :-)

For this to work you need to have SAAJOutInterceptor configured in your stack. You can either add it manually or simply make it part of your interceptor. Here is an example of an interceptor that pretty much does what you want.

Magnesium answered 2/8, 2011 at 2:51 Comment(4)
Thanks a lot for your answer Newtopian. message.getContent(SOAPMessage.class) always return Null though :-( At what phase should I put this code to access the SOAP Envelope please?Camelopardus
You would need to also configure in the SAAJOutInterceptor along with your interceptor that does the transform.Hiers
indeed, Daniel is right, you need to make sure that the SAAJOutInterceptor is configured as it is he that serializes the data to a SOAPMessage object. It came by default with my setup so I did not have to worry too much about it. Once configured the object will no longer be null and you will be able to play with the DOM tree as you please.Magnesium
on another note, if you are looking to have more control over the XML that goes through the stack I might recommend Spring-WS instead of CXF. That is the move I had to make in my project, CXF was just to difficult to get around when the use case starts to get screwy. Spring-WS's stack is somewhat more open which gives me the leverage I needed without too much pain. This said, CXF does work nicely and I found easier to get going just did not provide enough flexibility.Magnesium
H
3

Refer to this link for a description of the Interceptor Phases

https://web.archive.org/web/20131003140105/http://fusesource.com/docs/esb/4.2/cxf_interceptors/CXFInterceptPhasesAppx.html

Heracles answered 20/2, 2012 at 16:13 Comment(1)
your link is broken and goes to a generic redhat menu. It's always a good idea to summarize what information the link contains incase this happens.Emulsion
C
1

I posted an answer here https://mcmap.net/q/593833/-how-to-modify-the-raw-xml-message-of-an-outbound-cxf-request It is based on the whole body substitution.

Czerny answered 18/10, 2012 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.