3

I'm trying to find a way to add header elements to a SOAP message. If I use @WebParam with header set to true then it works fine, that parameter is nicely in <soap:header>. However, the requires adding a bunch of parameters to my API which is hardly an option.

At first view interceptors do not appear to be possible so I was looking at SOAPHandlers but it is so difficult to find clear information about this.

This is my spring config:

<bean id="testWebservice" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" >
    <property name="serviceInterface" value="com...TestWebService"/>
    <property name="wsdlDocumentUrl" value="http://localhost:9062/test-ws/remoting/test?wsdl"/>
    <property name="serviceName" value="TestWebService"/>
    <property name="namespaceUri" value="http://test.com"/>
    <property name="portName" value="TestServicePort"/>
    <property name="lookupServiceOnStartup" value="false"/>
    <property name="handlerResolver" ref="myResolver"/>
</bean>

<bean id="myResolver" class="com...TestHandlerResolver" />

My Handler (chained in the TestHandlerResolver):

public class TestHandler implements SOAPHandler<SOAPMessageContext> {

    @Override
    public void close(MessageContext context) {
    }

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

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return true;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        if (Boolean.TRUE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY))) {

            try {
                List<Header> headers = new ArrayList<Header>();
                Header dummyHeader;
                dummyHeader = new Header(new QName("dummy"), "decapitated", new JAXBDataBinding(String.class));
                headers.add(dummyHeader);
                context.put(Header.HEADER_LIST, headers);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

The problem is that I'm a bit at a loss. Is using handler the right way to do this and if so can you give or point me to a nice example? Is there a way to do it with interceptors? Or am I completely of the target here...

4

1 回答 1

2

让它像这样工作:

     @Override
     public boolean handleMessage(SOAPMessageContext context) {

            try {
                SOAPMessage msg = context.getMessage();
                SOAPPart part = msg.getSOAPPart();
                SOAPEnvelope envelope = part.getEnvelope();

                SOAPHeader header = envelope.getHeader();

                if (Boolean.TRUE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY))) {
                    if (header == null) {
                        header = envelope.addHeader();
                    }
                    this.addToHeader(envelope, header, "MessageID", UUID.randomUUID().toString());
    ...
                } else {
                    if (header != null) {
                        Iterator<?> i = header.getChildElements();
                        while (i.hasNext()) {
                            SOAPElement el = (SOAPElement) i.next();
                            String tagName = el.getLocalName();
                            String value = el.getValue();
    ...
                        }
                    }

                }
            } catch (Exception e) {
...

            }
            return true;
        }

        private void addToHeader(SOAPEnvelope envelope, SOAPHeader header, String key, String value) throws SOAPException {
            Name qname = envelope.createName(key, "ns1", "http://ecom.bnpp.com");
            SOAPHeaderElement element = header.addHeaderElement(qname);
            element.addTextNode(value);
        }
于 2012-12-19T07:58:34.037 回答