简体   繁体   English

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

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

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正确?

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. 顺便说一句,我没有测试这个特殊的标题,我修改了我以前的标题处理程序,所以它可能不准确,但我认为它非常接近,我真的希望它可以帮助你,尝试一下并告诉我们如何它来了,如果它仍然不起作用,我会尽力帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM