Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#isScopeKeyExist()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#isScopeKeyExist() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScopesApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the given scope already used in APIs.
 *
 * @param name           Base64 URL encoded form of scope name -Base64URLEncode{scope name}
 * @param messageContext
 * @return boolean to indicate existence
 */
@Override
public Response validateScope(String name, MessageContext messageContext) {

    boolean isScopeExist = false;
    String scopeName = new String(Base64.getUrlDecoder().decode(name));
    if (!APIUtil.isWhiteListedScope(scopeName)) {
        try {
            APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
            String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
            isScopeExist =
                    apiProvider.isScopeKeyExist(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain));
        } catch (APIManagementException e) {
            RestApiUtil.handleInternalServerError("Error occurred while checking scope name", e, log);
        }
    }

    if (isScopeExist) {
        return Response.status(Response.Status.OK).build();
    } else {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
 
Example 2
Source File: ScopesApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Add Shared Scope.
 *
 * @param body           Scope DTO object to add
 * @param messageContext CXF Message Context
 * @return Created Scope as DTO
 * @throws APIManagementException If an error occurs while adding shared scope.
 */
@Override
public Response addSharedScope(ScopeDTO body, MessageContext messageContext) throws APIManagementException {

    String scopeName = body.getName();
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String tenantDomain = RestApiUtil.getLoggedInUserTenantDomain();
        if (StringUtils.isEmpty(scopeName)) {
            throw new APIManagementException("Shared Scope Name cannot be null or empty",
                    ExceptionCodes.SHARED_SCOPE_NAME_NOT_SPECIFIED);
        }
        if (StringUtils.isEmpty(body.getDisplayName())) {
            throw new APIManagementException("Shared scope Display Name cannot be null or empty",
                    ExceptionCodes.SHARED_SCOPE_DISPLAY_NAME_NOT_SPECIFIED);
        }
        if (apiProvider.isScopeKeyExist(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain))) {
            throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.SCOPE_ALREADY_REGISTERED,
                    scopeName));
        }
        Scope scopeToAdd = SharedScopeMappingUtil.fromDTOToScope(body);
        String sharedScopeId = apiProvider.addSharedScope(scopeToAdd, tenantDomain);
        //Get registered shared scope
        Scope createdScope = apiProvider.getSharedScopeByUUID(sharedScopeId, tenantDomain);
        ScopeDTO createdScopeDTO = SharedScopeMappingUtil.fromScopeToDTO(createdScope);
        String createdScopeURIString = RestApiConstants.RESOURCE_PATH_SHARED_SCOPES_SCOPE_ID
                .replace(RestApiConstants.SHARED_SCOPE_ID_PARAM, createdScopeDTO.getId());
        URI createdScopeURI = new URI(createdScopeURIString);
        return Response.created(createdScopeURI).entity(createdScopeDTO).build();
    } catch (URISyntaxException e) {
        throw new APIManagementException("Error while creating shared scope: " + scopeName, e);
    }
}