Search in sources :

Example 11 with SubscriptionDTO

use of org.wso2.carbon.apimgt.rest.api.store.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)

Example 12 with SubscriptionDTO

use of org.wso2.carbon.apimgt.rest.api.store.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)

Aggregations

Subscription (org.wso2.carbon.apimgt.core.models.Subscription)7 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 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)4 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)3 Application (org.wso2.carbon.apimgt.core.models.Application)3 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 API (org.wso2.carbon.apimgt.core.models.API)2 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)2 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)2 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1