Search in sources :

Example 86 with JsonArray

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();
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 87 with JsonArray

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;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint)

Example 88 with JsonArray

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();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 89 with JsonArray

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();
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) Writer(java.io.Writer)

Example 90 with JsonArray

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;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6