Friday 22 March 2019

openCMIS: Get all the repositories using Atom pub binding


Atom publishing protocol is an application-level protocol for publishing and editing Web resources. The Atom Publishing Protocol is documented in rfc5023. The schema/format of Atom is documented in rfc4287.

If you would like to know more about the ATOM publishing protocol, You can go to my following tutorial series.

How to get all the repositories?
By using the getRepositories() method of SessionFactory class, you can get all the repositories at a given endpoint.

Following is the signature of getRepositorie method.
List<Repository> getRepositories(Map<String, String> parameters);
We need to pass basic information like username, password, atom pub url endpoint and type of binding as 'parameters' to the getRepositories method.

Example
// user credentials
parameters.put(SessionParameter.USER, this.getUserName());
parameters.put(SessionParameter.PASSWORD, this.getPassword());

// connection settings
parameters.put(SessionParameter.ATOMPUB_URL, this.getUrlToConnect());
parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

Find the following working application.

RepositoryUtil.java
package com.sample.util;

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

import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;

/**
 * Abstrcat super class for all the types of bindings.
 * 
 *
 */
public abstract class RepositoryUtil {
 private String userName;
 private String password;
 private String urlToConnect;
 
 protected Map<String, String> parameters = new HashMap<> ();

 public RepositoryUtil(String userName, String password, String urlToConnect) {
  this.userName = userName;
  this.password = password;
  this.urlToConnect = urlToConnect;
 }

 public abstract void populateProperties();
 
 /**
  * Return all the repositories for given end point.
  * 
  * @param parameters
  * @return
  */
 public List<Repository> getAllRepositories() {
  populateProperties();
  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  List<Repository> repositories = sessionFactory.getRepositories(parameters);
  return repositories;
 }

 public String getUserName() {
  return userName;
 }

 public String getPassword() {
  return password;
 }

 public String getUrlToConnect() {
  return urlToConnect;
 }

}


AtomPubBindingRepositoryUtil.java
package com.sample.util;

import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class AtomPubBindingRepositoryUtil extends RepositoryUtil {

 public AtomPubBindingRepositoryUtil(String userName, String password, String urlToConnect) {
  super(userName, password, urlToConnect);
 }

 @Override
 public void populateProperties() {
  // user credentials
  parameters.put(SessionParameter.USER, this.getUserName());
  parameters.put(SessionParameter.PASSWORD, this.getPassword());

  // connection settings
  parameters.put(SessionParameter.ATOMPUB_URL, this.getUrlToConnect());
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

 }

}


Test.java
package com.sample.util;

import java.util.List;

import org.apache.chemistry.opencmis.client.api.Repository;

public class Test {

 public static void main(String args[]){
  String urlToConnect = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/atom11";
  
  RepositoryUtil repoUtil = new AtomPubBindingRepositoryUtil("", "", urlToConnect);
  
  List<Repository> repositories = repoUtil.getAllRepositories();
  
  for(Repository repository : repositories){
   System.out.println(repository.getName());
  }
  
 }
}

Output
Apache Chemistry OpenCMIS InMemory Repository



Previous                                                 Next                                                 Home

No comments:

Post a Comment