如何修改出站CXF请求的原始XML消息?
我想修改一个传出的SOAP请求。我想从信封的主体中删除2个xml节点。我设法设置了一个拦截器,并将生成的消息的String值设置为端点。
但是,以下代码似乎不起作用,因为未按预期方式编辑外发消息。是否有人对如何执行此操作有一些代码或想法?
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);
}
回答:
我今天也有这个问题。经过大量的哭泣和咬牙切齿,我能够在CXF源附带的configuration_interceptor演示中更改StreamInterceptor类:
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消息。您应该能够操纵肥皂消息,将其刷新到输出流,并进行message.setContent(OutputStream.class...
调用以对消息进行修改。这没有保修,因为我本人对CXF还是很陌生!
注意:CachedStream是StreamInterceptor类中的私有类。不要忘记将拦截器配置为在PRE_STREAM阶段中运行,以便SOAP拦截器有机会编写SOAP消息。
以上是 如何修改出站CXF请求的原始XML消息? 的全部内容, 来源链接: utcz.com/qa/416532.html