use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStoreImpl method updateApplication.
@Override
public WorkflowResponse updateApplication(String uuid, Application application) throws APIManagementException {
try {
// get old app
Application existingApplication = getApplicationDAO().getApplication(uuid);
if (existingApplication != null) {
WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
ApplicationUpdateWorkflow workflow = new ApplicationUpdateWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
application.setId(uuid);
application.setUpdatedUser(getUsername());
application.setUpdatedTime(LocalDateTime.now());
Policy appTier = application.getPolicy();
if (appTier != null && !appTier.getPolicyName().equals(existingApplication.getPolicy().getPolicyName())) {
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, appTier.getPolicyName());
if (policy == null) {
String message = "Specified tier " + appTier + " is invalid";
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_NAME_INVALID);
}
application.setPolicy(policy);
}
workflow.setExistingApplication(existingApplication);
workflow.setUpdatedApplication(application);
workflow.setCreatedBy(getUsername());
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Update application " + existingApplication.getName() + " with tier " + existingApplication.getPolicy().getPolicyName() + " and description \'" + existingApplication.getDescription() + "\' To " + application.getName() + " with tier " + application.getPolicy().getPolicyName() + " and description \'" + application.getDescription() + "\' by " + getUsername();
workflow.setWorkflowDescription(workflowDescription);
// setting attributes for internal use. These are set to use from outside the executor's method
// these will be saved in the AM_WORKFLOW table so these can be retrieved later for external wf approval
// scenarios. this won't get stored for simple wfs
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME, application.getName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY, application.getUpdatedUser());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER, application.getPolicy().getPolicyName());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID, application.getPolicy().getUuid());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION, application.getDescription());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION, application.getPermissionString());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, existingApplication.getStatus());
WorkflowResponse response = executor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(executor, workflow);
} else {
getApplicationDAO().updateApplicationState(uuid, ApplicationStatus.APPLICATION_ONHOLD);
addWorkflowEntries(workflow);
}
return response;
} else {
String errorMsg = "Applicaiton does not exist - " + uuid;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating the application - " + uuid;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testCompleteApplicationWorkflowWithoutReference.
// End of exception testing
@Test(description = "Exception when completing application creation workflow without a reference", expectedExceptions = APIManagementException.class)
public void testCompleteApplicationWorkflowWithoutReference() throws Exception {
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, workflowDAO);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
WorkflowExecutor executor = new DefaultWorkflowExecutor();
Workflow workflow = new ApplicationCreationWorkflow(applicationDAO, workflowDAO, apiGateway);
workflow.setWorkflowReference(null);
apiStore.completeWorkflow(executor, workflow);
}
use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.
the class TenantWorkflowConfigHolder method load.
public void load() throws WorkflowException, RegistryException {
workflowExecutorMap = new ConcurrentHashMap<>();
InputStream in = null;
try {
String workFlowConfig = ServiceReferenceHolder.getInstance().getApimConfigService().getWorkFlowConfig(tenantDomain);
if (StringUtils.isNotEmpty(workFlowConfig)) {
in = new ByteArrayInputStream(workFlowConfig.getBytes());
StAXOMBuilder builder = new StAXOMBuilder(in);
secretResolver = SecretResolverFactory.create(builder.getDocumentElement(), true);
OMElement workflowExtensionsElem = builder.getDocument().getFirstChildWithName(new QName(WorkflowConstants.WORKFLOW_EXTENSIONS));
OMElement workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_CREATION));
String executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
Class clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.PRODUCTION_APPLICATION_REGISTRATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SANDBOX_APPLICATION_REGISTRATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.USER_SIGN_UP));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_CREATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_UPDATE));
if (workflowElem != null) {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
} else {
executorClass = DEFAULT_SUBSCRIPTION_UPDATE_EXECUTOR_CLASS;
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
}
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_DELETION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_DELETION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_STATE_CHANGE));
if (workflowElem == null) {
// TO handle migrated environment, create the default simple workflow executor
workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_STATE_CHANGE));
executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_STATE_CHANGE;
workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
} else {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
}
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_STATE, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
if (workflowElem == null) {
// To handle migration, create the default simple workflow executor
workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_PRODUCT_STATE_CHANGE;
workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
} else {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
}
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE, workFlowExecutor);
}
} catch (XMLStreamException e) {
log.error("Error building xml", e);
handleException("Error building xml", e);
} catch (ClassNotFoundException e) {
log.error("Unable to find class", e);
handleException("Unable to find class", e);
} catch (InstantiationException e) {
log.error("Unable to instantiate class", e);
handleException("Unable to instantiate class", e);
} catch (IllegalAccessException e) {
log.error("Illegal attempt to invoke class methods", e);
handleException("Illegal attempt to invoke class methods", e);
} catch (WorkflowException e) {
log.error("Unable to load workflow executor class", e);
handleException("Unable to load workflow executor class", e);
} catch (APIManagementException e) {
log.error("Unable to retrieve workflow configurations", e);
handleException("Unable to retrieve workflow configurations", e);
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor 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();
}
}
}
use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.
the class APIProviderImplTest method testDeleteAPIProductWorkflowTask.
@Test
public void testDeleteAPIProductWorkflowTask() throws APIManagementException, WorkflowException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
WorkflowExecutor productStateChangeWorkflowExecutor = Mockito.mock(WorkflowExecutor.class);
Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(productStateChangeWorkflowExecutor);
WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(workflowDTO);
APIProductIdentifier identifier = new APIProductIdentifier("admin", "APIProduct", "1.0.0", apiUUID);
apiProvider.deleteWorkflowTask(identifier);
Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
Aggregations