4
votes

I have a Problem with cxf soap-headers. I set up an cxf-project with the Contract-firs-development method. I want to call a web-service with an cxf-component, which looks like this.

<cxf:cxfEndpoint id="ICCSCustomerService" 
                 address="http://localhost:8080/iccs-xsoap/CustomerService/updateCustomer"
                 serviceClass="de.iccs.xsoap.customer.v1.CustomerServiceImpl" >
</cxf:cxfEndpoint>

I want to send an pojo-message throw an direct-component as an request for the ws. My route looks like this:

<route id="CustomerServiceUpdateCustomerTest">
        <camel:from uri="direct:iccsUpdateCustomerRequest"/>
        <camel:process ref="addCredentials"/>
        <to uri="cxf:bean:ICCSCustomerService"/>  
        <camel:to uri="stream:out"/>
</route>

I need to implement a soap header like this one:

<ns2:Header>
    <simpleAuth xmlns="http://xsoap.iccs.de/v1" password="abc" username="xxx"/>
</ns2:Header>

To archive this I wrote an processor like this one (see also example at http://camel.apache.org/cxf.html):

@Override 
public void process(Exchange exchange) throws Exception { 
   List<SoapHeader> soapHeaders = CastUtils.cast((List<?)exchange.getOut().getHeader(Header.HEADER_LIST)); 
    // Insert a new header 
    String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader " 
        + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" " 
        + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">" 
        + "<name>simpleAuth username=\"xxx\" password=\"abc\" xmlns=\"http://xsoap.iccs.de/v1\"</name></outofbandHeader>"; 

    SoapHeader newHeader = new SoapHeader(new QName("http://xsoap.iccs.de/v1", "simpleAuth"), 
                   DOMUtils.readXml(new StringReader(xml)).getDocumentElement()); 
    // make sure direction is OUT since it is a response message. 
    newHeader.setDirection(Direction.DIRECTION_OUT); 
    //newHeader.setMustUnderstand(false); 
    soapHeaders.add(newHeader); 
}

Unfortunately I get an null-pointer Exception in this statement: List soapHeaders = CastUtils.cast((List

Obviously there are no soap-headers in this message. And it seems not to be an soap message at all. Marschaling like this

       <camel:marshal>
            <soapjaxb contextPath="de.iccs.xsoap.customer.v1" />
        </camel:marshal>
        <camel:process ref="addCredentials"/>

does not work, as it produces only an soap-envelope without an soap-header. (and of corse this dosen't work as cxf-endpoint works in pogo-mode) Could you provide me an example how to set an soap message (with soap-header) from an pojo-message.

Thank you Gabriel

1

1 Answers

4
votes

Don't know if you solved your problem already, but I experienced something similar as well, so maybe someone will benefit.

If your NPE is due to there being no existing headers, it is perfectly acceptable to create a new list if needed.

if (message.getHeader(Header.HEADER_LIST) == null) {
    message.setHeader(Header.HEADER_LIST, new ArrayList<SoapHeader>());
}

But you may have another problem with the hand-crafted XML being used to populate the SoapHeader. You're still using the outofbandHeader element from the original CXF example; this is a specific header in the example, not a general wrapper for out-of-band headers. Also, your simpleAuth is not marked up as an element (although it's difficult to read...).

If you have annotated class (generated or created) with an @XMLRootElement for your simpleAuth element, you can use the SoapHeader constructor that takes a JAXBDataBinding. CXF will marshal the header for you.

@Override 
public void process(Exchange exchange) throws Exception {

    Message in = exchange.getIn();
    if (in.getHeader(Header.HEADER_LIST) == null) {
        in.setHeader(Header.HEADER_LIST, new ArrayList<SoapHeader>());
    }
    List<SoapHeader> headers = CastUtils.cast((List<?>)in.getHeader(Header.HEADER_LIST));

    SimpleAuth auth = new SimpleAuth();
    auth.setUsername("xxx");
    auth.setPassword("abc");

    try {
        SoapHeader header = new SoapHeader(new QName("http://xsoap.iccs.de/v1", "simpleAuth"),
                auth, new JAXBDataBinding(SimpleAuth.class));
        header.setDirection(Direction.DIRECTION_OUT);
        header.setMustUnderstand(true);
        soapHeaders.add(header);            
    } catch (JAXBException e) {
    e.printStackTrace();
    }
}