笔记:Apache Chemistry OpenCMIS Client端开发

本文档详细介绍了使用Apache Chemistry OpenCMIS进行客户端开发的基础知识,包括Document(基本文档对象)、Folder(文档容器)、Relationship(文档间关系)、Policy(权限策略)以及Repository、Navigation、Object等核心概念。同时,文中还涵盖了Multi-filing、Discovery、Change、Versioning、Relationship、Policy和ACL-Access Control List等相关操作。支持的协议包括AtomPub和WebService,适用于本地和远程的CMIS服务交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CMIS域模型
  1. Document 最基本,文档对象
  2. Folder 最基本,文档的容器
  3. Relationship 描述Document之间关系,有些Repository可能不支持
  4. Policy 描述权限策略


CMIS服务
  1. Repository
  2. Navigation
  3. Object
  4. Multi-filing
  5. Discovery
  6. Change
  7. Versioning
  8. Relationship
  9. Policy
  10. ACL-Access Control List


CMIS绑定
  1. AtomPub
  2. WebService
  3. 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




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值