Search This Blog

Wednesday, 17 July 2013

Conduits in CXF

If we observe any page request sent by a browser, it will be seen to be composed of multiple headers. Consider the below screenshot taken for a simple AJAX request.
As seen the browser has set several request headers. When using web services with HTTP transport layer we should also be able to take advantage of these settings. For e.g. the gzip encoding if supported by the header could reduce the size of the response while sent over the network. Chunking is also an option for improved performance.
But with web services how do we specify these headers ?
This is where CXF conduits come into the picture. Taken verbatim from the book Apache CXF Web Service Development:
Conduit simply means channel or pipe. HTTP conduits are channels allow us to apply 
certain HTTP related properties or attributes which can affect the way  messages are 
exchanged between endpoints. You typically specify HTTP connection attributes such as 
whether to allow chunking, connection timeout, and so on. The  conduit is always 
defined by the client or consumer of the service
I decided to create a conduit for my CXF client. The changes are all in the configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

 <jaxws:client id="client" serviceClass=""
   <bean id="loggingInInterceptor" 
            class="org.apache.cxf.interceptor.LoggingInInterceptor" >
    <property name="prettyLogging" value="true" />
   <bean id="loggingOutInterceptor" 
             class="org.apache.cxf.interceptor.LoggingOutInterceptor" >
    <property name="prettyLogging" value="true" />

 <http-conf:conduit name="{}IEchoPort.http-conduit">
  <http-conf:client Accept="text/xml" AcceptEncoding="gzip,deflate,sdch"
   AllowChunking="true" AutoRedirect="false" CacheControl="No-Cache"
   ContentType="text/xml" />
As seen we added a new element the conduit. This is from a different workspace. CXF requires that the name attribute's value be defined in a certain manner:
You don't have to worry too much of getting it right. The CXF client logs automatically indicate the expected name if you provide the wrong one.
2171 [main] DEBUG org.apache.cxf.configuration.spring.ConfigurerImpl  - Could n
ot find a definition for bean with id {}IEchoPor
t.http-conduit - no injection will be performed.
Within the XML file, we need to provide the remaining settings for the client. The client element sets the header values.
The above configuration indicates the
  • values for the accept header, content-type header and accept-encoding header. 
  • Chunking has also been allowed. 
  • Cached responses have been deemed unacceptable. 
The code in the application remains unchanged:
public static void main(String[] args) {
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
 IEcho client = (IEcho) context.getBean("client");
 String response = client.echoHi("Joe");
 System.out.println("Response: " + response);
The client logs indicate that the conduit has been detected and the changes applied:
2421 [main] DEBUG org.apache.cxf.transport.http.HTTPConduit  - Conduit '{http:/
/}IEchoPort.http-conduit' has been (re)configured for p
lain http.
 2687 [main] INFO  - Outbo
und Message
ID: 1
Address: http://localhost:8080/WithSpring/services/echo
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[text/xml], Accept-Encoding=[gzip,deflate,sdch], Cache-Control=
[No-Cache], Connection=[Keep-Alive], SOAPAction=[""]}
Payload: <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="">
    <ns2:echoHi xmlns:ns2="">

 2718 [main] DEBUG org.apache.cxf.transport.http.HTTPConduit  - Sending POST Mes
sage with Headers to http://localhost:8080/WithSpring/services/echo Conduit :{ht
As seen below conduit settings have been applied in the request. In a similar manner HTTP destination can be used to apply settings on the server side. Conduits include several more settings such as SSL, Challenge Authentication, basic authentication etc.
These settings can also be applied through code:
IEcho echo = (IEcho) context.getBean("client");
Client client = ClientProxy.getClient(echo);
HTTPConduit http = (HTTPConduit) client.getConduit();

HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();



String response = echo.echoHi("Joe");
System.out.println("Response: " + response);
HTTPClientPolicy instance is used to set the header values. If we run the code then the logs indicate that the values were set at the client.


  1. Very detailed explanation of the purpose of conduit. Thank you very much

  2. I'm using soap endpoints.I was tried to set timeout for soap calls using conduits.But not working.Below my code

    Please help me out.

  3. Ultimate explanation. Great thanks :)