Accessing dotCMS via CMIS in Java - Documentation topics on: cmis,examples,java,.

Accessing dotCMS via CMIS in Java

This provides an example of how to perform basic CMIS repository operations in Java such as:

  • Connecting to the CMIS repository
  • Reading and creating folders
  • Reading and creating documents.
  • Getting type definitions for documents
  • Getting the properties for an object
  • Executing CMIS queries
  • Determining the “allowable actions” for an object

This is based entirely on the AtomPub binding.

The example can be found in the your dotCMS installation examples folder at $DOTCMS_HOME/docs/examples/CMIS. Both the source file DotCMSHelloCMIS.java and all needed libraries are included. The .JAR files are under the “/lib” folder.

Running the Example

To run the example perform the following steps:

Linux:
  1. Compile the sample CMIS class:
    javac -classpath lib/chemistry-opencmis-client-api-0.13.0.jar:lib/chemistry-opencmis-client-bindings-0.13.0.jar:lib/chemistry-opencmis-client-impl-0.13.0.jar:lib/chemistry-opencmis-commons-api-0.13.0.jar:lib/chemistry-opencmis-commons-impl-0.13.0.jar:lib/commons-logging-1.1.1.jar:lib/slf4j-api-1.7.12.jar:lib/slf4j-simple-1.7.12.jar:lib/stax2-api-3.1.4.jar:lib/woodstox-core-asl-4.4.0.jar DotCMSHelloCMIS.java
    
  2. Execute the class:
    java -cp lib/chemistry-opencmis-client-api-0.13.0.jar:lib/chemistry-opencmis-client-bindings-0.13.0.jar:lib/chemistry-opencmis-client-impl-0.13.0.jar:lib/chemistry-opencmis-commons-api-0.13.0.jar:lib/chemistry-opencmis-commons-impl-0.13.0.jar:lib/commons-logging-1.1.1.jar:lib/slf4j-api-1.7.12.jar:lib/slf4j-simple-1.7.12.jar:lib/stax2-api-3.1.4.jar:lib/woodstox-core-asl-4.4.0.jar:. DotCMSHelloCMIS
    
Windows:
  1. Compile the sample CMIS class:
    javac -classpath lib/chemistry-opencmis-client-api-0.13.0.jar;lib/chemistry-opencmis-client-bindings-0.13.0.jar;lib/chemistry-opencmis-client-impl-0.13.0.jar;lib/chemistry-opencmis-commons-api-0.13.0.jar;lib/chemistry-opencmis-commons-impl-0.13.0.jar;lib/commons-logging-1.1.1.jar;lib/slf4j-api-1.7.12.jar;lib/slf4j-simple-1.7.12.jar;lib/stax2-api-3.1.4.jar;lib/woodstox-core-asl-4.4.0.jar DotCMSHelloCMIS.java
    
  2. Execute the class:
    java -cp lib/chemistry-opencmis-client-api-0.13.0.jar;lib/chemistry-opencmis-client-bindings-0.13.0.jar;lib/chemistry-opencmis-client-impl-0.13.0.jar;lib/chemistry-opencmis-commons-api-0.13.0.jar;lib/chemistry-opencmis-commons-impl-0.13.0.jar;lib/commons-logging-1.1.1.jar;lib/slf4j-api-1.7.12.jar;lib/slf4j-simple-1.7.12.jar;lib/stax2-api-3.1.4.jar;lib/woodstox-core-asl-4.4.0.jar;. DotCMSHelloCMIS
    

Code Walk-Through

The DotCMSHelloCMIS.java example code takes the following actions.

Set the URL, user and password and binding type in a map.
// Create a SessionFactory and set up the SessionParameter map
  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  Map<String, String> parameter = new HashMap<String, String>();
  // user credentials - 
  parameter.put(SessionParameter.USER, userEmail);
  parameter.put(SessionParameter.PASSWORD, password);
  // connection settings - substitute your own URL
  parameter.put(SessionParameter.ATOMPUB_URL, dotCMSCMISURL);
  parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
Find the repositories for the given the URL (there should only be one).
  List<Repository> repositories = new ArrayList<Repository>(); 
  repositories = sessionFactory.getRepositories(parameter);
  Repository repository = repositories.get(0);
Get everything in the root folder and print the names of the objects.
  Folder root = session.getRootFolder();
  ItemIterable<CmisObject> children = root.getChildren();
  System.out.println("Found the following objects in the root folder:-");
  for (CmisObject o : children) {
    System.out.println(o.getName() + " which is of type " + o.getType().getDisplayName());
  }
