Search in sources :

Example 11 with SubscriptionResponse

use of org.wso2.carbon.apimgt.api.model.SubscriptionResponse in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsMultiplePost.

/**
 * Create multiple new subscriptions with the list of subscription details specified in the body parameter.
 *
 * @param body list of new subscription details
 * @return list of newly added subscription as a SubscriptionDTO if successful
 */
@Override
public Response subscriptionsMultiplePost(List<SubscriptionDTO> body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    List<SubscriptionDTO> subscriptions = new ArrayList<>();
    for (SubscriptionDTO subscriptionDTO : body) {
        try {
            APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
            String applicationId = subscriptionDTO.getApplicationId();
            APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(subscriptionDTO.getApiId(), organization);
            // this will throw a APIMgtResourceNotFoundException
            if (!org.wso2.carbon.apimgt.rest.api.util.utils.RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(subscriptionDTO.getApiId(), organization)) {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, subscriptionDTO.getApiId(), log);
            }
            Application application = apiConsumer.getApplicationByUUID(applicationId);
            if (application == null) {
                // required application not found
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
            if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                // application access failure occurred
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
            ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(subscriptionDTO.getApiId(), organization);
            apiTypeWrapper.setTier(subscriptionDTO.getThrottlingPolicy());
            SubscriptionResponse subscriptionResponse = apiConsumer.addSubscription(apiTypeWrapper, username, application);
            SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
            SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, organization);
            subscriptions.add(addedSubscriptionDTO);
        } catch (APIMgtAuthorizationFailedException e) {
            // this occurs when the api:application:tier mapping is not allowed. The reason for the message is
            // taken from the message of the exception e
            RestApiUtil.handleAuthorizationFailure(e.getMessage(), e, log);
        } catch (SubscriptionAlreadyExistingException e) {
            RestApiUtil.handleResourceAlreadyExistsError("Specified subscription already exists for API " + subscriptionDTO.getApiId() + " for application " + subscriptionDTO.getApplicationId(), e, log);
        } catch (APIManagementException e) {
            if (RestApiUtil.isDueToResourceNotFound(e)) {
                // this happens when the specified API identifier does not exist
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, subscriptionDTO.getApiId(), e, log);
            } else {
                // unhandled exception
                RestApiUtil.handleInternalServerError("Error while adding the subscription API:" + subscriptionDTO.getApiId() + ", application:" + subscriptionDTO.getApplicationId() + ", throttling policy:" + subscriptionDTO.getThrottlingPolicy(), e, log);
            }
        }
    }
    return Response.ok().entity(subscriptions).build();
}
Also used : ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIMgtAuthorizationFailedException(org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException) ArrayList(java.util.ArrayList) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionAlreadyExistingException(org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscriptionResponse(org.wso2.carbon.apimgt.api.model.SubscriptionResponse) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SubscriptionDTO) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

SubscriptionResponse (org.wso2.carbon.apimgt.core.models.SubscriptionResponse)6 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 SubscriptionResponse (org.wso2.carbon.apimgt.api.model.SubscriptionResponse)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 Application (org.wso2.carbon.apimgt.core.models.Application)5 BeforeTest (org.testng.annotations.BeforeTest)4 Test (org.testng.annotations.Test)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 WorkflowResponse (org.wso2.carbon.apimgt.api.WorkflowResponse)4 API (org.wso2.carbon.apimgt.core.models.API)4 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)3 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)3 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)3 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)3