Friday, May 20, 2016

OIM SCIM Java Client

OIM SCIM Java Client.

/*Jar needed - commons-codec-1.10.jar & json-20140107.jar

Steps to run belwo code
      Update Host, Port & password and then call Create User method
      Once user is created in OIM then get the user key from DB and pass it to Update User method.
*/

package com.test;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONObject;

public class SCIMClient {

static String authStringEnc = "";
static String scimURL = "";
public static void main(String[] args) throws Exception {
        String name = "xelsysadm";
        String password = "password"; // Update Password
        scimURL = "http://host:port/idaas/im/scim/v1"; // Update Host, Port
     
        String authString = name + ":" + password;
        authStringEnc = "Basic " + new String(Base64.encodeBase64(authString.getBytes()));
        System.out.println("Base64 encoded auth string: " + authStringEnc);
     
       createUser();
       //updateUser("144"); // Change the user key '144' with actual one.

}

public static void createUser() throws Exception{
    JSONObject obj = workerJSON();
    boolean flag = processHTTPCall(scimURL + "/Users", "POST", obj);
        if (flag)
        System.out.println("User created successfully!");
}

public static void updateUser(String userKey) throws Exception{
        JSONObject obj = workerJSON();
        boolean flag = processHTTPCall(scimURL +"/Users/"+ userKey, "PUT", obj);
        if (flag)
        System.out.println("User updated successfully!");
}

public static boolean processHTTPCall(String httpUrl, String method, JSONObject obj) throws Exception{
boolean flag = false;

        HttpURLConnection connection = null;
        URL url = new URL(httpUrl);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod(method);
        connection.setRequestProperty("Content-Type","application/scim+json");
        connection.setRequestProperty("Authorization", authStringEnc);
        connection.setConnectTimeout(50000);
        connection.setDoOutput(true);
        connection.setReadTimeout(50000);
        OutputStreamWriter out =
            new OutputStreamWriter(connection.getOutputStream());
        out.write(obj.toString());
        out.close();

        System.out.println("Response code from server is ::" +connection.getResponseCode());
        System.out.println("Response Message from server is ::" +connection.getResponseMessage());
     
        if(connection.getResponseCode() < 300){
        System.out.println("HTTP Call was successful!");
        flag = true;
        }else{
        System.out.println("Error Encountered during HTTP Call");
        }
     
        return flag;
}

// get the JSON object by running curl command and build exact format for create/update. It supports single, multiple, full attributes update.
public static JSONObject workerJSON(){
        JSONObject workerObj = new JSONObject();

        JSONArray schemas = new JSONArray();
        schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User");
        schemas.put("urn:ietf:params:scim:schemas:core:2.0:User");
        schemas.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User");
        schemas.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User");
        workerObj.put("schemas", schemas);
     
        JSONObject userOIGObj = new JSONObject();
        userOIGObj.put("Department", "Security");

        JSONObject homeValue = new JSONObject();
        homeValue.put("value", "1");
        homeValue.put("$ref", scimURL + "/Organizations/1");

        userOIGObj.put("homeOrganization", homeValue);

        workerObj.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User", userOIGObj);
     

        workerObj.put("displayName", "John peter");
     
        JSONArray email = new JSONArray();
        JSONObject mailObj = new JSONObject();
        mailObj.put("value", "john.peter3@test.com");
        mailObj.put("type", "work");
        email.put(mailObj);
        workerObj.put("emails", email);
     
        workerObj.put("organization", "Xellerate Users");
     
        JSONObject userEntObj = new JSONObject();
        userEntObj.put("employeeNumber", "12345");
     
        JSONObject mgrObj = new JSONObject();
        mgrObj.put("value", "1");
        userEntObj.put("manager", mgrObj);
     
        workerObj.put("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User", userEntObj);
     
   
        JSONArray contactNumber = new JSONArray();
     
        JSONObject mobileNumber = new JSONObject();
        mobileNumber.put("value", "123-456-7999");
        mobileNumber.put("type", "mobile");
        contactNumber.put(mobileNumber);
     
        JSONObject workNumber = new JSONObject();
        workNumber.put("value", "123-456-7888");
        workNumber.put("type", "work");
        contactNumber.put(workNumber);

        workerObj.put("phoneNumbers", contactNumber);
     

        JSONObject nameObj = new JSONObject();
        nameObj.put("middleName", "M");
        nameObj.put("familyName", "John");
        nameObj.put("givenName", "Peter");
        workerObj.put("name", nameObj);


        JSONObject addrObj = new JSONObject();
        JSONArray homeAddr = new JSONArray();
        addrObj.put("region", "AMER");
        addrObj.put("country", "US");
        addrObj.put("type", "work");
        homeAddr.put(addrObj);
        workerObj.put("addresses", homeAddr);

        workerObj.put("title", "Lead Developer");
     
        System.out.println("Jason object is --->" + workerObj);
return workerObj;
}
}

/*
To get sample JSON object using CURL [https://curl.haxx.se/] in CMD
      curl --user xelsysadm:password http://host:port/idaas/im/scim/v1/Users/1
*/

No comments:

Post a Comment