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;
}
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);
}
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();
}
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();
}
}
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);
}
Aggregations