Search in sources :

Example 16 with SubscriptionWorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO in project carbon-apimgt by wso2.

the class SubscriptionCreationWSWorkflowExecutor method execute.

/**
 * This method is used to execute the workflow without giving a workflow response back to the caller to execute
 * some other task after completing the workflow
 *
 * @param workflowDTO - The WorkflowDTO which contains workflow contextual information related to the workflow.
 * @throws WorkflowException
 */
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
    try {
        String action = WorkflowConstants.CREATE_SUBSCRIPTION_WS_ACTION;
        ServiceClient client = getClient(action);
        String payload = "<wor:SubscriptionApprovalWorkFlowProcessRequest " + "         xmlns:wor=\"http://workflow.subscription.apimgt.carbon.wso2.org\">\n" + "         <wor:apiName>$1</wor:apiName>\n" + "         <wor:apiVersion>$2</wor:apiVersion>\n" + "         <wor:apiContext>$3</wor:apiContext>\n" + "         <wor:apiProvider>$4</wor:apiProvider>\n" + "         <wor:subscriber>$5</wor:subscriber>\n" + "         <wor:applicationName>$6</wor:applicationName>\n" + "         <wor:tierName>$7</wor:tierName>\n" + "         <wor:workflowExternalRef>$8</wor:workflowExternalRef>\n" + "         <wor:callBackURL>$9</wor:callBackURL>\n" + "      </wor:SubscriptionApprovalWorkFlowProcessRequest>";
        SubscriptionWorkflowDTO subsWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
        String callBackURL = subsWorkflowDTO.getCallbackUrl();
        payload = payload.replace("$1", subsWorkflowDTO.getApiName());
        payload = payload.replace("$2", subsWorkflowDTO.getApiVersion());
        payload = payload.replace("$3", subsWorkflowDTO.getApiContext());
        payload = payload.replace("$4", subsWorkflowDTO.getApiProvider());
        payload = payload.replace("$5", subsWorkflowDTO.getSubscriber());
        payload = payload.replace("$6", subsWorkflowDTO.getApplicationName());
        payload = payload.replace("$7", subsWorkflowDTO.getTierName());
        payload = payload.replace("$8", subsWorkflowDTO.getExternalWorkflowReference());
        payload = payload.replace("$9", callBackURL != null ? callBackURL : "?");
        client.fireAndForget(AXIOMUtil.stringToOM(payload));
        super.execute(workflowDTO);
    } catch (AxisFault axisFault) {
        log.error("Error sending out message", axisFault);
        throw new WorkflowException("Error sending out message", axisFault);
    } catch (XMLStreamException e) {
        log.error("Error converting String to OMElement", e);
        throw new WorkflowException("Error converting String to OMElement", e);
    }
    return new GeneralWorkflowResponse();
}
Also used : AxisFault(org.apache.axis2.AxisFault) XMLStreamException(javax.xml.stream.XMLStreamException) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) ServiceClient(org.apache.axis2.client.ServiceClient)

Example 17 with SubscriptionWorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO in project carbon-apimgt by wso2.

the class SubscriptionUpdateApprovalWorkflowExecutor method execute.

/**
 * Execute the Application Creation workflow approval process.
 *
 * @param workflowDTO
 */
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing Subscription Update Webservice Workflow.. ");
    }
    SubscriptionWorkflowDTO subsWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    String message = "Approve API " + subsWorkflowDTO.getApiName() + " - " + subsWorkflowDTO.getApiVersion() + " subscription creation request from subscriber - " + subsWorkflowDTO.getSubscriber() + " for the application - " + subsWorkflowDTO.getApplicationName();
    workflowDTO.setWorkflowDescription(message);
    workflowDTO.setProperties("apiName", subsWorkflowDTO.getApiName());
    workflowDTO.setProperties("apiVersion", subsWorkflowDTO.getApiVersion());
    workflowDTO.setProperties("subscriber", subsWorkflowDTO.getSubscriber());
    workflowDTO.setProperties("applicationName", subsWorkflowDTO.getApplicationName());
    workflowDTO.setProperties("currentTier", subsWorkflowDTO.getTierName());
    workflowDTO.setProperties("requestedTier", subsWorkflowDTO.getRequestedTierName());
    super.execute(workflowDTO);
    return new GeneralWorkflowResponse();
}
Also used : SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)

Example 18 with SubscriptionWorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO in project carbon-apimgt by wso2.

the class SubscriptionDeletionSimpleWorkflowExecutor method complete.

@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    SubscriptionWorkflowDTO subWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    String errorMsg = null;
    try {
        APIIdentifier identifier = new APIIdentifier(subWorkflowDTO.getApiProvider(), subWorkflowDTO.getApiName(), subWorkflowDTO.getApiVersion());
        identifier.setId(Integer.parseInt(subWorkflowDTO.getMetadata(WorkflowConstants.PayloadConstants.API_ID)));
        apiMgtDAO.removeSubscription(identifier, ((SubscriptionWorkflowDTO) workflowDTO).getApplicationId());
    } catch (APIManagementException e) {
        errorMsg = "Could not complete subscription deletion workflow for api: " + subWorkflowDTO.getApiName();
        throw new WorkflowException(errorMsg, e);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 19 with SubscriptionWorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO in project carbon-apimgt by wso2.

the class SubscriptionUpdateSimpleWorkflowExecutor method complete.

/**
 * This method completes subscription update simple workflow and return workflow response back to the caller
 *
 * @param workflowDTO The WorkflowDTO which contains workflow contextual information related to the workflow
 * @return workflow response back to the caller
 * @throws WorkflowException
 */
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    SubscriptionWorkflowDTO subscriptionWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    try {
        if (subscriptionWorkflowDTO.getStatus() == WorkflowStatus.APPROVED) {
            apiMgtDAO.updateSubscriptionStatusAndTier(Integer.parseInt(subscriptionWorkflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.UNBLOCKED);
        } else if (subscriptionWorkflowDTO.getStatus() == WorkflowStatus.CREATED || subscriptionWorkflowDTO.getStatus() == WorkflowStatus.REGISTERED) {
            apiMgtDAO.updateSubscriptionStatus(Integer.parseInt(subscriptionWorkflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.TIER_UPDATE_PENDING);
        } else if (subscriptionWorkflowDTO.getStatus() == WorkflowStatus.REJECTED) {
            apiMgtDAO.updateSubscriptionStatus(Integer.parseInt(subscriptionWorkflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.UNBLOCKED);
        }
    } catch (APIManagementException e) {
        log.error("Could not complete subscription update workflow", e);
        throw new WorkflowException("Could not complete subscription update workflow", e);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)

Example 20 with SubscriptionWorkflowDTO

use of org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO 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)

Aggregations

SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)19 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 ServiceReferenceHolderMockCreator (org.wso2.carbon.apimgt.impl.ServiceReferenceHolderMockCreator)5 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)5 Tier (org.wso2.carbon.apimgt.api.model.Tier)4 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)4 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)4 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 JSONObject (org.json.simple.JSONObject)3 API (org.wso2.carbon.apimgt.api.model.API)3 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)3 SubscriptionEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent)3 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)3