H-H H-H - 1 year ago 146
Java Question

Connecting to Alfresco with CMIS

I'm starting with Alfresco. I installed Alfresco 4 Community edition and I'm trying to connect to it using OpenCMIS. I took this snippet of code from the OpenCMIS page:

SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> parameter = new HashMap<String, String>();
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");

Session s = sessionFactory.createSession(parameter);

However, I couldn't find out what should be the Repository ID and how to specify Alfresco's URL. Could someone explain it to me? Thank you.

Answer Source


// User credentials.
parameters.put(SessionParameter.USER, "admin");
parameters.put(SessionParameter.PASSWORD, "admin");

// Connection settings.
parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/alfresco/service/cmis"); // URL to your CMIS server.
parameters.put(SessionParameter.AUTH_HTTP_BASIC, "true" );
parameters.put(SessionParameter.COOKIES, "true" );

// Create session.
// Alfresco only provides one repository.
Repository repository = sessionFactory.getRepositories(parameters).get(0);
Session session = repository.createSession();

From the CMIS spec:

An application MUST use the CMIS “"Get Repositories”" service (getRepositories) to obtain a list of repositories that are available at that endpoint

Repository IDs are opaque strings generated by a CMIS repository, that you usually discover rather than know upfront. Also, a single CMIS server can host multiple repositories (although Alfresco only supports one for the time being).

When it comes to Alfresco, the repository ID is different on a per-instance basis, so that if you start afresh from a clean database that ID will be regenerated, breaking your application if it was relying on a hard coded repo ID.

The Repository ID discovery is made possible by the Service Document in the AtomPub binding and via a cmisRepositoryEntryType in the Web Services binding.