Search in sources :

Example 46 with Workflow

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

the class APIStateChangeWSWorkflowExecutor method cleanUpPendingTask.

/**
 * Handle cleanup task for api state change workflow ws executor. This queries the BPMN process related to the given
 * workflow reference id and delete that process
 */
@Override
public void cleanUpPendingTask(String workflowExtRef) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Starting cleanup task for APIStateChangeWSWorkflowExecutor for :" + workflowExtRef);
    }
    String errorMsg;
    if (serviceEndpoint == null) {
        // set the bps endpoint from the global configurations
        WorkflowProperties workflowProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getWorkflowProperties();
        serviceEndpoint = workflowProperties.getServerUrl();
    }
    URL serviceEndpointURL = new URL(serviceEndpoint);
    HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
    // get the basic auth header value to connect to the bpmn process
    String authHeader = getBasicAuthHeader();
    JSONParser parser = new JSONParser();
    HttpGet httpGet = null;
    HttpDelete httpDelete = null;
    try {
        // Get the process instance details related to the given workflow reference id. If there is a process that
        // is already started with the given wf reference as the businesskey, that process needes to be deleted
        httpGet = new HttpGet(serviceEndpoint + RUNTIME_INSTANCE_RESOURCE_PATH + "?businessKey=" + workflowExtRef);
        httpGet.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String processId = null;
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // already exists a process related to the given workflow reference
            String responseStr = EntityUtils.toString(entity);
            if (log.isDebugEnabled()) {
                log.debug("Process instance details for ref : " + workflowExtRef + ": " + responseStr);
            }
            JSONObject obj = (JSONObject) parser.parse(responseStr);
            JSONArray data = (JSONArray) obj.get(PayloadConstants.DATA);
            if (data != null) {
                JSONObject instanceDetails = (JSONObject) data.get(0);
                // extract the id related to that process. this id is used to delete the process
                processId = (String) instanceDetails.get(PayloadConstants.ID);
            }
            if (processId != null) {
                // delete the process using the id
                httpDelete = new HttpDelete(serviceEndpoint + RUNTIME_INSTANCE_RESOURCE_PATH + "/" + processId);
                httpDelete.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
                response = httpClient.execute(httpDelete);
                if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
                    errorMsg = "Error while deleting process instance details for " + workflowExtRef + " code: " + response.getStatusLine().getStatusCode();
                    log.error(errorMsg);
                    throw new WorkflowException(errorMsg);
                }
                if (log.isDebugEnabled()) {
                    log.debug("Successfully deleted process instance for  : " + workflowExtRef);
                }
                // remove entry from the db
                ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
                apiMgtDAO.removeWorkflowEntry(workflowExtRef, WorkflowConstants.WF_TYPE_AM_API_STATE.toString());
            }
        } else {
            errorMsg = "Error while getting process instance details for " + workflowExtRef + " code: " + response.getStatusLine().getStatusCode();
            log.error(errorMsg);
            throw new WorkflowException(errorMsg);
        }
    } catch (ClientProtocolException e) {
        log.error("Error while creating the http client", e);
        throw new WorkflowException("Error while creating the http client", e);
    } catch (IOException e) {
        log.error("Error while connecting to the BPMN process server from the WorkflowExecutor.", e);
        throw new WorkflowException("Error while connecting to the external service", e);
    } catch (ParseException e) {
        log.error("Error while parsing response from BPS server", e);
        throw new WorkflowException("Error while parsing response from BPS server", e);
    } catch (APIManagementException e) {
        log.error("Error removing the workflow entry", e);
        throw new WorkflowException("Error removing the workflow entry", e);
    } finally {
        if (httpGet != null) {
            httpGet.reset();
        }
        if (httpDelete != null) {
            httpDelete.reset();
        }
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.simple.JSONArray) HttpResponse(org.apache.http.HttpResponse) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) IOException(java.io.IOException) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties) URL(org.apache.axis2.util.URL) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpClient(org.apache.http.client.HttpClient) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 47 with Workflow

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

the class SubscriptionUpdateApprovalWorkflowExecutor method complete.

/**
 * Complete the Approval workflow executor for Subscription creation.
 *
 * @param workflowDTO
 */
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    workflowDTO.setUpdatedTime(System.currentTimeMillis());
    super.complete(workflowDTO);
    if (log.isDebugEnabled()) {
        String logMessage = "Subscription Update [Complete] Workflow Invoked. Workflow ID : " + workflowDTO.getExternalWorkflowReference() + " Workflow State : " + workflowDTO.getStatus();
        log.debug(logMessage);
    }
    SubscriptionWorkflowDTO subscriptionWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        try {
            apiMgtDAO.updateSubscriptionStatusAndTier(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);
        }
    } else if (WorkflowStatus.REJECTED.equals(workflowDTO.getStatus())) {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        try {
            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 48 with Workflow

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

the class SubscriptionUpdateWSWorkflowExecutor 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.UPDATE_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 49 with Workflow

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

the class SubscriptionUpdateWSWorkflowExecutor method complete.

@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    workflowDTO.setUpdatedTime(System.currentTimeMillis());
    super.complete(workflowDTO);
    log.info("Subscription Update [Complete] Workflow Invoked. Workflow ID : " + workflowDTO.getExternalWorkflowReference() + "Workflow State : " + workflowDTO.getStatus());
    if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        try {
            apiMgtDAO.updateSubscriptionStatus(Integer.parseInt(workflowDTO.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);
        }
    } else if (WorkflowStatus.REJECTED.equals(workflowDTO.getStatus())) {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        try {
            apiMgtDAO.updateSubscriptionStatus(Integer.parseInt(workflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.REJECTED);
        } 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) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)

Example 50 with Workflow

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

the class UserSignUpWSWorkflowExecutor method complete.

@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    // workflowDTO.setStatus(workflowDTO.getStatus());
    workflowDTO.setUpdatedTime(System.currentTimeMillis());
    if (log.isDebugEnabled()) {
        log.debug("User Sign Up [Complete] Workflow Invoked. Workflow ID : " + workflowDTO.getExternalWorkflowReference() + "Workflow State : " + workflowDTO.getStatus());
    }
    super.complete(workflowDTO);
    String tenantDomain = workflowDTO.getTenantDomain();
    try {
        UserRegistrationConfigDTO signupConfig = SelfSignUpUtil.getSignupConfiguration(tenantDomain);
        String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(workflowDTO.getWorkflowReference());
        if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
            try {
                updateRolesOfUser(tenantAwareUserName, SelfSignUpUtil.getRoleNames(signupConfig), tenantDomain);
            } catch (Exception e) {
                // updateRolesOfUser throws generic Exception. Therefore generic Exception is caught
                throw new WorkflowException("Error while assigning role to user", e);
            }
        } else {
            try {
                /* Remove created user */
                deleteUser(tenantDomain, tenantAwareUserName);
            } catch (Exception e) {
                throw new WorkflowException("Error while deleting the user", e);
            }
        }
    } catch (APIManagementException e1) {
        throw new WorkflowException("Error while accessing signup configuration", e1);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Test (org.junit.Test)35 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)28 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)26 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)25 Test (org.testng.annotations.Test)24 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)22 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)21 HashMap (java.util.HashMap)20 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)20 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)19 JSONObject (org.json.simple.JSONObject)17 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)17 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)16 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)15 WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)15 Application (org.wso2.carbon.apimgt.api.model.Application)14 ArrayList (java.util.ArrayList)12 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)11