Wednesday 3 April 2019

openCMIS: Update the content of a document


In my previous post, I explained how to rename a document. In this post, I am going to explain how to update the content of a document. By setting the content stream of a document, you can update the content of a document.

Following step-by-step procedure explains how to update the content of a document ‘/f1/a.txt’.

Step 1: Get the session object

Step 2: Get the reference to the document that you want to update. You can get the reference by using the document path (or) by id.
                  Document document = (Document) session.getObjectByPath("/f1/a.txt");

Step 3: Create a content stream and set the content stream to the document.

                  File inputFile = new File("C:\\Users\\Krishna\\temp.log");
                  String mimeType = Files.probeContentType(inputFile.toPath());

                  try (InputStream inputStream = new FileInputStream(inputFile)) {
                           ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
                                             inputFile.length(), mimeType, inputStream);

                           document.setContentStream(contentStream, true, true);
                  }

Find the following working application.

TestCmis.java
package com.sample.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public static Session getSession() {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

  parameters.put(SessionParameter.USER, "");
  parameters.put(SessionParameter.PASSWORD, "");

  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  return sessionFactory.createSession(parameters);
 }

 public static void main(String args[]) throws IOException {
  Session session = getSession();

  Document document = (Document) session.getObjectByPath("/f1/a.txt");

  System.out.println("Content stream length before updating : " + document.getContentStreamLength());

  File inputFile = new File("C:\\Users\\Krishna\\temp.log");
  String mimeType = Files.probeContentType(inputFile.toPath());

  try (InputStream inputStream = new FileInputStream(inputFile)) {
   ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
     inputFile.length(), mimeType, inputStream);

   document.setContentStream(contentStream, true, true);
  }

  document.refresh();
  System.out.println("Content stream length after updating : " + document.getContentStreamLength());

 }
}

Sample Output
Content stream length before updating : 21
Content stream length after updating : 111


The stream in contentStream method is consumed but not closed by this method. We need to close this method explicitly. I used try-with-resource statement, so the inputStream is closed, automatically, once the usage of inputStream is done.


Previous                                                 Next                                                 Home

No comments:

Post a Comment