Search in sources :

Example 11 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method setPaginationParams.

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

Example 12 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAllAPIs.

@Override
public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String sortOrder, String xWSO2Tenant, String query, String ifNoneMatch, String accept, MessageContext messageContext) {
    List<API> allMatchedApis = new ArrayList<>();
    Object apiListDTO;
    // 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;
    query = query == null ? "" : query;
    sortBy = sortBy != null ? sortBy : RestApiConstants.DEFAULT_SORT_CRITERION;
    sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DESCENDING_SORT_ORDER;
    try {
        // revert content search back to normal search by name to avoid doc result complexity and to comply with REST api practices
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":")) {
            query = query.replace(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":", APIConstants.NAME_TYPE_PREFIX + ":");
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // boolean migrationMode = Boolean.getBoolean(RestApiConstants.MIGRATION_MODE);
        /*if (migrationMode) { // migration flow
                if (!StringUtils.isEmpty(targetTenantDomain)) {
                    tenantDomain = targetTenantDomain;
                }
                RestApiUtil.handleMigrationSpecificPermissionViolations(tenantDomain, username);
            }*/
        Map<String, Object> result;
        result = apiProvider.searchPaginatedAPIs(query, organization, offset, limit, sortBy, sortOrder);
        Set<API> apis = (Set<API>) result.get("apis");
        allMatchedApis.addAll(apis);
        apiListDTO = APIMappingUtil.fromAPIListToDTO(allMatchedApis);
        // Add pagination section in the response
        Object totalLength = result.get("length");
        Integer length = 0;
        if (totalLength != null) {
            length = (Integer) totalLength;
        }
        APIMappingUtil.setPaginationParams(apiListDTO, query, offset, limit, length);
        if (APIConstants.APPLICATION_GZIP.equals(accept)) {
            try {
                File zippedResponse = GZIPUtils.constructZippedResponse(apiListDTO);
                return Response.ok().entity(zippedResponse).header("Content-Disposition", "attachment").header("Content-Encoding", "gzip").build();
            } catch (APIManagementException e) {
                RestApiUtil.handleInternalServerError(e.getMessage(), e, log);
            }
        } else {
            return Response.ok().entity(apiListDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving APIs";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Set(java.util.Set) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) JSONObject(org.json.simple.JSONObject) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 13 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

public Response apisGet(String context, String version, String tenantDomain, MessageContext messageContext) {
    tenantDomain = GatewayUtils.validateTenantDomain(tenantDomain, messageContext);
    SubscriptionDataStore subscriptionDataStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (subscriptionDataStore == null) {
        log.warn("Subscription data store is not initialized for " + tenantDomain);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    if (StringUtils.isNotEmpty(context) && StringUtils.isNotEmpty(version)) {
        API api = subscriptionDataStore.getApiByContextAndVersion(context, version);
        if (api == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        APIListDTO apiListDTO = GatewayUtils.generateAPIListDTO(Collections.singletonList(api));
        return Response.ok().entity(apiListDTO).build();
    } else if ((StringUtils.isEmpty(context) && StringUtils.isNotEmpty(version)) || (StringUtils.isNotEmpty(context) && StringUtils.isEmpty(version))) {
        return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorDTO().moreInfo("required parameters " + "are missing")).build();
    } else {
        List<API> apiList = subscriptionDataStore.getAPIs();
        APIListDTO apiListDTO = GatewayUtils.generateAPIListDTO(apiList);
        return Response.status(Response.Status.OK).entity(apiListDTO).build();
    }
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.ErrorDTO) APIListDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) List(java.util.List) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)

Example 14 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method fromAPIListToDTO.

/**
 * Converts a List object of APIs into a DTO
 *
 * @param apiList List of APIs
 * @return APIListDTO object containing APIDTOs
 * @throws APIManagementException
 */
public static APIListDTO fromAPIListToDTO(List<Object> apiList, String organization) throws APIManagementException {
    APIListDTO apiListDTO = new APIListDTO();
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
    Map<String, Tier> tierMap = APIUtil.getTiers(organization);
    List<APIInfoDTO> apiInfoDTOs = apiListDTO.getList();
    if (apiList != null) {
        for (Object api : apiList) {
            APIInfoDTO apiInfoDTO = null;
            if (api instanceof API) {
                API api1 = (API) api;
                apiInfoDTO = fromAPIToInfoDTO((API) api);
                setThrottlePoliciesAndMonetization(api1, apiInfoDTO, deniedTiers, tierMap);
            } else if (api instanceof APIProduct) {
                APIProduct api1 = (APIProduct) api;
                apiInfoDTO = fromAPIToInfoDTO((API) api);
                setThrottlePoliciesAndMonetization(api1, apiInfoDTO, deniedTiers, tierMap);
            }
            apiInfoDTOs.add(apiInfoDTO);
        }
    }
    apiListDTO.setCount(apiInfoDTOs.size());
    return apiListDTO;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Tier(org.wso2.carbon.apimgt.api.model.Tier) APIListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIListDTO) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO)

Example 15 with APIListDTO

use of org.wso2.carbon.apimgt.rest.api.gateway.dto.APIListDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

@Override
public Response apisGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "" : query;
    APIListDTO apiListDTO = new APIListDTO();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // revert content search back to normal search by name to avoid doc result complexity and to comply with REST api practices
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":")) {
            query = query.replace(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":", APIConstants.NAME_TYPE_PREFIX + ":");
        }
        Map allMatchedApisMap = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        // This is a SortedSet
        Set<Object> sortedSet = (Set<Object>) allMatchedApisMap.get("apis");
        ArrayList<Object> allMatchedApis = new ArrayList<>(sortedSet);
        apiListDTO = APIMappingUtil.fromAPIListToDTO(allMatchedApis, organization);
        // Add pagination section in the response
        Object totalLength = allMatchedApisMap.get("length");
        Integer totalAvailableAPis = 0;
        if (totalLength != null) {
            totalAvailableAPis = (Integer) totalLength;
        }
        APIMappingUtil.setPaginationParams(apiListDTO, query, offset, limit, totalAvailableAPis);
        return Response.ok().entity(apiListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.rootCauseMessageMatches(e, "start index seems to be greater than the limit count")) {
            // this is not an error of the user as he does not know the total number of apis available. Thus sends
            // an empty response
            apiListDTO.setCount(0);
            apiListDTO.setPagination(new PaginationDTO());
            return Response.ok().entity(apiListDTO).build();
        } else {
            String errorMessage = "Error while retrieving APIs";
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : Set(java.util.Set) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ArrayList (java.util.ArrayList)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 API (org.wso2.carbon.apimgt.core.models.API)7 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 APIListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)5 HashMap (java.util.HashMap)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 API (org.wso2.carbon.apimgt.api.model.API)4 APIListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.APIListDTO)4 JSONObject (org.json.simple.JSONObject)3 APIListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.APIListDTO)3 File (java.io.File)2 Set (java.util.Set)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)2 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)2