By default, subscription workflow applies to all the APIs. But in case if you need to apply workflow only for the selected APIs, there is no way to achieve that. But this can be achieved via the available workflow extensions.
The solution we are going to implement is, apply the workflow by checking the properties of an API. An example use case is as below. In case if you have two types of APIs name secured and public. But you need to review and approve subscription creation for secured APIs. Subscription creation for the public APIs is approved without user approval. Let’s see how we can implement this in APIM 3.0.0. Type of the API is defined as an API property and it is done by the API developer.
The default subscription workflow implementation for workflow engines is “org.wso2.carbon.apimgt.impl.workflow.SubscriptionCreationWSWorkflowExecutor”. We can extend the implementation and apply the new logic by overriding its implementation. In the workflow extension, we can check the API properties and check API contain a property “type” with a “secured” value. In such cases, we engage the user approval workflow. If the above property is not there, subscription creation completes without user interaction.
<dependency> <groupId>org.wso2.carbon.apimgt</groupId> <artifactId>org.wso2.carbon.apimgt.impl</artifactId> <version>6.5.349</version> </dependency>
SubscriptionWorkflowDTO subsWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO; APIIdentifier apiIdentifier = new APIIdentifier(subsWorkflowDTO.getApiProvider(), subsWorkflowDTO.getApiName(), subsWorkflowDTO.getApiVersion()); try { APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(); API api = apiConsumer.getAPI(apiIdentifier); if (api.getAdditionalProperties().containsKey(WF_TYPE) && WF_TYPE_SECURED .equalsIgnoreCase(api.getAdditionalProperties().get(WF_TYPE).toString())) { return super.execute(workflowDTO); } } catch (APIManagementException e) { log.error("Error occurred while processing workflow request.", e); throw new WorkflowException("Error occurred while processing workflow request.", e); } //Default flow: complete the subscription creation workflowDTO.setStatus(WorkflowStatus.APPROVED); WorkflowResponse workflowResponse = complete(workflowDTO); super.publishEvents(workflowDTO); return workflowResponse;
<SubscriptionCreation executor="com.rukspot.sample.apimgt.workflow.apibase.SubscriptionWorkflow"> <Property name="serviceEndpoint">http://localhost:9765/services/SubscriptionApprovalWorkFlowProcess/</Property> <Property name="username">admin</Property> <Property name="password">admin</Property> <Property name="callbackURL">https://localhost:8243/services/WorkflowCallbackService</Property> </SubscriptionCreation>
Please find the sample code from GitHub
Add Comment
Comments (0)