Source:
public static void updateTenantConfRoleAliasMapping(JSONObject newRoleMappingJson, String username) throws APIManagementException { String tenantDomain = MultitenantUtils.getTenantDomain(username); JsonObject existingTenantConfObject = new JsonObject(); try { APIMRegistryService apimRegistryService = new APIMRegistryServiceImpl(); String existingTenantConf = apimRegistryService.getConfigRegistryResourceContent(tenantDomain, APIConstants.API_TENANT_CONF_LOCATION); existingTenantConfObject = new JsonParser().parse(existingTenantConf).getAsJsonObject(); } catch (RegistryException e) { APIUtil.handleException("Couldn't read tenant configuration from tenant registry", e); } catch (UserStoreException e) { APIUtil.handleException("Couldn't read tenant configuration from User Store", e); } Set<Map.Entry<String, JsonElement>> roleMappingEntries = newRoleMappingJson.entrySet(); for (Map.Entry<String, JsonElement> entry: roleMappingEntries) { List<String> currentRoles = Arrays.asList(String.valueOf(entry.getValue()).split(",")); boolean isOriginalRoleAlreadyInroles = false; for (String role: currentRoles) { if (role.equals(entry.getKey())) { isOriginalRoleAlreadyInroles = true; break; } } if (!isOriginalRoleAlreadyInroles) { String newRoles = entry.getKey() + "," + entry.getValue(); newRoleMappingJson.replace(entry.getKey(), entry.getValue(), newRoles); } } existingTenantConfObject.remove(APIConstants.REST_API_ROLE_MAPPINGS_CONFIG); JsonElement jsonElement = new JsonParser().parse(String.valueOf(newRoleMappingJson)); existingTenantConfObject.add(APIConstants.REST_API_ROLE_MAPPINGS_CONFIG, jsonElement); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String formattedTenantConf = gson.toJson(existingTenantConfObject); APIUtil.updateTenantConf(formattedTenantConf, tenantDomain); if (log.isDebugEnabled()) { log.debug("Finalized tenant-conf.json: " + formattedTenantConf); } }

Target:
public static void updateTenantConfRoleAliasMapping(JSONObject newRoleMappingJson, String username) throws APIManagementException { String tenantDomain = MultitenantUtils.getTenantDomain(username); JsonObject existingTenantConfObject = new JsonObject(); try { APIMRegistryService apimRegistryService = new APIMRegistryServiceImpl(); String existingTenantConf = apimRegistryService.getConfigRegistryResourceContent(tenantDomain, APIConstants.API_TENANT_CONF_LOCATION); existingTenantConfObject = new JsonParser().parse(existingTenantConf).getAsJsonObject(); } catch (RegistryException e) { APIUtil.handleException("Couldn't read tenant configuration from tenant registry", e); } catch (UserStoreException e) { APIUtil.handleException("Couldn't read tenant configuration from User Store", e); } Set<Map.Entry<String, JsonElement>> roleMappingEntries = newRoleMappingJson.entrySet(); for (Map.Entry<String, JsonElement> entry: roleMappingEntries) { List<String> currentRoles = Arrays.asList(String.valueOf(entry.getValue()).split(",")); boolean isOriginalRoleAlreadyInRoles = false; for (String role: currentRoles) { if (role.equals(entry.getKey())) { isOriginalRoleAlreadyInRoles = true; break; } } if (!isOriginalRoleAlreadyInRoles) { String newRoles = entry.getKey() + "," + entry.getValue(); newRoleMappingJson.replace(entry.getKey(), entry.getValue(), newRoles); } } existingTenantConfObject.remove(APIConstants.REST_API_ROLE_MAPPINGS_CONFIG); JsonElement jsonElement = new JsonParser().parse(String.valueOf(newRoleMappingJson)); existingTenantConfObject.add(APIConstants.REST_API_ROLE_MAPPINGS_CONFIG, jsonElement); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String formattedTenantConf = gson.toJson(existingTenantConfObject); APIUtil.updateTenantConf(formattedTenantConf, tenantDomain); if (log.isDebugEnabled()) { log.debug("Finalized tenant-conf.json: " + formattedTenantConf); } }