CMIS域模型
CMIS服务
CMIS绑定
如何连接到CMIS Server上的指定Repository
如何获取CMIS Server上的所有Repository
结论:每一个Repository有对应一个Session
如何获取根目录下的所有类型的对象
如何创建一个目录对象
如何创建一个简单的文档对象
如何读取一个文档对象的内容
如何更新文档属性
如何更新文档内容
如何删除一个文档
如何删除一个目录
如何导航文档树

1. 如何获取儿子节点
2. 如何获取子孙节点
3. 如何只获取目录子孙
- Document 最基本,文档对象
- Folder 最基本,文档的容器
- Relationship 描述Document之间关系,有些Repository可能不支持
- Policy 描述权限策略
CMIS服务
- Repository
- Navigation
- Object
- Multi-filing
- Discovery
- Change
- Versioning
- Relationship
- Policy
- ACL-Access Control List
CMIS绑定
- AtomPub
- WebService
- Local
如何连接到CMIS Server上的指定Repository
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map parameter = new HashMap();
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");
parameter.put(SessionParameter.ATOMPUB_URL, "http://repo.opencmis.org/inmemory/atom/");
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.REPOSITORY_ID, "A1");
Session session = sessionFactory.createSession(parameter);
如何获取CMIS Server上的所有Repository
List<Repository> repositories = sessionFactory.getRepositories(parameter);
for (Repository r : repositories) {
System.out.println("Found repository: " + r.getName());
}
Repository repository = repositories.get(0);
Session session = repository.createSession();
System.out.println("Got a connection to repository: "
+ repository.getName() + ", with id: "
+ repository.getId());
结论:每一个Repository有对应一个Session
如何获取根目录下的所有类型的对象
Folder root = session.getRootFolder();
ItemIterable<CmisObject> children = root.getChildren();
System.out.println("Found the following objects in the root folder:-");
for (CmisObject o : children) {
System.out.println(o.getName() + " which is of type " + o.getType().getDisplayName());
}
如何创建一个目录对象
System.out.println("Creating 'ADGNewFolder' in the root folder");
Map<String, String> newFolderProps = new HashMap<String, String>();
newFolderProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
newFolderProps.put(PropertyIds.NAME, "ADGNewFolder");
Folder newFolder = root.createFolder(newFolderProps);
// Did it work?
ItemIterable<CmisObject> children = root.getChildren();
System.out.println("Now finding the following objects in the root folder:-");
for (CmisObject o : children) {
System.out.println(o.getName());
}
如何创建一个简单的文档对象
final String textFileName = "test.txt";
System.out.println("creating a simple text file, " + textFileName);
String mimetype = "text/plain; charset=UTF-8";
String content = "This is some test content.";
String filename = textFileName;
byte[] buf = content.getBytes("UTF-8");
ByteArrayInputStream input = new ByteArrayInputStream(buf);
ContentStream contentStream = session.getObjectFactory().createContentStream(filename, buf.length, mimetype, input);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, filename);
Document doc = newFolder.createDocument(properties, contentStream, VersioningState.MAJOR);
System.out.println("Document ID: " + doc.getId());
如何读取一个文档对象的内容
// Get the contents of the file
Document doc = (Document) session.getObject(id);
ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content
if (contentStream != null) {
String content = getContentAsString(contentStream);
System.out.println("Contents of " + filename + " are: " + content);
} else {
System.out.println("No content.");
}
/**
* Helper method to get the contents of a stream
*
* @param stream
* @return
* @throws IOException
*/
private static String getContentAsString(ContentStream stream) throws IOException {
StringBuilder sb = new StringBuilder();
Reader reader = new InputStreamReader(stream.getStream(), "UTF-8");
try {
final char[] buffer = new char[4 * 1024];
int b;
while (true) {
b = reader.read(buffer, 0, buffer.length);
if (b > 0) {
sb.append(buffer, 0, b);
} else if (b == -1) {
break;
}
}
} finally {
reader.close();
}
return sb.toString();
}
String path = newFolder.getPath() + "/" + textFileName;
System.out.println("Getting object by path " + path);
Document doc = (Document) session.getObjectByPath(path);
ContentStream contentStream = doc.getContentStream();
if (contentStream != null) {
String content = getContentAsString(contentStream);
}
如何更新文档属性
Document doc2 = (Document) session.getObject(id2);
System.out.println("renaming " + doc2.getName() + " to test3.txt");
Map<String, String> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, "test3.txt");
doc2.updateProperties(properties, true);
System.out.println("renamed to " + doc2.getName());
如何更新文档内容
if (!session.getRepositoryInfo().getCapabilities().getContentStreamUpdatesCapability()
.equals(CapabilityContentStreamUpdates.ANYTIME)) {
System.out.println("update without checkout not supported in this repository");
} else {
System.out.println("updating content stream");
String mimetype = "text/plain; charset=UTF-8";
String content = "This is some updated test content for our renamed second document.";
byte[] buf = content.getBytes("UTF-8");
input = new ByteArrayInputStream(buf);
ContentStream contentStream = session.getObjectFactory().createContentStream("test3.txt", buf.length, mimetype, input);
doc2.setContentStream(contentStream, true);
// did it work?
contentStream = doc2.getContentStream();
if (contentStream != null) {
content = getContentAsString(contentStream);
System.out.println("Contents of " + doc2.getName() + " are: " + content);
} else {
System.out.println("Something went wrong.");
}
}
如何删除一个文档
ItemIterable<CmisObject> children = newFolder.getChildren();
System.out.println("Now finding the following objects in our folder:-");
for (CmisObject o : children) {
System.out.println(o.getName());
}
System.out.println("Deleting document " + doc2.getName());
doc2.delete(true);
System.out.println("Now finding the following objects in our folder:-");
for (CmisObject o : children) {
System.out.println(o.getName());
}
如何删除一个目录
System.out.println("Creating 'ADGFolder1' in the root folder");
Map<String, String> newFolderProps = new HashMap<String, String>();
newFolderProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
newFolderProps.put(PropertyIds.NAME, "ADGFolder1");
Folder folder1 = root.createFolder(newFolderProps);
newFolderProps.put(PropertyIds.NAME, "ADGFolder11");
Folder folder11 = folder1.createFolder(newFolderProps);
newFolderProps.put(PropertyIds.NAME, "ADGFolder12");
Folder folder12 = folder1.createFolder(newFolderProps);
System.out.println("delete the 'ADGFolder1' tree");
folder1.deleteTree(true, UnfileObject.DELETE, true);
如何导航文档树
1. 如何获取儿子节点
ItemIterable<CmisObject> children = folder1.getChildren();
System.out.println("Children of " + folder1.getName() + ":-");
for (CmisObject o : children) {
System.out.println(o.getName());
}
执行结果
Children of ADGFolder1:-
ADGFolder11
ADGFolder12
ADGFile1f1
2. 如何获取子孙节点
if (!session.getRepositoryInfo().getCapabilities().isGetDescendantsSupported()) {
System.out.println("getDescendants not supported in this repository");
} else {
System.out.println("Descendants of " + folder1.getName() + ":-");
for (Tree<FileableCmisObject> t : folder1.getDescendants(-1)) {
printTree(t);
}
}
...
private static void printTree(Tree<FileableCmisObject> tree) {
System.out.println("Descendant " + tree.getItem().getName());
for (Tree<FileableCmisObject> t : tree.getChildren()) {
printTree(t);
}
}
执行结果
Descendants of ADGFolder1:-
Descendant ADGFolder11
Descendant ADGFolder111
Descendant ADGFolder112
Descendant ADGFile11f1
Descendant ADGFile11f2
Descendant ADGFolder12
Descendant ADGFolder121
Descendant ADGFile121f1
Descendant ADGFolder122
Descendant ADGFile1f1
3. 如何只获取目录子孙
if (!session.getRepositoryInfo().getCapabilities().isGetFolderTreeSupported()) {
System.out.println("getFolderTree not supported in this repository");
} else {
System.out.println("Foldertree for " + folder1.getName() + ":-");
for (Tree<FileableCmisObject> t : folder1.getFolderTree(-1)) {
printFolderTree(t);
}
}
...
private static void printFolderTree(Tree<FileableCmisObject> tree) {
System.out.println("Folder " + tree.getItem().getName());
for (Tree<FileableCmisObject> t : tree.getChildren()) {
printFolderTree(t);
}
}
执行结果
Foldertree for ADGFolder1:-
Folder ADGFolder11
Folder ADGFolder111
Folder ADGFolder112
Folder ADGFolder12
Folder ADGFolder121
Folder ADGFolder122