Thursday, November 26, 2009

Web Service Examples

I would like to share a few web service examples, firstly I will walk through the process of developing and publishing web services using JAX-RPC and AXIS2 on Tomcat.
Step 1
Install AXIS2 and verify its working, Step by step instructions in installing AXIS2 is given in http://ws.apache.org/axis2/1_1/installationguide.html
Step 2
Develop a simple Hello world example using AXIS2 and deploy on tomcat to demonstrate its working. http://people.apache.org/~ruchithf/hw-axis2/

Lets now see a simple temperature conversion web service without handlers.

Setup:
Download axis2.war unzip it.
Download weblogic 10.0 and install it.
Copy the contents of axis2.war into deploy directory of weblogic i.e wlserver_10.0 for weblogic 10.0
we can verify whether axis has been successfully deployed by starting the admin console and going to the url http:localhost:7001/axis2.
You should see axis2 welcome page.

Step 1 : Write a simple Java class with temperature conversion methods:

package weather;
public class TC {
/**
* util method to convert celsius to fahrenheit
* @param cValue : double value of celsius
* @return calculated value of fahrenheit
*/
public double c2fConvertion(double cValue) {
return ((cValue * 9.0)/5.0 )+ 32.0;
}

/**
* util method to convert fahrenheit to celsius
* @param fValue : double value of fahrenheit
* @return calculated value of celsius
*/
public double f2cConvertion(double fValue) {
return ((fValue - 32.0) * 5.0) / 9.0;
}
}

Include services.xml file in the META-INF folder, which is to be placed in the root.
<service name="BalanceEnquiryService">
    <description>
    This is a sample Web Service with a logging module engaged.
    </description>
     <parameter name="ServiceClass" locked="xsd:false">BalanceEnquiryService</parameter>
    <operation name="getBalance">
    <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
     </operation>
 </service>

Step 2 :Compile the Program and make a aar file and deploy on axis2 which is deployed onto weblogic or tomcat.

Step 3: Successful deployment of web service will generate a WSDL file. Copy the url and save the file with .wsdl extension. paste the file into the client project.

Step 4 : At command prompt, go to the project where we copied the wsdl and type the command. WSDL2JAVA -uri .\****.wsdl -p client -d adb

Step 5: Stub would be generated at the package client in the project. Write a client test class to test the web service.

package weatherclient;

import java.rmi.RemoteException;

import weatherclient.TCStub.C2FConvertionResponse;

public class TCTest {

public static void main(String args[]) {
try {
TCStub abc = new TCStub("http://localhost:7001/axisnew/services/TC.TCHttpSoap12Endpoint");
weatherclient.TCStub.C2FConvertion req= new weatherclient.TCStub.C2FConvertion();
req.setArgs0(20);
C2FConvertionResponse res = abc.c2FConvertion(req);
System.out.print("converted value is " + res.get_return());
} catch(Exception e){
e.printStackTrace();
}
}
}


We can view the temperature on the console. Similarly we can include any business methods in our web service and the client can class them.

Now, lets see a web service example which uses Handlers.
The installation and deployment steps are similar to the web service described above.

Step 1: Write a service class as mentioned above and include services.xml. The services.xml file here would contain a module reference which points to the handler.

<service name="BalanceEnquiryService">
    <description>
    This is a sample Web Service with a logging module engaged.
    </description>
    <module ref="authentication"/>
     <parameter name="ServiceClass" locked="xsd:false">BalanceEnquiryService</parameter>
    <operation name="getBalance">
    <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
     </operation>
   </service>
     

Step 2: Write a module with the handler information and include a module.xml file which contains all the operations to be performed by the module.

The module class would contain the handler operations.

import org.apache.axis2.AxisFault;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;
public class AuthenticationModule implements Module {


// initialize the module
public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault {
}

public void engageNotify(AxisDescription axisDescription) throws AxisFault {
}

// shutdown the module
public void shutdown(ConfigurationContext configurationContext) throws AxisFault {
}

public String[] getPolicyNamespaces() {
return null;
}

public void applyPolicy(Policy policy, AxisDescription axisDescription) throws AxisFault {
}

public boolean canSupportAssertion(Assertion assertion) {
return true;
}
}


Module.xml for the above handler class is as follows.


<?xml version="1.0" encoding="UTF-8"?>
<module name="authentication" class="AuthenticationModule">
  
   <InFlow>
         <handler name="InFlowLogHandler" class="AuthenticationHandler">
            <order phase="authentication"/>
        </handler>
    </InFlow>

    <OutFlow>
        <handler name="OutFlowLogHandler" class="AuthenticationHandler">
            <order phase="authentication"/>
        </handler>
    </OutFlow>

    <OutFaultFlow>
        <handler name="FaultOutFlowLogHandler" class="AuthenticationHandler">
            <order phase="authentication"/>
        </handler>
    </OutFaultFlow>

    <InFaultFlow>
         <handler name="FaultInFlowLogHandler" class="AuthenticationHandler">
            <order phase="authentication"/>
        </handler>
    </InFaultFlow>
</module>

Make a .mar file of module class and module.xml and deploy it into the modules folder of axis2 in our application server. Upon successful deployment we can view the module in the axis2 page under modules sub directory.

Step3: Engage the module for our service. Also engage the operations for the module.
Step4: Similar to the web services mentioned above, ie. verify wsdl, generate stub, write test case and demonstrate the working of the application.

No comments:

Post a Comment