Search in sources :

Example 6 with ScopeDTO

use of org.wso2.carbon.apimgt.impl.dto.ScopeDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method getAPIScopesFromScopeDTOs.

/**
 * Convert ScopeDTO List to APIScopesDTO List adding the attribute 'isShared'.
 *
 * @param scopeDTOS ScopeDTO List
 * @return APIScopeDTO List
 * @throws APIManagementException if an error occurs while converting ScopeDTOs to APIScopeDTOs
 */
private static List<APIScopeDTO> getAPIScopesFromScopeDTOs(List<ScopeDTO> scopeDTOS, APIProvider apiProvider) throws APIManagementException {
    List<APIScopeDTO> apiScopeDTOS = new ArrayList<>();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    Set<String> allSharedScopeKeys = apiProvider.getAllSharedScopeKeys(tenantDomain);
    scopeDTOS.forEach(scopeDTO -> {
        APIScopeDTO apiScopeDTO = new APIScopeDTO();
        apiScopeDTO.setScope(scopeDTO);
        apiScopeDTO.setShared(allSharedScopeKeys.contains(scopeDTO.getName()) ? Boolean.TRUE : Boolean.FALSE);
        apiScopeDTOS.add(apiScopeDTO);
    });
    return apiScopeDTOS;
}
Also used : APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) ArrayList(java.util.ArrayList)

Example 7 with ScopeDTO

use of org.wso2.carbon.apimgt.impl.dto.ScopeDTO in project carbon-apimgt by wso2.

the class ScopesApiServiceImpl method getSharedScope.

/**
 * Get shared scope by Id.
 *
 * @param scopeId        UUID of the scope
 * @param messageContext CXF Message Context
 * @return Shared Scope DTO
 * @throws APIManagementException If an error occurs while getting shared scope
 */
@Override
public Response getSharedScope(String scopeId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    if (StringUtils.isEmpty(scopeId)) {
        throw new APIManagementException("Scope Id cannot be null or empty", ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
    }
    Scope scope = apiProvider.getSharedScopeByUUID(scopeId, tenantDomain);
    ScopeDTO scopeDTO = SharedScopeMappingUtil.fromScopeToDTO(scope);
    return Response.ok().entity(scopeDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 8 with ScopeDTO

use of org.wso2.carbon.apimgt.impl.dto.ScopeDTO in project carbon-apimgt by wso2.

the class ScopesApiServiceImpl method addSharedScope.

/**
 * 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 = RestApiCommonUtil.getLoggedInUserProvider();
        String tenantDomain = RestApiCommonUtil.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);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 9 with ScopeDTO

use of org.wso2.carbon.apimgt.impl.dto.ScopeDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method getScopeInfoDTO.

public static List<ScopeInfoDTO> getScopeInfoDTO(Set<Scope> scopes) {
    List<ScopeInfoDTO> scopeDto = new ArrayList<ScopeInfoDTO>();
    for (Scope scope : scopes) {
        ScopeInfoDTO scopeInfoDTO = new ScopeInfoDTO();
        scopeInfoDTO.setKey(scope.getKey());
        scopeInfoDTO.setName(scope.getName());
        scopeInfoDTO.setDescription(scope.getDescription());
        if (StringUtils.isNotBlank(scope.getRoles())) {
            scopeInfoDTO.setRoles(Arrays.asList(scope.getRoles().trim().split(",")));
        }
        scopeDto.add(scopeInfoDTO);
    }
    return scopeDto;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ScopeInfoDTO) ArrayList(java.util.ArrayList)

Example 10 with ScopeDTO

use of org.wso2.carbon.apimgt.impl.dto.ScopeDTO in project carbon-apimgt by wso2.

the class SystemScopesMappingUtil method fromRoleScopeMapToRoleScopeDTOList.

/**
 * Converts api scope-role mapping to RoleScopeDTO List.
 *
 * @param scopeRoleMapping Map of a Role Scope  Mapping
 * @return RoleScopeDTO list
 */
private static List<ScopeDTO> fromRoleScopeMapToRoleScopeDTOList(Map<String, String> scopeRoleMapping) throws APIManagementException {
    List<ScopeDTO> scopeDTOs = new ArrayList<>(scopeRoleMapping.size());
    if (portalScopeList.isEmpty()) {
        synchronized (lock) {
            if (portalScopeList.isEmpty()) {
                portalScopeList = RestApiUtil.getScopesInfoFromAPIYamlDefinitions();
            }
        }
    }
    for (Map.Entry<String, List<String>> mapping : portalScopeList.entrySet()) {
        // openid scope doesn't need a role mapping
        if (APIConstants.OPEN_ID_SCOPE_NAME.equals(mapping.getKey())) {
            continue;
        }
        if (scopeRoleMapping.containsKey(mapping.getKey())) {
            ScopeDTO roleScopeDTO = new ScopeDTO();
            roleScopeDTO.setName(mapping.getKey());
            String roles = scopeRoleMapping.get(mapping.getKey());
            List<String> roleList = new ArrayList<String>(Arrays.asList((roles.replaceAll("\\s+", "")).split(",")));
            roleScopeDTO.setRoles(roleList);
            roleScopeDTO.setDescription(mapping.getValue().get(0));
            roleScopeDTO.setTag(mapping.getValue().get(1));
            scopeDTOs.add(roleScopeDTO);
        } else {
            log.warn("The scope " + mapping.getKey() + " does not exist in tenant.conf");
        }
    }
    return scopeDTOs;
}
Also used : ScopeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeDTO) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Scope (org.wso2.carbon.apimgt.api.model.Scope)11 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)11 ArrayList (java.util.ArrayList)10 APIScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)8 HashMap (java.util.HashMap)7 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 Scope (org.wso2.carbon.apimgt.core.models.Scope)4 JSONObject (org.json.simple.JSONObject)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)2 CORSConfiguration (org.wso2.carbon.apimgt.api.model.CORSConfiguration)2