use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatus.
public APIStateChangeResponse changeLifeCycleStatus(APIIdentifier apiIdentifier, String action, String organization) throws APIManagementException, FaultGatewaysException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
GenericArtifact apiArtifact = getAPIArtifact(apiIdentifier);
String targetStatus;
if (apiArtifact != null) {
String providerName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiContext = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT);
String apiType = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String currentStatus = apiArtifact.getLifecycleState();
String uuid = apiMgtDAO.getUUIDFromIdentifier(apiIdentifier, organization);
String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
int apiId = apiMgtDAO.getAPIID(uuid);
WorkflowStatus apiWFState = null;
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
}
// if the workflow has started, then executor should not fire again
if (!WorkflowStatus.CREATED.equals(apiWFState)) {
try {
WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
WorkflowExecutor apiStateWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
APIStateWorkflowDTO apiStateWorkflow = new APIStateWorkflowDTO();
apiStateWorkflow.setApiCurrentState(currentStatus);
apiStateWorkflow.setApiLCAction(action);
apiStateWorkflow.setApiName(apiName);
apiStateWorkflow.setApiContext(apiContext);
apiStateWorkflow.setApiType(apiType);
apiStateWorkflow.setApiVersion(apiVersion);
apiStateWorkflow.setApiProvider(providerName);
apiStateWorkflow.setGatewayVendor(gatewayVendor);
apiStateWorkflow.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
apiStateWorkflow.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
apiStateWorkflow.setTenantId(tenantId);
apiStateWorkflow.setTenantDomain(this.tenantDomain);
apiStateWorkflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_API_STATE);
apiStateWorkflow.setStatus(WorkflowStatus.CREATED);
apiStateWorkflow.setCreatedTime(System.currentTimeMillis());
apiStateWorkflow.setWorkflowReference(Integer.toString(apiId));
apiStateWorkflow.setInvoker(this.username);
apiStateWorkflow.setApiUUID(uuid);
String workflowDescription = "Pending lifecycle state change action: " + action;
apiStateWorkflow.setWorkflowDescription(workflowDescription);
WorkflowResponse workflowResponse = apiStateWFExecutor.execute(apiStateWorkflow);
response.setWorkflowResponse(workflowResponse);
} catch (WorkflowException e) {
handleException("Failed to execute workflow for life cycle status change : " + e.getMessage(), e);
}
// get the workflow state once the executor is executed.
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiId), WorkflowConstants.WF_TYPE_AM_API_STATE);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
response.setStateChangeStatus(apiWFState.toString());
} else {
response.setStateChangeStatus(WorkflowStatus.APPROVED.toString());
}
}
// apiWFState is null when simple wf executor is used because wf state is not stored in the db.
if (WorkflowStatus.APPROVED.equals(apiWFState) || apiWFState == null) {
targetStatus = "";
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiId, currentStatus.toUpperCase(), targetStatus.toUpperCase(), this.username, this.tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), tenantId, tenantDomain, apiName, apiId, uuid, apiVersion, apiType, apiContext, providerName, targetStatus);
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
return response;
}
}
} catch (GovernanceException e) {
String cause = e.getCause().getMessage();
if (!StringUtils.isEmpty(cause)) {
if (cause.contains("FaultGatewaysException:")) {
Map<String, Map<String, String>> faultMap = new HashMap<String, Map<String, String>>();
String faultJsonString;
if (!StringUtils.isEmpty(cause) && cause.split("FaultGatewaysException:").length > 1) {
faultJsonString = cause.split("FaultGatewaysException:")[1];
try {
JSONObject faultGatewayJson = (JSONObject) new JSONParser().parse(faultJsonString);
faultMap.putAll(faultGatewayJson);
throw new FaultGatewaysException(faultMap);
} catch (ParseException e1) {
log.error("Couldn't parse the Failed Environment json", e);
handleException("Couldn't parse the Failed Environment json : " + e.getMessage(), e);
}
}
} else if (cause.contains("APIManagementException:")) {
// This exception already logged from APIExecutor class hence this no need to logged again
handleException("Failed to change the life cycle status : " + cause.split("APIManagementException:")[1], e);
} else {
/* This exception already logged from APIExecutor class hence this no need to logged again
This block handles the all the exception which not have custom cause message*/
handleException("Failed to change the life cycle status : " + e.getMessage(), e);
}
}
return response;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return response;
}
use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class APIStateChangeWSWorkflowExecutor method execute.
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
if (log.isDebugEnabled()) {
log.debug("Executing API State change Workflow.");
log.debug("Execute workflowDTO " + workflowDTO.toString());
}
if (stateList != null) {
Map<String, List<String>> stateActionMap = getSelectedStatesToApprove();
APIStateWorkflowDTO apiStateWorkFlowDTO = (APIStateWorkflowDTO) workflowDTO;
if (stateActionMap.containsKey(apiStateWorkFlowDTO.getApiCurrentState().toUpperCase()) && stateActionMap.get(apiStateWorkFlowDTO.getApiCurrentState().toUpperCase()).contains(apiStateWorkFlowDTO.getApiLCAction())) {
// set the auth application related info. This will be used to call the callback service
setOAuthApplicationInfo(apiStateWorkFlowDTO);
// build request payload
String jsonPayload = buildPayloadForBPMNProcess(apiStateWorkFlowDTO);
if (log.isDebugEnabled()) {
log.debug("APIStateChange payload: " + jsonPayload);
}
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());
HttpPost httpPost = new HttpPost(serviceEndpoint + RUNTIME_INSTANCE_RESOURCE_PATH);
// Generate the basic auth header using provided user credentials
String authHeader = getBasicAuthHeader();
httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
StringEntity requestEntity = new StringEntity(jsonPayload, ContentType.APPLICATION_JSON);
httpPost.setEntity(requestEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
String error = "Error while starting the process: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();
log.error(error);
throw new WorkflowException(error);
}
} catch (ClientProtocolException e) {
String errorMsg = "Error while creating the http client";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} catch (IOException e) {
String errorMsg = "Error while connecting to the BPMN process server from the WorkflowExecutor.";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} finally {
httpPost.reset();
}
super.execute(workflowDTO);
} else {
// For any other states, act as simpleworkflow executor.
workflowDTO.setStatus(WorkflowStatus.APPROVED);
// calling super.complete() instead of complete() to act as the simpleworkflow executor
super.complete(workflowDTO);
}
} else {
String msg = "State change list is not provided. Please check <stateList> element in ";
log.error(msg);
throw new WorkflowException(msg);
}
return new GeneralWorkflowResponse();
}
Aggregations