0

I developed an app which sends SOAP requests to different services through Apache CXF 2.7.10.

For some requests I have to pass a 'SOAP:HEADR' with additional data for processing, for others I haven't to do. By this reason I add headers dynamically in my code.

A pure request:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:getList xmlns:ns2="http://service.com/front/list">
            <code>1</code>
            <requestId>636978</requestId>
            <version>0.1</version>
            <buildId>401163</buildId>
            <clientId>500</clientId>
            <active>true</active>
        </ns2:getList>
    </soap:Body>
</soap:Envelope>

A request with header:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"/>
    <soap:Body>
        <getList xmlns="http://service.com/front/list" xmlns:ns2="http://service.com/front/list">
            <code>1</code>
            <requestId>636992</requestId>
            <version>0.1</version>
            <buildId>401163</buildId>
            <clientId>500</clientId>
            <active>true</active>
        </getList>
    </soap:Body>
</soap:Envelope>

The code for clearing headers:

bindingProvider.binding.handlerChain = null

The code for adding headers:

bindingProvider.binding.handlerChain = listOf(CacheSoapHeaderHandler()) 

CacheSoapHeaderHandler:

public class CacheSoapHeaderHandler implements SOAPHandler<SOAPMessageContext> {

    private static final ObjectFactory OBJECT_FACTORY = new ObjectFactory();

    private static final JAXBContext context = createContext();

    public CacheSoapHeaderHandler() {
    }

    @Override
    public boolean handleMessage(SOAPMessageContext smc) {
        return true;
    }

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

    @Override
    public void close(MessageContext arg0) {
    }

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

    private static JAXBContext createContext() {
        try {
            return JAXBContext.newInstance(CacheInfo.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

How can I return a 'ns2' qualifier in a body section when I add headers?

A solution: you can change a message header and a body in a SOAPHandler including namespaces and them prefixes (qualifiers):

@Override
public boolean handleMessage(SOAPMessageContext smc) {

        try {

            smc.getMessage().getSOAPBody().getFirstChild().setPrefix("ns2");
            smc.getMessage().getSOAPBody().getFirstChild().getAttributes().removeNamedItem("xmlns");

        } catch (Exception e) {
            throw new IllegalArgumentException("Can't generate a WS request", e);
        }

    return true;
}
JAlexey
  • 114
  • 4
  • 1
    Why do you need a namespace present in your headers when it actually has no element entry that is using a namespace anyway? If you'd return a `QName` object with a defined namspace, the namespace should be added to the namespace section of the headers AFAIK. You might want to [check how other users have added headers](https://stackoverflow.com/questions/11956562/how-to-add-soap-header-when-making-a-soap-request-using-the-java-objects-generat) or how [CXF documented](http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest%2Fresponse%3F) such a feature – Roman Vottner Mar 29 '19 at 17:28
  • Excuse me, apparently I described my question not quite exactly. I need qualifiers in a body section. Thanks for refs. It can be helpful I will try that on monday. – JAlexey Mar 29 '19 at 20:58

0 Answers0