use of org.wso2.carbon.apimgt.rest.api.service.catalog.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);
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.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;
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.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();
}
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.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;
}
use of org.wso2.carbon.apimgt.rest.api.service.catalog.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;
}
Aggregations