在SOAP Handler

时间:2016-05-30 14:02:01

标签: java xml soap axis jax-rpc

我使用Axis 1.4,我希望在客户端的SOAP主体的XML中插入一个额外的级别。有一个服务器响应,我可以在客户端使用javax.xml.rpc.handler.GenericHandler的子类获取:

现在我尝试用

识别正确的消息类型
SOAPMessageContext smc = (SOAPMessageContext) context;
SOAPMessage message = smc.getMessage();
SOAPBody sb = message.getSOAPBody();

NodeList nl = sb.getElementsByTagName("projectDataReturn");
if (nl.getLength() == 0) {
    return true;  // wrong message
}

log.info("we have a projectDataReturn structure");

NodeList cl = sb.getElementsByTagName("centres");
if (cl.getLength() == 0) {
    return true;  // no centres
}

log.info("we have centres tags");

此时我需要一个新标记,它包含所有现有的<centres>标记。我已在<centres>中存储的所有cl代码的列表,但我如何将新节点添加到<projectDataReturn>代码?我如何将现有的<centre>标签移动到新标签?我用

试了一下
Document doc = cl.item(0).getOwnerDocument();

Element array = doc.createElement("centres");
array.setAttribute("xmlns:ns5",  "http://beans.eo.xyz.de");
array.setAttribute("soapenc:arrayType", "ns5:CentreBean[" + cl.getLength() + "]");
array.setAttribute("xsi:type", "soapenc:Array");
array.setAttribute("xmlns:soapenc", "http://schemas.xmlsoap.org/soap/encoding/");

nl.item(0).appendChild(array); 

// move existing <centre> tags here
return true;

但它产生了一个SOAPException

javax.xml.rpc.JAXRPCException: javax.xml.soap.SOAPException: Could not get document from SOAPEnvelope

出了什么问题?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:

Document doc = cl.item(0).getOwnerDocument();

Element array = doc.createElementNS("", "centres");
array.setAttributeNS("http://schemas.xmlsoap.org/soap/encoding/", "soapenc:arrayType", "CentreBean[" + cl.getLength() + "]");
array.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
array.setAttributeNS("http://www.w3.org/2000/xsi/", "xsi:type", "soapenc:Array");

现在我克隆了旧的<centres>节点:

for (int i = 0; i < cl.getLength(); i++) {
    array.appendChild(cl.item(i).cloneNode(true));
}

现在我将新节点插入XML(旧<centres>标记前面):

nl.item(0).insertBefore(array, cl.item(0));

我删除了错误位置的旧节点:

for (int i = 0; i < cl.getLength(); i++) {
    nl.item(0).removeChild(cl.item(i));
}

如果在客户端的服务存根的处理程序链中注册处理程序代码,它将在服务器响应的XML反序列化之前执行:

import javax.xml.namespace.QName;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.HandlerRegistry;

// uses Axis1 generated client classes
MyService_Service serviceLocator = new MyService_ServiceLocator();

HandlerRegistry hr = serviceLocator.getHandlerRegistry();
List<HandlerInfo> handlerChain = hr.getHandlerChain((QName) serviceLocator.getPorts().next());

HandlerInfo hi = new HandlerInfo();
hi.setHandlerClass(MyXMLInjectionHandler.class);
handlerChain.add(hi);