Java(Web服务-SOAP)-如何在客户端添加SOAP处理程序并启用正确的MTOM? [英] Java (web service - SOAP) - How do I add a SOAP handler on the client side and enable MTOM correct?

查看:104
本文介绍了Java(Web服务-SOAP)-如何在客户端添加SOAP处理程序并启用正确的MTOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java-JDK 1.6.0.7-WSGEN版本:JAX-WS RI 2.2.3-b01-

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

我有以下问题:

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);

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

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

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

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

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...

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

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

推荐答案

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

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:

首先,您需要一个HeaderHandler类,该示例将创建soap header元素,它应如下所示:

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

    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;
        }
    }

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

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

    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;
       }
    }

然后,您添加客户端:

        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.

这篇关于Java(Web服务-SOAP)-如何在客户端添加SOAP处理程序并启用正确的MTOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