use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class SampleWorkFlowExecutor method execute.
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
workflowDTO.setStatus(WorkflowStatus.APPROVED);
WorkflowResponse workflowResponse = complete(workflowDTO);
if (workflowDTO instanceof ApplicationRegistrationWorkflowDTO) {
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
((ApplicationRegistrationWorkflowDTO) workflowDTO).setApplicationInfo(oAuthApplicationInfo);
((ApplicationRegistrationWorkflowDTO) workflowDTO).setAccessTokenInfo(accessTokenInfo);
}
return workflowResponse;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatus.
/**
* This method is to change registry lifecycle states for an API or API Product artifact
*
* @param orgId UUID of the organization
* @param apiTypeWrapper API Type Wrapper
* @param action Action which need to execute from registry lifecycle
* @param checklist checklist items
* @return APIStateChangeResponse API workflow state and WorkflowResponse
*/
@Override
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action, Map<String, Boolean> checklist) throws APIManagementException, FaultGatewaysException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
String targetStatus;
String providerName;
String apiName;
String apiContext;
String apiType;
String apiVersion;
String currentStatus;
String uuid;
int apiOrApiProductId;
boolean isApiProduct = apiTypeWrapper.isAPIProduct();
String workflowType;
if (isApiProduct) {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
providerName = apiProduct.getId().getProviderName();
apiName = apiProduct.getId().getName();
apiContext = apiProduct.getContext();
apiType = apiProduct.getType();
apiVersion = apiProduct.getId().getVersion();
currentStatus = apiProduct.getState();
uuid = apiProduct.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
} else {
API api = apiTypeWrapper.getApi();
providerName = api.getId().getProviderName();
apiName = api.getId().getApiName();
apiContext = api.getContext();
apiType = api.getType();
apiVersion = api.getId().getVersion();
currentStatus = api.getStatus();
uuid = api.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIID(uuid);
workflowType = WorkflowConstants.WF_TYPE_AM_API_STATE;
}
String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
WorkflowStatus apiWFState = null;
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
}
// if the workflow has started, then executor should not fire again
if (!WorkflowStatus.CREATED.equals(apiWFState)) {
response = executeStateChangeWorkflow(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType);
// get the workflow state once the executor is executed.
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
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 = LCManagerFactory.getInstance().getLCManager().getStateForTransition(action);
apiPersistenceInstance.changeAPILifeCycle(new Organization(orgId), uuid, targetStatus);
sendLCStateChangeNotification(apiName, apiType, apiContext, apiVersion, targetStatus, providerName, apiOrApiProductId, uuid);
if (!isApiProduct) {
API api = apiTypeWrapper.getApi();
api.setOrganization(orgId);
changeLifeCycle(api, currentStatus, targetStatus, checklist);
// Sending Notifications to existing subscribers
if (APIConstants.PUBLISHED.equals(targetStatus)) {
sendEmailNotification(api);
}
} else {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
apiProduct.setOrganization(orgId);
changeLifecycle(apiProduct, currentStatus, targetStatus);
}
addLCStateChangeInDatabase(currentStatus, targetStatus, uuid);
if (log.isDebugEnabled()) {
String logMessage = "LC Status changed successfully for artifact with name: " + apiName + ", version " + apiVersion + ", New Status : " + targetStatus;
log.debug(logMessage);
}
extractRecommendationDetails(apiTypeWrapper);
return response;
}
} catch (APIPersistenceException e) {
handleException("Error while accessing persistence layer", e);
} catch (PersistenceException e) {
handleException("Error while accessing lifecycle information ", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return response;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse 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.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class APIProviderImpl method executeStateChangeWorkflow.
/**
* Execute state change workflow
*
* @param currentStatus Current Status of the API or API Product
* @param action LC state change action
* @param apiName Name of API or API Product
* @param apiContext Context of API or API Product
* @param apiType API Type
* @param apiVersion Version of API or API Product
* @param providerName Provider of API or API Product
* @param apiOrApiProductId Unique ID API or API Product
* @param uuid UUID of the API or API Product
* @param gatewayVendor Gateway vendor
* @param workflowType Workflow Type
* @return APIStateChangeResponse
* @throws APIManagementException Error when executing the state change workflow
*/
private APIStateChangeResponse executeStateChangeWorkflow(String currentStatus, String action, String apiName, String apiContext, String apiType, String apiVersion, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType) throws APIManagementException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
WorkflowExecutor apiStateWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflowType);
APIStateWorkflowDTO apiStateWorkflow = setAPIStateWorkflowDTOParameters(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType, apiStateWFExecutor);
WorkflowResponse workflowResponse = apiStateWFExecutor.execute(apiStateWorkflow);
response.setWorkflowResponse(workflowResponse);
} catch (WorkflowException e) {
handleException("Failed to execute workflow for life cycle status change : " + e.getMessage(), e);
}
return response;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Creates a new subscriptions with the details specified in the body parameter
*
* @param body new subscription details
* @return newly added subscription as a SubscriptionDTO if successful
*/
@Override
public Response subscriptionsPost(SubscriptionDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String userOrganization = RestApiUtil.getValidatedSubjectOrganization(messageContext);
apiConsumer = RestApiCommonUtil.getConsumer(username, userOrganization);
String applicationId = body.getApplicationId();
// this will throw a APIMgtResourceNotFoundException
if (body.getApiId() != null) {
if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(body.getApiId(), organization)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, body.getApiId(), log);
}
} else {
RestApiUtil.handleBadRequest("Request must contain either apiIdentifier or apiProductIdentifier and the relevant type", log);
return null;
}
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
// required application not found
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
return null;
}
// If application creation workflow status is pending or rejected, throw a Bad request exception
if (application.getStatus().equals(WorkflowStatus.REJECTED.toString()) || application.getStatus().equals(WorkflowStatus.CREATED.toString())) {
RestApiUtil.handleBadRequest("Workflow status is not Approved", log);
return null;
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
// application access failure occurred
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(body.getApiId(), organization);
apiTypeWrapper.setTier(body.getThrottlingPolicy());
SubscriptionResponse subscriptionResponse = apiConsumer.addSubscription(apiTypeWrapper, username, application);
SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, apiTypeWrapper, organization);
WorkflowResponse workflowResponse = subscriptionResponse.getWorkflowResponse();
if (workflowResponse instanceof HttpWorkflowResponse) {
String payload = workflowResponse.getJSONPayload();
addedSubscriptionDTO.setRedirectionParams(payload);
}
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTIONS + "/" + addedSubscribedAPI.getUUID())).entity(addedSubscriptionDTO).build();
} catch (APIMgtAuthorizationFailedException e) {
// this occurs when the api:application:tier mapping is not allowed. The reason for the message is taken from
// the message of the exception e
RestApiUtil.handleAuthorizationFailure(e.getMessage(), e, log);
} catch (SubscriptionAlreadyExistingException e) {
RestApiUtil.handleResourceAlreadyExistsError("Specified subscription already exists for API " + body.getApiId() + ", for application " + body.getApplicationId(), e, log);
} catch (URISyntaxException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
// this happens when the specified API identifier does not exist
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, body.getApiId(), e, log);
} else {
// unhandled exception
RestApiUtil.handleInternalServerError("Error while adding the subscription API:" + body.getApiId() + ", application:" + body.getApplicationId() + ", tier:" + body.getThrottlingPolicy(), e, log);
}
}
return null;
}
Aggregations