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

心已入冬 提交于 2019-12-04 09:49:48
kingolego

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!