如何使用带有org.w3c.dom.Node的CXF拦截器修改Web服务请求 [英] howto modify a webservice request using CXF interceptors with org.w3c.dom.Node

查看:83
本文介绍了如何使用带有org.w3c.dom.Node的CXF拦截器修改Web服务请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CXF拦截器,我想向发送到服务器的xml附加一些Node.我创建了一个拦截器(见下文),将消息作为DOM Node进行拾取,修改并将其写回到消息对象中.

Using a CXF Interceptor I'd like to append some Node to the xml being sent out to the server. I've created a interceptor (see below) that picks up the message as DOM Node, modifies it and writes it back to the message object.

不幸的是,代码无法按预期工作-发送到服务器的XML不包含'magicWord'.恕我直言,我为此使用了错误的阶段.

Unfortunately the code does not work as expected - the XML sent to the server does not contain the 'magicWord'. IMHO I'm using the wrong phase for this.

所以问题是:如何使用org.w3c.dom.Node语法修改传出的Web服务请求?

So the question is: how can I modify an outgoing webservice request using the org.w3c.dom.Node syntax?

package dummy;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

class DummyInterceptor extends AbstractPhaseInterceptor {

    String magicWord = "abc";

    public DummyInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }

    public void handleMessage(Message message) {
        Document document = (Document) message.getContent(Node.class);
        NodeList nodes = document.getElementsByTagName("wsse:Security");
        if (nodes.getLength() == 1) {
            Node wsseSecurityNode = nodes.item(0);
            wsseSecurityNode.appendChild(document.createTextNode(magicWord));
        }
        message.setContent(Node.class, document);
    }
}

推荐答案

最后,我发现了自己该怎么做.

Finally I've found out myself how to do this.

  • 拦截器必须使用'Phase.PRE_PROTOCOL'
  • 拦截器必须使用addAfter(SaajOutInterceptor)-SaajOutInterceptor在消息中提供节点
  • 拦截器类应派生自AbstractSoapInterceptor
  • 拦截器的handleMessage不会做修改DOM本身的工作,而是使用message.getInterceptorChain().add(...)将新的拦截器添加到消息中.
  • 然后新添加的拦截器应该修改DOM

这篇关于如何使用带有org.w3c.dom.Node的CXF拦截器修改Web服务请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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