Thursday 4 April 2019

openCMIS: Get content stream of a document


openCMIS provides 'getContentStream' method to get the content stream associated with a document.

Following table summarizes the overloaded forms of ‘getContentStream’ method.

Method
Description
ContentStream getContentStream()
Retrieves the content stream of this document. it returns null, if the document has no content.
ContentStream getContentStream(BigInteger offset, BigInteger length)
Retrieves the content stream of this document from given offset to the length bytes.

offset: from where to read the contents of the document. If it is set to null, then stream read starts from beginning.

length: Maximum length of the stream to read, if it is set to null, then we can read till the end of the stream.
ContentStream getContentStream(String streamId)
Retrieves the content stream that is associated with the given stream ID. This is usually used to read the renditions of the document.

Following application creates a file "sampleDoc.json" under root folder, once the document is created, it creates content stream from the input file "C:\\Users\\test.json" and set the document "sampleDoc.json".

In summary following application perform 3 steps.

a. Create a document "sampleDoc.json".
                  Map<String, String> properties = new HashMap<>();
                  properties.put("cmis:objectTypeId", "cmis:document");
                  properties.put("cmis:name", "sampleDoc.json");

                  Folder rootFolder = session.getRootFolder();

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


b. Set the content stream to the document
                  ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();
                  File file = new File(localFilePath);

                  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();
                  }
                 
c. Read the contents of the 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());
                  }


Following is the complete working application.

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 localFilePath = "C:\\Users\\test.json";

 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 setTheContentStream(Document document) {
  /* Creating content stream */
  ObjectFactoryImpl objectFactory = new ObjectFactoryImpl();
  File file = new File(localFilePath);

  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", "sampleDoc.json");

  Folder rootFolder = session.getRootFolder();

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

  setTheContentStream(document);
  readTheContentsOfTheDocument(document);

 }

}




Previous                                                 Next                                                 Home

No comments:

Post a Comment