Setting up custom Soap-Header to pojo-message in Camel-CXF

I have a problem with cxf headers. I created a cxf project using the Contract-firs-development method. I want to call a webservice with a cxf component that 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 a pojo message to pass a direct component as a request for 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 a processor like this (see also the example 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 am getting a null pointer Exception in this expression: List soapHeaders = CastUtils.cast ((List

Obviously, there are no soapy headings in this post. And it looks like it wasn't a soap message. Marschaling like this

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

      

doesn't work as it only creates a soapy wrapper without a soapy header. (and from that it doesn't work as the cxf-endpoint is in pogo mode) Could you please give an example of how to set a soap message (with a soap header) from the pojo post.

Thank. Gabriel

+3


source to share


1 answer


I don’t know if you have already solved your problem, but I experienced something similar too, so maybe someone will win.

If your NPE is due to missing existing headers, it is entirely possible to create a new list if needed.

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

      



But you might have another problem with the manual XML processing used to populate the SoapHeader. You are still using the outofbandHeader element from the original CXF example; this is the specific heading in the example, not a generic wrapper for out of range headings. Also, your simpleAuth is not tagged as an element (although difficult to read ...).

If you have an annotated class (generated or generated) with the @XMLRootElement for your simpleAuth element, you can use the SoapHeader constructor that uses JAXBDataBinding. CXF will march the headline for you.

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

    Message out = exchange.getOut();
    if (out.getHeader(Header.HEADER_LIST) == null) {
        out.setHeader(Header.HEADER_LIST, new ArrayList<SoapHeader>());
    }
    List<SoapHeader> headers = CastUtils.cast((List<?)out.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();
    }
}

      

+3


source







All Articles