Oracle OIM - Create Organization using java API
In this blog I would like to explain how to create Organization in OIM using java APIs.
First we need to establish connection with OIM by passing url and credentials to oimClient. Once the connection is established, then initialize OrganizationManager. OrganizationManager gives us a way to manage organizations in OIM
Please refer to sample java code below.
import java.util.Hashtable;
import javax.security.auth.login.LoginException;
import oracle.adf.share.logging.ADFLogger;
import oracle.iam.identity.exception.AccessDeniedException;
import oracle.iam.identity.exception.OrganizationManagerException;
import oracle.iam.identity.exception.RoleCategorySearchException;
import oracle.iam.identity.orgmgmt.api.OrganizationManager;
import oracle.iam.identity.orgmgmt.vo.Organization;
import oracle.iam.platform.OIMClient;
public class CreateOrganization {
private static ADFLogger logger = ADFLogger.createADFLogger(CreateOrganization.class);
//OIM User name
public String oimUserName;
// OIM Password
public String oimPassword;
public static final String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public static final String WLS = "wls";
public static final String OIM_APP_SERVER_TYPE = "OIM.AppServerType";
public static final String APPSERVER_TYPE = "APPSERVER_TYPE";
//OIM URL to connect throught browser. here we use t3 protocal to communicate with server.
public static final String OIM_SERVER_URL = "t3://hotsname.domain.com:14000";
public static final String JAVA_SECURITY_AUTH_LOGIN_CONFIG = "java.security.auth.login.config";
/**
* Autherization configuration file location. In production environment we need
* read this file from weblogic location. Use weblogic location relative path instead
* of below value in that case
*/
public static final String SERVER_AUTH_CONF_FILE =
"D:\\MyPersonalDocuments\\Projects\\OIM_Objects_Creation\\propertyFile\\authwl.conf";
//OIMClient class to connect to OIM server
private OIMClient oimClient;
// OrganizationManager class helpful to create/update/delete organizations in OIM
public OrganizationManager orgManager;
public CreateOrganization() {
super();
}
/**
* I used this parameterized constructor to establish connection with OIM. Inside this constructor
* establishConnection method takes userName and password as parameter and establish connection
* with OIM.
* initialize() method will initialze OrganizationManager class.
* @param username
* @param password
*/
public CreateOrganization(String username, String password) {
super();
this.oimUserName = username;
this.oimPassword = password;
long startTime = System.currentTimeMillis();
logger.fine("CreateOrganization constructor Starts");
try {
//establishing OIM connection
establishConnection(oimUserName, oimPassword);
//initializing OrganizationManager class
initialize();
} catch (RoleCategorySearchException e) {
logger.severe("CreateOrganization constructor Exception: " + e.toString());
} catch (Throwable e) {
logger.severe("CreateOrganization constructor Exception: " + e.toString());
}
logger.fine("CreateOrganization constructor Ends");
logger.fine("Time taken for OIMServiceFacade constructor : " + (System.currentTimeMillis() - startTime));
}
/**
* This connect interacts with weblogic server where OIM deployed and establish connection with that.
* @param username
* @param password
* @throws RoleCategorySearchException
*/
private void establishConnection(String username, String password) throws RoleCategorySearchException {
long startTime = System.currentTimeMillis();
logger.fine("CreateOrganization establishConnection Starts");
logger.fine("CreateOrganization establishConnection Starts11");
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, INITIAL_CONTEXT_FACTORY);
System.setProperty(OIM_APP_SERVER_TYPE, WLS);
System.setProperty(APPSERVER_TYPE, WLS);
env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, OIM_SERVER_URL);
System.setProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG,
SERVER_AUTH_CONF_FILE); //Path of authwl.conf file according to the environment
oimClient = new oracle.iam.platform.OIMClient(env);
try {
logger.fine("CreateOrganization establishConnection: with password " + username + ":" + password);
logger.fine("Username : " + username);
logger.fine("password : " + password);
oimClient.login(oimUserName, oimPassword.toCharArray(), env);
logger.fine("CreateOrganization establishConnection: Connected to OIM");
} catch (LoginException e) {
logger.severe("CreateOrganization establishConnection exception :" + e.toString());
logger.severe("Time taken for establishConnection(exception): " +
(System.currentTimeMillis() - startTime));
return;
} catch (Throwable e) {
logger.severe("CreateOrganization establishConnection exception :" + e.toString());
logger.severe("Time taken for establishConnection(exception): " +
(System.currentTimeMillis() - startTime));
return;
}
logger.fine("CreateOrganization establishConnection Ends");
logger.fine("Time taken for establishConnection: " + (System.currentTimeMillis() - startTime));
}
/**
* This method initializes orgManager.
*/
private void initialize() {
logger.fine("CreateOrganization Initialize Starts");
try {
orgManager = oimClient.getService(OrganizationManager.class);
} catch (Throwable e) {
logger.severe("Unexpected exception occuredOIMServiceFacade constructor" + e.toString());
}
logger.fine("CreateOrganization Initialize Ends");
}
/**
* In this createOrganization method I am passing minum parameter it required to create
* Organization in OIM.
* parent_key: if we pass parent_key as null then this organization will be created at
* level. if we pass any other orgkey already available in OIM. Then this organization will
* be crated under parent_key organization.
*
* @param organizationName
* @param OrganizationType
* @return
* @throws OrganizationManagerException
* @throws AccessDeniedException
*/
public String createOrganization(String organizationName,
String OrganizationType) throws OrganizationManagerException,
AccessDeniedException {
//create organization accepts Organization object as parameter. So set all the required
//attributes to that class and pass to create method.
Organization organization = new Organization();
organization.setAttribute("Organization Name", organizationName);
organization.setAttribute("parent_key", null);
organization.setAttribute("Organization Customer Type", OrganizationType);
//After org creation it returns orgKey as return value. This is org key for newly
//created organization.
String orgKey = null;
try {
//call create service
orgKey = orgManager.create(organization);
} catch (OrganizationManagerException e) {
e.printStackTrace();
throw e;
} catch (AccessDeniedException e) {
e.printStackTrace();
throw e;
}
return orgKey;
}
/**
* I am testing above Create organization method using this main class.
*
* @param args
*/
public static void main(String[] args) {
CreateOrganization createOrganization = new CreateOrganization("username@abc.com", "password");
try {
createOrganization.createOrganization("CreateOrgForBlogInDev", "Company");
} catch (OrganizationManagerException e) {
e.printStackTrace();
}
}
}
First we need to establish connection with OIM by passing url and credentials to oimClient. Once the connection is established, then initialize OrganizationManager. OrganizationManager gives us a way to manage organizations in OIM
Please refer to sample java code below.
import java.util.Hashtable;
import javax.security.auth.login.LoginException;
import oracle.adf.share.logging.ADFLogger;
import oracle.iam.identity.exception.AccessDeniedException;
import oracle.iam.identity.exception.OrganizationManagerException;
import oracle.iam.identity.exception.RoleCategorySearchException;
import oracle.iam.identity.orgmgmt.api.OrganizationManager;
import oracle.iam.identity.orgmgmt.vo.Organization;
import oracle.iam.platform.OIMClient;
public class CreateOrganization {
private static ADFLogger logger = ADFLogger.createADFLogger(CreateOrganization.class);
//OIM User name
public String oimUserName;
// OIM Password
public String oimPassword;
public static final String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public static final String WLS = "wls";
public static final String OIM_APP_SERVER_TYPE = "OIM.AppServerType";
public static final String APPSERVER_TYPE = "APPSERVER_TYPE";
//OIM URL to connect throught browser. here we use t3 protocal to communicate with server.
public static final String OIM_SERVER_URL = "t3://hotsname.domain.com:14000";
public static final String JAVA_SECURITY_AUTH_LOGIN_CONFIG = "java.security.auth.login.config";
/**
* Autherization configuration file location. In production environment we need
* read this file from weblogic location. Use weblogic location relative path instead
* of below value in that case
*/
public static final String SERVER_AUTH_CONF_FILE =
"D:\\MyPersonalDocuments\\Projects\\OIM_Objects_Creation\\propertyFile\\authwl.conf";
//OIMClient class to connect to OIM server
private OIMClient oimClient;
// OrganizationManager class helpful to create/update/delete organizations in OIM
public OrganizationManager orgManager;
public CreateOrganization() {
super();
}
/**
* I used this parameterized constructor to establish connection with OIM. Inside this constructor
* establishConnection method takes userName and password as parameter and establish connection
* with OIM.
* initialize() method will initialze OrganizationManager class.
* @param username
* @param password
*/
public CreateOrganization(String username, String password) {
super();
this.oimUserName = username;
this.oimPassword = password;
long startTime = System.currentTimeMillis();
logger.fine("CreateOrganization constructor Starts");
try {
//establishing OIM connection
establishConnection(oimUserName, oimPassword);
//initializing OrganizationManager class
initialize();
} catch (RoleCategorySearchException e) {
logger.severe("CreateOrganization constructor Exception: " + e.toString());
} catch (Throwable e) {
logger.severe("CreateOrganization constructor Exception: " + e.toString());
}
logger.fine("CreateOrganization constructor Ends");
logger.fine("Time taken for OIMServiceFacade constructor : " + (System.currentTimeMillis() - startTime));
}
/**
* This connect interacts with weblogic server where OIM deployed and establish connection with that.
* @param username
* @param password
* @throws RoleCategorySearchException
*/
private void establishConnection(String username, String password) throws RoleCategorySearchException {
long startTime = System.currentTimeMillis();
logger.fine("CreateOrganization establishConnection Starts");
logger.fine("CreateOrganization establishConnection Starts11");
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, INITIAL_CONTEXT_FACTORY);
System.setProperty(OIM_APP_SERVER_TYPE, WLS);
System.setProperty(APPSERVER_TYPE, WLS);
env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, OIM_SERVER_URL);
System.setProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG,
SERVER_AUTH_CONF_FILE); //Path of authwl.conf file according to the environment
oimClient = new oracle.iam.platform.OIMClient(env);
try {
logger.fine("CreateOrganization establishConnection: with password " + username + ":" + password);
logger.fine("Username : " + username);
logger.fine("password : " + password);
oimClient.login(oimUserName, oimPassword.toCharArray(), env);
logger.fine("CreateOrganization establishConnection: Connected to OIM");
} catch (LoginException e) {
logger.severe("CreateOrganization establishConnection exception :" + e.toString());
logger.severe("Time taken for establishConnection(exception): " +
(System.currentTimeMillis() - startTime));
return;
} catch (Throwable e) {
logger.severe("CreateOrganization establishConnection exception :" + e.toString());
logger.severe("Time taken for establishConnection(exception): " +
(System.currentTimeMillis() - startTime));
return;
}
logger.fine("CreateOrganization establishConnection Ends");
logger.fine("Time taken for establishConnection: " + (System.currentTimeMillis() - startTime));
}
/**
* This method initializes orgManager.
*/
private void initialize() {
logger.fine("CreateOrganization Initialize Starts");
try {
orgManager = oimClient.getService(OrganizationManager.class);
} catch (Throwable e) {
logger.severe("Unexpected exception occuredOIMServiceFacade constructor" + e.toString());
}
logger.fine("CreateOrganization Initialize Ends");
}
/**
* In this createOrganization method I am passing minum parameter it required to create
* Organization in OIM.
* parent_key: if we pass parent_key as null then this organization will be created at
* level. if we pass any other orgkey already available in OIM. Then this organization will
* be crated under parent_key organization.
*
* @param organizationName
* @param OrganizationType
* @return
* @throws OrganizationManagerException
* @throws AccessDeniedException
*/
public String createOrganization(String organizationName,
String OrganizationType) throws OrganizationManagerException,
AccessDeniedException {
//create organization accepts Organization object as parameter. So set all the required
//attributes to that class and pass to create method.
Organization organization = new Organization();
organization.setAttribute("Organization Name", organizationName);
organization.setAttribute("parent_key", null);
organization.setAttribute("Organization Customer Type", OrganizationType);
//After org creation it returns orgKey as return value. This is org key for newly
//created organization.
String orgKey = null;
try {
//call create service
orgKey = orgManager.create(organization);
} catch (OrganizationManagerException e) {
e.printStackTrace();
throw e;
} catch (AccessDeniedException e) {
e.printStackTrace();
throw e;
}
return orgKey;
}
/**
* I am testing above Create organization method using this main class.
*
* @param args
*/
public static void main(String[] args) {
CreateOrganization createOrganization = new CreateOrganization("username@abc.com", "password");
try {
createOrganization.createOrganization("CreateOrgForBlogInDev", "Company");
} catch (OrganizationManagerException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment