Search in sources :

Example 6 with Names

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;
}
Also used : Response(feign.Response) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) ArrayList(java.util.ArrayList) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException)

Example 7 with Names

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;
}
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 8 with Names

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");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 9 with Names

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;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID) Name(org.wso2.ballerinalang.compiler.util.Name) CompilerOptionName(org.ballerinalang.compiler.CompilerOptionName)

Example 10 with Names

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;
}
Also used : CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) Name(org.wso2.ballerinalang.compiler.util.Name) Name(org.wso2.ballerinalang.compiler.util.Name)

Aggregations

ArrayList (java.util.ArrayList)10 Query (javax.persistence.Query)8 TaskStatus (org.wso2.carbon.humantask.core.dao.TaskStatus)8 Test (org.testng.annotations.Test)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)5 Name (org.wso2.ballerinalang.compiler.util.Name)5 Map (java.util.Map)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)4 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)4 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)3 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)3 Response (feign.Response)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Arrays (java.util.Arrays)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2