Playing With Alfresco Repository Using Cmis

Playing with Alfresco repository using CMIS

Before starting with this blog we need to know only one thing why we are using alfresco with CMIS for content access.

Table of Content :

What is alfresco and why we are using it?

Alfresco means Open Air. Alfresco is the one of the most popular Open Source Content Management Platform. Alfresco gain its popularity from 2005 on-wards.
Alfresco can be used for the wide variety of operation like
+ Record Management
+ Content Management or Document Management
+ For Version control repository
+ For simple and complex business process management
+ For digitization of documents
+ For mail room automation
+ For collaboration
+ Access control management system
many more….
Its all our choice how and where we are use Alfresco for our usability.

What is CMIS why we are using it with alfresco?

CMIS stands for Content Management Interoperability Services. CMIS is used when we want to access different content repository that follows CMIS standard within one complete system written on top of them or when we want different content repository follows CMIS standard to interact or exchange there data with each others permission. CMIS is the open standard its not given by Alfresco community its given by OASIS for accessing content repository. We will start how to use Apache Chemistry and Alfresco Open CMIS for respective operation:

One most important thing before starting with CMIS in alfresco you should use apache CMIS library along with alfresco open cmis library. But its not necessary if you are not dealing with alfresco session and objects.

Type of CMIS binding in Alfresco :

There are three type of CMIS binding which alfresco support:

  • CMIS Web Services binding.
  • CMIS AtomPub binding.
  • CMIS Local binding.

We are using CMIS AtomPub binding in this case we can use other also the only difference is in how we create session in each case else all operation are same. Lets start coding………..

Get alfresco CMIS repository ID and CMIS Atom Pub binding URL :

Below is how we can get Alfresco repository ID and atom pub binding URL

Getting repository ID for alfresco installation.

** For Getting repository ID for alfresco installation follow following steps :**

  • Hit URL for alfresco installation http://{server}:{post}/alfresco/service/cmis.
  • Enter your admin user name and password in login popup.
  • If user have appropriate permission New file xml is downloaded with name cmis.
  • Open file and get CMIS repository ID in cmisra:repositoryInfo between cmis:repositoryId tags.
  • In my case it is 6fc907f9-6790-46b5-9c25-d6855ff8ff5c.
    CMIS atom pub URL is same following URL pattern :

Alfresco CMIS AtomPub binding URL.

http://{server}:{port}/alfresco/service/cmis

How to create create session using Apache CMIS and Alfresco open CMIS ?

The main reason for creating alfresco session as we can't user things or standard in alfresco that are not related to CMIS using simple CMIS session like aspects can't be used using simple CMIS session.

Creating apache CMIS session :

// default factory implementation
SessionFactory factory = SessionFactoryImpl.newInstance();
// map to contain connection parameter any cmis repository using atom pub
Map<String, String> parameter = new HashMap<String, String>();
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");
parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9090/alfresco/service/cmis"); parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.REPOSITORY_ID, "6fc907f9-6790-46b5-9c25-d6855ff8ff5c");
// getting Instance from User Credential
try {
// create session
Session session = factory.createSession(parameter);
}
catch (Exception e) {
e.printStackTrace();
}

Creating Alfresco Cmis session :

// default factory implementation
SessionFactory factory = SessionFactoryImpl.newInstance();
// map to contain connection parameter for Alfresco repository using AtomPub Map
<String, String> parameter = new HashMap<String, String>();
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");
parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:9090/alfresco/service/cmis"); parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.REPOSITORY_ID, "6fc907f9-6790-46b5-9c25-d6855ff8ff5c"); parameter.put(SessionParameter.OBJECT_FACTORY_CLASS,"org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
// getting Instance from User Credential
try {
// create session Session session = factory.createSession(parameter);
} catch (Exception e)
{
e.printStackTrace();
}

Use login Ticket instead of username and password :

username : ROLETICKET
password : {USER
LOGIN_TICKET}

You can get login ticket by using Alfresco login service http://{server}:{port}/alfresco/service/api/login?u={username}&pw={password?}

Simple and Complex File operations?

There are many operation related to file or content we list some of the most rapidly used:

  • Create new document.
  • Getting existing document.
  • Update document.
  • Delete old file.
  • Create new document :

Type of file Objects in Alfresco using CMIS approach.

1. Document Object.
2. AlfrescoDocument Object.

Document Object.

This object is common in all CMIS approach whether it is alfresco or any other CMIS supported content repository. This Document object can't be used for dealing alfresco related operations on Document Like :- aspects etc

AlfrescoDocument Object.

This object is related to Alfresco content repository we have to explicity convert CMIS Document Object to AlfescoDocument object when dealing with alfresco related operation using Alfresco open CMIS extension JAR.

Create New Document :

1. First we need to follow code for creating Alfresco session.

2. Create object for Alfresco folder or CMIS folder Object for storing document.

Folder path could be any path within alfresco content repository for which user who is accessing it should have permission.

public Folder retriveCMISFolder(Session session, String folderPath) throws ServiceException{
if(session == null){
throw new ServiceException(ServiceException.SuccessCode.UnauthorisedAccess, "NUll or empty Session Object");
}
if(folderPath == null || folderPath.isEmpty()){
throw new ServiceException(ServiceException.SuccessCode.ApiError, "NUll or empty Folder Path");
}
if(!folderPath.startsWith("/")){
throw new ServiceException(ServiceException.SuccessCode.ApiError, "Invalid folder path must start and end with /");
}
try{
OperationContext context = session.createOperationContext();
context.setRenditionFilterString("cmis:thumbnail");
return (Folder) session.getObjectByPath(folderPath, context);
}catch (Exception e)
{
logger.error(e);
throw new ServiceException(ServiceException.SuccessCode.ApiError, "Unable to created folder object with path = "+ folderPath + " Expected Cause =" + e.getMessage());
}
}

