Tuesday, 19 December 2017

OIM API - Code snippet to get IT Resource Key by Application Instance Name.

public String getItResourceKey(String applicationInstanceName) {
    String methodName = "getItResourceKey()";
    System.out.println(methodName + " :: Enter");
    String itrKey = null;
    ApplicationInstance appInst = null;
   
    //get application instance service
    ApplicationInstanceService appService = Platform.getService(ApplicationInstanceService.class);
   
    if(!applicationInstanceName.trim().isEmpty() && applicationInstanceName.trim() != null) {
        System.out.println(methodName + " :: Fetching IT Resource Key for application [" + applicationInstanceName + "]");
        try {
            appInst = appService.findApplicationInstanceByName(applicationInstanceName);
            itrKey = String.valueOf(appInst.getItResourceKey());

            System.out.println("itrKey :: " + itrKey);
        } catch (Exception e) {
            System.out.println(methodName + " :: Exception occured :" + e);
        }
    } else {
        System.out.println(methodName + " :: Appliction Instance Name is null or empty");
    }
    System.out.println(methodName + " :: Exit");
    return itrKey;
}

OIM API - Stand Alone Code to Provision Account to User.


To execute below code you have to add following jars in classpath:


  • commons-logging.jar
  • eclipselink.jar
  • jrf-api.jar
  • oimclient.jar
  • spring.jar
  • wlfullclient.jar

Stand Alone Code:


import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;
import oracle.iam.provisioning.api.ApplicationInstanceService;
import oracle.iam.provisioning.api.ProvisioningService;
import oracle.iam.provisioning.vo.Account;
import oracle.iam.provisioning.vo.AccountData;
import oracle.iam.provisioning.vo.ApplicationInstance;
import oracle.iam.provisioning.vo.FormField;
import oracle.iam.provisioning.vo.FormInfo;

public class ProvisionAccountToUser {
        OIMClient client = null;
         
        //identity self service details
        String username = "xelsysadm";
        String password = "<password>"; //xelsysadm password
        String t3url = "t3://<hostname>:<port>"; //OIM HostName and Port
        String authwl_location = "<location of authwl.conf file in your local machine>";
     
        public void getOIMConnection(){
            System.out.println("getOIMConnection() : Start");
            //set system properties
            System.setProperty("java.security.auth.login.config", authwl_location);
            System.setProperty("OIM.AppServerType", "wls");
            System.setProperty("APPSERVER_TYPE", "wls");

            Hashtable oimenv = new Hashtable();
            oimenv.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, "weblogic.jndi.WLInitialContextFactory");
            oimenv.put(OIMClient.JAVA_NAMING_PROVIDER_URL,t3url);
            client = new OIMClient(oimenv);
            try {
                client.login(username, password.toCharArray());
                System.out.println("Connected Successfully...");
            } catch (LoginException e) {
                e.printStackTrace();
            }
            System.out.println("getOIMConnection() : End");
        }
      
        public void provisionAccount(String appInsName, String userId) throws Exception{
            ProvisioningService service= client.getService(ProvisioningService.class);
            ApplicationInstance appInstance = null;
            String usrKey = "";
            try {
                  //get application instance object by application instance name
                  appInstance = getApplicationInstanceByName(appInsName);
              
                  //get user key by user login
                  usrKey = getUserKey(userId);
                
                  //prepare process form
                  FormInfo formInfo = appInstance.getAccountForm();
                  FormField f1 = new FormField();
                  f1.setName("Account Name");
                  f1.setDefaultValue(userId);
                  formInfo.addFormField(f1);
      
                  String formKey = String.valueOf(formInfo.getFormKey());
      
                  AccountData accountData = new AccountData(formKey, null, null);
                  Account account = new Account(appInstance, accountData);
              
                  //set Account type
                  account.setAccountType(Account.ACCOUNT_TYPE.Primary);
                  service.provision(usrKey, account);
                
                  System.out.println(appInsName + " Provisioned to " + userId);
                            
                }catch(Exception e) {
                    e.printStackTrace();
                }
        }

        public String getUserKey(String userLogin) throws Exception{
            String userKey = "" ;
            Set<String> retAttrs = new HashSet<String>();
            retAttrs.add(UserManagerConstants.AttributeName.USER_KEY.getId());

            SearchCriteria criteria1 = new SearchCriteria(UserManagerConstants.AttributeName.USER_LOGIN.getId(), userLogin, SearchCriteria.Operator.EQUAL);
            UserManager userService = client.getService(UserManager.class);
            List<User> users = userService.search(criteria1, retAttrs, null);

            if (!users.isEmpty()) {
                for (User user : users) {
                        userKey = user.getEntityId();
                    }
            }
            return userKey;
        }
      
        public ApplicationInstance getApplicationInstanceByName(String applicationInstanceName) throws Exception {
            ApplicationInstanceService service= client.getService(ApplicationInstanceService.class);
            ApplicationInstance appInstance = service.findApplicationInstanceByName(applicationInstanceName);
            return appInstance;
          }
        
        public static void main(String[] args) throws Exception {
            ProvisionAccountToUser provisionAccountToUser = new ProvisionAccountToUser();
            provisionAccountToUser.getOIMConnection();
          
            //e.g: provisionAccountToUser.provisionAccount("ServiceNow","Test1");
            provisionAccountToUser.provisionAccount("<Application Instance Name>","<User Login>");
        }
}


