Search in sources :

Example 6 with ScopeListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO in project carbon-apimgt by wso2.

the class ScopesApiServiceImpl method getSharedScopes.

/**
 * Get all shared scopes for tenant.
 *
 * @param messageContext CXF Message Context
 * @return Shared Scopes DTO List
 * @throws APIManagementException if an error occurs while retrieving shared scope
 */
@Override
public Response getSharedScopes(Integer limit, Integer offset, MessageContext messageContext) throws APIManagementException {
    // pre-processing
    // setting default limit and offset values if they are not set
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    List<Scope> scopeList = apiProvider.getAllSharedScopes(tenantDomain);
    ScopeListDTO sharedScopeListDTO = SharedScopeMappingUtil.fromScopeListToDTO(scopeList, offset, limit);
    SharedScopeMappingUtil.setPaginationParams(sharedScopeListDTO, limit, offset, scopeList.size());
    return Response.ok().entity(sharedScopeListDTO).build();
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeListDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 7 with ScopeListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO in project carbon-apimgt by wso2.

the class SharedScopeMappingUtil method setPaginationParams.

/**
 * Sets pagination urls for a shared ScopeListDTO object given pagination parameters and url parameters.
 *
 * @param sharedScopeListDTO a ScopeListDTO object
 * @param limit              max number of objects returned
 * @param offset             starting index
 * @param size               max offset
 */
public static void setPaginationParams(ScopeListDTO sharedScopeListDTO, int limit, int offset, int size) {
    String paginatedPrevious = "";
    String paginatedNext = "";
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = RestApiCommonUtil.getScopesPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT));
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getScopesPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT));
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    sharedScopeListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)

Example 8 with ScopeListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO in project carbon-apimgt by wso2.

the class SharedScopeMappingUtil method fromScopeListToDTO.

/**
 * Converts a list of Scope objects into a SharedScopeListDTO.
 *
 * @param scopeList List of Scope objects
 * @param offset    max number of objects returned
 * @param limit     starting index
 * @return SharedScopeListDTO object
 */
public static ScopeListDTO fromScopeListToDTO(List<Scope> scopeList, int offset, int limit) {
    ScopeListDTO sharedScopeListDTO = new ScopeListDTO();
    List<ScopeDTO> scopeDTOList = sharedScopeListDTO.getList();
    if (scopeList == null) {
        scopeList = new ArrayList<>();
        sharedScopeListDTO.setList(scopeDTOList);
    }
    // identifying the proper start and end indexes
    int size = scopeList.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = Math.min(offset + limit - 1, size - 1);
    for (int i = start; i <= end; i++) {
        scopeDTOList.add(fromScopeToDTO(scopeList.get(i)));
    }
    sharedScopeListDTO.setCount(scopeDTOList.size());
    return sharedScopeListDTO;
}
Also used : ScopeListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeListDTO) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)

Example 9 with ScopeListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO in project carbon-apimgt by wso2.

the class SystemScopesMappingUtil method fromScopeListToScopeListDTO.

/**
 * Convert list of API Scope to ScopeListDTO
 *
 * @param scopeRoleMapping Map of a Role Scope  Mapping
 * @return ScopeListDTO list containing role scope mapping data
 */
public static ScopeListDTO fromScopeListToScopeListDTO(Map<String, String> scopeRoleMapping) throws APIManagementException {
    ScopeListDTO scopeListDTO = new ScopeListDTO();
    scopeListDTO.setCount(scopeRoleMapping.size());
    scopeListDTO.setList(fromRoleScopeMapToRoleScopeDTOList(scopeRoleMapping));
    return scopeListDTO;
}
Also used : ScopeListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO)

Example 10 with ScopeListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO in project carbon-apimgt by wso2.

the class SystemScopesMappingUtil method createJsonObjectOfScopeMapping.

/**
 * Extract scope and roles and create JSONObject
 *
 * @param body          body of a Role Scope  Mapping
 * @return JSONObject   role scope mapping data
 */
public static JSONObject createJsonObjectOfScopeMapping(ScopeListDTO body) throws APIManagementException {
    JSONObject responseJson = new JSONObject();
    JSONArray scopeJson = new JSONArray();
    for (ScopeDTO scope : body.getList()) {
        JSONObject scopeRoleJson = new JSONObject();
        String roles = scope.getRoles().toString().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "");
        if (!roles.isEmpty()) {
            scopeRoleJson.put("Name", scope.getName());
            scopeRoleJson.put("Roles", roles);
            scopeJson.put(scopeRoleJson);
        }
    }
    responseJson.put("Scope", scopeJson);
    return responseJson;
}
Also used : JSONObject(org.json.simple.JSONObject) ScopeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeDTO) JSONArray(org.json.JSONArray)

Aggregations

ScopeListDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeListDTO)3 ScopeListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.ScopeListDTO)3 HashMap (java.util.HashMap)2 JSONObject (org.json.simple.JSONObject)2 ScopeListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeListDTO)2 JSONArray (org.json.JSONArray)1 Test (org.testng.annotations.Test)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 Scope (org.wso2.carbon.apimgt.api.model.Scope)1 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 ScopeDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 ScopeList_listDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.ScopeList_listDTO)1 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)1 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)1