RXT Service Client Sample
Introduction
The RXT service client sample demonstrates the following:
- Creating an RXT configuration model and deploying it to the WSO2 Governance Registry.
- Generating a service stub for the deployed RXT artifact using WSO2 'wsdl2java' tool.
- Creating, executing and testing the client.
Pre-requisites
- A running instance of WSO2 Governance Registry. Download and setup instructions are given in section Installation Guide.
- WSO2 Application Server: Download and unpack the binary distribution from http://wso2.com/products/application-server.
- SOAP UI 4.5.0 tool : Downloads can be found in http://www.soapui.org. Note to select the non-JRE version if JRE is already installed in your server.
- Maven build tool: Download and installation instructions are given in section Installation Prerequisites.
Executing and Testing the Sample
Step 1: Create and Deploy RXT Configuration Model
The sample RXT chosen for this scenario is employee.rxt.
<artifactType type="application/vnd.wso2-employee+xml" shortName="employee" singularLabel="Employee" pluralLabel="Employees" hasNamespace="false" iconSet="11"> <storagePath>/employees/@{overview_name}</storagePath> <nameAttribute>overview_name</nameAttribute> <ui> <list> <column name="Name"> <data type="path" value="overview_name" href="@{storagePath}"/> </column> <column name="Address"> <data type="text" value="overview_address"/> </column> <column name="Sex"> <data type="text" value="overview_sex"/> </column> </list> </ui> <content> <table name="Overview"> <field type="text" required="true" readonly="true"> <name>Name</name> </field> <field type="text" required="true" readonly="true"> <name>Address</name> </field> <field type="options" readonly="true"> <name label="Sex">Sex</name> <values> <value>male</value> <value>female</value> </values> </field> </table> </content> </artifactType>
The employee.rxt shown above has been used to create the Employee[singular label of the configuration model]configurabele extension on the Governance Registry. Refer to Deploying an Extension File to deploy an extension file to the governance registry.
Step 2: Generate Service Stub
This step consists of three activities to generate the EmployeeStub using the 'wsdl2java' tool for the deployed Employee artifact.
Activity 1: View the wsdl representation for the service exposed by the Employee artifact. The wsdl uri is similar to https://{IP address}:9443/services/{singular label of the artifact}?wsdl.
Activity 2: Any wsdl2java tool can be used to generate the stub. In this sample, we use the WSO2 Application Server which has the wsdl2java facility.
- As the prerequisites state, download and unpack the WSO2 Application Server binary distribution from http://wso2.com/products/application-server if you haven't done already.
- Change the value of theÂ
<HideAdminServiceWSDLs>
 element in theÂ<AS_HOME>/repository/conf/carbon.xml
 file toÂfalse
