Thursday 6 February 2014

Java Web Services on Raspberry Pi

These are the steps for setting up a Simple Java Web Service on the Raspberry Pi.
The Raspberry Pi might not be the most suitable device for hosting a web service but it is suitable for educational purposes.

http://www.javatutoronline.com/webservice/Axis2-Web-Service.jsp
http://axis.apache.org/axis2/java/core/docs/quickstartguide.html

Install Java
Install the java run time environment and the java development kit
sudo apt-get install openjdk-7-jre
sudo apt-get install openjdk-7-jdk

Check where java is installed
pi@raspberrypi:~/webService$ which java
/usr/bin/java

Then set up the JAVA_HOME environment variable
export JAVA_HOME=/usr

Install Tomcat
sudo apt-get install tomcat7

Install ANT
sudo apt-get install ant

Install AXIS2
Download the axis2 file (attached to this page) and unzip the axis2 to /home/pi

Set up the AXIS2_HOME environment variable
export AXIS2_HOME=/home/pi/axis2-1.6.2

Go to $AXIS2_HOME/webapp and run ant create.war
cd $AXIS2_HOME/webapp
ant create.war

Copy the axis2.war file to tomcat directory
sudo cp $AXIS2_HOME/dist/axis2.war /var/lib/tomcat7/webapps

Restart tomcat to pick up the axis2 war file
sudo /etc/init.d/tomcat7 restart

Go to the axis2 webpage
go to http://<ipaddress>:8080/axis2

You should see the axis webpage. click on the validate link. This will show you if the system is configured correctly

Create the Web Service

First, create a directory META-INF and create a services.xml file with the following contents.
<service name="GetDate" scope="application">
        <description>
                Get the date
        </description>
        <parameter name="ServiceClass" locked="false">
                test.GetDate
        </parameter>
        <operation name="getDate">
                <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </operation>
</service>

Create a directory, test and create the java file, GetDate.java in it
package test;

import java.util.Calendar;

public class GetDate {
        public String getDate() {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH) + 1;
                int day = cal.get(Calendar.DAY_OF_MONTH);

                String date = String.format("%02d", day) + "/" +
                                String.format("%02d", month) + "/" + year;
                return date;
        }
}

Compile the java file
javac test/GetDate.java

If you need any external jars, make sure to place the jar files in a lib directory

Create the aar file
jar -cvf GetDate.aar *

Copy the aar file to the AXIS2 services directory
sudo cp GetDate.aar /var/lib/tomcat7/webapps/axis2/WEB-INF/services

There is no need to restart Tomcat, AXIS2 will pick up the changes automatically.
Go to the the following URL
http://<ip address of Pi>:8080/axis2/services/listServices

You should see the GetDate service. If you click on the GetDate link, you will be redirected to the WSDL.
http://<ip address of Pi>:8080/axis2/services/GetDate?wsdl

Creating the client

Import the WSDL
wsimport -keep -s src http://192.168.2.4:8080/axis2/services/GetDate?wsdl

With the generated files, create the following client that queries the web service
import test.GetDate_Service;

public class TestGetDateWebService {


public static void main(String[] args) {
GetDate_Service service = new GetDate_Service();
String response = service.getGetDateHttpSoap11Endpoint().getDate();
System.out.println("the date is = " + response);
}
}
If you import the WSDL into SOAPUI, you can see the SOAP messages that are sent and received by the webservice
Request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test">
   <soapenv:Header/>
   <soapenv:Body>
      <test:getDate/>
   </soapenv:Body>
</soapenv:Envelope>

Response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:getDateResponse xmlns:ns="http://test">
         <ns:return>31/08/2013</ns:return>
      </ns:getDateResponse>
   </soapenv:Body>
</soapenv:Envelope>

Echo Service
Now that we can use basic functionality of a WebService, we will use the webservice to echo what we send it

echo/EchoService.java
package echo;


public class EchoService {
public String getMessage(String msg) {
return "You sent: " + msg;
}
}

META-INF/services.xml
<service name="EchoService" scope="application">
        <description>
                This service echos whatever is sent into it
        </description>
        <parameter name="ServiceClass" locked="false">
                echo.EchoService
        </parameter>
        <operation name="getMessage">
                <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </operation>
</service>

Create and copy the aar file to the tomcat axis2 services directory.

Import the wsdl and generate the source code using wsimport. Create an Echo Client using the generated code.

EchoClient
import echo.EchoService;

public class EchoClient {

 /**
  * @param args
  */
 public static void main(String[] args) {
 EchoService echoService = new EchoService();
 String response = echoService.getEchoServiceHttpSoap11Endpoint().getMessage("Hello from the client");
 System.out.println(response);
 }

}

These are the SOAP requests and responses

SOAP request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo">
   <soapenv:Header/>
   <soapenv:Body>
      <echo:getMessage>
         <!--Optional:-->
         <echo:args0>test</echo:args0>
      </echo:getMessage>
   </soapenv:Body>
</soapenv:Envelope>

SOAP response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:getMessageResponse xmlns:ns="http://echo">
         <ns:return>You sent: test</ns:return>
      </ns:getMessageResponse>
   </soapenv:Body>
</soapenv:Envelope>

No comments:

Post a Comment