Java(Web 服务 - SOAP) - 如何在客户端添加 SOAP 处理程序并正确启用 MTOM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12459859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:58:24  来源:igfitidea点击:

Java (web service - SOAP) - How do I add a SOAP handler on the client side and enable MTOM correct?

javasoapjax-wsattachmentmtom

提问by user2017

Java - JDK 1.6.0.7 - WSGEN -version: JAX-WS RI 2.2.3-b01-

Java - JDK 1.6.0.7 - WSGEN -version: JAX-WS RI 2.2.3-b01-



I have the following problem:

我有以下问题:

SOAPBinding binding = (SOAPBinding)((BindingProvider)port).getBinding();
binding.setMTOMEnabled(true);

List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.addAll(binding.getHandlerChain());
handlerChain.add(new MyHandlerSecurity("admin", "admin"));
binding.setHandlerChain(handlerChain);

With this code the SoapHeader is correct, but the attachment is always a inline base64 text.

使用此代码,SoapHeader 是正确的,但附件始终是内联 base64 文本。

//List<Handler> handlerChain = new ArrayList<Handler>();
//handlerChain.addAll(binding.getHandlerChain());
//handlerChain.add(new MyHandlerSecurity("admin", "admin"));
//binding.setHandlerChain(handlerChain);

When handlerChain is commented out, you will see the attachment as an xop reference, but there is no SoapHeader and thus, the client is not authenticated...

当 handlerChain 被注释掉时,您将看到附件作为 xop 引用,但没有 SoapHeader,因此,客户端未通过身份验证...

How can I add a handler on the client side and enable MTOM correct?

如何在客户端添加处理程序并正确启用 MTOM?

回答by F. Mayoral

Im not sure if i got the question right, but i think i had your same problem a couple of months ago, so here is my solution:

我不确定我的问题是否正确,但我想几个月前我遇到了同样的问题,所以这是我的解决方案:

First you need a HeaderHandler class , wich creates the soap header element, it should look like this:

首先你需要一个 HeaderHandler 类,它创建了soap header 元素,它应该是这样的:



    import javax.xml.namespace.QName;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.handler.soap.SOAPHandler;
    import javax.xml.ws.handler.soap.SOAPMessageContext;


    public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

        public boolean handleMessage(SOAPMessageContext smc) {
            Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            String AUTH_TK = "http://www.myurl.com:port/subdir/etc/";
            String NOPREFIX="";//no prefix
            String PREFIX_XMLNS="xmlns";
            String value =  "123456";
            if (outboundProperty.booleanValue()) {
                try {
                    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
                    SOAPHeader header = envelope.addHeader();
                    //<AuthorizationToken xmlns="http://www.myurl.com:port/subdir/etc/">
                    SOAPElement authorizationToken = header.addChildElement("AuthorizationToken", PREFIX_XMLNS, AUTH_TK);
                    //<Token>value</Token>
                    SOAPElement usernameToken =
                        authorizationToken.addChildElement("Token", NOPREFIX);
                        usernameToken.addTextNode(value);
                        //<Token>value</Token>
                    SOAPElement usernameToken =
                        authorizationToken.addChildElement("Token", PREFIX);
                        usernameToken.addTextNode(value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return outboundProperty;
        }


        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext arg0) {

        }

        public boolean handleFault(SOAPMessageContext arg0) {
            return false;
        }
    }

After that you create a HeaderHandlerResolver to handle the header creation and insert it in a handler chain:

之后,您创建一个 HeaderHandlerResolver 来处理标头创建并将其插入处理程序链中:



    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.ws.handler.Handler;
    import javax.xml.ws.handler.HandlerResolver;
    import javax.xml.ws.handler.PortInfo;

    public class HeaderHandlerResolver implements HandlerResolver {

    @SuppressWarnings("unchecked")
    public List<Handler> getHandlerChain(PortInfo portInfo) {
          List<Handler> handlerChain = new ArrayList<Handler>();
          HeaderHandler hh = new HeaderHandler();
          handlerChain.add(hh);
          return handlerChain;
       }
    }

After that, you add in the Client:

之后,您在客户端中添加:



        try{
            //new service instance (your service should be extending javax.xml.ws.Service;)
            YourServiceProxy service = new YourServiceProxy();
            //calls the header handler resolver ;)
            service.setHandlerResolver(new HeaderHandlerResolver());
            //get the service
            YourService port = (YourService)service.getYourService();
            //call the service 
            port.yourMethod()   
        } catch (Exception e) {
            e.printStackTrace();
        }

By the way, i didn't tested this particular header, i modified a previous header handler i had, so it may be not accurate, but i think it's pretty close, i really hope it helps you, try it out and tell us how it comes, i'll try to help you if it still doesn't works.

顺便说一句,我没有测试这个特定的标头,我修改了我以前的标头处理程序,所以它可能不准确,但我认为它非常接近,我真的希望它可以帮助你,试试看并告诉我们如何它来了,如果它仍然不起作用,我会尽力帮助你。