Search in sources :

Example 6 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getIdOfUser.

@Override
public String getIdOfUser(String userName) throws IdentityProviderException {
    // should not user id outside this domain and should not log that id.
    try {
        userName = userNameMapper.getLoggedInUserIDFromPseudoName(userName);
    } catch (APIManagementException e) {
        throw new IdentityProviderException(e.getMessage(), ExceptionCodes.USER_MAPPING_RETRIEVAL_FAILED);
    }
    Response userResponse = scimServiceStub.searchUsers(FILTER_PREFIX_USER + userName);
    String userId;
    if (userResponse == null) {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    if (userResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        String responseBody = userResponse.body().toString();
        JsonParser parser = new JsonParser();
        JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
        JsonArray user = (JsonArray) parsedResponseBody.get(RESOURCES);
        JsonObject scimUser = (JsonObject) user.get(0);
        userId = scimUser.get(ID).getAsString();
        String message = "Id " + userId + " of user " + scimUser.get(USERNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    } else {
        String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : " + getErrorMessage(userResponse);
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return userId;
}
Also used : Response(feign.Response) JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) JsonObject(com.google.gson.JsonObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) JsonParser(com.google.gson.JsonParser)

Example 7 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImpl method getRoleId.

@Override
public String getRoleId(String roleName) throws IdentityProviderException {
    Response roleResponse = scimServiceStub.searchGroups(FILTER_PREFIX_ROLE + roleName);
    String roleId;
    if (roleResponse == null) {
        String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : Response is null.";
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    if (roleResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        String responseBody = roleResponse.body().toString();
        JsonParser parser = new JsonParser();
        JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
        JsonArray role = (JsonArray) parsedResponseBody.get(RESOURCES);
        JsonObject scimGroup = (JsonObject) role.get(0);
        roleId = scimGroup.get(ID).getAsString();
        String message = "Id " + roleId + " of role " + scimGroup.get(GROUPNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
        if (log.isDebugEnabled()) {
            log.debug(message);
        }
    } else {
        String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : " + getErrorMessage(roleResponse);
        log.error(errorMessage);
        throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
    }
    return roleId;
}
Also used : Response(feign.Response) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) JsonParser(com.google.gson.JsonParser)

Example 8 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.

@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) ConfigContext(org.wso2.carbon.apimgt.core.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) Template(org.apache.velocity.Template)

Example 9 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.

@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) Template(org.apache.velocity.Template)

Example 10 with Resources

use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20TestCase method testAddNewScopeToExistingOauthSecurity.

@Test()
public void testAddNewScopeToExistingOauthSecurity() throws IOException, APIManagementException {
    APIDefinitionFromSwagger20 apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
    String filePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization.yaml";
    File file = Paths.get(filePath).toFile();
    String sampleApi = IOUtils.toString(new FileInputStream(file));
    Scope scope = new Scope();
    scope.setName("apim:api_delete");
    scope.setDescription("Delete API");
    String scopeAddedSwagger = apiDefinitionFromSwagger20.addScopeToSwaggerDefinition(sampleApi, scope);
    Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(scopeAddedSwagger);
    Assert.assertTrue(scopes.containsKey("apim:api_delete"));
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)49 Test (org.testng.annotations.Test)41 HashMap (java.util.HashMap)30 File (java.io.File)26 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)21 IOException (java.io.IOException)20 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 FileInputStream (java.io.FileInputStream)19 Map (java.util.Map)17 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)16 JSONObject (org.json.simple.JSONObject)15 Resource (org.wso2.carbon.registry.core.Resource)15 List (java.util.List)14 Scope (org.wso2.carbon.apimgt.core.models.Scope)14 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 API (org.wso2.carbon.apimgt.api.model.API)12 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)12 API (org.wso2.carbon.apimgt.core.models.API)12 Collection (org.wso2.carbon.registry.core.Collection)12