Search code examples
javaweb-serviceshttp-headersapache-camelcxf

Http header Content-Length in camel cxf client request


We have a simple camel route "from->to":

<from uri="cxf:bean:testServiceProvider?loggingFeatureEnabled=true" />
<to uri="cxf:bean:testServiceClient?loggingFeatureEnabled=true" />

This route acts like a router or proxy for a third party's web service:

  • Clients use it as endpoint.
  • Adds WSS headers to Soap message.
  • Route requests to real endpoint.

Service and client in this proxy are created with cxf beans.

The endpoint's web service seems to require Content-Length HTTP header, but cxf requests to endpoint does not contain this header by default. All the requests done by this proxy receive the same response:

HTTP response '411: Length required' when communicating with https://host:port/testService

We tried to add this header with an OutInterceptor, adding it to PROTOCOL_HEADERS:

Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS);
headers.put("Content-Length", Collections.singletonList(String.valueOf(messageLength)));

Two questions:

How to know the value of messageLength? Is there an easier way to do this?

Thanks!


Solution

  • You can try with http:conduit, disabling AllowChunking. This will force cxf to include Content-Length header in the request. By default cxf behaviour is to allow chunking, so it can be generating the problem you're facing, even specifying the Content-length header.

    <http:conduit name="{http://namespace}WebService.http-conduit">
        <http:client AllowChunking="false" CacheControl="No-Cache" 
    ContentType="text/xml;charset=UTF-8" ConnectionTimeout="900000" 
    ReceiveTimeout="900000" Connection="Keep-Alive" />
    </http:conduit>