Showing posts with label Security. Show all posts
Showing posts with label Security. Show all posts

Tuesday, 24 April 2018

Java Code to get security on Object Stores

//Java Code to get security on Object Stores

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ObjectStoreSet;
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 GetObjectStoreSecurity {
    private static Connection conn = null;
   
    static String GranteeName =null;
    static String GranteeType =null;
    static String PermissionSource =null;
    static String accessType =null;
    static String InheritableDepth =null;
    static String AccessMask=null;
    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localhost:9080/wsi/FNCEWS40MTOM/";
            String userName ="user Name";
            String password ="password";
            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 ArrayList FetchOSList() {
        ArrayList<String> list = new ArrayList();
        try {
           
            //String[] oslist= new String[100];
            Connection conn= getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn, null, null);
            ObjectStoreSet osSet= domain.get_ObjectStores();
            ObjectStore store;
            Iterator iterator = osSet.iterator();
            Iterator<ObjectStore> osIter = iterator;
            while (osIter.hasNext()) {
            store = (ObjectStore)osIter.next();
            list.add(store.get_Name());
            System.out.println(store.get_Name());
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return list;
    }
    public static void getObjectStorePermissions(String osName) throws IOException{
       
        try{
                   
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
           
            System.out.println("ObjectStoreDisplayName :: "+objStore.get_DisplayName());
            System.out.println("ObjectStoreSymbolicName :: "+objStore.get_SymbolicName());
            System.out.println("ObjectStoreName :: "+objStore.get_Name());           
            System.out.println("ObjectStoreID :: "+objStore.get_Id());           
            System.out.println("ObjectStoreCreator :: "+objStore.get_Creator());
            System.out.println("ObjectStoreLastModifier :: "+objStore.get_LocaleName());           
            System.out.println("ObjectStoreDatabaseSchemaName :: "+objStore.get_DatabaseSchemaName());           
            System.out.println("ObjectStoreJNDIDataSource :: "+objStore.get_JNDIDataSource());           
            System.out.println("ObjectStore.get_JNDIXADataSource() :: "+objStore.get_JNDIXADataSource());           
            System.out.println("ObjectStoreDescriptiveText :: "+objStore.get_DescriptiveText());           
            System.out.println("ObjectStoreSchemaVersion :: "+objStore.get_SchemaVersion());           
            System.out.println("ObjectStoreTextSearchIndexingLanguage :: "+objStore.get_TextSearchIndexingLanguage());           
            System.out.println("ObjectStoreVerityPartitionProperty :: "+objStore.get_VerityPartitionProperty());           
            System.out.println("ObjectStoreClassName :: "+objStore.getClassName());
                       
             AccessPermissionList apos = objStore.get_Permissions();
             Iterator it = apos.iterator();
             while (it.hasNext())
             {
                 AccessPermission permission = (AccessPermission)it.next();
                   
                    GranteeName=permission.get_GranteeName();
                    GranteeType=permission.get_GranteeType().toString();
                    PermissionSource=permission.get_PermissionSource().toString();
                    accessType=permission.get_AccessType().toString();
                    InheritableDepth=permission.get_InheritableDepth().toString();       
                    AccessMask=permission.get_AccessMask().toString();
                    System.out.println(objStore.get_DisplayName());
                    System.out.println("GranteeName="+ GranteeName);
                    System.out.println("GranteeType=" + GranteeType);
                    System.out.println("PermissionSource=" +PermissionSource);       
                    System.out.println("Accesstype =" +accessType);
                    System.out.println("Inheritabledepth=" +InheritableDepth);
             }
           
            System.out.println("-------------End Object Store--------------------------");
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args) throws IOException {
        //getObjectStorePermissions("DMS");
   
        List objectStoreList = FetchOSList();
       
        String objectStore;
       
        Iterator it = objectStoreList.iterator();
       
        while(it.hasNext())
        {
            objectStore=(String) it.next();
            System.out.println("Object Store :: "+objectStore);
            getObjectStorePermissions(objectStore);
        }
       
        //    FetchOSList();
/*
         InheritableDepth
         0 - No inheritance (this object only).
        1 - This object and immediate children only.
        -1 - This object and all children (infinite levels deep).
        -2 - All children (infinite levels deep) but not this object.
        -3 - Immediate children only; not this object.
 
 */
    }
}

Java Code to get security on FileNet Domain

//Java Code to get security on FileNet Domain


import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetDomainSecurity {
private static Connection conn = null;

static String GranteeName =null;
static String GranteeType =null;
static String PermissionSource =null;
static String accessType =null;
static String InheritableDepth =null;
static String AccessMask=null;

public static Connection getCEConn()
{
try {
String ceURI="localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="userName";
String password ="password";
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 getObjectStorePermissions() throws IOException{

try{
  conn=null;
      conn = getCEConn();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
AccessPermissionList apld = domain.get_Permissions();
Iterator itd = apld.iterator();

while (itd.hasNext())
{
AccessPermission permission = (AccessPermission)itd.next();

GranteeName=permission.get_GranteeName();
GranteeType=permission.get_GranteeType().toString();
PermissionSource=permission.get_PermissionSource().toString();
accessType=permission.get_AccessType().toString();
InheritableDepth=permission.get_InheritableDepth().toString();
AccessMask=permission.get_AccessMask().toString();
System.out.println(domain.get_Name());
System.out.println("GranteeName="+ GranteeName);
System.out.println("GranteeType=" + GranteeType);
System.out.println("PermissionSource=" +PermissionSource);
System.out.println("Accesstype =" +accessType);
System.out.println("Inheritabledepth=" +InheritableDepth);

}
       
}
catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) throws IOException {


getObjectStorePermissions();
        
       
        }
}

Friday, 26 May 2017

Sample JAVA code to retrieve Realm, Group names and User names in FileNet (FileNet Content Engine API)

//Sample JAVA code to retrieve Realm, Group names and User names in FileNet

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.GroupSet;
import com.filenet.api.collection.UserSet;
import com.filenet.api.constants.PrincipalSearchAttribute;
import com.filenet.api.constants.PrincipalSearchSortType;
import com.filenet.api.constants.PrincipalSearchType;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Factory;
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 ="user";
            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 getRealm(String osName) throws IOException{
       
        try{
            Connection conn = getCEConn();
            com.filenet.api.core.EntireNetwork entireNetwork= Factory.EntireNetwork.fetchInstance(conn, null);
            com.filenet.api.security.Realm realm= entireNetwork.get_MyRealm();String realmName= realm.get_Name();
            System.out.println(realmName+ " is retrieved");
           
            //retrieve user names
            String pattern ="username";
            UserSet users = realm.findUsers(pattern, PrincipalSearchType.PREFIX_MATCH,PrincipalSearchAttribute.SHORT_NAME,PrincipalSearchSortType.NONE,Integer.valueOf("50"), null);
            com.filenet.api.security.User user;
            Iterator it = users.iterator();
            while (it.hasNext())
            {
            user = (com.filenet.api.security.User)it.next();
            System.out.println("distinguishedname =" +user.get_DistinguishedName());
            System.out.println("shortname =" + user.get_ShortName());
            System.out.println("name=" + user.get_Name());
            System.out.println("displayname =" + user.get_DisplayName());
            }
           
            //retrieve group names
            String pattern1="groupName";
            GroupSet groups = realm.findGroups(pattern1,PrincipalSearchType.PREFIX_MATCH,PrincipalSearchAttribute.SHORT_NAME,PrincipalSearchSortType.NONE,Integer.valueOf("50"), null);
            com.filenet.api.security.Group group;
            Iterator groupIt= groups.iterator();
            while (groupIt.hasNext())
            {
            group=(com.filenet.api.security.Group)groupIt.next();
            System.out.println("name=" + group.get_Name());
            System.out.println("Distinguishedname = " + group.get_DistinguishedName());
            System.out.println("shortname =" + group.get_ShortName());
            //System.out.println("Users =" + group.get_Users());
            }
           
            System.out.println("Done");


        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        getRealm("OSNAME");
    }
}

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");
    }
}

Sample code to get object permissions (FileNet Content Engine API)

//Sample code to get object 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.IndependentObjectSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Document;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
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 getObjectPermissions(String osName) throws IOException{
       
        try{
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
            SearchScope searchScope = new SearchScope(objStore);
            int count=0;
            String sqlStr = "Select * FROM docClass where DocumentTitle='doc123.pdf'";
            SearchSQL searchSQL = new SearchSQL(sqlStr);
            System.out.println("Query ::"+sqlStr);
            IndependentObjectSet independentObjectSet = searchScope.fetchObjects(searchSQL, new Integer(10), null, new Boolean(true));
            if(!(independentObjectSet.isEmpty())){
                Iterator it=independentObjectSet.iterator();
               
                while(it.hasNext())    {
                    Document document=(Document)it.next();
                    AccessPermissionList permissions= document.get_Permissions();
                    Iterator it1 = permissions.iterator();
                    while (it1.hasNext())
                    {
                    AccessPermission permission = (AccessPermission)it1.next();
                    System.out.println("GranteeName="+ permission.get_GranteeName());
                    System.out.println("GranteeType=" + permission.get_GranteeType().toString());
                    System.out.println("PermissionSource=" +permission.get_PermissionSource().toString());
                    //System.out.println("Accesslevel = " + AccessLevel.getInstanceFromInt(permission.get_AccessMask().intValue()).toString());
                    System.out.println("Accesstype =" +permission.get_AccessType().toString());
                    System.out.println("Inheritabledepth=" +permission.get_InheritableDepth());
                    }
                count++;
                System.out.println("Count:::"+count);
                }
             System.out.println("Count:::;"+count);
             System.out.println("Done");
                }

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        getObjectPermissions("OSNAME");
    }
}