API calls examples in Java

Deleting a user

Note: user ID can be found under id in the SearchUser.java output, e.g. – id":"ff808081910c2cb801910c32380e0006")

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONObject;
import static org.example.FetchToken.getToken;
public class DeleteUser {
public static void main(String[] args) {
try {
String url = "http://your-openiam-instance/webconsole/rest/api/prov/removeUser";
// [1] Create the JSON payload
JSONObject payload = new JSONObject();
payload.put("userId", "user-id-to-be-deleted");
// [2] Create the connection
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + getToken());
con.setDoOutput(true);
// [3] Send the request
OutputStream os = con.getOutputStream();
os.write(payload.toString().getBytes("UTF-8"));
os.close();
// [4] Read the response
Scanner in = new Scanner(con.getInputStream());
StringBuilder response = new StringBuilder();
while (in.hasNext()) {
response.append(in.nextLine());
}
in.close();
// [5] Print the response
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
{"primaryKey":null,"status":200,"errorList":null,"redirectURL":"users","successToken":{"message":"USER_REMOVED","params":null},"successMessage":"User was removed successfully","contextValues":null,"stackTrace":null,"possibleErrors":null,"error":false}

Creating a role

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONObject;
import static org.example.FetchToken.getToken;
public class CreateRole {
public static void main(String[] args) {
try {
String url = "http://your-openiam-instance/webconsole/rest/api/roles/save";
// [1] Create the JSON payload
JSONObject targetObject = new JSONObject();
targetObject.put("mdTypeId", "ACCESS_ROLE");
targetObject.put("name", "role-name");
targetObject.put("managerSysId", "0");
JSONObject payload = new JSONObject();
payload.put("targetObject", targetObject);
// [2] Create the connection
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + getToken());
con.setDoOutput(true);
// [3] Send the request
OutputStream os = con.getOutputStream();
os.write(payload.toString().getBytes("UTF-8"));
os.close();
// [4] Read the response
Scanner in = new Scanner(con.getInputStream());
StringBuilder response = new StringBuilder();
while (in.hasNext()) {
response.append(in.nextLine());
}
in.close();
// [5] Print the response
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Example output is given below.

{"primaryKey":"ff8080819133afc9019133cca26f000c","status":200,"errorList":null,"redirectURL":"editRole?id=ff8080819133afc9019133cca26f000c","successToken":{"message":"ROLE_SAVED","params":null},"successMessage":"Role was saved successfully","contextValues":null,"stackTrace":null,"possibleErrors":null,"error":false}

Deleting a role

Note: role ID can be found under primaryKey in the output for CreateRole.java, e.g. – primaryKey":"ff8080819133afc9019133cca26f000c". Replace the roleid in the URL with this value.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import static org.example.FetchToken.getToken;
public class DeleteRole {
public static void main(String[] args) {
try {
String url = "http://your-openiam-instance/webconsole/rest/api/roles/roleid";
// [1] Create the connection
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("DELETE");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + getToken());
// [2] Read in the response
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// [3] Print the response
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output example is given below.

{"primaryKey":null,"status":200,"errorList":null,"redirectURL":"roles","successToken":{"message":"ROLE_DELETE","params":null},"successMessage":"Role was deleted successfully","contextValues":null,"stackTrace":null,"possibleErrors":null,"error":false}