Sunday 7 April 2019

openCMIS: get mime type of a document


From the content stream of a document, you can get the mime type of the document. ContentStream interface provides 'getMimeType' method, to get the mime type of a document.

String getMimeType()
Returns the MIME type of the stream or null if the MIME type is unknown

Example
                  ContentStream contentStream = document.getContentStream();
                 
                  if(contentStream == null){
                           System.out.println("There is no content stream associated with this document");
                           return;
                  }

                  String mimeType = contentStream.getMimeType();
                 
                  System.out.println("mimeType : " + mimeType);
  
TestCmis.java
package com.sample.util;

import java.io.IOException;
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";

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

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

  Session session = getSession();

  Document document = (Document) session.getObjectByPath("/application.log");
  
  ContentStream contentStream = document.getContentStream();
  
  if(contentStream == null){
   System.out.println("There is no content stream associated with this document");
   return;
  }

  String mimeType = contentStream.getMimeType();
  
  System.out.println("mimeType : " + mimeType);

 }

}

Output
mimeType : application/octet-stream


Previous                                                 Next                                                 Home

No comments:

Post a Comment