Friday 5 April 2019

openCMIS: Delete content stream of a document


openCMIS provides 'deleteContentStream' method to delete the content stream associated with the document.

Following table summaizes the overloaded forms of 'deleteContentStream' method.

Method
Description
Document deleteContentStream()
Removes the current content stream from 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.

It returns the updated document, or null if the repository did not return an object ID.
ObjectId deleteContentStream(boolean refresh)
Removes the current content stream from 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.

If 'refresh' is set to true, this object will be refreshed after the content stream has been deleted.

Following applicatin creates a document 'sample.log', prints the contents of the document 'sample.log' to console, after that it deletes the content stream associated with the document 'sample.log'.

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 localFile = "C:\\Users\\app1.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 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();

  if (contentStream == null) {
   System.out.println("*************************");
   System.out.println("Content stream is not associated with the document");
   System.out.println("*************************");
   return;
  }

  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", "sample.log");

  Folder rootFolder = session.getRootFolder();

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

  setContentStream(document, new File(localFile));

  System.out.println("Printing the contents of the document " + localFile);
  readTheContentsOfTheDocument(document);

  System.out.println("\nDeleting the content stream associated with this document");
  document.deleteContentStream(true);

  readTheContentsOfTheDocument(document);

 }

}


Output
Printing the contents of the document C:\Users\app1.log
Testing the deleteContentStream function of Document instance. 
Deleting the content stream associated with this document
*************************
Content stream is not associated with the document
*************************




Previous                                                 Next                                                 Home

No comments:

Post a Comment