Processing Request/Response Messages of a Web Service Using Handler Chain
Here is the ant script build.xml to package the EAR. Note that Web.xml and application.xml should be available before building the EAR.
build.xml
<project name="webservices-handler" default="all" basedir=".">
<!-- set global properties for this build -->
<property environment="env" />
<property name="compiler" value="modern" />
<property name="debug" value="yes" />
<property name="deprecation" value="yes" />
<property name="build.compiler" value="${compiler}" />
<property name="source" value="." />
<property name="build" value="${source}/build" />
<property name="war_file" value="BalanceEnquiry.war" />
<property name="ear_file" value="BalanceEnquiry.ear" />
<property name="namespace" value="http://www.bea.com" />
<property name="client.classes.dir" value="{source}/clientclasses" />
<target name="all" depends="clean, war, ear" />
<target name="clean">
<delete dir="${build}" />
<delete file="${source}/${ear_file}" />
<delete file="${war_file}" />
</target>
<target name="war">
<delete dir="${build}" />
<mkdir dir="${build}" />
<mkdir dir="${build}/WEB-INF" />
<mkdir dir="${build}/WEB-INF/classes" />
<javac srcdir="${source}" includes="AuthenticationHandler.java,
BalanceEnquiryService.java"
destdir="${build}/WEB-INF/classes" />
<copy todir="${build}/WEB-INF">
<fileset dir="${source}">
<include name="web-services.xml" />
<include name="web.xml" />
</fileset>
</copy>
<jar jarfile="${war_file}" basedir="${build}" />
</target>
<target name="ear" depends="war">
<delete dir="${build}" />
<mkdir dir="${build}" />
<mkdir dir="${build}/META-INF" />
<copy todir="${build}">
<fileset dir="${source}">
<include name="${war_file}" />
</fileset>
</copy>
<copy todir="${build}/META-INF">
<fileset dir="${source}">
<include name="application.xml" />
</fileset>
</copy>
<jar jarfile="${source}/${ear_file}" basedir="${build}" />
</target>
</project>
The ant script creates a WAR and then packages the EAR.
Deploy the generated EAR in Weblogic server. The following is the client to invoke the BalanceEnquiryService.
Client.java
import org.apache.axis.client.Call;
import org.apache.axis.client.ServiceFactory;
import org.apache.axis.client.Service;
import org.apache.axis.MessageContext;
import org.apache.axis.attachments.Attachments;
import org.apache.axis.message.SOAPEnvelope;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import java.util.Iterator;
public class Client
{
private static String TARGET_NAMESPACE = "http://www.bea.com";
private static QName xsdFloat = new
QName("http://www.w3.org/2001/XMLSchema", "float");
public static org.apache.axis.message.SOAPEnvelope env = null;
public static SOAPMessage message = null;
public static void main(String[] argv) throws Exception
{
Client client = new Client();
env = client.constructSOAPEnvelope();
client.constructHeader(env);
client.constructBody(env);
System.setProperty( "javax.xml.rpc.ServiceFactory",
"org.apache.axis.client.ServiceFactory" );
String url =
"http://localhost:7001/BalanceEnquiry/BalanceEnquiryService";
ServiceFactory factory =
(org.apache.axis.client.ServiceFactory)ServiceFactory.
newInstance();
QName serviceName = new QName(TARGET_NAMESPACE,
"BalanceEnquiryService");
Service service =
(org.apache.axis.client.Service)factory.
createService(serviceName);
Call call = (org.apache.axis.client.Call)service.createCall();
call.setTargetEndpointAddress(url);
SOAPEnvelope result = call.invoke(env);
System.out.println(result);
}
public SOAPEnvelope constructSOAPEnvelope() throws Exception
{
org.apache.axis.message.SOAPEnvelope env = new
org.apache.axis.message.SOAPEnvelope();
return env;
}
public void constructHeader(SOAPEnvelope envelope) throws Exception
{
SOAPHeader header = envelope.getHeader();
Name headerElementName =
envelope.createName("AccountDetails","",
"http://schemas.xmlsoap.org/soap/
envelope/");
SOAPHeaderElement headerElement =
header.addHeaderElement(headerElementName);
headerElement.setMustUnderstand(false);
headerElement.addNamespaceDeclaration("soap",
"http://schemas.xmlsoap.org/soap/envelope/");
SOAPElement accNo = headerElement.addChildElement("accountNo");
accNo.addTextNode("12345");
SOAPElement pinNo = headerElement.addChildElement("pin");
pinNo.addTextNode("6789");
}
public void constructBody(SOAPEnvelope envelope) throws Exception
{
SOAPBody body = envelope.getBody();
Name bodyRootElementName =
envelope.createName("getBalance","",
"http://schemas.xmlsoap.org/soap/
encoding/");
SOAPBodyElement bodyRootElement =
body.addBodyElement(bodyRootElementName);
SOAPElement bodyElement =
bodyRootElement.addChildElement("param0");
bodyElement.addAttribute(envelope.createName("xsi:type"),
"xsd:string");
bodyElement.addTextNode("12");
}
}
If there is a client side handler available, make the following changes:
- Import the following two classes.
import javax.xml.rpc.handler.HandlerInfo; import javax.xml.rpc.handler.HandlerRegistry;
- Include the following lines before call.invoke().
QName portName = new QName( "http://bea.com/", "HelloWorldServicePort"); HandlerRegistry registry = service.getHandlerRegistry(); List handlerList = new ArrayList(); handlerList.add( new HandlerInfo( ClientHandler.class, null, null ) ); registry.setHandlerChain( portName, handlerList );
Conclusion
We hope this article helps you understand more about the the Handler chain mechanism and its usage in Web Services.
About the Authors
Nandhini Arumugam holds a Masters degree in Computer Applications from PSG College of Technology, Coimbatore. She has been working as a software engineer in the Telecom and Mobile Solutions Lab, Hewlett-Packard, Bangalore for more than one year. The domain of her experience includes Web Services and J2EE-related technologies. She can be reached at nandhini.arumugam@hp.com.
Jeyarani Venkatasamy holds a Masters degree in Computer Applications from Sri Sarada College for Women, Tirunelveli. She has been working as a senior sofware engineer in the Telecom and Mobile Solutions Lab, Hewlett-Packard, Bangalore for more than one year. The domain of her experience includes Web Services, Java, and J2EE-related technologies. She can be reached at jeyarani.venkatasamy@hp.com.
