Search in sources :

Example 71 with SubscribedAPI

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

the class APIConsumerImpl method removeSubscription.

@Override
public void removeSubscription(Identifier identifier, String userId, int applicationId, String organization) throws APIManagementException {
    APIIdentifier apiIdentifier = null;
    APIProductIdentifier apiProdIdentifier = null;
    if (identifier instanceof APIIdentifier) {
        apiIdentifier = (APIIdentifier) identifier;
    }
    if (identifier instanceof APIProductIdentifier) {
        apiProdIdentifier = (APIProductIdentifier) identifier;
    }
    String applicationName = apiMgtDAO.getApplicationNameFromId(applicationId);
    try {
        SubscriptionWorkflowDTO workflowDTO;
        WorkflowExecutor createSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        WorkflowExecutor removeSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
        String workflowExtRef = apiMgtDAO.getExternalWorkflowReferenceForSubscription(identifier, applicationId, organization);
        // in a normal flow workflowExtRef is null when workflows are not enabled
        if (workflowExtRef == null) {
            workflowDTO = new SubscriptionWorkflowDTO();
        } else {
            workflowDTO = (SubscriptionWorkflowDTO) apiMgtDAO.retrieveWorkflow(workflowExtRef);
            // set tiername to the workflowDTO only when workflows are enabled
            SubscribedAPI subscription = apiMgtDAO.getSubscriptionById(Integer.parseInt(workflowDTO.getWorkflowReference()));
            workflowDTO.setTierName(subscription.getTier().getName());
        }
        workflowDTO.setApiProvider(identifier.getProviderName());
        API api = null;
        APIProduct product = null;
        String context = null;
        ApiTypeWrapper wrapper;
        if (apiIdentifier != null) {
            // The API is retrieved without visibility permission check, since the subscribers should be allowed
            // to delete already existing subscriptions made for restricted APIs
            wrapper = getAPIorAPIProductByUUIDWithoutPermissionCheck(apiIdentifier.getUUID(), organization);
            api = wrapper.getApi();
            context = api.getContext();
        } else if (apiProdIdentifier != null) {
            // The API Product is retrieved without visibility permission check, since the subscribers should be
            // allowe to delete already existing subscriptions made for restricted API Products
            wrapper = getAPIorAPIProductByUUIDWithoutPermissionCheck(apiProdIdentifier.getUUID(), organization);
            product = wrapper.getApiProduct();
            context = product.getContext();
        }
        workflowDTO.setApiContext(context);
        workflowDTO.setApiName(identifier.getName());
        workflowDTO.setApiVersion(identifier.getVersion());
        workflowDTO.setApplicationName(applicationName);
        workflowDTO.setTenantDomain(tenantDomain);
        workflowDTO.setTenantId(tenantId);
        workflowDTO.setExternalWorkflowReference(workflowExtRef);
        workflowDTO.setSubscriber(userId);
        workflowDTO.setCallbackUrl(removeSubscriptionWFExecutor.getCallbackURL());
        workflowDTO.setApplicationId(applicationId);
        workflowDTO.setMetadata(WorkflowConstants.PayloadConstants.API_ID, String.valueOf(identifier.getId()));
        String status = null;
        if (apiIdentifier != null) {
            status = apiMgtDAO.getSubscriptionStatus(apiIdentifier.getUUID(), applicationId);
        } else if (apiProdIdentifier != null) {
            status = apiMgtDAO.getSubscriptionStatus(apiProdIdentifier.getUUID(), applicationId);
        }
        if (APIConstants.SubscriptionStatus.ON_HOLD.equals(status)) {
            try {
                createSubscriptionWFExecutor.cleanUpPendingTask(workflowExtRef);
            } catch (WorkflowException ex) {
                // failed cleanup processes are ignored to prevent failing the deletion process
                log.warn("Failed to clean pending subscription approval task");
            }
        }
        // update attributes of the new remove workflow to be created
        workflowDTO.setStatus(WorkflowStatus.CREATED);
        workflowDTO.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
        workflowDTO.setCreatedTime(System.currentTimeMillis());
        workflowDTO.setExternalWorkflowReference(removeSubscriptionWFExecutor.generateUUID());
        Tier tier = null;
        if (api != null) {
            Set<Tier> policies = api.getAvailableTiers();
            Iterator<Tier> iterator = policies.iterator();
            boolean isPolicyAllowed = false;
            while (iterator.hasNext()) {
                Tier policy = iterator.next();
                if (policy.getName() != null && (policy.getName()).equals(workflowDTO.getTierName())) {
                    tier = policy;
                }
            }
        } else if (product != null) {
            Set<Tier> policies = product.getAvailableTiers();
            Iterator<Tier> iterator = policies.iterator();
            boolean isPolicyAllowed = false;
            while (iterator.hasNext()) {
                Tier policy = iterator.next();
                if (policy.getName() != null && (policy.getName()).equals(workflowDTO.getTierName())) {
                    tier = policy;
                }
            }
        }
        if (api != null) {
            // check whether monetization is enabled for API and tier plan is commercial
            if (api.getMonetizationStatus() && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
                removeSubscriptionWFExecutor.deleteMonetizedSubscription(workflowDTO, api);
            } else {
                removeSubscriptionWFExecutor.execute(workflowDTO);
            }
        } else if (product != null) {
            // check whether monetization is enabled for API product and tier plan is commercial
            if (product.getMonetizationStatus() && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
                removeSubscriptionWFExecutor.deleteMonetizedSubscription(workflowDTO, product);
            } else {
                removeSubscriptionWFExecutor.execute(workflowDTO);
            }
        }
        JSONObject subsLogObject = new JSONObject();
        subsLogObject.put(APIConstants.AuditLogConstants.API_NAME, identifier.getName());
        subsLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
        subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_ID, applicationId);
        subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_NAME, applicationName);
        APIUtil.logAuditMessage(APIConstants.AuditLogConstants.SUBSCRIPTION, subsLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
    } catch (WorkflowException e) {
        String errorMsg = "Could not execute Workflow, " + WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION + " for resource " + identifier.toString();
        handleException(errorMsg, e);
    }
    if (log.isDebugEnabled()) {
        String logMessage = "Subscription removed from app " + applicationName + " by " + userId + " For Id: " + identifier.toString();
        log.debug(logMessage);
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) Tier(org.wso2.carbon.apimgt.api.model.Tier) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) JSONObject(org.json.simple.JSONObject) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) Iterator(java.util.Iterator) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API)