Happy Learning!!!

OIM API - Code snippet to get Application Instance from OIM for the given Application Instance Name.


Using Search Criteria:

public ApplicationInstance getApplicationInstance(String appIntName){
        final String logp = CN + "::
getApplicationInstance - ";
        LOGGER.debug(logp + "START");

        if(null == appIntName || appIntName.trim().length() == 0){
                LOGGER.error(logp + "Application Instance name is null or empty");
                return null;
        }
        appIntName = appIntName.trim();

        ApplicationInstance appInst = null;
        try{
            SearchCriteria criteria = new SearchCriteria(ApplicationInstance.APPINST_NAME, appIntName, SearchCriteria.Operator.EQUAL);
           
            //get application instance service
            ApplicationInstanceService appInstService = Platform.getService(ApplicationInstanceService.class);
            List<ApplicationInstance> appInstList = appInstService.findApplicationInstance(criteria, null);

            if(appInstList.size() == 0 || appInstList.size() > 1) {
                LOGGER.error(logp + "Improper number of ApplicationInstance found in OIM for Application Instance name " + appIntName + " - " + appInstList.size());
            }else{
                appInst = appInstList.get(0);


                LOGGER.info(logp + "Successfully obtained ApplicationInstance - " + appInst);
                LOGGER.info(logp + "Application Instance Key :: " + appInst.getApplicationInstanceKey());
                LOGGER.info(logp + "Display Name :: " + appInst.getDisplayName());
                LOGGER.info(logp + "Application Instance Name :: " + appInst.getApplicationInstanceName());
            }
        }catch(Exception e) {
                LOGGER.error(logp + "Exception while fetching ApplicationInstance for Application Instance name " + appIntName + " - " + e, e);
        }

        LOGGER.debug(logp + "END");
        return appInst;
 }


Without Search Criteria:

public ApplicationInstance getApplicationInstance(String applicationInstanceName) throws Exception{
        //get application instance service
        ApplicationInstanceService service= Platform.getService(ApplicationInstanceService.class);
      
        ApplicationInstance appInstance = service.findApplicationInstanceByName(applicationInstanceName);
       
        System.out.println("Display Name :: " + appInstance.getDisplayName());


        System.out.println("Application Instance Name :: " + appInstance.getApplicationInstanceName());


        System.out.println("Application Instance Key :: " + appInstance.getApplicationInstanceKey());
        return appInstance;
 }


Happy Learning!!!

OIM API - Code Snippet to Create Application Instance.

public void createApplicationInstance() throws Exception{
    ApplicationInstanceService appService = Platform.getService(ApplicationInstanceService.class);
  
    String applicationInstanceName = "TestDisconnectedApp";
   
    ApplicationInstance newAppInstance = new ApplicationInstance();
    //Set Application Instance Name
    newAppInstance.setApplicationInstanceName(applicationInstanceName);
    //Set Application Instance type(Set it as per requirement)
    newAppInstance.setType(ApplicationInstance.TYPE.Disconnected);
    //Set Application Instance Display Name
    newAppInstance.setDisplayName(applicationInstanceName);
    appService.addApplicationInstance(newAppInstance);
    System.out.println("Application Created Successfully :: " + applicationInstanceName);
}


