Thursday 4 April 2019

openCMIS: Append to content stream of a document


openCMIS provides 'appendContentStream' method to append to the content stream of a document. Below table summarizes the overloaded forms of 'appendContentStream' method.

Method
Description
Document appendContentStream(ContentStream contentStream, boolean isLastChunk)
Appends a content stream to the content stream of the document and refreshes this object afterwards. If the repository created a new version, this new document is returned. Otherwise the current document is returned.
ObjectId appendContentStream(ContentStream contentStream, boolean isLastChunk, boolean refresh)
Appends a content stream to the content stream of the document. If the repository created a new version, the object ID of this new version is returned. Otherwise the object ID of the current document is returned.

Followng are true for above overloaded methods.
a.   The stream in 'contentStream' is consumed but not closed by this method.
b.   A repository MAY automatically create new document versions as part of this service method. Therefore, the objectId output NEED NOT be identical to the objectId input.

Following snippet is used to append content of the file to the document.

         private static void appendContentStream(Document document, File file) {
                  /* Creating content stream */
                  ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();
                  try (InputStream stream = new FileInputStream(file)) {
                           String mimetype = Files.probeContentType(file.toPath());
                           ContentStream contentStream = objectFactory.createContentStream(file.getName(), file.length(), mimetype,
                                             stream);
                           document.appendContentStream(contentStream, true, true);
                  } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                  }
         }

Below application creates a document "application.log" with initial content from the file "C:\\Users\\app1.log". Once the document is created, it appends “C:\\Users\\app2.log" contents to the document.

TestCmis.java
package com.sample.util;

import java.io.BufferedInputStream;
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.Folder;
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.client.runtime.repository.ObjectFactoryImpl;
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";
 private static String localFile1 = "C:\\Users\\app1.log";
 private static String localFile2 = "C:\\Users\\app2.log";

 private 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);
 }

 private static void appendContentStream(Document document, File file) {
  /* Creating content stream */
  ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();
  try (InputStream stream = new FileInputStream(file)) {
   String mimetype = Files.probeContentType(file.toPath());
   ContentStream contentStream = objectFactory.createContentStream(file.getName(), file.length(), mimetype,
     stream);
   document.appendContentStream(contentStream, true, true);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 private static void setContentStream(Document document, File file) {
  /* Creating content stream */
  ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();
  try (InputStream stream = new FileInputStream(file)) {
   String mimetype = Files.probeContentType(file.toPath());
   ContentStream contentStream = objectFactory.createContentStream(file.getName(), file.length(), mimetype,
     stream);
   document.setContentStream(contentStream, true, true);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 private static void readTheContentsOfTheDocument(Document document) {
  ContentStream contentStream = document.getContentStream();

  try (InputStream inputStream = contentStream.getStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {

   byte[] buffer = new byte[65535];
   int bytesRead = -1;

   while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
    String str = new String(buffer, 0, bytesRead);
    System.out.print(str);
   }
  } catch (IOException e) {
   System.out.println("Error occurred while processing the file content");
   System.out.println(e.getMessage());
  }

 }

 public static void main(String args[]) throws IOException {

  Session session = getSession();

  /* Create the document 'sampleDoc.json' */
  Map<String, String> properties = new HashMap<>();
  properties.put("cmis:objectTypeId", "cmis:document");
  properties.put("cmis:name", "application.log");

  Folder rootFolder = session.getRootFolder();

  Document document = rootFolder.createDocument(properties, null, null);

  setContentStream(document, new File(localFile1));
  appendContentStream(document, new File(localFile2));

  readTheContentsOfTheDocument(document);

 }

}



Previous                                                 Next                                                 Home

No comments:

Post a Comment