Using OIM API and Groovy
Many times we have to write code using OIM API to interact with the system.
The OIM documentation has a chapter on this, but there are several steps to follow(a number of jars have to be copied, a project will have to be created) before being able to write and run the code. Here I am describing another approach on how one can do the same and use Groovy for writing the actual code.
Also this approach has a preliminary step. You have to create in your $OIM_HOME/server/apps/oim.ear/xlWebApp.war folder a groovy.jsp file with the following content:
<%@ page contentType="text/html;UTF-8" import="java.util.HashMap, java.util.Map, java.nio.file.Files, java.nio.file.Paths, groovy.lang.*"%>
<html>
<head>
<title>Groovy console</title>
<style type="text/css" media="screen">
#editor { position: absolute; width: 1000px; height: 580px; }
#submit { position: absolute; left:950px; top: 600px; }
#result { position: absolute; top: 630px; }
</style>
</head>
<body>
<% String code = (request.getParameter("code") == null) ? "" : request.getParameter("code");%>
<div id="editor"><% if (!"".equals(code)) out.print(code); %></div>
<div id="submit"><button onClick="submitForm()">Run</div>
<%
GroovyShell shell = new GroovyShell();
Script script = null;
String result = "";
try { script = shell.parse(code);
result = script.run().toString();
} catch(Exception ex) {
java.io.StringWriter errors = new java.io.StringWriter();
ex.printStackTrace(new java.io.PrintWriter(errors));
result = errors.toString();
} %>
<div id="result"><pre><%= code != "" ? result : "" %></pre></div>
<form name='codeRunner' method='POST'>
<input name='code' type='hidden'>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.3/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/groovy");
editor.setShowPrintMargin(false);
function submitForm() {
document.forms["codeRunner"]["code"].value = editor.getValue();
document.forms["codeRunner"].submit();
}
</script>
</body>
</html>
This page will create a console on the OIM server where Groovy code, that calls OIM APIs, can be run. In order to access the console first you have to authenticate on the server by accessing:
http://OIM_HOST:14000/identity/
then in the same browser, in order to preserve the session
http://OIM_HOST:14000/xlWebApp/groovy.jsp
In the console that appears you can write the code, then by pressing the Run button at the bottom of the console this will be executed directly on the server. The result of the run, or in case there are any errors, these are displayed below the console.
Below is some sample code to create 10 users and 10 roles:
Using Groovy the code is more intuitive and one has to write much less lines of code. Enjoy!
The OIM documentation has a chapter on this, but there are several steps to follow(a number of jars have to be copied, a project will have to be created) before being able to write and run the code. Here I am describing another approach on how one can do the same and use Groovy for writing the actual code.
Also this approach has a preliminary step. You have to create in your $OIM_HOME/server/apps/oim.ear/xlWebApp.war folder a groovy.jsp file with the following content:
<%@ page contentType="text/html;UTF-8" import="java.util.HashMap, java.util.Map, java.nio.file.Files, java.nio.file.Paths, groovy.lang.*"%>
<html>
<head>
<title>Groovy console</title>
<style type="text/css" media="screen">
#editor { position: absolute; width: 1000px; height: 580px; }
#submit { position: absolute; left:950px; top: 600px; }
#result { position: absolute; top: 630px; }
</style>
</head>
<body>
<% String code = (request.getParameter("code") == null) ? "" : request.getParameter("code");%>
<div id="editor"><% if (!"".equals(code)) out.print(code); %></div>
<div id="submit"><button onClick="submitForm()">Run</div>
<%
GroovyShell shell = new GroovyShell();
Script script = null;
String result = "";
try { script = shell.parse(code);
result = script.run().toString();
} catch(Exception ex) {
java.io.StringWriter errors = new java.io.StringWriter();
ex.printStackTrace(new java.io.PrintWriter(errors));
result = errors.toString();
} %>
<div id="result"><pre><%= code != "" ? result : "" %></pre></div>
<form name='codeRunner' method='POST'>
<input name='code' type='hidden'>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.3/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/groovy");
editor.setShowPrintMargin(false);
function submitForm() {
document.forms["codeRunner"]["code"].value = editor.getValue();
document.forms["codeRunner"].submit();
}
</script>
</body>
</html>
This page will create a console on the OIM server where Groovy code, that calls OIM APIs, can be run. In order to access the console first you have to authenticate on the server by accessing:
http://OIM_HOST:14000/identity/
then in the same browser, in order to preserve the session
http://OIM_HOST:14000/xlWebApp/groovy.jsp
In the console that appears you can write the code, then by pressing the Run button at the bottom of the console this will be executed directly on the server. The result of the run, or in case there are any errors, these are displayed below the console.
Below is some sample code to create 10 users and 10 roles:
import oracle.iam.platform.Platform;
import oracle.iam.identity.rolemgmt.api.RoleManager;
import oracle.iam.identity.rolemgmt.api.RoleManagerConstants;
import oracle.iam.identity.rolemgmt.vo.Role;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.identity.usermgmt.vo.User;
//obtaining the service
roleService = Platform.getService(RoleManager.class);
userService = Platform.getService(UserManager.class);
//calling the API
roleName = 'role'
userName = 'user'
for (i in 1..10) {
createRole(roleService, roleName + i)
createUser(userService, userName + i, userName + i, 1L)
}
return "Succes!"
//create role helper method, populating just the manadatory fields
def createRole(service, name) {
map = new HashMap()
map.put(RoleManagerConstants.ROLE_NAME, name);
map.put(RoleManagerConstants.ROLE_DISPLAY_NAME, name);
role = new Role(map);
service.create(role);
}
//create user helper method, populating just the manadatory fields
def createUser(service, userLogin, lastName, orgKey) {
User user = new User("");
user.setAttribute(UserManagerConstants.AttributeName.USER_LOGIN.getId(), userLogin);
user.setAttribute(UserManagerConstants.AttributeName.LASTNAME.getId(), lastName);
user.setAttribute(UserManagerConstants.AttributeName.PASSWORD.getId(), "Welcome1");
user.setAttribute(UserManagerConstants.AttributeName.USER_ORGANIZATION.getId(), orgKey);
user.setAttribute(UserManagerConstants.AttributeName.EMPTYPE.getId(), "Full-Time");
service.create(user);
}
Using Groovy the code is more intuitive and one has to write much less lines of code. Enjoy!
Comments
Post a Comment