Happy Learning!!!

OIM API - Stand Alone Code to Register/Unregister Plugin.


To execute below code you have to add following jars in classpath:


  • commons-logging.jar
  • eclipselink.jar
  • jrf-api.jar
  • oimclient.jar
  • spring.jar
  • wlfullclient.jar


Stand Alone Code:

 

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Hashtable;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.exception.NoSuchUserException;
import oracle.iam.identity.exception.SearchKeyNotUniqueException;
import oracle.iam.identity.exception.UserManagerException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authz.exception.AccessDeniedException;
import oracle.iam.platform.pluginframework.PluginException;
import oracle.iam.platformservice.api.PlatformService;
import oracle.iam.platformservice.exception.PlatformServiceAccessDeniedException;
import com.bea.common.security.xacml.IOException;


public class PluginUtility {
    static OIMClient oimClient = null;
       
    //identity self service details
    String username = "xelsysadm";
    String password = "<password>"; //xelsysadm password
    String t3url = "t3://<hostname>:<port>"; //OIM HostName and Port
    String authwl_location = "<location of authwl.conf file in your local machine>";
   
    public void getOIMConnection() {
        System.out.println("getOIMConnection() : Start");
        //set system properties
        System.setProperty("java.security.auth.login.config", authwl_location);
        System.setProperty("OIM.AppServerType", "wls");
        System.setProperty("APPSERVER_TYPE", "wls");

        Hashtable oimenv = new Hashtable();
        oimenv.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, "weblogic.jndi.WLInitialContextFactory");
        oimenv.put(OIMClient.JAVA_NAMING_PROVIDER_URL,t3url);
        oimClient = new OIMClient(oimenv);
        try {
            oimClient.login(username, password.toCharArray());
            System.out.println("Connected");
        } catch (LoginException e) {
            e.printStackTrace();
        }
        System.out.println("getOIMConnection() : End");
    }

    public void registerPlugin() throws Exception{
        System.out.println("registerPlugin() : Start");
        System.setProperty("java.security.policy","<location of xl.policy file in your local machine>"); //eg. D:\\designconsole\\config\\xl.policy"

        PlatformService service = oimClient.getService(PlatformService.class);
        System.out.println("Platforn service initialised..: "+service.toString());
      
        File zipFile = new File("<plugin file path>"); //e.g: D:\\plugin.zip
        System.out.println("File read from the system...");
        FileInputStream fis = new FileInputStream(zipFile);
        System.out.println("Input stream created...");
      
        int size = (int) zipFile.length();
        byte[] b = new byte[size];
        int bytesRead = fis.read(b, 0, size);
      
        while (bytesRead < size) {
            bytesRead += fis.read(b, bytesRead, size - bytesRead);
        }
        fis.close();
      
        System.out.println("Register plugin...");
        service.registerPlugin(b);      
   
        System.out.println("Plugin Registered Successfully");
        System.out.println("registerPlugin() : End");
    }
   
    public void unregisterPlugin() throws Exception{
        System.out.println("unregisterPlugin() : Start");
        System.setProperty("java.security.policy","<location of xl.policy file in your local machine>"); //eg. D:\\designconsole\\config\\xl.policy"

        PlatformService service = oimClient.getService(PlatformService.class);
        System.out.println("Platforn service initialised..: "+service.toString());
        System.out.println("Unregister plugin...");
      
        //e.g: service.unRegisterPlugin("com.plugin.example.OIMNotification", "1.0");  
        service.unRegisterPlugin("<full qualified class name>", "<version>");      

        System.out.println("Plugin Unregistered Successfully");
        System.out.println("unregisterPlugin() : End");
    }
   
    public static void main(String[] args) {
        try {
            PluginUtility pluginUtility = new PluginUtility();
          
            //get OIM handle
            pluginUtility.getOIMConnection();
          
            //register plugin
            pluginUtility.registerPlugin();
          
            //unregister plugin
            //pluginUtility.unregisterPlugin();
      
        } catch (Exception e) {
            e.printStackTrace();
        }                   
    }

 Happy Learning!!!