Create a new folder under the default host.
  Map<String, String> newFolderProps = new HashMap<String, String>();
  newFolderProps.put(PropertyIds.OBJECT_TYPE_ID, "folder");
  newFolderProps.put(PropertyIds.NAME, "CMISTest" + new java.util.Date().getTime());
  Folder newFolder = defaultHost.createFolder(newFolderProps);
Create a simple text document in the new folder under default host
  // First, create the content stream
  final String textFileName = "test.txt";
  System.out.println(EOL + "Creating a simple text document : " + textFileName + " inside the above created new folder" + EOL);
  String mimetype = "text/plain; charset=UTF-8";
  String content = "This is some test content.";
  String filename = textFileName;
  byte[] buf = null;
  try {
    buf = content.getBytes("UTF-8");
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }
  ByteArrayInputStream input = new ByteArrayInputStream(buf);
  ContentStream contentStream = session.getObjectFactory().createContentStream(filename,
  buf.length, mimetype, input);
  // Create the Document Object
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(PropertyIds.OBJECT_TYPE_ID, "fileasset");
  properties.put(PropertyIds.NAME, filename);
  ObjectId id = newFolder.createDocument(properties, contentStream, VersioningState.NONE);
Get type definitions for the document.
  ObjectType objectType = session.getTypeDefinition(doc.getType().getId());
Obtain all the properties of a document.
  System.out.println(doc.getName() + " properties start");
  List<Property<?>> props = doc.getProperties();
  for (Property<?> p : props) {
    System.out.println(p.getDefinition().getDisplayName() + "=" + p.getValuesAsString());
  }
  System.out.println(doc.getName() + " properties end" + EOL);
Execute a CMIS query
  String query = "SELECT * FROM News";// News content type
  ItemIterable<QueryResult> q = session.query(query, false);
  int i = 1;
  for (QueryResult qr : q) {
    System.out.println("--------------------------------------------\n" + i + " , "
      + qr.getPropertyByQueryName("cmis:objectTypeId").getFirstValue() + " , "
      + qr.getPropertyByQueryName("cmis:name").getFirstValue() + " , "
      + qr.getPropertyByQueryName("cmis:objectId").getFirstValue() + " , ");
    i++;
  }
Get the capabilities for the CMIS repository
  System.out.println("Printing repository capabilities...");
  final RepositoryInfo repInfo = session.getRepositoryInfo();
  RepositoryCapabilities cap = repInfo.getCapabilities();
  System.out.println("\nNavigation Capabilities");
  System.out.println("-----------------------");
  System.out.println("Get descendants supported: "
    + (cap.isGetDescendantsSupported() ? "true" : "false"));
  System.out.println("Get folder tree supported: "
    + (cap.isGetFolderTreeSupported() ? "true" : "false"));
  System.out.println("\nObject Capabilities");
  System.out.println("-----------------------");
  System.out.println("Content Stream: " + cap.getContentStreamUpdatesCapability().value());
  System.out.println("Changes: " + cap.getChangesCapability().value());
  System.out.println("Renditions: " + cap.getRenditionsCapability().value());
  System.out.println("\nFiling Capabilities");
  System.out.println("-----------------------");
  System.out.println("Multifiling supported: "
    + (cap.isMultifilingSupported() ? "true" : "false"));
  System.out.println("Unfiling supported: " + (cap.isUnfilingSupported() ? "true" : "false"));
  System.out.println("Version specific filing supported: "
    + (cap.isVersionSpecificFilingSupported() ? "true" : "false"));
  System.out.println("\nVersioning Capabilities");
  System.out.println("-----------------------");
  System.out.println("PWC searchable: " + (cap.isPwcSearchableSupported() ? "true" : "false"));
  System.out.println("PWC Updatable: " + (cap.isPwcUpdatableSupported() ? "true" : "false"));
  System.out.println("All versions searchable: "
    + (cap.isAllVersionsSearchableSupported() ? "true" : "false"));
  System.out.println("\nQuery Capabilities");
  System.out.println("-----------------------");
  System.out.println("Query: " + cap.getQueryCapability().value());
  System.out.println("Join: " + cap.getJoinCapability().value());
  System.out.println("\nACL Capabilities");
  System.out.println("-----------------------");
  System.out.println("ACL: " + cap.getAclCapability().value());
Determine the “allowable actions” for an object.
  for (Action a : doc.getAllowableActions().getAllowableActions()) {
    System.out.println("\t" + a.value());
  }