use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionListToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO
*
* @param subscriptions a list of SubscribedAPI objects
* @param query query to filter subscriptions
* @return SubscriptionListDTO object containing SubscriptionDTOs
*/
public static SubscriptionListDTO fromSubscriptionListToDTO(List<SubscribedAPI> subscriptions, String query) throws APIManagementException {
SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
if (subscriptionDTOs == null) {
subscriptionDTOs = new ArrayList<>();
subscriptionListDTO.setList(subscriptionDTOs);
}
query = query.toLowerCase().trim();
for (SubscribedAPI sub : subscriptions) {
SubscriptionDTO subscription = fromSubscriptionToDTO(sub);
if (subscription.getApplicationInfo().getName().toLowerCase().contains(query) || subscription.getApplicationInfo().getSubscriber().toLowerCase().contains(query) || subscription.getThrottlingPolicy().toLowerCase().contains(query)) {
subscriptionDTOs.add(subscription);
}
}
subscriptionListDTO.setCount(subscriptionDTOs.size());
return subscriptionListDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method getPaginatedSubscriptions.
/**
* Get the subscriptions within the specified pagination range
*
* @param subscriptionListDTO list of all subscription DTOs
* @param limit max number of objects returned
* @param offset starting index
* @return SubscriptionListDTO object containing SubscriptionDTOs within the pagination range
*/
public static SubscriptionListDTO getPaginatedSubscriptions(SubscriptionListDTO subscriptionListDTO, Integer limit, Integer offset) {
SubscriptionListDTO paginatedSubscriptionListDTO = new SubscriptionListDTO();
List<SubscriptionDTO> subscriptionDTOs = paginatedSubscriptionListDTO.getList();
if (subscriptionDTOs == null) {
subscriptionDTOs = new ArrayList<>();
paginatedSubscriptionListDTO.setList(subscriptionDTOs);
}
// identifying the proper start and end indexes
int size = subscriptionListDTO.getCount();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
List<SubscriptionDTO> subscriptions = subscriptionListDTO.getList();
for (int i = start; i <= end; i++) {
subscriptionDTOs.add(subscriptions.get(i));
}
paginatedSubscriptionListDTO.setCount(subscriptionDTOs.size());
return paginatedSubscriptionListDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionListToDTO.
/**
* 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
*/
public static SubscriptionListDTO fromSubscriptionListToDTO(List<SubscribedAPI> subscriptions, Integer limit, Integer offset, String organization) throws APIManagementException {
SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
if (subscriptionDTOs == null) {
subscriptionDTOs = new ArrayList<>();
subscriptionListDTO.setList(subscriptionDTOs);
}
// 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);
subscriptionDTOs.add(fromSubscriptionToDTO(subscription, organization));
} catch (APIManagementException e) {
log.error("Error while obtaining api metadata", e);
}
}
subscriptionListDTO.setCount(subscriptionDTOs.size());
return subscriptionListDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Get all subscriptions.
* {@code <p/>}
* If apiId is specified this will return the subscribed applications of that api
* If application id is specified this will return the api subscriptions of that application
*
* @param apiId ID of the API
* @param applicationId ID of the Application
* @param offset offset value
* @param limit limit value
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return Subscription List
* @throws NotFoundException If failed to get the subscription
*/
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
List<Subscription> subscribedApiList = null;
SubscriptionListDTO subscriptionListDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!StringUtils.isEmpty(apiId)) {
subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else if (!StringUtils.isEmpty(applicationId)) {
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null) {
if (!StringUtils.isEmpty(apiType)) {
ApiType apiTypeEnum = ApiType.fromString(apiType);
if (apiTypeEnum == null) {
throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
}
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
} else {
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
}
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else {
String errorMessage = "Application not found: " + applicationId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} else {
// mandatory parameters not provided
String errorMessage = "Either applicationId or apiId should be provided";
ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(subscriptionListDTO).build();
}
use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class MappingUtilTestCase method convertToSubscriptionListDTOTest.
@Test
public void convertToSubscriptionListDTOTest() {
List<SubscriptionValidationData> validationDataList = new ArrayList<>();
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
List<SubscriptionDTO> subscriptionListDTO = MappingUtil.convertToSubscriptionListDto(validationDataList);
Assert.assertNotNull(subscriptionListDTO);
Assert.assertEquals(validationDataList.size(), subscriptionListDTO.size());
for (int i = 0; i < subscriptionListDTO.size(); i++) {
Assert.assertEquals(subscriptionListDTO.get(i).getApiName(), validationDataList.get(i).getApiName());
Assert.assertEquals(subscriptionListDTO.get(i).getApiContext(), validationDataList.get(i).getApiContext());
Assert.assertEquals(subscriptionListDTO.get(i).getApiProvider(), validationDataList.get(i).getApiProvider());
Assert.assertEquals(subscriptionListDTO.get(i).getApiVersion(), validationDataList.get(i).getApiVersion());
}
}
Aggregations