Невозможно вручную добавить SoapMessageHandler в SoapBinding - PullRequest
1 голос
/ 03 февраля 2020

По многим причинам я пытаюсь добавить пользовательский 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<DataHandler> documentList = new ArrayList<>();
documentList.add(doc);

soapBinding.getHandlerChain().add(new CustomSoapMessageHandler(documentList));

Моя реализация выглядит следующим образом:

public class CustomSoapMessageHandler implements SOAPHandler<SOAPMessageContext> {

    private SOAPMessage soapMessage;
    private boolean outbound;
    private List<DataHandler> attachments;

    public CustomSoapMessageHandler() {
        super();
    }

    public CustomSoapMessageHandler(List<DataHandler> attachments) {
        super();
        this.attachments = attachments;
    }

    // Overriding handleMessage(SOAPMessageContext), handleFault(SOAPMessageContext), etc

Когда я добираюсь до метода add(), ничего не происходит. Отладчик сообщает, что я успешно прошел, я правильно создаю CustomSoapMessageHandler, метод add возвращает true , но в фактическом списке моего binding мой обработчик вообще не добавляется. Что здесь происходит ? Является ли список неизменным / заблокированным по какой-то причине, или я что-то упускаю по этому вопросу?

1 Ответ

0 голосов
/ 24 февраля 2020

Первая часть: Если кто-то сталкивается с той же проблемой, решение, которое работает, состоит в том, чтобы заменить весь HandlerChain вручную. Таким образом, вы сможете добавить другие обработчики вручную:

// DataHandlers are constructed before from files, not relevant to the issue I guess
final List<DataHandler> documentList = new ArrayList<>();
documentList.add(doc);

final List<Handler> 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 <code>setHandlerChain</code>
 * is required to configure the binding instance with the new chain.
 *
 *  @return java.util.List&lt;Handler> Handler chain
 */
public java.util.List<javax.xml.ws.handler.Handler> getHandlerChain();

BindingImpl.class:

public abstract class BindingImpl implements WSBinding {
    private HandlerConfiguration handlerConfig;

    ...

    public
    @NotNull
    List<Handler> getHandlerChain() {
        return handlerConfig.getHandlerChain();
    }

HandlerConfiguration.class:

/**
 *
 * @return return a copy of handler chain
 */
public List<Handler> getHandlerChain() {
    if(handlerChain == null)
        return Collections.emptyList();
    return new ArrayList<Handler>(handlerChain);
}

Так что add() работает (очевидно), но не на том, что вы ожидаете.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...