Showing posts with label axis2. Show all posts
Showing posts with label axis2. Show all posts

Wednesday, February 12, 2014

Deploy and Enable Rampart in Axis2 ( Tomcat)


Please follow below steps to deploy rampart module and enable rampart for axis services which is deployed in Tomcat. Also you can follow same for deploying axis2 standalone or in another application server.

Step 1: Install and configure  axis2 in tomcat

Deploy axis2 in tomcat as explained here ( http://jayalalk.blogspot.com/2014/01/writing-axis2-services-and-deploying-in.html ). you can ignore this if you have already tomcat with axis2 module.

Step 2: Download the rampart module

Download the rampart latest binaries from http://axis.apache.org/axis2/java/rampart/download.html

Step 3: Extract the rampart distribution to a folder and copy modules and jar files


- Copy the  rahas-x.x.x.mar and rampart-x.x.x.mar  files from module directory to the TOMCAT_HOME/webapps/axis2/WEB-INF/modules
- Copy all the jars from lib folder to the TOMCAT_HOME/axis2/WEB-INF/lib

Step 4: Enable the rampart in axis2


- Add     <module ref="rampart"/> in  TOMCAT_HOME/webapps/axis2/WEB-INF/conf/axis2.xml to enable rampart for all services.
- If you want to enable rampart only for given service then you can edit services.xml and add <module ref="rampart"/>

Step 5: Verify the rampart enabled

- Login to adminstrative console  (http://localhost:8080/axis2/axis2-admin/ )
- Default user name password ( admin/axis2)
- Verify the modules engagement
System components --> Available Modules
System components --> Globally Engaged Modules

Sunday, January 12, 2014

Writing Axis2 Services and deploying in Tomcat

There are three different ways which we can write Axis2 services (Web service  )  and can be deployed in Axis2 Server itself or in a web container like Tomcat.

Different ways of deploying services


  1. As simple POJO class in ..\webapps\axis2\WEB-INF\pojo\HelloWorld.class
  2. As JAR bundle  in ..\webapps\axis2\WEB-INF\pojo\myservice.jar
  3. As a service archive in  ..\webapps\axis2\WEB-INF\services\myservice.aar

We will focus on deploying service as AAR archive, which is best out of 3 deployment types mentioned above.


Getting Ready:

- Download and install Java (Minimum version is JDK1.5). Set the JAVA_HOME environment .
- Install latest version of  Tomcat in your environment ( http://tomcat.apache.org), and verify working properly. ( Sample code on this blog was test with Tomcat 7 as it was the latest stable version)

Download and install Axis2 module in tomcat


1. Download Axis2 WAR Distribution from below location. ( Version 1.6.2  was the latest at this time )

2. Deploy the Axis2 War into tomcat as a web application

- Unzip the axis2-1.6.2-war.zip
- You will get axis2.war file , then unzip this again. you will get axis2 folder containing axis2 deployable web app.
- Now copy the "axis2" folder into <TOMCAT_HOME>/webapps

3. Now start the Tomcat if it is down already, and verify the axis2 default services are running.

- Check available services in axis2 module  http://localhost:8080/axis2/services/listServices
- You will notice the getVersion application is running.
- Verify the webservice is running properly by invoking http://localhost:8080/axis2/services/Version/getVersion
You will get the axis2 version details in your screen  :) , you have verify your web service with REST style

If you need to verify this as Web-Service call using SOAP UI or Java client you can get WSDL with from http://localhost:8080/axis2/services/Version?wsdl

Writing our sample service

Lets write our own sample Web-Service using simple button up method ,where we have not even required to write any WSDL rather just know the java methods/operations needs to be exposed.

1. Writing service class

We will write simple POJO class which we are going to expose as web-service through Axis2 services, and simple supportive class to help on writing our main service class.

Our main service class (CountryService.java)  will expose its public methods as axis2 web services.



package com.jk.axis2.ws;

public class CountryService {
      
       public Country getCountryDetails(String countryCode) {
             
              System.out.println("countryCode =" + countryCode);
              Country country = null;

              if (countryCode != null) {
                     if (countryCode.equalsIgnoreCase("SL")) {
                           country = new Country("Sri Lanka");
                           country.setCountryCode(countryCode);
                           country.setCity("Colombo");
                           return country;
                     } else if (countryCode.equalsIgnoreCase("FR")) {
                           country = new Country("France");
                           country.setCountryCode(countryCode);
                           country.setCity("Paris");
                           return country;
                     }
              }

              return new Country("NotDefined");
       }
            
       public String getServiceVersion(){
              return "This is initial version 1.0";
       }
      
       public String testEcho(String name){
              return "Testing echo with " + name ;
       }
}





Support Country bean  to provide services through CountryService.


package com.jk.axis2.ws;

public class Country {

       String countryCode =null;
       String city=null;
       String name =null;
      
       public Country(){
             
       }
      
       public Country(String name){
              this.name=name;
       }
      
       public String getCountryCode() {
              return countryCode;
       }
       public void setCountryCode(String countryCode) {
              this.countryCode = countryCode;
       }
       public String getCity() {
              return city;
       }
       public void setCity(String city) {
              this.city = city;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
                    
}




2. Writing  deployment configuration ( services.xml )

To deploy your service archive , you need to have deployment descriptor inside service archive  which tells deployment module to deploy your service as axis2 service.

Basically you can have one or many services inside one single archive file , and can be listed around <ServiceGroup> tag. In this sample we will have only one service inside our service archive.

Our sample services.xml file content.

<serviceGroup>
<service name="CountryService" scope="application">
<messageReceivers>
                                <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
                                                                class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
                                <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out"
                                                                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<parameter locked="false" name="ServiceClass">com.jk.axis2.ws.CountryService</parameter>
</service>
</serviceGroup>


We can define our message receivers inside services.xml  for  each operation separately or as a default for all the services inside services.xml.  We have defined message receivers for all services as .

you can overwrite this as you wish by simply adding messages receiver for specific operation in services.xml as below.

<service>
...
<operation name="testEcho">
<messageReceiver class="org.apache.axis2.receivers. RawXMLINOutMessageReceiver"/> </operation>
</service>


Deploy the sample CountryService.aar  in Axis2



1. Create your service archive file

-Create simple ZIP file and rename as "CountryService.aar"
-Content will be as below




2. Now copy the  CountryService.aar in to the <TOMCAT_HOME>/webapps/axis2/WEB-INF/services   directory

Now your WebService is ready to access.
You will see the CountryService is deployed already.

Try calling service in REST style.

If you need to access as SOAP webservice ,use the WSDL from the path.