use of org.json.simple.JSONArray in project carbon-apimgt by wso2.
the class APIPublisherImpl method replaceGroupNamesWithId.
/**
* This method replaces the groupId field's value to the role id instead of the name passed by the user
*
* @param permissionString - the permission json string which contains role names in groupId field
* @return permission string with replaced groupId
* @throws ParseException - if there is an error parsing the json string
* @throws APIManagementException - if there is an error getting the IdentityProvider instance
*/
private String replaceGroupNamesWithId(String permissionString) throws ParseException, APIManagementException {
JSONArray updatedPermissionArray = new JSONArray();
JSONParser jsonParser = new JSONParser();
JSONArray originalPermissionArray = (JSONArray) jsonParser.parse(permissionString);
try {
for (Object permissionObj : originalPermissionArray) {
JSONObject jsonObject = (JSONObject) permissionObj;
String groupName = (String) jsonObject.get(APIMgtConstants.Permission.GROUP_ID);
String groupId = getIdentityProvider().getRoleId(groupName);
JSONObject updatedPermissionJsonObj = new JSONObject();
updatedPermissionJsonObj.put(APIMgtConstants.Permission.GROUP_ID, groupId);
updatedPermissionJsonObj.put(APIMgtConstants.Permission.PERMISSION, jsonObject.get(APIMgtConstants.Permission.PERMISSION));
updatedPermissionArray.add(updatedPermissionJsonObj);
}
} catch (IdentityProviderException e) {
String errorMessage = "There are invalid roles in the permission string";
log.error(errorMessage, e);
throw new APIManagementException(errorMessage, e, ExceptionCodes.UNSUPPORTED_ROLE);
}
return updatedPermissionArray.toJSONString();
}
use of org.json.simple.JSONArray in project carbon-apimgt by wso2.
the class APIPublisherImpl method getAPIPermissionArray.
/**
* This method will return map with role names and its permission values.
*
* @param permissionJsonString Permission json object a string
* @return Map of permission values.
* @throws ParseException If failed to parse the json string.
*/
private HashMap<String, Integer> getAPIPermissionArray(String permissionJsonString) throws ParseException, APIManagementException {
HashMap<String, Integer> rolePermissionList = new HashMap<String, Integer>();
JSONParser jsonParser = new JSONParser();
JSONArray baseJsonArray = (JSONArray) jsonParser.parse(permissionJsonString);
for (Object aBaseJsonArray : baseJsonArray) {
JSONObject jsonObject = (JSONObject) aBaseJsonArray;
String groupId = jsonObject.get(APIMgtConstants.Permission.GROUP_ID).toString();
JSONArray subJsonArray = (JSONArray) jsonObject.get(APIMgtConstants.Permission.PERMISSION);
int totalPermissionValue = 0;
for (Object aSubJsonArray : subJsonArray) {
if (APIMgtConstants.Permission.READ.equals(aSubJsonArray.toString().trim())) {
totalPermissionValue += APIMgtConstants.Permission.READ_PERMISSION;
} else if (APIMgtConstants.Permission.UPDATE.equals(aSubJsonArray.toString().trim())) {
totalPermissionValue += APIMgtConstants.Permission.UPDATE_PERMISSION;
} else if (APIMgtConstants.Permission.DELETE.equals(aSubJsonArray.toString().trim())) {
totalPermissionValue += APIMgtConstants.Permission.DELETE_PERMISSION;
} else if (APIMgtConstants.Permission.MANAGE_SUBSCRIPTION.equals(aSubJsonArray.toString().trim())) {
totalPermissionValue += APIMgtConstants.Permission.MANAGE_SUBSCRIPTION_PERMISSION;
}
}
rolePermissionList.put(groupId, totalPermissionValue);
}
return rolePermissionList;
}
use of org.json.simple.JSONArray in project carbon-apimgt by wso2.
the class APIPublisherImpl method replaceGroupIdWithName.
/**
* This method replaces the groupId field's value of the api permissions string to the role name before sending to
* frontend
*
* @param permissionString - permissions string containing role ids in the groupId field
* @return the permission string replacing the groupId field's value to role name
* @throws ParseException - if there is an error parsing the permission json
* @throws APIManagementException - if there is an error getting the IdentityProvider instance
*/
private String replaceGroupIdWithName(String permissionString) throws ParseException, APIManagementException {
JSONArray updatedPermissionArray = new JSONArray();
JSONParser jsonParser = new JSONParser();
JSONArray originalPermissionArray = (JSONArray) jsonParser.parse(permissionString);
for (Object permissionObj : originalPermissionArray) {
JSONObject jsonObject = (JSONObject) permissionObj;
String groupId = (String) jsonObject.get(APIMgtConstants.Permission.GROUP_ID);
try {
String groupName = getIdentityProvider().getRoleName(groupId);
JSONObject updatedPermissionJsonObj = new JSONObject();
updatedPermissionJsonObj.put(APIMgtConstants.Permission.GROUP_ID, groupName);
updatedPermissionJsonObj.put(APIMgtConstants.Permission.PERMISSION, jsonObject.get(APIMgtConstants.Permission.PERMISSION));
updatedPermissionArray.add(updatedPermissionJsonObj);
} catch (IdentityProviderException e) {
// lets the execution continue after logging the exception
String errorMessage = "Error occurred while calling SCIM endpoint to retrieve role name of role " + "with Id " + groupId;
log.warn(errorMessage, e);
}
}
return updatedPermissionArray.toJSONString();
}
use of org.json.simple.JSONArray in project elastic-core-maven by OrdinaryDude.
the class EventListener method onTimeout.
/**
* Async operation timeout (AsyncListener interface)
*
* @param event Async event
*/
@Override
public void onTimeout(AsyncEvent event) {
AsyncContext context = event.getAsyncContext();
lock.lock();
try {
pendingWaits.remove(context);
JSONObject response = new JSONObject();
response.put("events", new JSONArray());
response.put("requestProcessingTime", System.currentTimeMillis() - timestamp);
try (Writer writer = context.getResponse().getWriter()) {
response.writeJSONString(writer);
} catch (IOException exc) {
Logger.logDebugMessage(String.format("Unable to return API response to %s: %s", address, exc.toString()));
}
context.complete();
timestamp = System.currentTimeMillis();
} finally {
lock.unlock();
}
}
use of org.json.simple.JSONArray in project elastic-core-maven by OrdinaryDude.
the class EventWait method formatResponse.
/**
* Format the EventWait response
*
* @param events Event list
* @return JSON stream
*/
static JSONObject formatResponse(List<PendingEvent> events) {
JSONArray eventsJSON = new JSONArray();
events.forEach(event -> {
JSONArray idsJSON = new JSONArray();
if (event.isList())
idsJSON.addAll(event.getIdList());
else
idsJSON.add(event.getId());
JSONObject eventJSON = new JSONObject();
eventJSON.put("name", event.getName());
eventJSON.put("ids", idsJSON);
eventsJSON.add(eventJSON);
});
JSONObject response = new JSONObject();
response.put("events", eventsJSON);
return response;
}
Aggregations