Adding a handler in AXIS2

Adding a handler in AXIS2

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. 

 
 
 
 
 
 
 
 
 
 
 
 
 


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. 


shiv
11

你可能感兴趣的:(soa)