在Camel-CXF中将自定义Soap-Header设置为pojo-message

时间:2013-01-24 08:52:33

标签: cxf apache-camel soap-client

我遇到了cxf soap-headers的问题。我用Contract-firs开发方法建立了一个cxf项目。我想用一个cxf组件调用一个web服务,看起来像这样。

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

我想发送一条pojo-message抛出一个直接组件作为对ws的请求。我的路线看起来像这样:

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

我需要像这样实现一个soap标头:

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

为了存档,我写了一个像这样的处理器(另见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); 
}

不幸的是我在这个语句中得到了一个空指针Exception:            列出soapHeaders = CastUtils.cast((列表

显然此消息中没有soap-header。它似乎根本不是肥皂信息。 像这样火热

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

不起作用,因为它只产生没有肥皂头的肥皂袋。 (而且,因为cxf-endpoint在pogo模式下工作,所以这不会起作用) 你能给我举个例子,说明如何从pojo消息中设置一个soap消息(带有soap-header)。

谢谢 加布里埃尔

1 个答案:

答案 0 :(得分:4)

不知道你是否已经解决了问题,但我也经历过类似的事情,所以也许有人会受益。

如果您的NPE是由于没有现有标题,那么在需要时创建新列表是完全可以接受的。

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

但是,您可能还有另一个问题,即手工制作的XML用于填充SoapHeader。您仍在使用原始CXF示例中的outofbandHeader元素;这是示例中的特定标头,是带外标头的常规包装器。此外,您的simpleAuth未标记为元素(尽管难以阅读......)。

如果您为simpleAuth元素添加了带有@XMLRootElement的注释类(生成或创建),则可以使用带有JAXBDataBinding的SoapHeader构造函数。 CXF将为您整理标题。

@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();
    }
}