Friday, 26 May 2017

Sample Java code to set folder permissions (FileNet Content Engine API)

//Sample code to set folder permissions

import java.io.IOException;
import java.util.Iterator;

import javax.security.auth.Subject;

import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.FolderSet;
import com.filenet.api.constants.AccessLevel;
import com.filenet.api.constants.AccessType;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;
public class CEUtil {
    private static Connection conn = null;

    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localhost:9080/wsi/FNCEWS40MTOM/";
            String userName ="username";
            String password ="pwd";
            if(conn==null){
            conn = Factory.Connection.getConnection(ceURI);
            Subject subject = UserContext.createSubject(conn, userName, password, null);
            UserContext uc = UserContext.get();
            uc.pushSubject(subject);
            }

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("CE Connection"+conn);
        return conn;
    }
    public static void setPermissions(String osName) throws IOException{
       
        try{
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
            String folder= "/Test";
           
            com.filenet.api.core.Folder folderOj= Factory.Folder.fetchInstance(objStore,folder, null);
            System.out.println("Folder :: "+folderOj.get_Name());
   
            AccessPermission permission = Factory.AccessPermission.createInstance();
            permission.set_GranteeName("User");
            permission.set_AccessType(AccessType.ALLOW);
            permission.set_InheritableDepth(new Integer(-1));
            permission.set_AccessMask(new Integer(AccessLevel.FULL_CONTROL_FOLDER_AS_INT));
           
            AccessPermissionList permissions = folderOj.get_Permissions();
            permissions.add(permission);
            folderOj.set_Permissions(permissions);
            folderOj.save(RefreshMode.REFRESH);   
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        setPermissions("OSNAME");
    }
}

No comments:

Post a Comment