AXIS2 handler is similar to servlet filter. AXIS2 handler gives you access to MessageContext similar to servlet filter which gives you access to request and response objects.
The typical use case of using AXIS2 handler are: adding security header in SOAPEnvelope, logging outgoing and incommming SOAPEnvelope. Lets take an example where I wan to add username as soap header in all the out going message from client.
You have to do following changes to engage handler.
AXIS2.xml
Since I want to add header in out going message so I will register my handler in outFlow phase of AXIS2. The mandatory attributes of handler tags are name and handler class name. Here I have created handler with the class name AXIS_Handler.
<phaseOrder type="OutFlow">
<!-- user can add his own phases to this area -->
<phase name="soapmonitorPhase"/>
<phase name="OperationOutPhase"/>
<!--system predefined phase-->
<!--these phase will run irrespective of the service-->
<phase name="RMPhase"/>
<phase name="PolicyDetermination"/>
<phase name="MessageOut">
<handler name="userID" class="AXIS_Handler" />
</phase>
<phase name="Security"/>
</phaseOrder>
To qualify a class as a handler either we have to extend AbstractHandler or implement Handler interface.
public class AXIS_Handler extends AbstractHandler {
public InvocationResponse invoke(MessageContext ctx) throws AxisFault {
SOAPEnvelope env = ctx.getEnvelope();
SOAPHeader hdr = env.getHeader();
SOAPFactory factory = (SOAPFactory) env.getOMFactory();
OMNamespace ns = factory.createOMNamespace("http://ws.apache.org/axis2", "hns");
SOAPHeader head = factory.createSOAPHeader(env);
SOAPHeaderBlock header = head.addHeaderBlock("userID", ns);
header.setText("shiv");
return InvocationResponse.CONTINUE;
}
}
The next thing would be using modified axis2.xml file as configuartion context. There are two ways we can do.
ConfigurationContext cc= ConfigurationContextFactory.createConfigurationContextFromFileSystem(pathToRepository, pathToAxis2xml);
ServiceClient sc = new ServiceClient(cc, nulll, null, null);
But if you are not passing cc as parameter then you can pass as system property as JVM argument as follows.
-Daxis2.xml="location of axis2.xml file"
Once you are done with above. Run the client and you should able to see SOAPEnvelope containing header added by handler.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:axis="http://ws.apache.org/axis2">
<soapenv:Header><hns:userID xmlns:hns="http://ws.apache.org/axis2">shiv</hns:userID></soapenv:Header>
<soapenv:Body><axis:echoString><axis:param0>11</axis:param0></axis:echoString></soapenv:Body>
</soapenv:Envelope>
Hi, thanks for the great article
ReplyDeleteJust one question, where do you deploy the handler and how?