Jerry Jacob's Blog

Using CMIS(Content Management Interoperability Services) API with Alfresco ECM

Posted in Uncategorized by jerlinworld on October 16, 2013

Start Alfresco and go to the URL, http://localhost:8080/alfresco/service/cmis/index.html

Expand the CMIS Repository Information to find the Repository ID

Use the code below to connect to Alfresco.

private Session connectAlfresco() {

final String ALFRSCO_CMIS_URL = "http://localhost:8080/alfresco/cmisatom";
 final String REPOSITORY_ID = "ab3cd777-9999-5555-55a5-a55cc7777777";

final Map<String, String> parameters = new HashMap<String, String>();
 parameters.put(SessionParameter.ATOMPUB_URL, ALFRSCO_CMIS_URL);
 parameters.put(SessionParameter.REPOSITORY_ID, REPOSITORY_ID);
 parameters.put(SessionParameter.USER, "admin");
 parameters.put(SessionParameter.PASSWORD, "admin");

// create the session
 final Session session = SessionFactoryImpl.newInstance().createSession(
 return session;

Now you can create documents, update documents, delete them, read a file or query for documents. Doing all of them at-once doesn’t make any sense; but the idea is to show how the API can be used.

public static void main(final String[] args) {

 final static String FOLDER_PATH = "/Jerry's Folders";
 final AlfrescoServiceMediator md = new AlfrescoServiceMediator();
 final Session session = md.connectAlfresco();

// get repository info
 final RepositoryInfo repInfo = session.getRepositoryInfo();
 System.out.println("Repository name: " + repInfo.getName());


final String filename = "newFile.txt";
 final CmisObject folder = session.getObjectByPath(AlfrescoServiceMediator.FOLDER_PATH);

final byte[] content = "This is a new File and it got updated now!!!!!" .getBytes();

 md.createCmisDocument(folder, filename, content);

 AlfrescoServiceMediator.updateCmisDocument(session, filename, content);


final CmisObject delfile = session.getObjectByPath(AlfrescoServiceMediator.FOLDER_PATH + "/jerrysDocument3.txt");

 System.out.println(md.readCmisFile(session, filename));

 final ItemIterable<QueryResult> results = session.query(
 "SELECT * FROM cmis:document", false);

for (final QueryResult hit : results) {
 for (final PropertyData<?> property : hit.getProperties()) {
 final String queryName = property.getQueryName();
 final Object value = property.getFirstValue();
 System.out.println(queryName + ": " + value);

private void createCmisDocument(final CmisObject cmisFolder,final String filename, final byte[] content) {
 final Folder parent = (Folder) cmisFolder;

// properties
 // (minimal set: name and object type id)
 final Map<String, Object> properties = new HashMap<String, Object>();
 properties.put(PropertyIds.OBJECT_TYPE_ID, "ui:claimdocs");
 properties.put(PropertyIds.NAME, filename);
 properties.put("ui:claimDocumentType", "ClaimsPartialState");
 properties.put("ui:applicationName", "ClaimWebApp");
 // content
 final InputStream stream = new ByteArrayInputStream(content);
 final ContentStream contentStream = new ContentStreamImpl(filename,BigInteger.valueOf(content.length), "text/plain", stream);
 // create a major version
 final Document newDoc = parent.createDocument(properties,contentStream, VersioningState.MAJOR);

private void updateCmisDocument(final Session session,final String filename, final byte[] content) {

final CmisObject cmisFile = session.getObjectByPath(AlfrescoServiceMediator.FOLDER_PATH + "/"+ filename);
 Document docFile = (Document) cmisFile;
 docFile = (Document) session.getObject(docFile.checkOut());
 final InputStream filestream = new ByteArrayInputStream(content);
 final ContentStream filecontentStream = new ContentStreamImpl(filename,
 BigInteger.valueOf(content.length), "text/plain", filestream);
 docFile.checkIn(false, null, filecontentStream, "just a minor change");


private void deleteCmisDocument(final CmisObject cmisFile) {

final Document delFile = (Document) cmisFile;

private Folder createFolder(final Folder target, final String newFolderName) {
 final Map<String, String> props = new HashMap<String, String>();
 props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
 props.put(PropertyIds.NAME, newFolderName);
 final Folder newFolder = target.createFolder(props);
 return newFolder;

private String readCmisFile(final Session session, final String filename) {

final CmisObject cmisFile = session.getObjectByPath(AlfrescoServiceMediator.FOLDER_PATH + "/"+ filename);
 final Document document = (Document) cmisFile;
 final InputStream stream = document.getContentStream().getStream();
 return this.getStringFromInputStream(stream);

// convert InputStream to String
 private String getStringFromInputStream(final InputStream is) {

BufferedReader br = null;
 final StringBuilder sb = new StringBuilder();
 String line;
 try {
 br = new BufferedReader(new InputStreamReader(is));
 while ((line = br.readLine()) != null) {
 } catch (final IOException e) {
 } finally {
 if (br != null) {
 try {
 } catch (final IOException e) {
 return sb.toString();

private void browseFolder(final Folder rootFolder) {

final ItemIterable<CmisObject> children = rootFolder.getChildren();
 for (final CmisObject object : children) {
 System.out.println(" Id: " + object.getId());
 System.out.println(" Name: " + object.getName());
 System.out.println(" Base Type: " + object.getBaseTypeId());
 System.out.println(" Property 'bla': " + object.getPropertyValue("bla"));

final ObjectType type = object.getType();
 System.out.println(" Type Id: " + type.getId());
 System.out.println(" Type Name: " + type.getDisplayName());
 System.out.println(" Type Query Name: " + type.getQueryName());

final AllowableActions actions = object.getAllowableActions();
 System.out.println(" canGetProperties: " + actions.getAllowableActions().contains(Action.CAN_GET_PROPERTIES));
 System.out.println(" canDeleteObject: " + actions.getAllowableActions().contains(Action.CAN_DELETE_OBJECT));


2 Responses

Subscribe to comments with RSS.

  1. Alejandro Martínez said, on March 18, 2014 at 10:26 am

    Thanks for this blog.
    I’ll try, but I would like to know what imports I need, I need some JARs?
    much appreciate your help.

  2. Pooranachandran said, on June 18, 2014 at 3:21 pm

    Very much useful link

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: