Search in sources :

Example 21 with SubscriptionDTO

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

the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdGet.

/**
 * Retrieves a single subscription
 *
 * @param subscriptionId  ID of the subscription
 * @param ifNoneMatch     If-Match header value
 * @param ifModifiedSince If-Modified-Since value
 * @param request         ms4j request object
 * @return Requested subscription details
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsSubscriptionIdGet(String subscriptionId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = subscriptionsSubscriptionIdGetFingerprint(subscriptionId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        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();
        }
        SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(subscription);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(subscriptionDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while getting 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) 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)

Example 22 with SubscriptionDTO

use of org.wso2.carbon.apimgt.rest.api.core.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
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<SubscribedAPI> subscriptions, Integer limit, Integer offset) 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++) {
        SubscribedAPI subscription = subscriptions.get(i);
        subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
    }
    subscriptionListDTO.setCount(subscriptionDTOs.size());
    return subscriptionListDTO;
}
Also used : SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)

Example 23 with SubscriptionDTO

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

the class SubscriptionsApiServiceImpl method blockSubscription.

/**
 * Blocks a subscription
 *
 * @param subscriptionId Subscription identifier
 * @param blockState block state; either BLOCKED or PROD_ONLY_BLOCKED
 * @param ifMatch If-Match header value
 * @return 200 response and the updated subscription if subscription block is successful
 */
public Response blockSubscription(String subscriptionId, String blockState, String ifMatch, MessageContext messageContext) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
        // validates the subscriptionId if it exists
        SubscribedAPI currentSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
        if (currentSubscription == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION, subscriptionId, log);
        }
        SubscribedAPI subscribedAPI = new SubscribedAPI(subscriptionId);
        subscribedAPI.setSubStatus(blockState);
        apiProvider.updateSubscription(subscribedAPI);
        SubscribedAPI updatedSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
        SubscriptionDTO subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(updatedSubscription);
        return Response.ok().entity(subscriptionDTO).build();
    } catch (APIManagementException e) {
        String msg = "Error while blocking the subscription " + subscriptionId;
        RestApiUtil.handleInternalServerError(msg, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO)

Example 24 with SubscriptionDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO 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 25 with SubscriptionDTO

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

the class SubscriptionsApiServiceImpl method subscriptionsGet.

public Response subscriptionsGet(String apiUUID, String appUUID, String tenantDomain, MessageContext messageContext) {
    tenantDomain = GatewayUtils.validateTenantDomain(tenantDomain, messageContext);
    SubscriptionDataStore subscriptionDataStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (subscriptionDataStore == null) {
        log.warn("Subscription data store is not initialized for " + tenantDomain);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    if (StringUtils.isNotEmpty(apiUUID) && StringUtils.isNotEmpty(appUUID)) {
        Subscription subscription = subscriptionDataStore.getSubscriptionByUUID(apiUUID, appUUID);
        if (subscription == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        SubscriptionDTO subscriptionDTO = GatewayUtils.convertToSubscriptionDto(subscription);
        return Response.ok().entity(subscriptionDTO).build();
    } else {
        return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorDTO().moreInfo("required parameters " + "are missing")).build();
    }
}
Also used : ErrorDTO(org.wso2.carbon.apimgt.rest.api.gateway.dto.ErrorDTO) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.gateway.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