use of org.wso2.ballerinalang.compiler.util.Names in project carbon-apimgt by wso2.
the class DefaultIdentityProviderImpl method getRoleNamesOfUser.
@Override
public List<String> getRoleNamesOfUser(String userId) throws IdentityProviderException {
List<String> roleNames = new ArrayList<>();
Response response = scimServiceStub.getUser(userId);
if (response == null) {
String errorMessage = "Error occurred while retrieving user with Id " + userId + ". Error : Response is null.";
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
try {
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
SCIMUser scimUser = (SCIMUser) new GsonDecoder().decode(response, SCIMUser.class);
if (scimUser != null) {
List<SCIMUser.SCIMUserGroups> roles = scimUser.getGroups();
if (roles != null) {
roles.forEach(role -> roleNames.add(role.getDisplay()));
String message = "Role names of user " + scimUser.getName() + " are successfully retrieved as " + StringUtils.join(roleNames, ", ") + ".";
if (log.isDebugEnabled()) {
log.debug(message);
}
}
} else {
String errorMessage = "Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.";
log.error(errorMessage);
throw new IdentityProviderException("Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.", ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
} else {
String errorMessage = "Error occurred while retrieving role names of user with Id " + userId + ". Error : " + getErrorMessage(response);
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
} catch (IOException e) {
String errorMessage = "Error occurred while parsing response from SCIM endpoint.";
log.error(errorMessage);
throw new IdentityProviderException("Error occurred while parsing response from SCIM endpoint for ", e, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
return roleNames;
}
use of org.wso2.ballerinalang.compiler.util.Names 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.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class HTTPSessionEssentialMethodsTest method testGetAttributeNamesFunction.
@Test(description = "Test for getAttributeNames function")
public void testGetAttributeNamesFunction() {
HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/sample2/names", "GET");
HTTPCarbonMessage response = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, cMsg);
Assert.assertNotNull(response);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
Assert.assertNotNull(responseMsgPayload);
Assert.assertEquals(responseMsgPayload, "arraysize:2");
}
use of org.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class WorkspaceUtils method prepareCompilerContext.
private static CompilerContext prepareCompilerContext(String fileName, String source) {
CompilerContext context = new CompilerContext();
List<Name> names = new ArrayList<>();
names.add(new Name("."));
// Registering custom PackageRepository to provide ballerina content without a file in file-system
context.put(PackageRepository.class, new InMemoryPackageRepository(new PackageID(Names.ANON_ORG, names, new Name("0.0.0")), "", fileName, source.getBytes(StandardCharsets.UTF_8)));
return context;
}
use of org.wso2.ballerinalang.compiler.util.Names in project ballerina by ballerina-lang.
the class ParserUtils method prepareCompilerContext.
/**
* Returns a CompilerContext for the provided fileName and Ballerina source content.
*
* @param fileName - File name. This can be any arbitrary name as as we haven't save the file yet.
* @param source - Ballerina source content that needs to be parsed.
* @return CompilerContext
*/
private static CompilerContext prepareCompilerContext(String fileName, String source) {
CompilerContext context = new CompilerContext();
List<Name> names = new ArrayList<>();
names.add(new org.wso2.ballerinalang.compiler.util.Name("."));
// Registering custom PackageRepository to provide ballerina content without a file in file-system
context.put(PackageRepository.class, new InMemoryPackageRepository(PackageID.DEFAULT, "", fileName, source.getBytes(StandardCharsets.UTF_8)));
return context;
}
Aggregations