. - Navigate to <AS_HOME>/bin directory execute wso2server.sh (for Linux) or wso2server.bat (for Windows) to start the Application Server.
- Log-in to the AS management console and click the "tools" menu. Management console log-in is similar to that of Governance Registry.Â
- Select wsdl2java from the tools pane which is in the left hand side of the management console.
- Provide the uri of the wsdl or file path of the wsdl. If the uri does not work, copy the wsdl from the browser to a text document and save it as .wsdl file and give the file path.
Activity 3: Result contains the stub java file and other skeletons with build.xml
and pom.xml
.
Note: Add the following dependencies to the pom.xml
file if they are missing:
<dependency> <groupId>org.wso2.carbon</groupId> <artifactId>org.wso2.carbon.utils</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>org.wso2.carbon</groupId> <artifactId>org.wso2.carbon.base</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-json</artifactId> <version>$ {axis2_version}</version> </dependency> <dependency> <groupId>org.apache.ws.commons.axiom</groupId> <artifactId>om-aspects</artifactId> <version>1.2.15</version> </dependency>
1. Convert that project to maven project using the mvn eclipse:eclipse command.
2. Open the project on any IDE which supports maven project.
Step 3: Create and Execute the Client
1. Add the following file to the project Employee.java.
package org.apache.ws.axis2; import java.rmi.RemoteException; import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.ws.axis2.EmployeeStub.AddEmployee; import org.apache.ws.axis2.EmployeeStub.AddEmployeeResponse; import org.apache.ws.axis2.EmployeeStub.DeleteEmployee; import org.apache.ws.axis2.EmployeeStub.DeleteEmployeeResponse; import org.apache.ws.axis2.EmployeeStub.GetEmployee; import org.apache.ws.axis2.EmployeeStub.GetEmployeeArtifactIDs; import org.apache.ws.axis2.EmployeeStub.GetEmployeeArtifactIDsResponse; import org.apache.ws.axis2.EmployeeStub.GetEmployeeDependencies; import org.apache.ws.axis2.EmployeeStub.GetEmployeeDependenciesResponse; import org.apache.ws.axis2.EmployeeStub.GetEmployeeResponse; import org.wso2.carbon.utils.CarbonUtils; public class Employee { /** * @param args * @throws AddEmployeeServiceGovernanceException * @throws RemoteException * @throws GetEmployeeArtifactIDsServiceGovernanceException * @throws GetEmployeeServiceGovernanceException * @throws UpdateEmployeeServiceGovernanceException * @throws DeleteEmployeeServiceGovernanceException * */ EmployeeStub stub; public static void main(String[] args) throws RemoteException, AddEmployeeServiceGovernanceException, GetEmployeeArtifactIDsServiceGovernanceException, GetEmployeeServiceGovernanceException, UpdateEmployeeServiceGovernanceException, DeleteEmployeeServiceGovernanceException, GetEmployeeDependenciesServiceGovernanceException{ // TODO Autogenerated method stub try{ String GREG_HOME ="/home/user/GREG/final_test/wso2greg-5.1.0-SNAPSHOT/"; System.setProperty("javax.net.ssl.trustStore","/home/rajith/GREG/final_test/wso2greg-5.1.0-SNAPSHOT/repository/resources/security/client-truststore.jks"); System.setProperty("javax.net.ssl.trustStorePassword","wso2carbon"); System.setProperty("javax.net.ssl.trustStoreType","JKS"); String axis2Repo =GREG_HOME + "repository/deployment/client"; String axis2Conf =GREG_HOME +"repository/conf/axis2/axis2_client.xml"; ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(axis2Repo, axis2Conf); String backendServerURL = "https://localhost:9443/services/"; String epr =backendServerURL+"Employee"; Employee employee = new Employee(configContext,epr,false); /**@metadata defines the employee data to be added. The xml representation of the content should be passed which can be retrieved from the standard view of the content panel of an the employee artifact.*/ String metadata = "<metadata xmlns=\"http://www.wso2.org/governance/metadata\">"+"<overview><sex>female</sex><name>Leonii</name>"+"<address>spain</address></overview></metadata>"; employee.addEmployee(metadata); String[] artifactIDs = employee.getEmployeeArtifactIDs(); String id = artifactIDs[0]; for(int i=0; i<artifactIDs.length; i++){ employee.getEmployee(artifactIDs[i]); employee.getEmployeeDependencies(artifactIDs[i]); } employee.deleteEmployee(id); } catch (AxisFault e) { // TODO Autogenerated catch block e.printStackTrace(); } } public Employee(ConfigurationContext configContext,String epr, boolean listener) throws AxisFault{ stub = new EmployeeStub(configContext,epr,listener); CarbonUtils.setBasicAccessSecurityHeaders("admin", "admin", stub._getServiceClient()); } /**this method add the employee details to the running GReg instance by invoking the addEmployee() method of the EmployeeStub*/ public String addEmployee(String addEmployeeXmlString) throws RemoteException, AddEmployeeServiceGovernanceException{ AddEmployee addEmp = new AddEmployee(); addEmp.setInfo(addEmployeeXmlString); AddEmployeeResponse response = stub.addEmployee(addEmp); //prints the artifact id of the added employee artifact System.out.println(response.get_return()); return response.get_return(); } /**Get all the artifact ids of the Employees, method returns the array of artifacts */ public String[] getEmployeeArtifactIDs() throws RemoteException, GetEmployeeArtifactIDsServiceGovernanceException{ GetEmployeeArtifactIDs artifactIDs = new GetEmployeeArtifactIDs(); GetEmployeeArtifactIDsResponse getEmployeeArtifactIDsResponse = stub.getEmployeeArtifactIDs(artifactIDs); String[] employeeArifactIDs = getEmployeeArtifactIDsResponse.get_return(); return employeeArifactIDs; } /**Method takes artifactID as an input and returns the corresponding employee instance */ public String getEmployee(String artifactID) throws RemoteException, GetEmployeeServiceGovernanceException{ GetEmployee getEmployee = new GetEmployee(); getEmployee.setArtifactId(artifactID); GetEmployeeResponse getEmployeeResponse = stub.getEmployee(getEmployee); System.out.println(artifactID+":"+getEmployeeResponse.get_return()); return getEmployeeResponse.get_return(); } /**Method takes artifactID of the employee instance to be deleted and returns the boolean value */ public boolean deleteEmployee(String artifactID) throws RemoteException, DeleteEmployeeServiceGovernanceException{ DeleteEmployee deleteEmployee = new DeleteEmployee(); deleteEmployee.setArtifactId(artifactID); DeleteEmployeeResponse deleteEmployeeResponse = stub.deleteEmployee(deleteEmployee); boolean deleted = deleteEmployeeResponse.get_return(); if(deleted){ System.out.println(artifactID+":The specified artifact has been deleted"); } return deleted; } /**Get all the dependencies for a given artifactID, method returns the array of dependencies */ public String[] getEmployeeDependencies(String artifactID) throws RemoteException, GetEmployeeDependenciesServiceGovernanceException{ GetEmployeeDependencies employeeDependencies = new GetEmployeeDependencies(); employeeDependencies.setArtifactId(artifactID); GetEmployeeDependenciesResponse employeeDependenciesResponse = stub.getEmployeeDependencies(employeeDependencies); return employeeDependenciesResponse.get_return(); } }
2. Start the Governance Registry instance and open its Management Console. Then run the Employee.java as a java application from you IDE.
3. User can see the printed messages from the IDE's Console panel along with the log at the terminal.
Step 4: Functional Testing with SOAP UI
1. Download the SOAP UI tool bundle, unzip it and run the soapui.sh using the "sh soapui.sh" command inside the /bin directory.
2. Create a soap ui project.
3. Go to File -->Preferences --> HTTP setting --> tick the "authenticate preemptively".
4. When the first soap request is made the user has to give the authentication credentials for the relevant request [username, password]. It is not manadatory to give authentication credentials for the subsequent soap requests.
Adding an Employee
Soap request and response diagram for the add employee operation:
Get Employee ArtifactIDs
SOAP request and response diagram for the get employee artifacts operation:
Getting an Employee
SOAP request and response diagram for the get employee operation:
Delete an Employee
SOAP request and response diagram for the delete employee operation:
Get Employee Dependencies
SOAP request and response diagram for the get employee dependencies operation: