use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtilTestCase method testFromSubscriptionListToDTO.
@Test
public void testFromSubscriptionListToDTO() {
String subUuid1 = UUID.randomUUID().toString();
String subUuid2 = UUID.randomUUID().toString();
Subscription subscription1 = SampleTestObjectCreator.createSubscription(subUuid1);
Subscription subscription2 = SampleTestObjectCreator.createSubscription(subUuid2);
List<Subscription> subscriptionList = new ArrayList<>();
subscriptionList.add(subscription1);
subscriptionList.add(subscription2);
SubscriptionListDTO subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscriptionList, 10, 0);
assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
assertEquals(subscription1.getApi().getName(), subscriptionListDTO.getList().get(0).getApiName());
assertEquals(subscription1.getApi().getId(), subscriptionListDTO.getList().get(0).getApiIdentifier());
assertEquals(subscription1.getApi().getVersion(), subscriptionListDTO.getList().get(0).getApiVersion());
assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription1.getStatus().toString()).name(), subscriptionListDTO.getList().get(0).getLifeCycleStatus().name());
assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
assertEquals(subscription2.getApi().getName(), subscriptionListDTO.getList().get(1).getApiName());
assertEquals(subscription2.getApi().getId(), subscriptionListDTO.getList().get(1).getApiIdentifier());
assertEquals(subscription2.getApi().getVersion(), subscriptionListDTO.getList().get(1).getApiVersion());
assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription2.getStatus().toString()).name(), subscriptionListDTO.getList().get(1).getLifeCycleStatus().name());
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Retrieve subscriptions
*
* @param apiContext Context of the API
* @param apiVersion API version
* @param limit Limit value
* @return Subscriptions of the API
* @throws NotFoundException If failed to retrieve subscriptions
*/
@Override
public Response subscriptionsGet(String apiContext, String apiVersion, Integer limit, String accept, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
List<SubscriptionValidationData> subscriptionsOfApi;
if (StringUtils.isEmpty(apiContext) || StringUtils.isEmpty(apiVersion)) {
APIUtils.logDebug("API Context or version is null or empty. Retrieving subscriptions of all APIs", log);
subscriptionsOfApi = apiMgtAdminService.getAPISubscriptions(limit);
} else {
subscriptionsOfApi = apiMgtAdminService.getAPISubscriptionsOfApi(apiContext, apiVersion);
}
SubscriptionListDTO subscriptionsList = new SubscriptionListDTO();
subscriptionsList.setList(MappingUtil.convertToSubscriptionListDto(subscriptionsOfApi));
return Response.ok(subscriptionsList).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions.";
HashMap<String, String> paramList = new HashMap<String, String>();
if (!StringUtils.isEmpty(apiContext)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_CONTEXT, apiContext);
}
if (!StringUtils.isEmpty(apiVersion)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, apiVersion);
}
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class ApplicationMappingUtil method setPaginationParams.
/**
* Sets pagination urls for a ApplicationListDTO object given pagination parameters and url parameters
*
* @param applicationListDTO a SubscriptionListDTO object
* @param limit max number of objects returned
* @param offset starting index
* @param size max offset
*/
public static void setPaginationParams(ApplicationListDTO applicationListDTO, int limit, int offset, int size) {
Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
String paginatedPrevious = "";
String paginatedNext = "";
if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
paginatedPrevious = RestApiCommonUtil.getApplicationPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), null);
}
if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
paginatedNext = RestApiCommonUtil.getApplicationPaginatedURL(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), null);
}
PaginationDTO paginationDTO = getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
applicationListDTO.setPagination(paginationDTO);
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method getSubscriptions.
/**
* Retrieves all subscriptions or retrieves subscriptions for a given API Id
*
* @param apiId API identifier
* @param limit max number of objects returns
* @param offset starting index
* @param ifNoneMatch If-None-Match header value
* @return Response object containing resulted subscriptions
*/
public Response getSubscriptions(String apiId, Integer limit, Integer offset, String ifNoneMatch, String query, MessageContext messageContext) {
// pre-processing
// setting default limit and offset if they are null
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
String organization = RestApiUtil.getValidatedOrganization(messageContext);
SubscriptionListDTO subscriptionListDTO;
List<SubscribedAPI> apiUsages;
if (apiId != null) {
String apiUuid = apiId;
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId);
if (apiRevision != null && apiRevision.getApiUUID() != null) {
apiUuid = apiRevision.getApiUUID();
}
apiUsages = apiProvider.getAPIUsageByAPIId(apiUuid, organization);
} else {
UserApplicationAPIUsage[] allApiUsage = apiProvider.getAllAPIUsageByProvider(username);
apiUsages = SubscriptionMappingUtil.fromUserApplicationAPIUsageArrayToSubscribedAPIList(allApiUsage);
}
if (query != null && !query.isEmpty()) {
SubscriptionListDTO filteredSubscriptionList = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, query);
subscriptionListDTO = SubscriptionMappingUtil.getPaginatedSubscriptions(filteredSubscriptionList, limit, offset);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, filteredSubscriptionList.getCount());
} else {
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, limit, offset);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, apiUsages.size());
}
return Response.ok().entity(subscriptionListDTO).build();
} catch (APIManagementException e) {
// existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String msg = "Error while retrieving subscriptions of API " + apiId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class AdditionalSubscriptionInfoMappingUtil method fromAdditionalSubscriptionInfoListToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO
*
* @param subscriptions a list of SubscribedAPI objects
* @param limit max number of objects returned
* @param offset starting index
* @param organization identifier of the organization
* @return SubscriptionListDTO object containing SubscriptionDTOs
* @throws APIManagementException if error occurred when creating AdditionalSubscriptionInfoListDTO
*/
public static AdditionalSubscriptionInfoListDTO fromAdditionalSubscriptionInfoListToDTO(List<SubscribedAPI> subscriptions, Integer limit, Integer offset, String organization) throws APIManagementException {
AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO = new AdditionalSubscriptionInfoListDTO();
List<AdditionalSubscriptionInfoDTO> additionalSubscriptionInfoDTOs = additionalSubscriptionInfoListDTO.getList();
if (additionalSubscriptionInfoDTOs == null) {
additionalSubscriptionInfoDTOs = new ArrayList<>();
additionalSubscriptionInfoListDTO.setList(additionalSubscriptionInfoDTOs);
}
// Identifying the proper start and end indexes
int size = subscriptions.size();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
for (int i = start; i <= end; i++) {
try {
SubscribedAPI subscription = subscriptions.get(i);
additionalSubscriptionInfoDTOs.add(fromAdditionalSubscriptionInfoToDTO(subscription, organization));
} catch (APIManagementException e) {
log.error("Error while obtaining additional info of subscriptions", e);
}
}
// Set count for list
additionalSubscriptionInfoListDTO.setCount(additionalSubscriptionInfoDTOs.size());
return additionalSubscriptionInfoListDTO;
}
Aggregations