Web service integration using Java
From KnowledgeTree Document Management Made Simple
- work in progress - semi accurate, but not pretty **
Contents |
requirements
You will need a library the provides SOAP client functionality.
This guide will attempt to use the Apache Axis. http://ws.apache.org/axis/
From experimentation and some research, I found that there is some encoding done by the PEAR::SOAP libraries that is not supported by Axis v2. My research told me that I would have some success with Axis 1.x.
The following was done using Apache Axis v1.4.
setup
Setup the working area:
mkdir /home/conrad/java
Unzip the axis 1.4 zip into the java directory. You should now have a full path: /home/conrad/java/axis-1_4
create proxy files from wsdl
Ensure you are in the root of the working area:
cd /home/conrad/java
Now run WSDL2Java:
java -classpath axis-1_4/lib/axis.jar:axis-1_4/lib/commons-discovery-0.2.jar:axis-1_4/lib/commons-logging-1.0.4.jar:axis-1_4/lib/jaxrpc.jar:axis-1_4/lib/log4j-1.2.8.jar:axis-1_4/lib/saaj.jar:axis-1_4/lib/wsdl4j-1.5.1.jar:axis-1_4/xerces.jar org.apache.axis.wsdl.WSDL2Java -c T1 -p com.knowledgetree.interphace http://ktdms.trunk/ktwebservice/webservice.php?wsdl
This creates a folder structure based on the wsdl: /home/conrad/java/com/knowledgetree/interphace. This folder contains the proxy classes that you can use to interface with KnowledgeTree.
compile the proxy files
cd com/knowledgetree/interphace HOMEPATH=/home/conrad/java AXISLIBPATH="$HOMEPATH/axis-1_4/lib" XCLASSPATH="$AXISLIBPATH/axis.jar" XCLASSPATH="$XCLASSPATH:$AXISLIBPATH/jaxrpc.jar" XCLASSPATH="$XCLASSPATH:$AXISLIBPATH/saaj.jar" XCLASSPATH="$XCLASSPATH:$AXISLIBPATH/commons-logging-1.0.4.jar" XCLASSPATH="$XCLASSPATH:$AXISLIBPATH/commons-discovery-0.2.jar" XCLASSPATH="$XCLASSPATH:$AXISLIBPATH/wsdl4j-1.5.1.jar" XCLASSPATH="$XCLASSPATH:$HOMEPATH/xerces.jar " javac -d /home/conrad/java/build -cp $XCLASSPATH *
This compiles the classes and the binaries are located in: /home/conrad/java/build
create a nice little manifest file - MANIFEST.MF
Change back to the root of the working area:
cd /home/conrad/java
Create the file: MANIFEST.MF
Manifest-Version: 1.0 Class-Path: lib/axis.jar lib/jaxrpc.jar lib/saaj.jar lib/commons-logging-1.0.4.jar lib/commons-discovery-0.2.jar lib/wsdl4j-1.5.1.jar lib/xerces.jar
I created the manifest file so that I can just have a library directory containing my dependencies relative to the location of my library.
create a nice little jar file
jar cvfm ktws.jar MANIFEST.MF -C build/ .
This creates a little jar file of all the proxy classes.
create my client application - myclass.java
import com.knowledgetree.interphace.KnowledgeTreeBindingStub;
import com.knowledgetree.interphace.Kt_response;
import com.knowledgetree.interphace.Kt_folder_contents;
import org.apache.axis.client.Service;
import java.net.URL;
import com.knowledgetree.interphace.Kt_folder_item;
class myclass
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://ktdms.trunk/ktwebservice/webservice.php");
KnowledgeTreeBindingStub stub = new KnowledgeTreeBindingStub(url, new Service());
System.out.println("Logging into KnowledgeTree");
Kt_response response = stub.login("admin","admin","127.0.0.1");
int status_code=response.getStatus_code();
String session;
if (status_code == 0)
{
session = response.getMessage();
System.out.println("Session: " + session);
}
else
{
System.out.println("Status Code: " + status_code);
System.out.println("Session: " + response.getMessage());
return;
}
System.out.println("Getting content for root folder");
Kt_folder_contents content = stub.get_folder_contents(session,1,1, "DF");
if (status_code != 0)
{
System.out.println("Status Code: " + status_code);
System.out.println("Session: " + response.getMessage());
return;
}
System.out.println("Full Path: " + content.getFull_path());
Kt_folder_item[] items = content.getItems();
for(int i=0;i<items.length;i++)
{
Kt_folder_item item = items[i];
String item_type = item.getItem_type().equals("D")?"Document":"Folder";
System.out.println("----------");
System.out.println("Type: " + item_type);
System.out.println("Filename: " + item.getFilename());
System.out.println("Filesize: " + item.getFilesize());
System.out.println("Document Type: " + item.getDocument_type());
System.out.println("Workflow: " + item.getWorkflow());
System.out.println("Workflow State: " + item.getWorkflow_state());
}
System.out.println("----------");
System.out.println("Logging out of KnowledgeTree");
response = stub.logout(session );
status_code=response.getStatus_code();
if (status_code != 0)
{
System.out.println("Status Code: " + status_code);
System.out.println("Session: " + response.getMessage());
return;
}
System.out.println("Done!");
}
}
compile my client application
javac -cp ktws.jar myclass.java
Compiles class and produces myclass.class.
test my client application
java -cp ktws.jar:. myclass
Note: including the current directory is always important with classpath settings. ;)
del.icio.us
reddit

