4.3 Creating a DataService Client
This section explains how to create a Java SOAP client for the SoapDataService service. This is a service which is used to execute queries and data worksheets. The instructions below are appropriate for Metro 2.0, the default SOAP implementation.
Follow the steps below:
1. Make sure the 'SoapRepository' service is deployed and running on the host server. For Metro, do this by entering the following URL: http://machine:port/webapp/SoapDataService (e.g., http://localhost:8080/sree/SoapDataService).
2. Create the client Java Stubs:
a. Download Metro (https://metro.dev.java.net).
b. Choose a folder where the stub files will be created. (Preferably create a new one). We will refer to this folder as {StubHome}.
c. Create two folders in {StubHome}, e.g. 'classes' and 'source' or any other suitable names.
d. Use the {MetroHome}/bin/wsimport utility to create the stubs with the following command:
wsimport.bat -s {folder1} -d {folder2} -keep -Xendorsed {URL}?wsdl
For example, change the working directory to {StubHome}, and enter the following command:
set PATH=%METRO_HOME%\bin;%PATH%
wsimport -s source -d classes -keep -Xendorsed http://localhost:8080/sree/SoapDataService?wsdl
This will create the Java stub files in the classes folder and source code (optional) in the source folder.
Sample client code for the 'SoapDataService' service is shown below. Make sure the stub classes (described above) are added to your classpath.
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Calendar;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeFactory;
import inetsoft.sree.dataserv.*;
public class MyDataClient {
public static String TIME_INSTANT_FORMAT = "MM/dd/yyyy HH:mm:ss";
public static String DATE_FORMAT = "MM/dd/yyyy";
public MyDataClient(){}
public static void main(String[] args) throws Exception {
SoapDataServiceService service = new SoapDataServiceService();
SoapDataService port = service.getSoapDataServicePort();
//Login with user, password and locale
String ticket = port.login("anonymous", "password");
//List available assets
List<String> paths = port.getPaths(ticket);
System.out.println("PATHS: " + paths);
//Aqcuire meta-data for 'orders by state' query
XTableStruct table = port.getMetaData(ticket, "Orders:orders by state");
//List column types for 'orders by state' query
List<String> colTypes = table.getColTypes();
System.out.println("TYPES: " + colTypes);
//Obtain query parameters, if any
List<UserVariableStruct> variableNamesList = port.getParameters(ticket, "Orders:orders by state");
//Create a VariableTableStruct to hold any parameters
VariableTableStruct vts = new VariableTableStruct();
List<String> vtsParamNameList = vts.getVariableNames();
List<ParameterValue> vtsParamValueList = vts.getVariableValues();
for(int i = 0; i < variableNamesList.size(); i++) {
try {
UserVariableStruct v = (UserVariableStruct)variableNamesList.get(i);
System.out.println("Parameter Name: " + v.getLabel());
System.out.println("Parameter Type: " + v.getXType());
System.out.println("Input the value ("+TIME_INSTANT_FORMAT+") :");
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String value = buf.readLine();
//add the parameters to the VariableTableStruct
vtsParamNameList.add(v.getLabel());
ParameterValue pvalue = new ParameterValue();
pvalue.setValue(value);
pvalue.setType(ParameterValueType.TIME_INSTANT);
pvalue.setFormat(TIME_INSTANT_FORMAT);
vtsParamValueList.add(pvalue);
}
catch(Exception e){
System.err.println(e);
}
}
String rid = port.execute(ticket, "Orders:orders by state", vts);
table = null;
while((table = port.nextBlock(ticket, rid, 50)) != null) {
//Print header rows
for(int row = 0; row < table.getHeaderRowCount(); row++) {
List<String> xrow = table.getObjectRows().getObjectRow().get(row).getRow();
System.out.println(xrow);
}
//Print body rows
for(int row = table.getHeaderRowCount(); row < table.getRowCount();row++) {
List<String> xrow = table.getObjectRows().getObjectRow().get(row).getRow();
Object[] data = new Object[table.getColCount()];
for(int col = 0; col < table.getColCount(); col++) {
String colType = colTypes.get(col);
String colValue = xrow.get(col);
//Test return type
if("java.lang.Boolean".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Boolean.valueOf(colValue);
}
else if("java.lang.Byte".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Byte.valueOf(colValue);
}
else if("java.lang.Character".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Character.valueOf(colValue.charAt(0));
}
else if("java.lang.Double".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Double.valueOf(colValue);
}
else if("java.lang.Float".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Float.valueOf(colValue);
}
else if("java.lang.Integer".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Integer.valueOf(colValue);
}
else if("java.lang.Long".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Long.valueOf(colValue);
}
else if("java.lang.Short".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : Short.valueOf(colValue);
}
else if("java.util.Date".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : new java.util.Date(Long.parseLong(colValue));
}
else if("java.sql.Date".equals(colType)) {
data[col] = colValue == null || "".equals(colValue) ? null : new java.sql.Date(Long.parseLong(colValue));
}
else {
data[col] = colValue == null || "".equals(colValue) ? null : colValue;
}
}
System.out.print("[");
for(int i = 0; i < data.length; i++) {
if(i > 0) {
System.out.print(", ");
}
System.out.print(String.valueOf(data[i]) + ":" + (data[i] == null ? "null" : data[i].getClass().getName()));
}
System.out.println("]");
}
}
port.logout(ticket);
}
}
Compile the client in the usual manner. For example, if the client class is saved as MyDataClient.java, compile it as follows:
javac MyDataClient.java
To run the client, enter the following:
java MyDataClient
| << 4.2.4 Example: Scheduling a Server Task | © 1996-2013 InetSoft Technology Corporation (v11.5) | 5 Design for Multi-Tenant Environment >> |