gpt4 book ai didi

java - 如果不存在,如何将唯一消息 ID 添加到 soap 1.2 请求?

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:58 26 4
gpt4 key购买 nike

我们的网络服务被未正确发送消息 ID 的系统使用。 (根本不在 soap 请求中!)如果不是,我正在寻找如何添加消息 ID 的方法出现在实际通话中。如果它不存在,则整个调用将失败,因为所需的属性不存在。

有没有简单的方法可以做到这一点?

最佳答案

找到解决方案 here ,但这似乎不起作用。

想出了这个对我有用的解决方案:创建一个拦截器来检查 MessageID header 并将其添加到消息中(如果不存在):

package my.package;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.headers.Header;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class HeaderInterceptor extends AbstractPhaseInterceptor<Message> {

private static final String SOAP_HEADER_KEY = "org.apache.cxf.headers.Header.list";
private static final String NAMESPACE_URI = "http://www.w3.org/2005/08/addressing";
private static final String QUALIFIED_NAME = "wsa:MessageID";
private static final String LOCAL_NAME = "MessageID";

public HeaderInterceptor() {
// phases: http://cxf.apache.org/docs/interceptors.html
// 'pre protocol' seems to be the best moment to check the header, if we do it earlier the headers don't exist in the message object so we can't re-use the 'owner document'
// if we do it later the unmarshalMAPs method (in MAPCodec.java) will already have processed the headers and will not process our added header
super(Phase.PRE_PROTOCOL);
}

@Override
public void handleMessage(Message message) throws Fault {
ArrayList<SoapHeader> headers = (ArrayList<SoapHeader>) message.get(SOAP_HEADER_KEY);

// if the header doesn't exist and we have at least one header to access 'owner document' we can create and add our own MessageID header
if(!messageIdHeaderExists(headers) && headers.size() > 0) {
Element existingHeaderElement = (Element) headers.get(0).getObject();

// use the existing header element to create our own MessageID header with random UUID
Element element = existingHeaderElement.getOwnerDocument().createElementNS(NAMESPACE_URI, QUALIFIED_NAME);
element.appendChild(existingHeaderElement.getOwnerDocument().createTextNode("uuid:" + UUID.randomUUID().toString()));

QName qname = new QName(NAMESPACE_URI, LOCAL_NAME);
SoapHeader header = new SoapHeader(qname, element);

// by default a SoapHeader is created with 'direction out'
header.setDirection(Header.Direction.DIRECTION_IN);

headers.add(header);
}
}

/**
* Checks if the MessageID header exists in the list of headers.
*
* @param headers list of headers
* @return true if the MessageID header exists, false if not
*/
private Boolean messageIdHeaderExists(ArrayList<SoapHeader> headers) {
for(SoapHeader header:headers) {
if(header.getName().getLocalPart().equalsIgnoreCase(LOCAL_NAME)) {
return true;
}
}
return false;
}

}

在您的 Spring 上下文中定义拦截器:

<bean id="headerInterceptor" class="my.package.HeaderInterceptor" />

<jaxws:endpoint implementor="#service" address="/ws">
<jaxws:inInterceptors>
<ref bean="headerInterceptor" />
</jaxws:inInterceptors>
</jaxws:endpoint>

使用 CXF 版本 2.6.1 进行测试。有关拦截器的更多信息:http://cxf.apache.org/docs/interceptors.html

关于java - 如果不存在,如何将唯一消息 ID 添加到 soap 1.2 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10419374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com