Sunday, 14 April 2019

openCMIS: Create a policy


Policy can be filed (or) unfiled.

How to create unfiled policy?
By using session object, you can create unfiled policy.

Session interface provide below methods to create policy.

ObjectId createPolicy(Map<String, ?> properties, ObjectId folderId)
ObjectId createPolicy(Map<String, ?> properties, ObjectId folderId, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces)

Argument
Description
Properties
The policy properties
folderId
The folder ID of the parent folder, null for an unfiled policy.
Policies
A list of policys that MUST be applied to the newly-created policy object.
addAces
A list of ACEs that MUST be added to the newly-created policy object, either using the ACL from folderId if specified, or being applied if no folderId is specified.
removeAces
A list of ACEs that MUST be removed from the newly-created policy object, either using the ACL from folderId if specified, or being ignored if no folderId is specified.

Below statements are used to create a policy object.

                  Map<String, Object> properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "a new unfiled policy");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "AuditPolicy");
                  properties.put(PropertyIds.POLICY_TEXT, "my un policy description");

                  ObjectId policyId = session.createPolicy(properties, null);

                  Policy policy = (Policy) session.getObject(policyId);


‘AuditPolicy’ is already comes with in-memory server.

TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Policy;
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.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 private static Session getSession() {
  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[]) throws IOException {

  Session session = getSession();

  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(PropertyIds.NAME, "a new unfiled policy");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "AuditPolicy");
  properties.put(PropertyIds.POLICY_TEXT, "my un policy description");

  ObjectId policyId = session.createPolicy(properties, null);

  Policy policy = (Policy) session.getObject(policyId);

  System.out.println("Created By : " + policy.getCreatedBy());
  System.out.println("Description : " + policy.getDescription());
  System.out.println("Name : " + policy.getName());
  System.out.println("Policy Text : " + policy.getPolicyText());
  System.out.println("Policy Id : " + policy.getId());

 }

}

How to create filed policy?
Folder class provides below methods to define filed policies.

Policy createPolicy(Map<String, ?> properties);
Policy createPolicy(Map<String, ?> properties, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces, OperationContext context);

Argument
Description
Properties
The policy properties
Policies
A list of policys that MUST be applied to the newly-created policy object.
addAces
A list of ACEs that MUST be added to the newly-created policy object, either using the ACL from folderId if specified, or being applied if no folderId is specified.
removeAces
A list of ACEs that MUST be removed from the newly-created policy object, either using the ACL from folderId if specified, or being ignored if no folderId is specified.
context
Operational context

Below statements are used to create policy in a folder.

                  Map<String, Object> properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "a new unfiled policy");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "AuditPolicy");
                  properties.put(PropertyIds.POLICY_TEXT, "my un policy description");
                 

                  Folder folder = (Folder)session.getObjectByPath("/My_Folder-0-0");

                  ObjectId policyId = folder.createPolicy(properties);


TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Policy;
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.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 private static Session getSession() {
  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[]) throws IOException {

  Session session = getSession();

  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(PropertyIds.NAME, "a new unfiled policy");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "AuditPolicy");
  properties.put(PropertyIds.POLICY_TEXT, "my un policy description");
  

  Folder folder = (Folder)session.getObjectByPath("/My_Folder-0-0");

  ObjectId policyId = folder.createPolicy(properties);

  Policy policy = (Policy) session.getObject(policyId);

  System.out.println("Created By : " + policy.getCreatedBy());
  System.out.println("Description : " + policy.getDescription());
  System.out.println("Name : " + policy.getName());
  System.out.println("Policy Text : " + policy.getPolicyText());
  System.out.println("Policy Id : " + policy.getId());

 }

}





Previous                                                 Next                                                 Home

No comments:

Post a Comment