Search in sources :

Example 11 with WorkflowException

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowException 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 12 with WorkflowException

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowException in project carbon-apimgt by wso2.

the class AbstractApplicationRegistrationWorkflowExecutor method execute.

public WorkflowResponse execute(WorkflowDTO workFlowDTO) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing AbstractApplicationRegistrationWorkflowExecutor...");
    }
    ApiMgtDAO dao = ApiMgtDAO.getInstance();
    try {
        // dao.createApplicationRegistrationEntry((ApplicationRegistrationWorkflowDTO) workFlowDTO, false);
        ApplicationRegistrationWorkflowDTO appRegDTO;
        if (workFlowDTO instanceof ApplicationRegistrationWorkflowDTO) {
            appRegDTO = (ApplicationRegistrationWorkflowDTO) workFlowDTO;
        } else {
            String message = "Invalid workflow type found";
            log.error(message);
            throw new WorkflowException(message);
        }
        dao.createApplicationRegistrationEntry(appRegDTO, false);
        // appRegDTO.getAppInfoDTO().saveDTO();
        super.execute(workFlowDTO);
    } catch (APIManagementException e) {
        log.error("Error while creating Application Registration entry.", e);
        throw new WorkflowException("Error while creating Application Registration entry.", e);
    }
    return new GeneralWorkflowResponse();
}
Also used : ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)

Example 13 with WorkflowException

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowException in project carbon-apimgt by wso2.

the class ApplicationDeletionSimpleWorkflowExecutor method complete.

@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    ApplicationWorkflowDTO applicationWorkflowDTO = (ApplicationWorkflowDTO) workflowDTO;
    Application application = applicationWorkflowDTO.getApplication();
    String errorMsg = null;
    try {
        apiMgtDAO.deleteApplication(application);
    } catch (APIManagementException e) {
        if (e.getMessage() == null) {
            errorMsg = "Couldn't complete simple application deletion workflow for application: " + application.getName();
        } else {
            errorMsg = e.getMessage();
        }
        throw new WorkflowException(errorMsg, e);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 14 with WorkflowException

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowException in project carbon-apimgt by wso2.

the class ApplicationCreationApprovalWorkflowExecutor method complete.

/**
 * Complete the Application creation approval workflow peocess.
 *
 * @param workFlowDTO
 */
@Override
public WorkflowResponse complete(WorkflowDTO workFlowDTO) throws WorkflowException {
    workFlowDTO.setUpdatedTime(System.currentTimeMillis());
    ApiMgtDAO dao = ApiMgtDAO.getInstance();
    try {
        if (dao.getApplicationById(Integer.parseInt(workFlowDTO.getWorkflowReference())) != null) {
            super.complete(workFlowDTO);
            if (log.isDebugEnabled()) {
                String logMessage = "Application Creation [Complete] Workflow Invoked. Workflow ID : " + workFlowDTO.getExternalWorkflowReference() + " Workflow State : " + workFlowDTO.getStatus();
                log.debug(logMessage);
            }
            String status = null;
            if (WorkflowStatus.CREATED.equals(workFlowDTO.getStatus())) {
                status = APIConstants.ApplicationStatus.APPLICATION_CREATED;
            } else if (WorkflowStatus.REJECTED.equals(workFlowDTO.getStatus())) {
                status = APIConstants.ApplicationStatus.APPLICATION_REJECTED;
            } else if (WorkflowStatus.APPROVED.equals(workFlowDTO.getStatus())) {
                status = APIConstants.ApplicationStatus.APPLICATION_APPROVED;
            }
            try {
                dao.updateApplicationStatus(Integer.parseInt(workFlowDTO.getWorkflowReference()), status);
            } catch (APIManagementException e) {
                String msg = "Error occurred when updating the status of the Application creation process";
                log.error(msg, e);
                throw new WorkflowException(msg, e);
            }
        } else {
            String msg = "Application does not exist";
            throw new WorkflowException(msg);
        }
    } catch (APIManagementException e) {
        String msg = "Error occurred when retrieving the Application creation with workflow ID :" + workFlowDTO.getWorkflowReference();
        log.error(msg, e);
        throw new WorkflowException(msg, e);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)

Example 15 with WorkflowException

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowException in project carbon-apimgt by wso2.

the class ApplicationRegistrationApprovalWorkflowExecutor 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 Application registration Workflow..");
    }
    ApplicationRegistrationWorkflowDTO appRegDTO = (ApplicationRegistrationWorkflowDTO) workflowDTO;
    Application application = appRegDTO.getApplication();
    String message = "Approve request to create " + appRegDTO.getKeyType() + " keys for " + application.getName() + " from application creator - " + appRegDTO.getUserName() + " with throttling tier - " + application.getTier();
    workflowDTO.setWorkflowDescription(message);
    workflowDTO.setProperties("keyType", appRegDTO.getKeyType());
    workflowDTO.setProperties("applicationName", application.getName());
    workflowDTO.setProperties("userName", appRegDTO.getUserName());
    workflowDTO.setProperties("applicationTier", application.getTier());
    super.execute(workflowDTO);
    return new GeneralWorkflowResponse();
}
Also used : ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)56 Test (org.junit.Test)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)49 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)32 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)25 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)24 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)24 Application (org.wso2.carbon.apimgt.api.model.Application)18 WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)17 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)15 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)13 HashMap (java.util.HashMap)12 JSONObject (org.json.simple.JSONObject)12 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)12 ServiceReferenceHolderMockCreator (org.wso2.carbon.apimgt.impl.ServiceReferenceHolderMockCreator)10 XMLStreamException (javax.xml.stream.XMLStreamException)9 UserRegistrationConfigDTO (org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO)9 WorkflowResponse (org.wso2.carbon.apimgt.api.WorkflowResponse)8 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)7 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)6