use of org.wso2.carbon.apimgt.impl.workflow.GeneralWorkflowResponse 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();
}
use of org.wso2.carbon.apimgt.impl.workflow.GeneralWorkflowResponse 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();
}
use of org.wso2.carbon.apimgt.impl.workflow.GeneralWorkflowResponse 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();
}
use of org.wso2.carbon.apimgt.impl.workflow.GeneralWorkflowResponse 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();
}
use of org.wso2.carbon.apimgt.impl.workflow.GeneralWorkflowResponse in project carbon-apimgt by wso2.
the class APIConsumerImpl method updateSubscription.
@Override
public SubscriptionResponse updateSubscription(ApiTypeWrapper apiTypeWrapper, String userId, Application application, String inputSubscriptionId, String currentThrottlingPolicy, String requestedThrottlingPolicy) throws APIManagementException {
API api = null;
APIProduct product = null;
Identifier identifier = null;
int apiId;
String apiUUId;
final boolean isApiProduct = apiTypeWrapper.isAPIProduct();
String state;
String apiContext;
if (isApiProduct) {
product = apiTypeWrapper.getApiProduct();
state = product.getState();
apiId = product.getProductId();
apiUUId = product.getUuid();
identifier = product.getId();
apiContext = product.getContext();
} else {
api = apiTypeWrapper.getApi();
state = api.getStatus();
identifier = api.getId();
apiId = identifier.getId();
apiUUId = api.getUuid();
apiContext = api.getContext();
}
checkSubscriptionAllowed(apiTypeWrapper);
WorkflowResponse workflowResponse = null;
int subscriptionId;
if (APIConstants.PUBLISHED.equals(state)) {
subscriptionId = apiMgtDAO.updateSubscription(apiTypeWrapper, inputSubscriptionId, APIConstants.SubscriptionStatus.TIER_UPDATE_PENDING, requestedThrottlingPolicy);
boolean isTenantFlowStarted = false;
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = startTenantFlowForTenantDomain(tenantDomain);
}
try {
WorkflowExecutor updateSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE);
SubscriptionWorkflowDTO workflowDTO = new SubscriptionWorkflowDTO();
workflowDTO.setStatus(WorkflowStatus.CREATED);
workflowDTO.setCreatedTime(System.currentTimeMillis());
workflowDTO.setTenantDomain(tenantDomain);
workflowDTO.setTenantId(tenantId);
workflowDTO.setExternalWorkflowReference(updateSubscriptionWFExecutor.generateUUID());
workflowDTO.setWorkflowReference(String.valueOf(subscriptionId));
workflowDTO.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE);
workflowDTO.setCallbackUrl(updateSubscriptionWFExecutor.getCallbackURL());
workflowDTO.setApiName(identifier.getName());
workflowDTO.setApiContext(apiContext);
workflowDTO.setApiVersion(identifier.getVersion());
workflowDTO.setApiProvider(identifier.getProviderName());
workflowDTO.setTierName(identifier.getTier());
workflowDTO.setRequestedTierName(requestedThrottlingPolicy);
workflowDTO.setApplicationName(application.getName());
workflowDTO.setApplicationId(application.getId());
workflowDTO.setSubscriber(userId);
Tier tier = null;
Set<Tier> policies = Collections.emptySet();
if (!isApiProduct) {
policies = api.getAvailableTiers();
} else {
policies = product.getAvailableTiers();
}
for (Tier policy : policies) {
if (policy.getName() != null && (policy.getName()).equals(workflowDTO.getTierName())) {
tier = policy;
}
}
boolean isMonetizationEnabled = false;
if (api != null) {
isMonetizationEnabled = api.getMonetizationStatus();
// check whether monetization is enabled for API and tier plan is commercial
if (isMonetizationEnabled && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
workflowResponse = updateSubscriptionWFExecutor.monetizeSubscription(workflowDTO, api);
} else {
workflowResponse = updateSubscriptionWFExecutor.execute(workflowDTO);
}
} else {
isMonetizationEnabled = product.getMonetizationStatus();
// check whether monetization is enabled for API and tier plan is commercial
if (isMonetizationEnabled && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
workflowResponse = updateSubscriptionWFExecutor.monetizeSubscription(workflowDTO, product);
} else {
workflowResponse = updateSubscriptionWFExecutor.execute(workflowDTO);
}
}
} catch (WorkflowException e) {
throw new APIManagementException("Could not execute Workflow", e);
} finally {
if (isTenantFlowStarted) {
endTenantFlow();
}
}
// to handle on-the-fly subscription rejection (and removal of subscription entry from the database)
// the response should have {"Status":"REJECTED"} in the json payload for this to work.
boolean subscriptionRejected = false;
String subscriptionStatus = null;
String subscriptionUUID = "";
SubscribedAPI updatedSubscription = getSubscriptionById(subscriptionId);
if (workflowResponse != null && workflowResponse.getJSONPayload() != null && !workflowResponse.getJSONPayload().isEmpty()) {
try {
JSONObject wfResponseJson = (JSONObject) new JSONParser().parse(workflowResponse.getJSONPayload());
if (APIConstants.SubscriptionStatus.REJECTED.equals(wfResponseJson.get("Status"))) {
subscriptionRejected = true;
subscriptionStatus = APIConstants.SubscriptionStatus.REJECTED;
}
} catch (ParseException e) {
log.error('\'' + workflowResponse.getJSONPayload() + "' is not a valid JSON.", e);
}
}
if (!subscriptionRejected) {
subscriptionStatus = updatedSubscription.getSubStatus();
subscriptionUUID = updatedSubscription.getUUID();
JSONObject subsLogObject = new JSONObject();
subsLogObject.put(APIConstants.AuditLogConstants.API_NAME, identifier.getName());
subsLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_ID, application.getId());
subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_NAME, application.getName());
subsLogObject.put(APIConstants.AuditLogConstants.TIER, identifier.getTier());
subsLogObject.put(APIConstants.AuditLogConstants.REQUESTED_TIER, requestedThrottlingPolicy);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.SUBSCRIPTION, subsLogObject.toString(), APIConstants.AuditLogConstants.UPDATED, this.username);
if (workflowResponse == null) {
workflowResponse = new GeneralWorkflowResponse();
}
}
// get the workflow state once the executor is executed.
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(subscriptionId), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE);
// wfDTO is null when simple wf executor is used because wf state is not stored in the db and is always approved.
if (wfDTO != null) {
if (WorkflowStatus.APPROVED.equals(wfDTO.getStatus())) {
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), tenantId, tenantDomain, subscriptionId, updatedSubscription.getUUID(), apiId, apiUUId, application.getId(), application.getUUID(), requestedThrottlingPolicy, subscriptionStatus);
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
}
} else {
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), tenantId, tenantDomain, subscriptionId, updatedSubscription.getUUID(), apiId, apiUUId, application.getId(), application.getUUID(), requestedThrottlingPolicy, subscriptionStatus);
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
}
if (log.isDebugEnabled()) {
String logMessage = "API Name: " + identifier.getName() + ", API Version " + identifier.getVersion() + ", Subscription Status: " + subscriptionStatus + " subscribe by " + userId + " for app " + application.getName();
log.debug(logMessage);
}
return new SubscriptionResponse(subscriptionStatus, subscriptionUUID, workflowResponse);
} else {
throw new APIMgtResourceNotFoundException("Subscriptions not allowed on APIs/API Products in the state: " + state);
}
}
Aggregations