Friday, 6 January 2012

Programmatic configuration of logging Handler for Axis 1.x client

This took ages to work out.

Mostly the web examples involve writing  a WSDD to configure a Handler subclass, but I wanted to create a handler which logged outgoing and incoming SOAP messages via Axis 1.4 and I wanted to programmatically control whether the handler was added to the axis client so that when logging was turned off, there were no unnecessary calls being made.

The code I used was to create a subclass of Axis BasicHandler to do what I wanted (by looking at the example LogHandler class provided by axis: http://kickjava.com/src/org/apache/axis/handlers/LogHandler.java.htm)

Then register it (when the configuration was turned on) using the below.  Note "css" is a service locator object that is created when you use WSDL2Java on the WSDL for the service being connected to.  This is done before you call getPort on the locator to get the service stub.


            SimpleProvider clientConfig = new SimpleProvider();
            AxisClientLogHandler logHandler = new AxisClientLogHandler();
            SimpleChain reqHandler = new SimpleChain();
            SimpleChain respHandler = new SimpleChain();
            reqHandler.addHandler(logHandler);
            respHandler.addHandler(logHandler);
            Handler pivot = new HTTPSender();
            Handler transport = new SimpleTargetedChain(reqHandler, pivot, respHandler);
            clientConfig.deployTransport(HTTPTransport.DEFAULT_TRANSPORT_NAME, transport);

            css.setEngineConfiguration(clientConfig);
            css.setEngine(new AxisClient(clientConfig));
     

1 comment:

  1. This is extremely helpful. I got sick of trying to overcome the client-config.wsdd file on the classpath. Funnily, some of the other posts that mention trying to add to the handler chain of the service don't work.

    ReplyDelete