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. 

<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值