Search in sources :

Example 16 with SubscriptionDTO

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

Example 17 with SubscriptionDTO

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

the class SubscriptionMappingUtil method fromSubscriptionToDTO.

public static SubscriptionDTO fromSubscriptionToDTO(SubscribedAPI subscription, ApiTypeWrapper apiTypeWrapper, String organization) throws APIManagementException {
    SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
    subscriptionDTO.setSubscriptionId(subscription.getUUID());
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
    Map<String, Tier> tierMap = APIUtil.getTiers(organization);
    if (apiTypeWrapper != null && !apiTypeWrapper.isAPIProduct()) {
        API api = apiTypeWrapper.getApi();
        subscriptionDTO.setApiId(api.getUUID());
        APIInfoDTO apiInfo = APIMappingUtil.fromAPIToInfoDTO(api);
        APIMappingUtil.setThrottlePoliciesAndMonetization(api, apiInfo, deniedTiers, tierMap);
        subscriptionDTO.setApiInfo(apiInfo);
    } else {
        APIProduct apiProduct = apiTypeWrapper.getApiProduct();
        subscriptionDTO.setApiId(apiProduct.getUuid());
        APIInfoDTO apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiProduct, organization);
        APIMappingUtil.setThrottlePoliciesAndMonetization(apiProduct, apiInfo, deniedTiers, tierMap);
        subscriptionDTO.setApiInfo(apiInfo);
    }
    Application application = subscription.getApplication();
    subscriptionDTO.setApplicationId(subscription.getApplication().getUUID());
    subscriptionDTO.setStatus(SubscriptionDTO.StatusEnum.valueOf(subscription.getSubStatus()));
    subscriptionDTO.setThrottlingPolicy(subscription.getTier().getName());
    subscriptionDTO.setRequestedThrottlingPolicy(subscription.getRequestedTier().getName());
    ApplicationInfoDTO applicationInfoDTO = ApplicationMappingUtil.fromApplicationToInfoDTO(application);
    subscriptionDTO.setApplicationInfo(applicationInfoDTO);
    return subscriptionDTO;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Tier(org.wso2.carbon.apimgt.api.model.Tier) API(org.wso2.carbon.apimgt.api.model.API) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ApplicationInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationInfoDTO) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionDTO) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 18 with SubscriptionDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionDTO 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());
    }
}
Also used : ArrayList(java.util.ArrayList) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 19 with SubscriptionDTO

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

the class TestMappingUtilTestCase method testSubscriptionToSubscriptionDTOMapping.

@Test(description = "Subscription to Subscription DTO mapping")
void testSubscriptionToSubscriptionDTOMapping() {
    Policy subscriptionPolicy = SampleTestObjectCreator.goldSubscriptionPolicy;
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Application application = SampleTestObjectCreator.createDefaultApplication();
    String uuid = UUID.randomUUID().toString();
    Subscription subscription = new Subscription(uuid, application, api, subscriptionPolicy);
    subscription.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
    SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(subscription);
    assertEquals(subscription.getId(), subscriptionDTO.getSubscriptionId());
    assertEquals(subscription.getStatus().name(), subscriptionDTO.getSubscriptionStatus().name());
    assertEquals(subscription.getApplication().getId(), subscriptionDTO.getApplicationInfo().getApplicationId());
    assertEquals(subscription.getPolicy().getPolicyName(), subscriptionDTO.getPolicy());
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO) Test(org.testng.annotations.Test)

Example 20 with SubscriptionDTO

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

the class SubscriptionsApiServiceImpl method subscriptionsUnblockSubscriptionPost.

/**
 * @param subscriptionId    ID of the subscription
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Modified-Since value
 * @param request           ms4j request object
 * @return ms4j request object
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsUnblockSubscriptionPost(String subscriptionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        Subscription subscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
        if (subscription == null) {
            String errorMessage = "Subscription not found : " + subscriptionId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        } else if (subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.REJECTED) || subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.ON_HOLD)) {
            String errorMessage = "Cannot update subcription from " + subscription.getStatus() + "to " + APIMgtConstants.SubscriptionStatus.ACTIVE;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_STATE_INVALID);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        apiPublisher.updateSubscriptionStatus(subscriptionId, APIMgtConstants.SubscriptionStatus.ACTIVE);
        Subscription newSubscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
        SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(newSubscription);
        return Response.ok().entity(subscriptionDTO).build();
    } catch (GatewayException e) {
        String errorMessage = "Failed to unblock subscription :" + subscriptionId + " in gateway";
        log.error(errorMessage, e);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while unblocking the subscription " + subscriptionId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)7 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)7 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionDTO)7 Application (org.wso2.carbon.apimgt.api.model.Application)6 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO)6 HashMap (java.util.HashMap)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO)5 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO)5 ArrayList (java.util.ArrayList)4 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 APIMgtAuthorizationFailedException (org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException)3 SubscriptionAlreadyExistingException (org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3