Example 72 with SubscribedAPI

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

the class APIConsumerImpl method getPaginatedSubscribedAPIsByApplication.

@Override
public Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application application, Integer offset, Integer limit, String organization) throws APIManagementException {
    Set<SubscribedAPI> subscribedAPIs = null;
    try {
        subscribedAPIs = apiMgtDAO.getPaginatedSubscribedAPIsByApplication(application, offset, limit, organization);
        if (subscribedAPIs != null && !subscribedAPIs.isEmpty()) {
            Map<String, Tier> tiers = APIUtil.getTiers(tenantId);
            for (SubscribedAPI subscribedApi : subscribedAPIs) {
                Tier tier = tiers.get(subscribedApi.getTier().getName());
                subscribedApi.getTier().setDisplayName(tier != null ? tier.getDisplayName() : subscribedApi.getTier().getName());
            }
        }
    } catch (APIManagementException e) {
        handleException("Failed to get subscribed APIs of application " + application.getUUID(), e);
    }
    return subscribedAPIs;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI)

Example 73 with SubscribedAPI

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

the class APIConsumerImpl method getSubscribedIdentifiers.

@Override
public Set<SubscribedAPI> getSubscribedIdentifiers(Subscriber subscriber, Identifier identifier, String groupingId, String organization) throws APIManagementException {
    Set<SubscribedAPI> subscribedAPISet = new HashSet<>();
    Set<SubscribedAPI> subscribedAPIs = getSubscribedAPIs(organization, subscriber, groupingId);
    for (SubscribedAPI api : subscribedAPIs) {
        if (identifier instanceof APIIdentifier && identifier.equals(api.getApiId())) {
            Set<APIKey> keys = getApplicationKeys(api.getApplication().getId());
            for (APIKey key : keys) {
                api.addKey(key);
            }
            subscribedAPISet.add(api);
        } else if (identifier instanceof APIProductIdentifier && identifier.equals(api.getProductId())) {
            Set<APIKey> keys = getApplicationKeys(api.getApplication().getId());
            for (APIKey key : keys) {
                api.addKey(key);
            }
            subscribedAPISet.add(api);
        }
    }
    return subscribedAPISet;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 74 with SubscribedAPI

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

the class APIConsumerImpl method getLightWeightSubscribedAPIs.

private Set<SubscribedAPI> getLightWeightSubscribedAPIs(String organization, Subscriber subscriber, String groupingId) throws APIManagementException {
    Set<SubscribedAPI> originalSubscribedAPIs;
    Set<SubscribedAPI> subscribedAPIs = new HashSet<SubscribedAPI>();
    try {
        originalSubscribedAPIs = apiMgtDAO.getSubscribedAPIs(organization, subscriber, groupingId);
        if (originalSubscribedAPIs != null && !originalSubscribedAPIs.isEmpty()) {
            Map<String, Tier> tiers = APIUtil.getTiers(tenantId);
            for (SubscribedAPI subscribedApi : originalSubscribedAPIs) {
                Application application = subscribedApi.getApplication();
                if (application != null) {
                    int applicationId = application.getId();
                }
                Tier tier = tiers.get(subscribedApi.getTier().getName());
                subscribedApi.getTier().setDisplayName(tier != null ? tier.getDisplayName() : subscribedApi.getTier().getName());
                subscribedAPIs.add(subscribedApi);
            }
        }
    } catch (APIManagementException e) {
        handleException("Failed to get APIs of " + subscriber.getName(), e);
    }
    return subscribedAPIs;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Application(org.wso2.carbon.apimgt.api.model.Application) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 Application (org.wso2.carbon.apimgt.api.model.Application)28 Tier (org.wso2.carbon.apimgt.api.model.Tier)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ArrayList (java.util.ArrayList)12 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)12 HashSet (java.util.HashSet)11 TreeMap (java.util.TreeMap)11 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)9 JSONObject (net.minidev.json.JSONObject)9 API (org.wso2.carbon.apimgt.api.model.API)9