Search in sources :

Example 1 with AdditionalSubscriptionInfoListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO 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;
}
Also used : AdditionalSubscriptionInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoDTO) AdditionalSubscriptionInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI)

Example 2 with AdditionalSubscriptionInfoListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO in project carbon-apimgt by wso2.

the class AdditionalSubscriptionInfoMappingUtil method setPaginationParams.

/**
 * Sets pagination urls for a AdditionalSubscriptionInfoListDTO object given pagination parameters and url
 * parameters
 *
 * @param additionalSubscriptionInfoListDTO a AdditionalSubscriptionInfoListDTO object
 * @param apiId               uuid/id of API
 * @param groupId             group id of the applications to be returned
 * @param limit               max number of objects returned
 * @param offset              starting index
 * @param size                max offset
 */
public static void setPaginationParams(AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO, String apiId, String groupId, 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.getSubscriptionPaginatedURLForAPIId(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), apiId, groupId);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getSubscriptionPaginatedURLForAPIId(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), apiId, groupId);
    }
    PaginationDTO pagination = new PaginationDTO();
    pagination.setOffset(offset);
    pagination.setLimit(limit);
    pagination.setNext(paginatedNext);
    pagination.setPrevious(paginatedPrevious);
    pagination.setTotal(size);
    additionalSubscriptionInfoListDTO.setPagination(pagination);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)

Example 3 with AdditionalSubscriptionInfoListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method getAdditionalInfoOfAPISubscriptions.

/**
 * Get additional Info details of subscriptions attached with given API
 *
 * @param apiId         apiId
 * @param offset        starting index of the subscription list
 * @param limit         max num of subscriptions returned
 * @param ifNoneMatch   If-None-Match header value
 * @param messageContext message context
 * @return Response with additional Info of the GraphQL API
 */
@Override
public Response getAdditionalInfoOfAPISubscriptions(String apiId, String groupId, String xWSO2Tenant, Integer offset, Integer limit, String ifNoneMatch, MessageContext messageContext) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    Subscriber subscriber = new Subscriber(username);
    Set<SubscribedAPI> subscriptions;
    List<SubscribedAPI> subscribedAPIList = new ArrayList<>();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO;
        ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
        if (apiTypeWrapper.isAPIProduct()) {
            subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApiProduct().getId(), groupId, organization);
        } else {
            subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApi().getId(), groupId, organization);
        }
        // Sort subscriptions by application name
        subscribedAPIList.addAll(subscriptions);
        subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
        additionalSubscriptionInfoListDTO = AdditionalSubscriptionInfoMappingUtil.fromAdditionalSubscriptionInfoListToDTO(subscribedAPIList, limit, offset, organization);
        AdditionalSubscriptionInfoMappingUtil.setPaginationParams(additionalSubscriptionInfoListDTO, apiId, "", limit, offset, subscribedAPIList.size());
        return Response.ok().entity(additionalSubscriptionInfoListDTO).build();
    } catch (APIManagementException e) {
        // to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving additional information details of the API : " + apiId, e, log);
        } else {
            String msg = "Error while retrieving additional information details of the API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : AdditionalSubscriptionInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) WorkflowResponse(org.wso2.carbon.apimgt.api.WorkflowResponse) WorkflowStatus(org.wso2.carbon.apimgt.api.WorkflowStatus) SubscriptionsApiService(org.wso2.carbon.apimgt.rest.api.store.v1.SubscriptionsApiService) SubscriptionMappingUtil(org.wso2.carbon.apimgt.rest.api.store.v1.mappings.SubscriptionMappingUtil) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO) SubscriptionResponse(org.wso2.carbon.apimgt.api.model.SubscriptionResponse) APIMgtAuthorizationFailedException(org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException) URISyntaxException(java.net.URISyntaxException) HttpWorkflowResponse(org.wso2.carbon.apimgt.impl.workflow.HttpWorkflowResponse) RestAPIStoreUtils(org.wso2.carbon.apimgt.rest.api.util.utils.RestAPIStoreUtils) StringUtils(org.apache.commons.lang3.StringUtils) APIMappingUtil(org.wso2.carbon.apimgt.rest.api.store.v1.mappings.APIMappingUtil) ArrayList(java.util.ArrayList) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionDTO) RestApiCommonUtil(org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil) AdditionalSubscriptionInfoMappingUtil(org.wso2.carbon.apimgt.rest.api.store.v1.mappings.AdditionalSubscriptionInfoMappingUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) RestApiConstants(org.wso2.carbon.apimgt.rest.api.common.RestApiConstants) Map(java.util.Map) Monetization(org.wso2.carbon.apimgt.api.model.Monetization) SubscriptionAlreadyExistingException(org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException) URI(java.net.URI) Application(org.wso2.carbon.apimgt.api.model.Application) MapUtils(org.apache.commons.collections.MapUtils) APIMonetizationUsageDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIMonetizationUsageDTO) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Set(java.util.Set) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) List(java.util.List) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Response(javax.ws.rs.core.Response) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Comparator(java.util.Comparator) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) AdditionalSubscriptionInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 AdditionalSubscriptionInfoListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Response (javax.ws.rs.core.Response)1 MapUtils (org.apache.commons.collections.MapUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIMgtAuthorizationFailedException (org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException)1 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)1 SubscriptionAlreadyExistingException (org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException)1