CXF - catching/handling HTTP exceptions

so_mv picture so_mv · May 19, 2011 · Viewed 7.1k times · Source

This is rather a basic cxf usage question. How/where can we catch the actual HTTP exception/error. I kind of followed the Interceptor/MessageObserver concept but could not capture the HTTP error using them.

I see this error in the log4j log file.

Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response
'401: Unauthorized' when communicating with http://10.107.172.79/test/_vti_bin/lists.asmx
                at  org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1502)
                at org.apache.cxf.transpot.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1448)
                at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1356)
                at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
                at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:614)
                at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
                ... 9 more

Only javax.xml.ws.WebServiceException with "Could not send Message." message is thrown while calling the service

try{

      GetListCollectionResult result = port.getListCollection();

   }catch (javax.xml.ws.WebServiceException excep){

}

This is how we call the service.

To provide NTLM credentials:

Authenticator.setDefault( extended class of Authenticator);

Create the service.

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ListsSoap.class);
factory.setAddress(list_url);
ListsSoap port = (ListsSoap) factory.create();

Update the conduit.

..

        Client client = ClientProxy.getClient(port);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(36000);
        httpClientPolicy.setAllowChunking(false);
        http.setClient(httpClientPolicy);

Call service and get the result.

GetListCollectionResult result  = port.getListCollection();

Answer

so_mv picture so_mv · May 20, 2011

Nevermind, I found the answer in the CXF mailing list.

excep.getCause()

gives access to the underlying exception, in my case it is the HTTP Transport exception.