Create User in OIM through SCIM/Rest Services-
-----------------------------------------------------------------------------------
Required Jar File-
java-json.jar
sun.misc.BASE64Decoder.jar
apache-httpcomponents-httpcore.jar
apache-jakarta-commons-httpclient.jar
commons-codec-1.9.jar
commons-logging.jar
commons-httpclient-3.1.jar
=================================================
package oracle.iam.webservice;
public class OIMUserManagementBySCIM
{
/*
Operation-1: Create User Method
*/
public void createUser()
{
try
{
//1. Define URL
java.net.URL url = null;
//2. Define Authorization
System.out.println(" Passing Credential ");
String name = "xelsysadm";
String password = "password";
String authString = name + ":" + password;
String authStringEnc = "Basic "+ new String(new Decoder.BASE64Encoder().encode(authString.getBytes()));
//System.out.println("Base64 encoded auth string: " + authStringEnc);
//3. Creating object.
org.json.JSONObject request_body = new org.json.JSONObject();
org.json.JSONArray request_body_schemas= new org.json.JSONArray();
org.json.JSONObject request_body_nameAttribute = new org.json.JSONObject();
org.json.JSONArray request_body_email= new org.json.JSONArray();
org.json.JSONObject request_body_emailValue = new org.json.JSONObject();
org.json.JSONObject request_body_ManagerValue = new org.json.JSONObject();
org.json.JSONObject request_body_manager= new org.json.JSONObject();
org.json.JSONObject request_body_organizationValue = new org.json.JSONObject();
org.json.JSONObject request_body_organizationName= new org.json.JSONObject();
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:core:2.0:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User");
request_body.put("schemas",request_body_schemas);
//*******Basic Attributes********
//obj.put("userName","");
request_body_nameAttribute.put("familyName","Testrws03");
request_body_nameAttribute.put("givenName","Userrws03");
request_body_nameAttribute.put("middleName","turws03");
request_body.put("name",request_body_nameAttribute);
request_body.put("displayName","Testrws03 Userrws03");
request_body.put("profileUrl","http://oimhost:14000/userName");
request_body_emailValue.put("value","testrws01ervice03@test.com");
request_body_emailValue.put("type","work");
request_body_email.put(request_body_emailValue);
request_body.put("emails",request_body_email);
request_body.put("userType","Associate");
request_body.put("title","Supervisor");
request_body.put("employeeNumber","1100001");
request_body.put("locale","en-US");
request_body.put("timezone","America/Los_Angeles");
request_body_ManagerValue.put("value", "13324");
request_body_manager.put("manager",request_body_ManagerValue);
request_body.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",request_body_manager);
request_body_organizationValue.put("value","6");
request_body_organizationValue.put("$ref", "http://oimhost:14000/idaas/im/scim/v1/Organizations/6");
request_body_organizationName.put("homeOrganization", request_body_organizationValue);
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",request_body_organizationName);
//********Custom Attributes********
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:EmployeeStatus","Active");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:EmailNonProd","testrws03@gmail.com");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:Level","30");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:Division","Corporate");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:SW1_Category","S");
System.out.println("JSON object is ::"+request_body);
//5. parameters for connection
url = new java.net.URL("http://oimhost:14000/idaas/im/scim/v1/Users");
java.net.HttpURLConnection connection = null;
connection = (java.net.HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); // For Creation put POST Operation
connection.setRequestProperty("Content-Type", "application/scim+json");
connection.setRequestProperty("Authorization", authStringEnc);
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.setReadTimeout(5000);
//7. Sending Data in OIM
java.io.OutputStreamWriter out=null;
out = new java.io.OutputStreamWriter(connection.getOutputStream());
out.write(request_body.toString());
out.close();
//7. Getting output
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(connection.getInputStream()));
System.out.println("Buffered input is"+in);
System.out.println("Response code from server is ::"+connection.getResponseCode());
System.out.println("*****User created successfully******");
String line ="";
while ((line = in.readLine()) != null)
{
System.out.println("Result is ::"+line);
}
in.close();
}
catch (java.net.ProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (java.net.MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (java.io.IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (org.json.JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[])
{
OIMUserManagementBySCIM usermgmt=new OIMUserManagementBySCIM();
usermgmt.createUser();
}
}
-----------------------------------------------------------------------------------
Required Jar File-
java-json.jar
sun.misc.BASE64Decoder.jar
apache-httpcomponents-httpcore.jar
apache-jakarta-commons-httpclient.jar
commons-codec-1.9.jar
commons-logging.jar
commons-httpclient-3.1.jar
=================================================
package oracle.iam.webservice;
public class OIMUserManagementBySCIM
{
/*
Operation-1: Create User Method
*/
public void createUser()
{
try
{
//1. Define URL
java.net.URL url = null;
//2. Define Authorization
System.out.println(" Passing Credential ");
String name = "xelsysadm";
String password = "password";
String authString = name + ":" + password;
String authStringEnc = "Basic "+ new String(new Decoder.BASE64Encoder().encode(authString.getBytes()));
//System.out.println("Base64 encoded auth string: " + authStringEnc);
//3. Creating object.
org.json.JSONObject request_body = new org.json.JSONObject();
org.json.JSONArray request_body_schemas= new org.json.JSONArray();
org.json.JSONObject request_body_nameAttribute = new org.json.JSONObject();
org.json.JSONArray request_body_email= new org.json.JSONArray();
org.json.JSONObject request_body_emailValue = new org.json.JSONObject();
org.json.JSONObject request_body_ManagerValue = new org.json.JSONObject();
org.json.JSONObject request_body_manager= new org.json.JSONObject();
org.json.JSONObject request_body_organizationValue = new org.json.JSONObject();
org.json.JSONObject request_body_organizationName= new org.json.JSONObject();
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:core:2.0:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User");
request_body_schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User");
request_body.put("schemas",request_body_schemas);
//*******Basic Attributes********
//obj.put("userName","");
request_body_nameAttribute.put("familyName","Testrws03");
request_body_nameAttribute.put("givenName","Userrws03");
request_body_nameAttribute.put("middleName","turws03");
request_body.put("name",request_body_nameAttribute);
request_body.put("displayName","Testrws03 Userrws03");
request_body.put("profileUrl","http://oimhost:14000/userName");
request_body_emailValue.put("value","testrws01ervice03@test.com");
request_body_emailValue.put("type","work");
request_body_email.put(request_body_emailValue);
request_body.put("emails",request_body_email);
request_body.put("userType","Associate");
request_body.put("title","Supervisor");
request_body.put("employeeNumber","1100001");
request_body.put("locale","en-US");
request_body.put("timezone","America/Los_Angeles");
request_body_ManagerValue.put("value", "13324");
request_body_manager.put("manager",request_body_ManagerValue);
request_body.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",request_body_manager);
request_body_organizationValue.put("value","6");
request_body_organizationValue.put("$ref", "http://oimhost:14000/idaas/im/scim/v1/Organizations/6");
request_body_organizationName.put("homeOrganization", request_body_organizationValue);
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",request_body_organizationName);
//********Custom Attributes********
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:EmployeeStatus","Active");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:EmailNonProd","testrws03@gmail.com");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:Level","30");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:Division","Corporate");
request_body.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User:SW1_Category","S");
System.out.println("JSON object is ::"+request_body);
//5. parameters for connection
url = new java.net.URL("http://oimhost:14000/idaas/im/scim/v1/Users");
java.net.HttpURLConnection connection = null;
connection = (java.net.HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); // For Creation put POST Operation
connection.setRequestProperty("Content-Type", "application/scim+json");
connection.setRequestProperty("Authorization", authStringEnc);
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.setReadTimeout(5000);
//7. Sending Data in OIM
java.io.OutputStreamWriter out=null;
out = new java.io.OutputStreamWriter(connection.getOutputStream());
out.write(request_body.toString());
out.close();
//7. Getting output
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(connection.getInputStream()));
System.out.println("Buffered input is"+in);
System.out.println("Response code from server is ::"+connection.getResponseCode());
System.out.println("*****User created successfully******");
String line ="";
while ((line = in.readLine()) != null)
{
System.out.println("Result is ::"+line);
}
in.close();
}
catch (java.net.ProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (java.net.MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (java.io.IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (org.json.JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[])
{
OIMUserManagementBySCIM usermgmt=new OIMUserManagementBySCIM();
usermgmt.createUser();
}
}
No comments:
Post a Comment