Difference between revisions of "Java client - OpenKM 5.1"
From OpenKM Documentation
(→List folders and documents) |
(→List folders and documents) |
||
Line 97: | Line 97: | ||
+ document.getAuthor() + ", Size: " + document.getActualVersion().getSize()); | + document.getAuthor() + ", Size: " + document.getActualVersion().getSize()); | ||
} | } | ||
+ | |||
+ | // Logout | ||
+ | okmAuth.logout(token); | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == Create document == | ||
+ | <source lang="java"> | ||
+ | package com.openkm.ws.test; | ||
+ | |||
+ | import java.io.FileInputStream; | ||
+ | |||
+ | import com.openkm.ws.client.Document; | ||
+ | import com.openkm.ws.client.OKMAuth; | ||
+ | import com.openkm.ws.client.OKMAuthService; | ||
+ | import com.openkm.ws.client.OKMDocument; | ||
+ | import com.openkm.ws.client.OKMDocumentService; | ||
+ | |||
+ | public class CreateDocument { | ||
+ | public static void main(String[] args) { | ||
+ | String file = "/etc/hosts"; | ||
+ | |||
+ | try { | ||
+ | OKMAuthService okmAuthService = new OKMAuthService(); | ||
+ | OKMAuth okmAuth = okmAuthService.getOKMAuthPort(); | ||
+ | OKMDocumentService okmDocumentService = new OKMDocumentService(); | ||
+ | OKMDocument okmDocument = okmDocumentService.getOKMDocumentPort(); | ||
+ | |||
+ | // Login | ||
+ | String token = okmAuth.login("okmAdmin", "admin"); | ||
+ | System.out.println("Es: " + token); | ||
+ | |||
+ | Document doc = new Document(); | ||
+ | doc.setPath("/okm:root/hosts.txt"); | ||
+ | FileInputStream fis = new FileInputStream(file); | ||
+ | byte[] content = new byte[fis.available()]; | ||
+ | int size = fis.read(content); | ||
+ | System.out.println("File size: " + size); | ||
+ | fis.close(); | ||
+ | |||
+ | Document newDoc = okmDocument.create(token, doc, content); | ||
+ | System.out.println("[DOCUMENT] Path: " + newDoc.getPath() + ", Author: " + newDoc.getAuthor() | ||
+ | + ", Size: " + newDoc.getActualVersion().getSize()); | ||
// Logout | // Logout |
Revision as of 10:54, 12 March 2010
First, we need to generate the client stuff using this command. Keep on mind that you need JDK 1.6 to run it:
$ wsimport -d client -s client http://localhost:8080/OpenKM/OKMAuth?wsdl
You can use the following script to generate a complete OpenKM webservices client library:
#/bin/bash
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMAuth?wsdl
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMDocument?wsdl
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMFolder?wsdl
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMSearch?wsdl
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMNotification?wsdl
wsimport -p com.openkm.ws.client http://localhost:8080/OpenKM/OKMRepository?wsdl
jar cvf okm-ws-client-2.1.jar com
rm -rf com
Authentication
package com.openkm.ws.test;
import com.openkm.ws.client.OKMAuth;
import com.openkm.ws.client.OKMAuthService;
public class Authentication {
public static void main(String[] args) {
try {
OKMAuthService okmAuthService = new OKMAuthService();
OKMAuth okmAuth = okmAuthService.getOKMAuthPort();
// Login
String token = okmAuth.login("okmAdmin", "admin");
System.out.println("Es: " + token);
// Logout
okmAuth.logout(token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
List folders and documents
package com.openkm.ws.test;
import java.util.List;
import com.openkm.ws.client.Document;
import com.openkm.ws.client.DocumentArray;
import com.openkm.ws.client.Folder;
import com.openkm.ws.client.FolderArray;
import com.openkm.ws.client.OKMAuth;
import com.openkm.ws.client.OKMAuthService;
import com.openkm.ws.client.OKMDocument;
import com.openkm.ws.client.OKMDocumentService;
import com.openkm.ws.client.OKMFolder;
import com.openkm.ws.client.OKMFolderService;
public class ListNodes {
public static void main(String[] args) {
String path = "/okm:root";
try {
OKMAuthService okmAuthService = new OKMAuthService();
OKMAuth okmAuth = okmAuthService.getOKMAuthPort();
OKMDocumentService okmDocumentService = new OKMDocumentService();
OKMDocument okmDocument = okmDocumentService.getOKMDocumentPort();
OKMFolderService okmFolderService = new OKMFolderService();
OKMFolder okmFolder = okmFolderService.getOKMFolderPort();
// Login
String token = okmAuth.login("okmAdmin", "admin");
System.out.println("Es: " + token);
// List folders
FolderArray fldArray = okmFolder.getChilds(token, path);
List<Folder> folders = fldArray.getValue();
for (Folder folder : folders) {
System.out.println("[FOLDER] Path: " + folder.getPath() + ", Author: " + folder.getAuthor());
}
// List documents
DocumentArray docArray = okmDocument.getChilds(token, path);
List<Document> documents = docArray.getValue();
for (Document document : documents) {
System.out.println("[DOCUMENT] Path: " + document.getPath() + ", Author: "
+ document.getAuthor() + ", Size: " + document.getActualVersion().getSize());
}
// Logout
okmAuth.logout(token);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Create document
package com.openkm.ws.test;
import java.io.FileInputStream;
import com.openkm.ws.client.Document;
import com.openkm.ws.client.OKMAuth;
import com.openkm.ws.client.OKMAuthService;
import com.openkm.ws.client.OKMDocument;
import com.openkm.ws.client.OKMDocumentService;
public class CreateDocument {
public static void main(String[] args) {
String file = "/etc/hosts";
try {
OKMAuthService okmAuthService = new OKMAuthService();
OKMAuth okmAuth = okmAuthService.getOKMAuthPort();
OKMDocumentService okmDocumentService = new OKMDocumentService();
OKMDocument okmDocument = okmDocumentService.getOKMDocumentPort();
// Login
String token = okmAuth.login("okmAdmin", "admin");
System.out.println("Es: " + token);
Document doc = new Document();
doc.setPath("/okm:root/hosts.txt");
FileInputStream fis = new FileInputStream(file);
byte[] content = new byte[fis.available()];
int size = fis.read(content);
System.out.println("File size: " + size);
fis.close();
Document newDoc = okmDocument.create(token, doc, content);
System.out.println("[DOCUMENT] Path: " + newDoc.getPath() + ", Author: " + newDoc.getAuthor()
+ ", Size: " + newDoc.getActualVersion().getSize());
// Logout
okmAuth.logout(token);
} catch (Exception e) {
e.printStackTrace();
}
}
}