接上一篇 java2wsdl+Axis2+hibernate开发webservice学习(1)
一、自定义module
---com.mywebservice.ws.module
---AuditLogModule.java
---AuditInHandler.java
---AuditOutHandler.java
AuditLogModule.java
public class AuditLogModule implements Module { // initialize the module public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault { System.out.println("initializing AuditLogModule"); } public void engageNotify(AxisDescription axisDescription) throws AxisFault { } // shutdown the module public void shutdown(ConfigurationContext configurationContext) throws AxisFault { System.out.println("shutdown AuditLogModule"); } public String[] getPolicyNamespaces() { return null; } public void applyPolicy(Policy policy, AxisDescription axisDescription) throws AxisFault { } public boolean canSupportAssertion(Assertion assertion) { return true; } }
AuditInHandler.java
public class AuditInHandler extends AbstractHandler implements Handler { public InvocationResponse invoke(MessageContext msgContext) throws AxisFault { // get http request HttpServletRequest request = (HttpServletRequest) msgContext .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); System.out.println("AuditInHandler beginning..."); return InvocationResponse.CONTINUE; } }
AuditOutHandler.java
public class AuditOutHandler extends AbstractHandler implements Handler { @Override public InvocationResponse invoke(MessageContext msgContext) throws AxisFault { System.out.println("AuditOutHandler beginning..."); return InvocationResponse.CONTINUE; } }
二、编译并copy build/classes mode文件到WebConten/WEB-INF/modules下,并创建module.xml
---mymodule
---com
---...
---META-INF
---module.xml
<module name="auditLogger" class="com.mywebservice.ws.module.AuditLogModule"> <InFlow> <handler name="InFlowAuditLogHandler" class="com.mywebservice.ws.module.AuditInHandler"> <order phase="auditLogPhase" /> </handler> </InFlow> <OutFlow> <handler name="OutFlowAuditLogHandler" class="com.mywebservice.ws.module.AuditOutHandler"> <order phase="auditLogPhase" /> </handler> </OutFlow> <OutFaultFlow> <handler name="FaultOutAuditFlowLogHandler" class="com.mywebservice.ws.module.AuditOutHandler"> <order phase="auditLogPhase" /> </handler> </OutFaultFlow> <InFaultFlow> <handler name="FaultInAuditFlowLogHandler" class="com.mywebservice.ws.module.AuditInHandler"> <order phase="auditLogPhase" /> </handler> </InFaultFlow> </module>
三、修改axis2.xml
打开/conf/axis2.xml,增加module的配置:
<phaseOrder type="InFlow"> <!-- System predefined phases --> <phase name="Transport"> <handler name="RequestURIBasedDispatcher" class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher"> <order phase="Transport"/> </handler> <handler name="SOAPActionBasedDispatcher" class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher"> <order phase="Transport"/> </handler> </phase> <phase name="Addressing"> <handler name="AddressingBasedDispatcher" class="org.apache.axis2.dispatchers.AddressingBasedDispatcher"> <order phase="Addressing"/> </handler> </phase> <phase name="Security"/> <phase name="PreDispatch"/> <phase name="Dispatch" class="org.apache.axis2.engine.DispatchPhase"> <handler name="RequestURIBasedDispatcher" class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher"/> <handler name="SOAPActionBasedDispatcher" class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher"/> <handler name="RequestURIOperationDispatcher" class="org.apache.axis2.dispatchers.RequestURIOperationDispatcher"/> <handler name="SOAPMessageBodyBasedDispatcher" class="org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher"/> <handler name="HTTPLocationBasedDispatcher" class="org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher"/> <handler name="GenericProviderDispatcher" class="org.apache.axis2.jaxws.dispatchers.GenericProviderDispatcher"/> <handler name="MustUnderstandValidationDispatcher" class="org.apache.axis2.jaxws.dispatchers.MustUnderstandValidationDispatcher"/> </phase> <phase name="RMPhase"/> <!-- System predefined phases --> <!-- After Postdispatch phase module author or service author can add any phase he want --> <phase name="OperationInPhase"> <handler name="MustUnderstandChecker" class="org.apache.axis2.jaxws.dispatchers.MustUnderstandChecker"> <order phase="OperationInPhase"/> </handler> </phase> <phase name="auditLogPhase"/> <phase name="soapmonitorPhase"/> </phaseOrder> <phaseOrder type="OutFlow"> <!-- user can add his own phases to this area --> <phase name="soapmonitorPhase"/> <phase name="OperationOutPhase"/> <phase name="auditLogPhase"/> <!--system predefined phase--> <!--these phase will run irrespective of the service--> <phase name="RMPhase"/> <phase name="PolicyDetermination"/> <phase name="MessageOut"/> <phase name="Security"/> </phaseOrder> <phaseOrder type="InFaultFlow"> <phase name="Addressing"> <handler name="AddressingBasedDispatcher" class="org.apache.axis2.dispatchers.AddressingBasedDispatcher"> <order phase="Addressing"/> </handler> </phase> <phase name="Security"/> <phase name="PreDispatch"/> <phase name="Dispatch" class="org.apache.axis2.engine.DispatchPhase"> <handler name="RequestURIBasedDispatcher" class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher"/> <handler name="SOAPActionBasedDispatcher" class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher"/> <handler name="RequestURIOperationDispatcher" class="org.apache.axis2.dispatchers.RequestURIOperationDispatcher"/> <handler name="SOAPMessageBodyBasedDispatcher" class="org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher"/> <handler name="HTTPLocationBasedDispatcher" class="org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher"/> <handler name="GenericProviderDispatcher" class="org.apache.axis2.jaxws.dispatchers.GenericProviderDispatcher"/> <handler name="MustUnderstandValidationDispatcher" class="org.apache.axis2.jaxws.dispatchers.MustUnderstandValidationDispatcher"/> </phase> <phase name="RMPhase"/> <!-- user can add his own phases to this area --> <phase name="OperationInFaultPhase"/> <phase name="auditLogPhase"/> <phase name="soapmonitorPhase"/> </phaseOrder> <phaseOrder type="OutFaultFlow"> <!-- user can add his own phases to this area --> <phase name="soapmonitorPhase"/> <phase name="OperationOutFaultPhase"/> <phase name="auditLogPhase"/> <phase name="RMPhase"/> <phase name="PolicyDetermination"/> <phase name="MessageOut"/> <phase name="Security"/> </phaseOrder>
其中<phase name="auditLogPhase"/> 为自定义的。
四、修改services.xml
增加<module ref="auditLogger"/>
<service name="OnepassServices"> <messageReceivers> <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="com.axis2.ws.skeleton.OnepassServicesMessageReceiverInOut"/> </messageReceivers> <module ref="auditLogger"/> <parameter name="ServiceClass">com.axis2.ws.skeleton.OnepassServicesSkeleton</parameter> <parameter name="useOriginalwsdl">true</parameter> <parameter name="modifyUserWSDLPortAddress">true</parameter> <operation name="getUserProfile" mep="http://www.w3.org/ns/wsdl/in-out" namespace="http://axis2.com/ws/service"> <actionMapping>urn:getUserProfile</actionMapping> <outputActionMapping>urn:getUserProfileResponse</outputActionMapping> </operation> </service>
五:deploy工程
以Tomcat为例
六:测试,run the test
OnePassTester.java/@Test testGetUserProfile1()
测试结果:
后台打印信息
--- initializing AuditLogModule
--- AuditInHandler beginning...
--- AuditOutHandler beginning...