Sunday 24 March 2019

openCMIS: Creating session to a repository


In my previous posts, I explained how to get all the repositories at given end point. In this post, I am going to explain how to create a session to the repository.

There are couple of ways to create a session, I am going to explain the creation of session using SesionFactory in this post. My later posts explain other ways.

If you know the repository id, you can create a session like below.
         public static Session getSession(String repositoryId, String serverURL) {
                  Map<String, String> parameters = new HashMap<>();
                  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

                  parameters.put(SessionParameter.USER, "");
                  parameters.put(SessionParameter.PASSWORD, "");

                  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
                  parameters.put(SessionParameter.BROWSER_URL, serverURL);

                  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
                  return sessionFactory.createSession(parameters);
         }

Following is the complete working application.
TestCmis.java
package com.sample.util;

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

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.enums.BindingType;

public class TestCmis {

 public static Session getSession(String repositoryId, String serverURL) {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

  parameters.put(SessionParameter.USER, "");
  parameters.put(SessionParameter.PASSWORD, "");

  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  return sessionFactory.createSession(parameters);
 }

 public static void main(String args[]) {
  String repositoryId = "A1";
  String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
  Session session = getSession(repositoryId, serverURL);

  System.out.println(session.getRepositoryInfo().getName());
 }
}

Output
Apache Chemistry OpenCMIS InMemory Repository


Previous                                                 Next                                                 Home

No comments:

Post a Comment