如何修改出站 CXF 请求的原始 XML 消息?
我想修改传出的 SOAP 请求。 我想从信封主体中删除 2 个 xml 节点。 我设法设置了一个拦截器,并将生成的消息集的字符串值获取到端点。
但是,以下代码似乎不起作用,因为传出消息未按预期进行编辑。有没有人有一些关于如何做到这一点的代码或想法?
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据第一条评论,我创建了一个抽象类,可以轻松地使用它来更改整个肥皂信封。
以防万一有人想要现成的代码部分。
Based on the first comment, I created an abstract class which can easily be used to change the whole soap envelope.
Just in case someone wants a ready-to-use code part.
我今天也遇到这个问题了。经过多次哭泣和咬牙切齿之后,我能够更改 configuration_interceptor 演示:
soapMessage
变量将包含完整的 SOAP 消息。您应该能够操作肥皂消息,将其刷新到输出流并执行message.setContent(OutputStream.class...
调用以对消息进行修改。这没有任何保证,因为我自己对 CXF 还很陌生!注意:CachedStream 是 StreamInterceptor 类中的私有类,不要忘记将拦截器配置为在 PRE_STREAM 阶段运行,以便 SOAP 拦截器有机会编写 SOAP。 信息。
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:
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 amessage.setContent(OutputStream.class...
call to put your modifications on the message. This comes with no warranty, since I'm pretty new to CXF myself!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.
以下能够冒泡服务器端异常。在之前的解决方案中使用 os.close() 而不是 IOUtils.closeQuietly(os) 也能够引发异常。
Following is able to bubble up server side exceptions. Use of os.close() instead of IOUtils.closeQuietly(os) in previous solution is also able to bubble up exceptions.
基于 这个
Good example for replacing outbound soap content based on this
更好的方法是使用 DOM 接口修改消息,您需要首先添加 SAAJOutInterceptor (这可能会对大请求产生性能影响),然后在 USER_PROTOCOL 阶段执行的自定义拦截器
a better way would be to modify the message using the DOM interface, you need to add the SAAJOutInterceptor first (this might have a performance hit for big requests) and then your custom interceptor that is executed in phase USER_PROTOCOL
这个为我工作。它基于 Apache CXF 示例中的 configuration_interceptor 示例中的 StreamInterceptor 类。
它使用的是 Scala 而不是 Java,但转换非常简单。
我尝试添加评论来解释发生的事情(据我所知)。
this one's working for me. It's based on StreamInterceptor class from configuration_interceptor example in Apache CXF samples.
It's in Scala instead of Java but the conversion is straightforward.
I tried to add comments to explain what's happening (as far as I understand).