Wednesday, 6 March 2013

Web Services Using Tomcat & Axis Framework

Developing SOAP Based Web Services Using Tomcat-6 & Axis-1....

Download Tomcat 6  & Axis 1.2RC2

Installing Axis on Tomcat
Explode the axis zip folder. Now you need to deploy the Axis engine to Tomcat:
Here's how to do it:

Copy "..\axis-1_2\webapps\axis" into "..\apache-tomcat-6.0.36\webapps" folder.
Also add activation.jar & mail.jar into "..\apache-tomcat-6.0.36\webapps\axis\WEB-INF\lib"


Verify that Axis has been deployed correctly:

http://localhost:8085/axis/




Publishing Web Services with Axis

Let's say we have a simple class like the following:
HelloService.java 
 
package com.ws;
public class HelloService {
 public String sayHello(String name){
  return "Hello " + name + " !";
 } 
        public String sayBye(String name){
  return "Bye " + name + " !";
 }
}

How do we go about making this class available via SOAP?
There are a couple of answers to that question, but we'll start with an easy solution.

At first compile the class :

Goto HelloService.java's folder in command promt & compile,

javac -d . HelloService.java

Now copy that .class file & paste it into ..\apache-tomcat-6.0.36\webapps\axis\WEB-INF\classes along with the package structure(here com\ws\HelloService.class).

Ok, now the last step is registering your Web Service so that Axis is aware of it. A Web Service can be registered with a Web Service Deployment Descriptor (WSDD) file. A deployment descriptor contains a bunch of things you want to "deploy" into Axis - i.e. make available to the Axis engine.
Here's a simple wsdd file for our WebService:

HelloService.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"                     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="HelloService" provider="java:RPC">
        <parameter name="className" value="com.ws.HelloService" />
        <parameter name="allowedMethods" value="*" />
    </service>
</deployment>





Pretty simple, really - the outermost element tells the engine that this is a WSDD deployment,and defines the "java" namespace. Then the service element actually defines the service for us. A service is a targeted chain which means it may have any of: a request flow, a provider, and a response flow.

In this case, our provider is "java:RPC", which is built into Axis, and indicates a Java RPC service. We need to tell the RPCProvider that it should instantiate and call the correct class (e.g. test.HelloService), and another to tell the engine that any public method on that class may be called via SOAP (that's what the "*" means)
Now save your wsdd file as HelloService.wsdd and run the Admin which is an Utility to deploy the WebService :

java -classpath %AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.utils.Admin server HelloService.wsdd

If everything was Ok now you should see the WebService deployed on Axis: check the "List" option from the URL http://localhost:8085/axis/

Obtaining WSDL for deployed services
Now you can see your wsdl file something like this.......

http://localhost:8085/axis/services/HelloService?wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost:8085/axis/services/HelloService"           xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8085/axis/services/HelloService" xmlns:intf="http://localhost:8085/axis/services/HelloService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->

   <wsdl:message name="sayHelloRequest">
      <wsdl:part name="in0" type="xsd:string"/>
   </wsdl:message>
   <wsdl:message name="sayHelloResponse">
      <wsdl:part name="sayHelloReturn" type="xsd:string"/>
   </wsdl:message>
   <wsdl:portType name="HelloService">
      <wsdl:operation name="sayHello" parameterOrder="in0">
         <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
         <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="HelloServiceSoapBinding" type="impl:HelloService">
      <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="sayHello">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="sayHelloRequest">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.com" use="encoded"/>
         </wsdl:input>
         <wsdl:output name="sayHelloResponse">
            <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8085/axis/services/HelloService" use="encoded"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="HelloServiceService">
      <wsdl:port binding="impl:HelloServiceSoapBinding" name="HelloService">
         <wsdlsoap:address location="http://localhost:8085/axis/services/HelloService"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>



The client
Let's take a look at an example Web Service Client that will call the sayhello method of the HelloService Web Service
 
Client.java
 
package com.ws;

import java.net.MalformedURLException;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;

public class Client{
  public static void main(String [] args) throws MalformedURLException, AxisFault  {
   String endpoint = "http://localhost:8085/axis/services/HelloService?wsdl";
   Call client = new Call(endpoint);
   String response = (String)client.invoke("http://localhost:8085/axis/services/HelloService",
          "sayHello",new Object [] {"Sidhartha"});
   System.out.println(response);
     }
}


Generating Java Classes from WSDL

Another approach is to generate Java Classes starting from WSDL contract. You can use the
AdminClient to follow this approach:

java -classpath %AXIS_HOME%/lib/wsdl4j-1.5.1.jar;%AXIS_HOME%/lib/axis.jar;%AXIS_HOME%/lib/commons-discovery-0.2.jar;%AXIS_HOME%/lib/commons-logging-1.0.4.jar;%AXIS_HOME%/lib/saaj.jar;%AXIS_HOME%/lib/jaxrpc.jar org.apache.axis.wsdl.WSDL2Java http://localhost:8085/axis/services/HelloService?wsdl

This will generate the following classes under the service namespace which is localhost\axis\services\HelloService

No comments:

Post a Comment