use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class APIStateChangeWSWorkflowExecutor method setOAuthApplicationInfo.
/**
* set information that are needed to invoke callback service
*/
private void setOAuthApplicationInfo(APIStateWorkflowDTO apiStateWorkFlowDTO) throws WorkflowException {
// if credentials are not defined in the workflow-extension.xml file call dcr endpoint and generate a
// oauth application and pass the client id and secret
WorkflowProperties workflowProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getWorkflowProperties();
if (clientId == null || clientSecret == null) {
String dcrUsername = workflowProperties.getdCREndpointUser();
String dcrPassword = workflowProperties.getdCREndpointPassword();
byte[] encodedAuth = Base64.encodeBase64((dcrUsername + ":" + dcrPassword).getBytes(Charset.forName("ISO-8859-1")));
JSONObject payload = new JSONObject();
payload.put(PayloadConstants.KEY_OAUTH_APPNAME, WorkflowConstants.WORKFLOW_OAUTH_APP_NAME);
payload.put(PayloadConstants.KEY_OAUTH_OWNER, dcrUsername);
payload.put(PayloadConstants.KEY_OAUTH_SAASAPP, "true");
payload.put(PayloadConstants.KEY_OAUTH_GRANT_TYPES, WorkflowConstants.WORKFLOW_OAUTH_APP_GRANT_TYPES);
URL serviceEndpointURL = new URL(workflowProperties.getdCREndPoint());
HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
HttpPost httpPost = new HttpPost(workflowProperties.getdCREndPoint());
String authHeader = "Basic " + new String(encodedAuth);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
StringEntity requestEntity = new StringEntity(payload.toJSONString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(requestEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
String responseStr = EntityUtils.toString(entity);
if (log.isDebugEnabled()) {
log.debug("Workflow oauth app created: " + responseStr);
}
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject) parser.parse(responseStr);
clientId = (String) obj.get(PayloadConstants.VARIABLE_CLIENTID);
clientSecret = (String) obj.get(PayloadConstants.VARIABLE_CLIENTSECRET);
} else {
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 dcr endpoint";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} catch (ParseException e) {
String errorMsg = "Error while parsing response from DCR endpoint";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} finally {
httpPost.reset();
}
}
apiStateWorkFlowDTO.setClientId(clientId);
apiStateWorkFlowDTO.setClientSecret(clientSecret);
apiStateWorkFlowDTO.setScope(WorkflowConstants.API_WF_SCOPE);
apiStateWorkFlowDTO.setTokenAPI(workflowProperties.getTokenEndPoint());
}
use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class WorkflowExecutorFactory method createWorkflowDTO.
/**
* Create a DTO object related to a given workflow type.
* @param wfType Type of the workflow.
*/
public WorkflowDTO createWorkflowDTO(String wfType) {
WorkflowDTO workflowDTO = null;
if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equals(wfType)) {
workflowDTO = new ApplicationWorkflowDTO();
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION.equals(wfType)) {
workflowDTO = new ApplicationRegistrationWorkflowDTO();
((ApplicationRegistrationWorkflowDTO) workflowDTO).setKeyType(APIConstants.API_KEY_TYPE_PRODUCTION);
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX.equals(wfType)) {
workflowDTO = new ApplicationRegistrationWorkflowDTO();
((ApplicationRegistrationWorkflowDTO) workflowDTO).setKeyType(APIConstants.API_KEY_TYPE_SANDBOX);
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equals(wfType)) {
workflowDTO = new SubscriptionWorkflowDTO();
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE.equals(wfType)) {
workflowDTO = new SubscriptionWorkflowDTO();
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_USER_SIGNUP.equals(wfType)) {
workflowDTO = new WorkflowDTO();
workflowDTO.setWorkflowType(wfType);
} else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equals(wfType) || WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE.equals(wfType)) {
workflowDTO = new APIStateWorkflowDTO();
workflowDTO.setWorkflowType(wfType);
}
return workflowDTO;
}
use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class WorkflowUtils method sendNotificationAfterWFComplete.
public static void sendNotificationAfterWFComplete(WorkflowDTO workflowDTO, String wfType) throws APIManagementException {
if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equalsIgnoreCase(wfType)) {
String applicationId = workflowDTO.getWorkflowReference();
int appId = Integer.parseInt(applicationId);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Application application = apiMgtDAO.getApplicationById(appId);
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
appWFDto.setApplication(application);
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_CREATE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), application.getSubscriber().getName());
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION.equalsIgnoreCase(wfType)) {
ApplicationWorkflowDTO appWFDto = (ApplicationWorkflowDTO) workflowDTO;
ApplicationEvent applicationEvent = new ApplicationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_DELETE.name(), appWFDto.getTenantId(), appWFDto.getTenantDomain(), appWFDto.getApplication().getId(), appWFDto.getApplication().getUUID(), appWFDto.getApplication().getName(), appWFDto.getApplication().getTokenType(), appWFDto.getApplication().getTier(), appWFDto.getApplication().getGroupId(), appWFDto.getApplication().getApplicationAttributes(), "");
APIUtil.sendNotification(applicationEvent, APIConstants.NotifierType.APPLICATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_CREATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION.equalsIgnoreCase(wfType)) {
SubscriptionWorkflowDTO subWFDto = (SubscriptionWorkflowDTO) workflowDTO;
SubscribedAPI sub = ApiMgtDAO.getInstance().getSubscriptionById(Integer.parseInt(subWFDto.getWorkflowReference()));
SubscriptionEvent subscriptionEvent;
if (sub.getApiId() != null) {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getIdentifier().getId(), sub.getIdentifier().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
} else {
subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), subWFDto.getTenantId(), subWFDto.getTenantDomain(), Integer.parseInt(subWFDto.getWorkflowReference()), sub.getUUID(), sub.getProductId().getId(), sub.getProductId().getUUID(), sub.getApplication().getId(), sub.getApplication().getUUID(), sub.getTier().getName(), sub.getSubCreatedStatus());
}
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
} else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equalsIgnoreCase(wfType) || WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE.equalsIgnoreCase(wfType)) {
APIStateWorkflowDTO apiStateWFDto = (APIStateWorkflowDTO) workflowDTO;
APIEvent apiEvent = new APIEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.API_LIFECYCLE_CHANGE.name(), apiStateWFDto.getTenantId(), apiStateWFDto.getTenantDomain(), apiStateWFDto.getApiName(), Integer.parseInt(apiStateWFDto.getWorkflowReference()), apiStateWFDto.getApiUUID(), apiStateWFDto.getApiVersion(), apiStateWFDto.getApiType(), apiStateWFDto.getApiContext(), apiStateWFDto.getApiProvider(), apiStateWFDto.getApiLCAction());
APIUtil.sendNotification(apiEvent, APIConstants.NotifierType.API.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
} else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX.equalsIgnoreCase(wfType)) {
ApplicationRegistrationWorkflowDTO appRegWFDto = (ApplicationRegistrationWorkflowDTO) workflowDTO;
ApplicationRegistrationEvent applicationRegistrationEvent = new ApplicationRegistrationEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.APPLICATION_REGISTRATION_CREATE.name(), appRegWFDto.getTenantId(), appRegWFDto.getTenantDomain(), appRegWFDto.getApplication().getId(), appRegWFDto.getApplication().getUUID(), appRegWFDto.getApplicationInfo().getClientId(), appRegWFDto.getApplication().getTokenType(), appRegWFDto.getKeyManager());
APIUtil.sendNotification(applicationRegistrationEvent, APIConstants.NotifierType.APPLICATION_REGISTRATION.name());
}
}
use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO in project carbon-apimgt by wso2.
the class APIProviderImpl method setAPIStateWorkflowDTOParameters.
/**
* Set API or API Product state change workflow parameters
*
* @param currentStatus Current state of the API or API Product
* @param action LC state change action
* @param name Name of the API or API Product
* @param context Context of the API or API Product
* @param apiType API or API Product
* @param version Version of API or API Product
* @param providerName Owner of the API or API Product
* @param apiOrApiProductId Unique ID of the API or API Product
* @param uuid Unique UUID of the API or API Product
* @param gatewayVendor Gateway Vendor
* @param workflowType Workflow Type
* @param apiStateWFExecutor WorkflowExecutor
* @return APIStateWorkflowDTO Object
*/
private APIStateWorkflowDTO setAPIStateWorkflowDTOParameters(String currentStatus, String action, String name, String context, String apiType, String version, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType, WorkflowExecutor apiStateWFExecutor) {
WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
APIStateWorkflowDTO stateWorkflowDTO = new APIStateWorkflowDTO();
stateWorkflowDTO.setApiCurrentState(currentStatus);
stateWorkflowDTO.setApiLCAction(action);
stateWorkflowDTO.setApiName(name);
stateWorkflowDTO.setApiContext(context);
stateWorkflowDTO.setApiType(apiType);
stateWorkflowDTO.setApiVersion(version);
stateWorkflowDTO.setApiProvider(providerName);
stateWorkflowDTO.setGatewayVendor(gatewayVendor);
stateWorkflowDTO.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
stateWorkflowDTO.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
stateWorkflowDTO.setTenantId(tenantId);
stateWorkflowDTO.setTenantDomain(this.tenantDomain);
stateWorkflowDTO.setWorkflowType(workflowType);
stateWorkflowDTO.setStatus(WorkflowStatus.CREATED);
stateWorkflowDTO.setCreatedTime(System.currentTimeMillis());
stateWorkflowDTO.setWorkflowReference(Integer.toString(apiOrApiProductId));
stateWorkflowDTO.setInvoker(this.username);
stateWorkflowDTO.setApiUUID(uuid);
String workflowDescription = "Pending lifecycle state change action: " + action;
stateWorkflowDTO.setWorkflowDescription(workflowDescription);
return stateWorkflowDTO;
}
use of org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO 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;
}
Aggregations