WSO2 Web Services Framework/Jython - Quick Start Guide

This section is aimed to help you write a service client quickly and easily using the WSO2 WSF/Jython API. 

Building the Source Distribution:

Note:: JDK 1.5 and Maven 2 is required to build the source.

   1. Type mvn clean install in the base directory (where the source was downloaded to).
   2. If the build fails due to a missing artifact, build again by typing mvn clean install -up

Running a Client Written in Jython:

First, you must set up the environment by adding the Jython .jar file and the necessary Axis2 .jar files to the classpath. Then add the WSF/Jython .jar file to the classpath. Once you have set up the environment correctly, you should be able to run the Jython scripts that are compliant with the WSO2 WSF/Jython API specifications. 

WSO2 WSF/Jython is shipped with a shell script that will set this environment for you. This makes it possible to execute a client script by simply providing its absolute path. 

Example:

sh wsfjython.sh /home/heshan/wsf-jython/jython/distribution/target/wsf-jython-SNAPSHOT-bin/samples/amazon.py
Let's write a simple client to consume a Yahoo service.

step 1

First, we need to prepare the payload as expected by the service.

req_payload_string = "<deduct><var1>1.8</var1><var2>4.87594</var2></deduct>"

NOTE: remember to import WSF/Jython classes

from org.wso2.wsf.jython.client import WSClient
from org.wso2.wsf.jython.client import WSFault
from org.wso2.wsf.jython.client import WSMessage

step 2

Next, we need to create a WSClient, with the options to be used by the client.

client = WSClient({}, LOG_FILE_NAME)

Here is the complete source code for the service client:

from org.wso2.wsf.jython.jythonwsclient import *
req_payload_string = "<deduct><var1>1.8</var1><var2>4.87594</var2></deduct>"
LOG_FILE_NAME = "/home/heshan/IdeaProjects/MRclient/src/python_client.log"
END_POINT = "http://localhost:8070/axis2/services/annotationScript/deduct"
try:
client = WSClient({}, LOG_FILE_NAME)
req_message = WSMessage(req_payload_string, {"to" :END_POINT}) print " Sending OM : " , req_payload_string
res_message = client.request(req_message)
print " Response Message: " , res_message
except WSFault, e:
e.printStackTrace();

step 3

Execute the Jython script.