3. Create document in alfresco folder for which object is created in step 2.

public Document createNewDocument(String documentName, String documentTitle, String documentDescription, String mimeType, Folder folder, InputStream oInputStream) throws ServiceException{
if(documentName == null || documentName.isEmpty()){
throw new ServiceException(ServiceException.SuccessCode.ApiError, "Document name is mandatory");
}
if(documentTitle == null || documentTitle.isEmpty()){
documentTitle = documentName;
}
if(documentDescription == null || documentDescription.isEmpty()){
documentDescription = documentName;
}
if(oInputStream == null){
oInputStream = CreatePdfFile.createPdfFile(documentName, documentDescription);
}
if(mimeType == null || mimeType.isEmpty()){
mimeType = "application/pdf";
}
//trying to create new document
try
{
// Setting properties for new document to be created within repository
Map<String, Object> propertiesDocument = new HashMap<String, Object>();
propertiesDocument.put(PropertyIds.NAME, documentName);
propertiesDocument.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");
propertiesDocument.put("cm:title", documentTitle);
propertiesDocument.put("cm:description", documentDescription);
// Reading the content in Bytes stream and Writing it to new file in
ContentStream contentStream = new ContentStreamImpl(documentName, null, mimeType, oInputStream);
// creating document with name title and description
return folder.createDocument(propertiesDocument, contentStream, VersioningState.MAJOR);
} catch (Exception e)
{
logger.error(e);
throw new ServiceException(ServiceException.SuccessCode.ApiError, "Unable to create document " + documentName + ". " + e.getMessage(), e);
}
}

Getting existing document :

FileNode is the node Id of document which we have for all document in Alfresco. We have different node Id for each document in alfresco repository. For getting document node Id we can get all child document under any Folder in repository. There can be other way also to get document in Folder like using name etc.

public AlfrescoDocument retriveAlfrescoDocument(Session session, String fileNode) throws ServiceException{
if(session == null){
throw new ServiceException(ServiceException.SuccessCode.UnauthorisedAccess, "NUll or empty Session Object");
}
if(fileNode == null || fileNode.isEmpty()){
throw new ServiceException(ServiceException.SuccessCode.ApiError, "NUll or empty file Node");
}
try{
return (AlfrescoDocument) session.getObject(fileNode);
} catch (Exception e)
{
logger.error(e);
throw new ServiceException(ServiceException.SuccessCode.ApiError, "unable to convert cmis folder to alfresco folder = " + e.getMessage());
}
}

Update document :

// getting Instance from User Credential
Session session = //creating session we did it above
// getting alfresco document
AlfrescoDocument alfrescoDocument = retriveAlfrescoDocument(session, documentId);
// try to edit document description
try
{
// Map to contain document properties
Map<String, Object> propertiesDocument = new HashMap<String, Object>();
if(!Utils.isBlank(documentName)){
propertiesDocument.put(PropertyIds.NAME, documentName);
}
propertiesDocument.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");
if(!Utils.isBlank(documentTitle)){
propertiesDocument.put("cm:title", documentTitle);
}
if(!Utils.isBlank(documentDescription)){
propertiesDocument.put("cm:description", documentDescription);
}
// updating document property
CmisObject updateDocument = alfrescoDocument.updateProperties(propertiesDocument);
// return document properties
System.out.println(updateDocument.getName());
} catch (Exception e){
throw new ServiceException(ServiceException.SuccessCode.ApiError, "unable to edit document with name " + alfrescoDocument.getName() + e.getMessage());
}

Delete old file :

//creating new session object
Session session = //Create session we did it above
//alfresco document object
AlfrescoDocument alfrescoDocument = retriveAlfrescoDocument(session, fileNode);
// try to delete document object
try {
// deleting File with given File ID from repository
alfrescoDocument.delete();
//return property map for deleting document
System.out.println("Document deleted successfully");
}catch (Exception e) {
logger.error(e);
throw new ServiceException(
ServiceException.SuccessCode.ApiError,"Unable to delete document with name " + e.getMessage());
}

So these are the file operation we can have in Alfresco there could be more.

Folder Operations in Alfresco?

We can have two type of folder object in CMIS approach :

Type of folder Objects in Alfresco using CMIS approach.

1. Folder Object.
2. AlfrescoFolder Object.

Folder Object.

This object is common in all CMIS approach whether it is alfresco or any other CMIS supported content repository. This folder object can't be used for dealing alfresco related operations on folders Like :- aspects etc

AlfrescoFolder Object.

This object is related to Alfresco content repository we have to explicitly convert CMIS Folder Object to AlfescoFolder object when dealing with alfresco related operation using Alfresco open CMIS extension JAR.

For code on Alfresco Folder operations we are creating separate bog.

Full index and metadata search of document using CMIS?

For working with alfresco document search visit our blog Alfresco custom field search in CMIS approach.

For more hacks about alfresco visit

Leave a Reply

Your email address will not be published. Required fields are marked *