Daniel, your code works like a charm! :-) Thank you very much to you and Sergey for your time. I'm still having some other little issues (maybe more questions coming...) but I'm slowly getting there and I learned a lot on CXF. Cheers,
On Wed, Aug 3, 2011 at 18:55, Daniel Kulp [via CXF] < [email protected]> wrote: > > If you are willing to use SAAJ (and assuming CXF 2.4.1), you could try > something like: > > import java.util.ArrayList; > import java.util.Collection; > import java.util.List; > > import javax.xml.soap.SOAPMessage; > import javax.xml.xpath.XPath; > import javax.xml.xpath.XPathConstants; > import javax.xml.xpath.XPathExpressionException; > import javax.xml.xpath.XPathFactory; > > import org.w3c.dom.Element; > import org.w3c.dom.NodeList; > > import org.apache.cxf.binding.soap.SoapMessage; > import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor; > import org.apache.cxf.interceptor.Fault; > import org.apache.cxf.message.Message; > import org.apache.cxf.phase.AbstractPhaseInterceptor; > import org.apache.cxf.phase.Phase; > import org.apache.cxf.phase.PhaseInterceptor; > > public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> { > private List<PhaseInterceptor<? extends Message>> extras > = new ArrayList<PhaseInterceptor<? extends Message>>(1); > > private XPath xpath = XPathFactory.newInstance().newXPath(); > > public MyInterceptor() { > super(Phase.POST_PROTOCOL); > extras.add(new SAAJOutInterceptor()); > } > > public Collection<PhaseInterceptor<? extends Message>> > getAdditionalInterceptors() { > return extras; > } > > public void handleMessage(SoapMessage message) throws Fault { > SOAPMessage msg = message.getContent(SOAPMessage.class); > try { > removeNodes("idJustification", msg.getSOAPBody()); > removeNodes("indicRdv", msg.getSOAPBody()); > } catch (Exception e) { > throw new Fault(e); > } > } > private synchronized void removeNodes(String path, Element el) throws > XPathExpressionException { > NodeList l = (NodeList)xpath.evaluate("//" + path, el, > XPathConstants.NODESET); > if (l != null) { > for (int x = 0; x < l.getLength(); x++) { > Element el2 = (Element)l.item(0); > el2.getParentNode().removeChild(el2); > } > } > } > } > > > On Tuesday, August 02, 2011 7:47:29 PM [hidden > email]<http://user/SendEmail.jtp?type=node&node=4662921&i=0>wrote: > > > Hi everyone (this is my first post on this mailing-list) > > > > I would like to modify an outgoing SOAP Request. I would like to remove 2 > > > xml nodes from the Envelope's body. I managed to set up an Interceptor > and > > get the generated String value of the message set to the endpoint. > > > > However, the following code does not seem to work as the outgoing message > is > > not edited as expected. Does anyone have some code or ideas on how to do > > this? > > > > public class MyOutInterceptor extends AbstractSoapInterceptor { > > > > public MyOutInterceptor() { > > super(Phase.SEND); > > } > > > > public void handleMessage(SoapMessage message) throws Fault { > > // Get message content for dirty editing... > > StringWriter writer = new StringWriter(); > > CachedOutputStream cos = > > (CachedOutputStream)message.getContent(OutputStream.class); > > InputStream inputStream = cos.getInputStream(); > > IOUtils.copy(inputStream, writer, "UTF-8"); > > String content = writer.toString(); > > > > // remove the substrings from envelope... > > content = content.replace("<idJustification>0</idJustification>", > > > ""); > > content = content.replace("<indicRdv>false</indicRdv>", ""); > > ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); > > > outputStream.write(content.getBytes(Charset.forName("UTF-8"))); > > message.setContent(OutputStream.class, outputStream); > > } > > > > Am I missing something? What is a better way to to this? > > It seems too complicated to achieve my pretty simple requirement... > -- > Daniel Kulp > [hidden email] <http://user/SendEmail.jtp?type=node&node=4662921&i=1> > http://dankulp.com/blog > Talend - http://www.talend.com > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://cxf.547215.n5.nabble.com/How-To-Modify-The-Raw-XML-message-of-an-Outbound-CXF-Request-tp4659707p4662921.html > To unsubscribe from cxf-user, click > here<http://cxf.547215.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=547216&code=dG91dGNvbkBnbWFpbC5jb218NTQ3MjE2fC03MjM4ODg4OTM=>. > >
