如何手动将SoapMessageHandler添加到SoapBinding
来源:爱站网时间:2022-03-30编辑:网友分享
本篇文章主要给大家介绍下如何手动将SoapMessageHandler添加到SoapBinding的内容,感兴趣的小伙伴千万不要错过这篇文章,希望爱站技术频道小编所整理的文章帮助到你解决问题。
问题描述
出于多种原因,我试图在SOAPHandler
上手动添加自定义SOAPBinding
。此绑定已经有一个自动连接的SOAPHandler
,我必须再输入一个。我的代码是这样的:
BindingProvider port = (BindingProvider) jaxProxyService;
// Configuration
port.getRequestContext().put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
port.getRequestContext().put(JAXWSProperties.MTOM_THRESHOLOD_VALUE, 256);
SOAPBinding soapBinding = (SOAPBinding) port.getBinding();
soapBinding.setMTOMEnabled(true);
// DataHandlers are constructed before from files, not relevant to the issue I guess
final List documentList = new ArrayList();
documentList.add(doc);
soapBinding.getHandlerChain().add(new CustomSoapMessageHandler(documentList));
我的实现如下:
public class CustomSoapMessageHandler implements SOAPHandler {
private SOAPMessage soapMessage;
private boolean outbound;
private List attachments;
public CustomSoapMessageHandler() {
super();
}
public CustomSoapMessageHandler(List attachments) {
super();
this.attachments = attachments;
}
// Overriding handleMessage(SOAPMessageContext), handleFault(SOAPMessageContext), etc
[当我使用add()
方法时,什么也没有发生。调试器告诉我我已成功完成操作,我正确创建了CustomSoapMessageHandler
,add
方法返回true,但是在我的binding
的实际列表中,根本没有添加处理程序。这里会发生什么?列表是否由于某些原因不可变/锁定,还是我在此问题上缺少任何内容?
思路:
第一部分:如果任何人遇到相同的问题,一个可行的解决方案是手动替换整个HandlerChain
。这样,您便可以手动添加其他处理程序:
// DataHandlers are constructed before from files, not relevant to the issue I guess
final List documentList = new ArrayList();
documentList.add(doc);
final List handlerList = new ArrayList();
// Don't forget to add the pre-loaded handlers
handlerList.addAll(soapBinding.getHandlerChain());
handlerList.add(new CustomSoapMessageHandler(documentList));
soapBinding.setHandlerChain(handlerList);
第二部分:它不起作用,因为getHandlerChain()
的实际实现返回的是实际ArrayList
的副本,而不是列表本身。他们以此保护了原件,从而防止您简单地手动添加Handler
:
Binding.class:
/**
* Gets a copy of the handler chain for a protocol binding instance.
* If the returned chain is modified a call to setHandlerChain
* is required to configure the binding instance with the new chain.
*
* @return java.util.List<Handler> Handler chain
*/
public java.util.List getHandlerChain();
BindingImpl.class:
public abstract class BindingImpl implements WSBinding {
private HandlerConfiguration handlerConfig;
...
public
@NotNull
List getHandlerChain() {
return handlerConfig.getHandlerChain();
}
HandlerConfiguration.class:
/**
*
* @return return a copy of handler chain
*/
public List getHandlerChain() {
if(handlerChain == null)
return Collections.emptyList();
return new ArrayList(handlerChain);
}
所以add()
可以工作(很明显),但不能达到您的期望。
如何手动将SoapMessageHandler添加到SoapBinding的内容小编都一一给大家整理出来了,有什么问题需要补充或者不清楚的,来爱站技术频道网站给小编留言就可以了。