This section explains how to attach a custom workflow to the API subscription operation in the API Manager. First, see Workflow Extensions for information on different types of workflows executors.
Attaching a custom workflow to API subscription enables you to add throttling tiers to an API that consumers cannot choose at the time of subscribing. Only admins can set these tiers to APIs. It also allows you to restrict API consumers to only subscribe to sandbox, and then go through an approval process to go to the next level of subscription.
Configuring the Business Process Server
Download WSO2 Business Process Server.
Set an offset of 2 to the default BPS port in
<BPS_HOME>/repository/conf/carbon.xml
file. This prevents port conflicts that occur when you start more than one WSO2 product on the same server. Also see Changing the Default Ports with Offset.<Offset>2</Offset>
If you change the port offset to some other value or run the API Manager and BPS on different machines (therefore, want to set the
hostname
to a different value thanlocalhost
), you must do the following:- Search and replace the value 9765 in all the files (.epr, .wsd files inside the ZIP archives) inside
<APIM_HOME>/business-processes
folder with the new port - Zip the files you unzipped earlier and deploy the newly created zip file in BPS as explained in the steps below
- Search and replace port 9445 in
<AM_HOME>/repository/deployment/server/jaggeryapps/admin-dashboard/site/conf/site.json
file
- Search and replace the value 9765 in all the files (.epr, .wsd files inside the ZIP archives) inside
Copy the following from
<APIM_HOME>/business-processes/epr
to<BPS_HOME>/repository/conf/epr
folder.
-
SubscriptionService.epr
SubscriptionCallbackService.epr
-
Start the BPS server and log in to its management console (
https://<Server Host>:9443+<port offset>/carbon
).- Select Add under Processes menu and upload t he
<APIM_HOME>
/business-processes/subscription-creation/BPEL/SubscriptionApprovalWorkFlowProcess_1.0.0.zip
file to BPS. This is the business process archive file.
- Similarly, upload
<APIM_HOME>/business-processes/subscription-creation/HumanTask/SubscriptionsApprovalTask-1.0.0.zip
to BPS. This is the human task archived file.
Engaging the WS Workflow Executor in the API Manager
First, enable the API subscription workflow.
Log in to APIM admin console (
https://<Server Host>:9443/carbon
) and select Browse under Resources.
Go to
/_system/governance/apimgt/applicationdata/workflow-extensions.xml
resource, disable the Simple Workflow Executor and enable WS Workflow Executor. Also specify the service endpoint where the workflow engine is hosted and the credentials required to access the said service via basic authentication (i.e., username/password based authentication).<WorkFlowExtensions> <!--SubscriptionCreation executor="org.wso2.carbon.apimgt.impl.workflow.SubscriptionCreationSimpleWorkflowExecutor"/--> <SubscriptionCreation executor="org.wso2.carbon.apimgt.impl.workflow.SubscriptionCreationWSWorkflowExecutor"> <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> </WorkFlowExtensions>
The application creation WS Workflow Executor is now engaged.
- Go to the API Store Web interface and subscribe to an API.
It invokes the API subscription process and creates a Human Task instance that holds the execution of the BPEL until some action is performed on it. Note the message that appears if the BPEL is invoked correctly, saying that the request is successfully submitted.
Log in to the workflow-admin app (
https://localhost:9443/workflow-admin
), list all the tasks for API subscription and approve the task. It resumes the BPEL process and completes the API subscription.Go back to the API Store and see that the user is now subscribed to the API.
Whenever a user tries to subscribe to an API, a request of the following format is sent to the workflow endpoint:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wor="http://workflow.subscription.apimgt.carbon.wso2.org"> <soapenv:Header/> <soapenv:Body> <wor:createSubscription> <wor:apiName>sampleAPI</wor:apiName> <wor:apiVersion>1.0.0</wor:apiVersion> <wor:apiContext>/sample</wor:apiContext> <wor:apiProvider>admin</wor:apiProvider> <wor:subscriber>subscriber1</wor:subscriber> <wor:applicationName>application1</wor:applicationName> <wor:tierName>gold</wor:tierName> <wor:workflowExternalRef></wor:workflowExternalRef> <wor:callBackURL>?</wor:callBackURL> </wor:createSubscription> </soapenv:Body> </soapenv:Envelope>
Elements of the above configuration are described below:
Element Description apiName
Name of the API to which subscription is requested. apiVersion
Version of the API the user subscribes to. apiContext
Context in which the requested API is to be accessed. apiProvider
Provider of the API. subscriber
Name of the user requesting subscription. applicationName
Name of the application through which the user subscribes to the API. tierName
Throttling tiers specified for the application. workflowExternalRef The unique reference against which a workflow is tracked. This needs to be sent back from the workflow engine to the API Manager at the time of workflow completion. callBackURL
The URL to which the Workflow completion request is sent to by the workflow engine, at the time of workflow completion. This property is configured under the callBackURL property in the api-manager.xml
(Can be either a SOAP or REST endpoint)
Also, when executing a workflow, an entry is added to the
AM_WORKFLOWS
table in the API Manager Database, indicating the workflow status and workflow external reference along with other information. This entry is used to track the progress of the workflow throughout its lifecycle. At a given time, the status of a workflow can beCREATED
,APPROVED
orREJECTED
.CREATED
is the default status of a workflow. It gets promoted toAPPROVED
orREJECTED
, based on the response from the workflow engine.