Search in sources :

Example 21 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class ApplicationMappingUtil method setPaginationParamsWithSortParams.

/**
 * Sets pagination urls for a ApplicationListDTO object given pagination parameters and url parameters with
 * sortOrder and sortBy params.
 *
 * @param applicationListDTO a SubscriptionListDTO object
 * @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
 * @param sortOrder          sorting order
 * @param sortBy             specified sort param
 */
public static void setPaginationParamsWithSortParams(ApplicationListDTO applicationListDTO, String groupId, int limit, int offset, int size, String sortOrder, String sortBy) {
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
    String paginatedPrevious = "";
    String paginatedNext = "";
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = RestApiCommonUtil.getApplicationPaginatedURLWithSortParams(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), groupId, sortOrder, sortBy);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = RestApiCommonUtil.getApplicationPaginatedURLWithSortParams(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), groupId, sortOrder, sortBy);
    }
    PaginationDTO paginationDTO = CommonMappingUtil.getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    applicationListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)

Example 22 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method fromSubscriptionToSubscriptionListDTO.

public static SubscriptionListDTO fromSubscriptionToSubscriptionListDTO(List<Subscription> model) {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    if (model != null) {
        for (Subscription subsModel : model) {
            SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
            subscriptionDTO.setSubscriptionUUID(subsModel.getSubscriptionUUID());
            subscriptionDTO.setApplicationUUID(subsModel.getApplicationUUID());
            subscriptionDTO.setApiUUID(subsModel.getApiUUID());
            subscriptionDTO.setApiId(subsModel.getApiId());
            subscriptionDTO.setAppId(subsModel.getAppId());
            subscriptionDTO.setSubscriptionId(subsModel.getSubscriptionId());
            subscriptionDTO.setPolicyId(subsModel.getPolicyId());
            subscriptionDTO.setSubscriptionState(subsModel.getSubscriptionState());
            subscriptionListDTO.getList().add(subscriptionDTO);
        }
        subscriptionListDTO.setCount(model.size());
    } else {
        subscriptionListDTO.setCount(0);
    }
    return subscriptionListDTO;
}
Also used : Subscription(org.wso2.carbon.apimgt.api.model.subscription.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.internal.service.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO)

Example 23 with 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 that are of user or shared subscriptions of the user's group.
 * <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         api identifier
 * @param applicationId application identifier
 * @param offset        starting index of the subscription list
 * @param limit         max num of subscriptions returned
 * @param ifNoneMatch   If-None-Match header value
 * @return matched subscriptions as a list of SubscriptionDTOs
 */
@Override
public Response subscriptionsGet(String apiId, String applicationId, 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<>();
    // pre-processing
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    // currently groupId is taken from the user so that groupId coming as a query parameter is not honored.
    // As a improvement, we can check admin privileges of the user and honor groupId.
    groupId = RestApiUtil.getLoggedInUserGroupId();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        SubscriptionListDTO subscriptionListDTO;
        if (!StringUtils.isEmpty(apiId)) {
            // todo : FIX properly, need to done properly with backend side pagination.
            // todo : getSubscribedIdentifiers() method should NOT be used. Appears to be too slow.
            // This will fail with an authorization failed exception if user does not have permission to access the API
            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 by application name
            subscribedAPIList.addAll(subscriptions);
            subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
            SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, subscribedAPIList.size());
            return Response.ok().entity(subscriptionListDTO).build();
        } else if (!StringUtils.isEmpty(applicationId)) {
            Application application = apiConsumer.getApplicationByUUID(applicationId);
            if (application == null) {
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
                return null;
            }
            if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
            subscriptions = apiConsumer.getPaginatedSubscribedAPIsByApplication(application, offset, limit, organization);
            subscribedAPIList.addAll(subscriptions);
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
            return Response.ok().entity(subscriptionListDTO).build();
        } else {
            // neither apiId nor applicationId is given
            RestApiUtil.handleBadRequest("Either applicationId or apiId should be available", log);
            return null;
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting subscriptions of the user " + username, 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) 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) Application(org.wso2.carbon.apimgt.api.model.Application) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionListDTO)

Example 24 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class AsyncAPIMappingUtil method fromSubscriptionListToDTO.

/**
 * Converts Set of Subscription objects to SubscriptionListDTO.
 *
 * @param subscriptions Set of Subscription objects
 * @return WebhookSubscriptionListDTO containing SubscriptionDTOs
 */
public static WebhookSubscriptionListDTO fromSubscriptionListToDTO(Set<Subscription> subscriptions) {
    WebhookSubscriptionListDTO webhookSubscriptionListDTO = new WebhookSubscriptionListDTO();
    List<WebhookSubscriptionDTO> subscriptionDTOs = webhookSubscriptionListDTO.getList();
    webhookSubscriptionListDTO.setCount(subscriptions.size());
    if (subscriptionDTOs == null) {
        subscriptionDTOs = new ArrayList<>();
        webhookSubscriptionListDTO.setList(subscriptionDTOs);
    }
    for (Subscription subscription : subscriptions) {
        subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
    }
    return webhookSubscriptionListDTO;
}
Also used : WebhookSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.WebhookSubscriptionListDTO) WebhookSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.WebhookSubscriptionDTO) Subscription(org.wso2.carbon.apimgt.api.model.webhooks.Subscription)

Example 25 with SubscriptionListDTO

use of org.wso2.carbon.apimgt.internal.service.dto.SubscriptionListDTO in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method setPaginationParams.

/**
 * Sets pagination urls for a SubscriptionListDTO object given pagination parameters and url parameters
 *
 * @param subscriptionListDTO a SubscriptionListDTO 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(SubscriptionListDTO subscriptionListDTO, 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);
    subscriptionListDTO.setPagination(pagination);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)6 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)6 ArrayList (java.util.ArrayList)4 Response (javax.ws.rs.core.Response)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO)3 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)3 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)2 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO)2 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)2 PaginationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ZoneId (java.time.ZoneId)1