Create a Handler class implementing SOAPHandler
package hello.handler; import java.util.Set; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; public class LoggingHandler implements SOAPHandler { public Set getHeaders() { // TODO Auto-generated method stub return null; } public boolean handleMessage(MessageContext arg0) { System.out.printf("Start handle message..."); log(arg0); return true; } public boolean handleFault(MessageContext arg0) { System.out.printf("Start handle fault..."); log(arg0); return true; } public void close(MessageContext arg0) { // TODO Auto-generated method stub } private void log(MessageContext ctx){ Boolean outbound = (Boolean)ctx.get( MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound.booleanValue()){ System.out.println("Out bound message"); }else{ System.out.println("In bound message"); } } }
If from Java
Create a handler configuration file
and add the handler class to handler-chain.
<?xml version="1.0" encoding="UTF-8"?> <handler-chains xmlns="http://java.sun.com/xml/ns/javaee"> <handler-chain> <handler> <handler-class>hello.handler.LoggingHandler</handler-class> </handler> </handler-chain> </handler-chains>
Annotate server implementation class
and point to handler config file. Use either absolute path or relative path to the server class.
import javax.jws.HandlerChain; import javax.jws.WebService; @WebService @HandlerChain(file="handlers.xml") public class HelloworldImpl {
Include handler class and config xml files in the Ant generate-server script
<target name="generate-server" depends="setup"> <apt fork="true" debug="true" verbose="true" destdir="${build.classes.home}" sourcedestdir="${build.generated.home}" sourcepath="${basedir}/src"> <classpath> <path refid="jaxws.classpath"/> <pathelement location="${basedir}/src"/> </classpath> <option key="r" value="${build.home}"/> <source dir="${basedir}/src"> <include name="**/server/*.java"/> <include name="**/handler/*.java"/> </source> </apt> <!-- copy handlers descriptor file --> <copy todir="${build.classes.home}"> <fileset dir="${basedir}/src"> <include name="**/server/**/*.xml"/> </fileset> </copy> </target>
If from WSDL
Create a custom-server.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="wedms.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <bindings node="wsdl:definitions" xmlns:jws="http://java.sun.com/xml/ns/javaee"> <jws:handler-chains> <jws:handler-chain> <jws:handler> <jws:handler-class>hello.handler.LoggingHandler</jws:handler-class> </jws:handler> </jws:handler-chain> </jws:handler-chains> </bindings> </bindings>
Add the binding attribute to wsimport task
<wsimport binding="${basedir}/etc/custom-server.xml" wsdl="${basedir}/etc/AddNumbers.wsdl"> </wsimport> </xml> Include the generated handler xml files in compiled classes <pre lang="xml"> <copy todir="${build.classes.home}"> <fileset dir="${gen.class.dir}"> <include name="**/*.xml"/> </fileset> </copy>
Build and Deploy