Pages

Connecting to Alfresco repository using OpenCmis

This is the code snippest for accessing Alfresco repository using Apache Chemistry OpenCmis library for java. In this code we have used AtomPub protocal for binding. 
The url for AtomPub binding is"http://192.168.1.103:8081/alfresco/cmisatom"

package com.esc.cmis.client.services;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.Repository;
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.RepositoryInfo;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ashutosh kumar
 *
 */
public class AlfrescoService {

 private static Session session;

   
 public Session connect() {
  System.out.println("Trying to connect Fo alfresco.......");
  SessionFactory sessionFactory=SessionFactoryImpl.newInstance();

  //Connection parameter
  Map<String, String> parameter = new HashMap<>();

  parameter.put(SessionParameter.USER, "admin");
  parameter.put(SessionParameter.PASSWORD, "admin");
  parameter.put(SessionParameter.ATOMPUB_URL,
                     "http://192.168.1.103:8081/alfresco/cmisatom");
  parameter.put(SessionParameter.BINDING_TYPE,
                                    BindingType.ATOMPUB.value());
  parameter.put(SessionParameter.OBJECT_FACTORY_CLASS,
      "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

  List<Repository> repositories = sessionFactory

                                     .getRepositories(parameter);

  // Establish a seesion to a repository

  System.out.println("Connection establishes to FO alfresco");
  session = repositories.get(0).createSession();

  // Get some repository info

  System.out.println("Repository Name: "
                + session.getRepositoryInfo().getName());
  System.out.println("Repository ID: "
                + session.getRepositoryInfo().getId());
  System.out.println("CMIS Version: "
                + session.getRepositoryInfo().getCmisVersion());
  RepositoryInfo i = session.getRepositoryInfo();
  System.out.println(i.getDescription());
  System.out.println(i.getProductName());
  return session;
        // company home
        // Folder folder = session.getRootFolder();
        // return folder;
    }

  

    public void createFolder() {

    }


  public void listFolder(Folder folder) {


    ItemIterable<CmisObject> children = folder.getChildren();

        for (CmisObject o : children) {
            System.out.println(o.getName());
            System.out.println(o.getId());
        }
    }

   public CmisObject getCmisObject(String path) {

     CmisObject cmisObject = session.getObjectByPath(path);
     return cmisObject;
    }

   // Getting the id of an object from its path

   public void getId(String path) {
     String path1 = "/vvf";
     Document doc = (Document) session.getObjectByPath(path1);
     String id = doc.getId();
     System.out.println(id + "''''''''");
    }

    public void moveDocument() {

    }
}