Search in sources :

Example 31 with Pagination

use of org.wso2.carbon.apimgt.api.model.Pagination 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)

Example 32 with Pagination

use of org.wso2.carbon.apimgt.api.model.Pagination in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method setPaginationParams.

/**
 * Sets pagination urls for a TierListDTO object given pagination parameters and url parameters
 *
 * @param tierListDTO a TierListDTO object
 * @param limit       max number of objects returned
 * @param offset      starting index
 * @param size        max offset
 */
public static void setPaginationParams(ThrottlingPolicyListDTO tierListDTO, String tierLevel, 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.getTiersPaginatedURL(tierLevel, paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT));
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getTiersPaginatedURL(tierLevel, paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT));
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    tierListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)

Example 33 with Pagination

use of org.wso2.carbon.apimgt.api.model.Pagination in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesTierLevelGet.

/**
 * Retrieve a list of tiers for a particular tier level
 *
 * @param tierLevel   Tier level
 * @param limit       maximum number of tiers to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-Non-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying tiers
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response policiesTierLevelGet(String tierLevel, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    TierListDTO tierListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Policy> tierList = apiStore.getPolicies(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel));
        tierListDTO = TierMappingUtil.fromTierListToDTO(tierList, tierLevel, limit, offset);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tiers";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(tierListDTO).build();
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 34 with Pagination

use of org.wso2.carbon.apimgt.api.model.Pagination in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisGet.

/**
 * Retrives all APIs that qualifies for the given fitering attributes
 *
 * @param limit       maximum APIs to return
 * @param offset      starting position of the pagination
 * @param query       search query
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return a list of qualifying APIs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisGet(Integer limit, Integer offset, String query, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    APIListDTO apiListDTO = null;
    try {
        apiListDTO = MappingUtil.toAPIListDTO(RestAPIPublisherUtil.getApiPublisher(username).searchAPIs(limit, offset, query));
        return Response.ok().entity(apiListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving APIs";
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList, e);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)

Example 35 with Pagination

use of org.wso2.carbon.apimgt.api.model.Pagination in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testGetSubscriptionForProvider.

@Test
public void testGetSubscriptionForProvider() throws Exception {
    // add test apis, apps and subscriptions
    // app1: api2
    // app2: api1, api2, api3
    // app3: api3
    // app4: api1, api2, api3, api4
    createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    // TODO getAPISubscriptionsForUser() pagination not implemented properly
    List<Subscription> subscriptions = subscriptionDAO.getAPISubscriptionsForUser(0, Integer.MAX_VALUE, "admin");
    // The number of total subscriptions created from createApisAppsAndSubscriptions() is 9
    Assert.assertEquals(subscriptions.size(), 9);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)19 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 JSONObject (org.json.simple.JSONObject)12 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)12 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 PaginationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.PaginationDTO)11 API (org.wso2.carbon.apimgt.api.model.API)10 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)8 PaginationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)8 GovernanceArtifact (org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact)8 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)8 TreeSet (java.util.TreeSet)7 Registry (org.wso2.carbon.registry.core.Registry)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 APINameComparator (org.wso2.carbon.apimgt.impl.utils.APINameComparator)6 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5