如何修改出站CXF请求的原始XML消息? [英] How To Modify The Raw XML message of an Outbound CXF Request?

查看:161
本文介绍了如何修改出站CXF请求的原始XML消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改传出的SOAP请求。
我想从Envelope的正文中删除2个xml节点。
我设法设置了一个Interceptor,并将生成的消息集的String值获取到端点。

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);
} 


推荐答案

我也有这个问题今天。经过多次哭泣和咬牙切齿之后,我能够改变 configuration_interceptor 演示:

I had this problem as well today. After much weeping and gnashing of teeth, I was able to alter the StreamInterceptor class in the configuration_interceptor demo that comes with the CXF source:

OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);

message.getInterceptorChain().doIntercept(message);

try {
    cs.flush();
    CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);

    String soapMessage = IOUtils.toString(csnew.getInputStream());
    ...

soapMessage 变量将包含完整的SOAP消息。您应该能够操作soap消息,将其刷新到输出流并执行 message.setContent(OutputStream.class ... 调用以对消息进行修改这没有保修,因为我自己对CXF很新!

The soapMessage variable will contain the complete SOAP message. You should be able to manipulate the soap message, flush it to an output stream and do a message.setContent(OutputStream.class... call to put your modifications on the message. This comes with no warranty, since I'm pretty new to CXF myself!

注意:CachedStream是StreamInterceptor类中的私有类。不要忘记配置您的拦截器在PRE_STREAM阶段运行,以便SOAP拦截器有机会编写SOAP消息。

Note: CachedStream is a private class in the StreamInterceptor class. Don't forget to configure your interceptor to run in the PRE_STREAM phase so that the SOAP interceptors have a chance to write the SOAP message.

这篇关于如何修改出站CXF请求的原始XML消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