Working with the REST API - Activiti 5.x Business Process Management Beginner's Guide (2014)

Activiti 5.x Business Process Management Beginner's Guide (2014)

Chapter 7. Working with the REST API

In the previous chapter, we provided information regarding the Activiti Process Engine's APIs and how to use the Process Engine's JAR file and APIs. In this chapter, we will work with the Process Engine using the REST service.

In this chapter, we will go through the following topics:

· An overview of the Activiti REST API

· Implementing REST services

· Working with Repository APIs as REST

· Using Process APIs as REST

· Using Task APIs as REST

· Working with users using REST

· Using management services as REST

With the help of REST services, we can access the process engine from any sort of device implemented in any of the languages. So, with the help of REST, there is no barrier to communicating with the Activiti Engine.

Overview of the Activiti REST API

In Chapter 1, Installing Activiti, we saw that Activiti provides activiti-rest.war to allow you to access the process engine as a REST Service. As the activiti-rest.war file is provided by Activiti, we need not create a separate web service for the process engine. InChapter 6, The Activiti ProcessEngine API, we saw that we can access the process engine using APIs by adding the respective JAR files. We can also access the Process Engine APIs without importing the respective JAR files, that is, using REST services. To access the process engine as REST, you need to deploy the activiti-rest.war file into a web server such as apache-tomcat. You will find activiti-rest.war in the wars folder at the path where you extracted activiti-5.x.zip/activiti-5.13.0/wars, as shown in the following screenshot:

Overview of the Activiti REST API

Time for action – implementing the REST service

To access Activiti as a REST service, we need to perform some configurations in our Tomcat server. Now, we will take a look at these configurations. Perform the following steps to implement the REST services:

1. As we want to access the REST services of Activiti, we will deploy the activiti-rest.war file in the apache-tomcat web server. Copy the activiti-rest.war file and paste it to the webapps folder that can be found at apache-tomcat ../apache-tomcat-7.0.37/webapps, as shown in the following screenshot:

Time for action – implementing the REST service

2. If your Tomcat server is already running, the activiti-rest.war file will be deployed automatically. If Tomcat is not running, start your Tomcat server; then, your activiti-rest.war file will be deployed and you will be able to find the deployed folder (activiti-rest) in apache-tomcat's webapps folder (apache-tomcat ../apache-tomcat-7.0.3/webapps), as shown in the following screenshot:

Time for action – implementing the REST service

3. After the successful deployment of the activiti-rest file, we need to connect the activiti-rest file to the database that we had configured in Chapter 4, Management and Monitoring Using the Activiti Explorer. Browse to the classes folder of activiti-rest(../apache-tomcat-7.0.37/webapps/activiti-rest/WEB-INF/classes) and edit the db.properties file shown in the following screenshot:

Time for action – implementing the REST service

4. By default, the db.properties file will be connected to the H2 database; we need to change it to mysql (which we have configured in Chapter 4, Management and Monitoring, using the Activiti Explorer), as shown in the following code:

5. db=mysql

6. jdbc.driver=com.mysql.jdbc.Driver

7. jdbc.url=jdbc:mysql://localhost:3306/activiti

8. jdbc.username=root

jdbc.password=root

9. After executing all the preceding configurations, you need to restart your web server so that the changes are applied.

10. To check whether or not the REST service can be accessed, you need to add the RESTClient plugin to your browser.

11. To install RESTClient in the Firefox browser, we need to download it from the URL https://addons.mozilla.org/en-US/firefox/addon/restclient/. This URL will take you to the page shown in the following screenshot:

Time for action – implementing the REST service

12. Once you have successfully installed the plugin, the REST icon will be displayed on the toolbar as shown in the following screenshot; you can open the RESTClient by clicking on this icon:

Time for action – implementing the REST service

13. On opening the RESTClient, the following screenshot will appear; we then need to perform the authentication to be able to access the REST services of the Activiti Engine. Navigate to Authentication | Basic Authentication, as shown in the following screenshot:

Time for action – implementing the REST service

14. A Basic Authorization window will pop up, where we need to specify the username and password. Enter kermit in the Username and Password fields for authentication, as shown in the following screenshot:

Time for action – implementing the REST service

15. On successful authentication, you can access the REST API of the Activiti Engine. For testing, you can provide the URL http://localhost:8080/activiti-rest/service/deployments in the URL field in the RESTClient window and the Method field should be set toGET. On executing this, you should get the output in the Response Headers section, as shown in the following screenshot:

Time for action – implementing the REST service

Note

Before execution, the Tomcat server in which you have deployed the activiti-rest.war file should be running; otherwise, you won't be able to see the output.

What just happened?

We just went through how to configure activiti-rest and access the Activiti Process Engine by performing REST calls using RESTClient.

Working with REST API services

As we have configured the RESTClient in the browser, we will now start accessing the REST API using RESTClient.

Time for action – working with Repository APIs as REST

As in Chapter 6, The Activiti ProcessEngine API, we have gone through the deployment of a business process and used the Repository API for deployment purposes. In this section, we will perform the following steps to install and deploy the business process using the Repository REST API:

1. We will start by fetching all the deployed processes from the repository. For this operation, we will enter the URL url/repository/deployments in the browser with the GET method; the response will fetch all the deployed processes from the repository.

Time for action – working with Repository APIs as REST

2. All the processes deployed in the process engine will be displayed when we navigate to the preceding URL, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

3. To fetch a specific process or resource from a deployment, we need to specify the process ID with the URL, for example, /repository /deployments/{deploymentId}, with the GET method. In the following screenshot, we have provided the deployment ID 23:

Time for action – working with Repository APIs as REST

4. On navigating to the preceding URL, only the process that we want to retrieve will be displayed, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

5. Apart from fetching the resources for and displaying the process, we can also delete it. To delete the process, we need to set the Method field to DELETE with the URL field set to url /repository/deployment/{deploymentId}, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

6. On navigating to the preceding URL, the business process will be deleted from the repository and the response will be as follows:

Time for action – working with Repository APIs as REST

7. We can also achieve the preceding functionality by performing class implementation. Create a new project in Eclipse and add some external JAR files, as shown in the following screenshot, that will be required to execute this example as mentioned previously:

Time for action – working with Repository APIs as REST

8. We will create a class file Repository_Deployment with the following code:

9. private static String REST_URI = "http://localhost:8080/activiti-rest/service";

10.

11. private static ClientResource getClientResource(String uri) {

12. ClientResource resource = new ClientResource(uri);

13. resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit");

14. return resource;

15. }

16.

17. public static JSONArray getDeployments() throws JSONException, IOException {

18. String deploymentURI = REST_URI + "/repository/deployments";

19. Representation response = getClientResource(deploymentURI).get(

20. MediaType.APPLICATION_JSON);

21. JSONObject object = new JSONObject(response.getText());

22. if (object != null) {

23. JSONArray dataArray = (JSONArray) object.get("data");

24. return dataArray;

25.

26. }

27. return null;

}

28. In the preceding code, we created a link to the REST URL and authenticated it with the user kermit; the getDeployments() method will fetch all the deployed processes from the engine and return it as a JSONArray object.

29. Now, to test the REST services, we need to create a Test Rest API class within src/test/java, which will have the following code:

30.@Test

31. public void deploymentTest() throws JSONException, IOException {

32. JSONArray procesArray = Repository_Deployment.getDeployments();

33. for (int i = 0; i < processArray.length(); i++) {

34. JSONObject jasonObject = (JSONObject) processArray.get(i);

35. assertNotNull(jasonObject);

36. System.out.println(jasonObject.toString());

37. }

}

38. To test the given code, you can right-click on the project and navigate to Run As | JUnit Test, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

39. On successful execution of the REST service, the output will show the number of processes deployed into the process engine, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

40. Now we will provide a URL with a process ID to retrieve a specific process from the repository. We will create a method in the Repository_Deployment class that takes the process ID as an input parameter and provides the result for that process, as shown in the following code:

41.public static JSONObject getDeployments(int id) throws JSONException, IOException {

42. String deploymentURI = REST_URI + "/repository/deployments/" + id;

43. Representation response = getClientResource(deploymentURI).get(

44. MediaType.APPLICATION_JSON);

45. JSONObject object = new JSONObject(response.getText());

46. if (object != null) {

47. return object;

48. }

49. return null;

}

50. To test our business process, we will create a method through which we will pass the process ID and retrieve the details of that process, as shown in the following code:

51. @Test

52. public void deploymentById() throws JSONException, IOException {

53. JSONObject processObject = Repository_Deployment.getDeployments(23);

54. assertNotNull(processObject);

55. System.out.println(processObject.toString());

}

56. We have provided an ID to fetch a unique process, so on executing the test file the following output will be displayed:

Time for action – working with Repository APIs as REST

57. So far, we have seen how to get the process deployment files; now we will learn how to delete a process from the repository. Create the deleteDeployment method in the Repository_Deployment class; here in the response, we are calling the delete method, as shown in the following code:

58.public static void deleteDeployment(int id) {

59. String deploymentURI = REST_URI + "/repository/deployments/" + id;

60. Representation response = getClientResource(deploymentURI).delete(

61. MediaType.ALL);

}

62. Now, to access this method in the test file, create a test method from which you will make a call to the deleteDeployment method of Repository_Deployment; on performing the test, the process with the ID 23 will be deleted from the repository, as shown in the following code:

63.@Test

64. public void deleteDeployment() throws JSONException, IOException {

65. Repository_Deployment.deleteDeployment(23);

}

66. To retrieve the resources from a specific deployment, we have to navigate to url /repository/deployments/{deploymentId}/resources.

67. We will create the getDeploymentResources method in the Repository_Deployments class, where we will pass the deployment ID to access its resources, as shown in the following code:

68.public static JSONArray getDeploymentResources(int id) throws IOException,

69. JSONException {

70. String deploymentURI = REST_URI + "/repository/deployments/" + id + "/resources";

71. Representation response = getClientResource(deploymentURI).get( MediaType.APPLICATION_JSON);

72.

73. String json = "{data :" + response.getText() + "}";

74.

75. JSONObject object = new JSONObject(json);

76.

77. if (object != null) {

78. JSONArray arr = (JSONArray) object.get("data");

79. return arr;

80. }

81. return null;

}

82. To test, we will create the deploymentResource method in the Repository_Deployment class as follows:

83.@Test

84. public void deploymentResource() throws JSONException, IOException {

85.

86. JSONArray processArray = Repository_Deployment.getDeploymentResources(23);

87. assertNotNull(processArray);

88.

89. for (int i=0;i<processArray.length();i++){

90. JSONObject object = (JSONObject) processArray.get(i);

91. assertNotNull(object);

92. System.out.println(object.toString());

93. }

}

94. On running the test file, we should be able to view all the resources of the deployment, as shown in the following screenshot:

Time for action – working with Repository APIs as REST

What just happened?

We just saw how to use the REST API to deploy and delete the process. We have also fetched the resources from a specified deployment and created some test cases to test our REST API calls.

Time for action – working with processes

We are comfortable with fetching deployments from the repositories. Now, we will become familiar with fetching the process-definitions folder from the repository; you can paste the URL http://localhost:8080/activiti-rest/service/repository/process-definitions in RESTClient. If an error 400 occurs, as shown in the following screenshot, we will have to modify the activiti-rest folder:

Time for action – working with processes

To resolve this error, we have to perform the following steps:

1. If you get the preceding error, first of all, modify your activiti-context.xml file located in the ../apache-tomcat-7.0.37/webapps/activiti-rest/WEB-INF/classes folder, as shown in the following screenshot:

Time for action – working with processes

2. Edit the actviti-context.xml file by placing the following code in the processEngineConfiguration bean:

3. <property name="customFormTypes">

4. <list>

5. <bean class="org.activiti.explorer.form.UserFormType"/>

6. <bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/>

7. <bean class="org.activiti.explorer.form.MonthFormType"/>

8. </list>

</property>

9. Your modified activiti-context.xml file should look as shown in the following screenshot:

Time for action – working with processes

10. After editing the activity-context.xml file, you need to add the activity-explorer{version}.jar file to the ../apache-tomcat-7.0.37/webapps/activiti-rest/WEB-INF/lib folder.

11. Now, restart the Tomcat server and from the RESTClient, you can retrieve process-definitions by setting the GET method along with the URL ../repository/process-definitions, as shown in the following screenshot:

Time for action – working with processes

12. On navigating to the preceding REST URL, you should be able to view the list of process definitions available within the repository as shown in the following screenshot:

Time for action – working with processes

13. To access process-definitions within a class, we need to create a Process_Deployment class in which we will create a method to fetch the process definitions, as shown in the following code:

14.private static String REST_URI = "http://localhost:8080/activiti-rest/service";

15. private static ClientResource getClientResource(String uri) {

16. ClientResource resource = new ClientResource(uri);

17. resource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit");

18. return resource;

19. }

20. public static JSONArray getProcessDefinitions() throws JSONException, IOException {

21. String uri = REST_URI + "/repository/process-definitions";

22. Representation response = getClientResource(uri).get(MediaType.ALL);

23. JSONObject object = new JSONObject(response.getText());

24. if (object != null) {

25. JSONArray processArray = (JSONArray) object.get("data");

26. return processArray;

27. }

28. return null;

}

29. To fetch the process definitions, we need to create a test method in the test file as follows:

30.@Test

31. public void processDefinitons() throws Exception {

32. JSONArray processArray = Process_Deployment.getProcessDefinitions();

33. for (int i = 0; i < processArray.length(); i++) {

34. JSONObject jsonObject = (JSONObject) processArray.get(i);

35. assertNotNull(jsonObject);

36. System.out.println(jsonObject.toString());

37. }

}

38. On testing the method, we should get all the processes deployed in the repository. All the processes will be displayed, as shown in the following screenshot:

Time for action – working with processes

39. We can also fetch a specific process from the repository by providing the process ID with the GET method and the URL repository/process-definition/{processeId}, as shown in the following screenshot:

Time for action – working with processes

40. We will create an implementation class with the getProcessDefinitionById method in the Process_Deployment class, which will retrieve the process from the repository based on the process ID provided, as shown in the following code:

41.public static JSONObject getProcessDefinitionById(String processId) throws JSONException, IOException {

42. String uri = REST_URI + "/repository/process-definitions/"+ processId;

43. Representation response = getClientResource(uri).get(MediaType.APPLICATION_JSON);

44. JSONObject object = new JSONObject(response.getText());

45. if (object != null) {

46. return object;

47. }

48. return null;

}

49. Now, we will create the processDefinitionById method in the Test REST API class to test the results, as shown in the following code:

50.@Test

51. public void processDefinitionById() throws Exception {

52. JSONObject jsonObject = Process_Deployment.getProcessDefinitionById("createTimersProcess:1:37");

53. assertNotNull(jsonObject);

54. System.out.println(jsonObject.toString());

}

55. So, on execution of the test case, we get the details of the process specified within it, as shown in the following screenshot:

Time for action – working with processes

56. Using the REST API, we can not only fetch the process definitions, but also update the processes by setting the Method type to PUT. We will create the updateProcess method to update the process in the Process_Deployment class and provide the processIdargument for which we want to perform the update, as shown in the following code:

57. public static JSONObject updateProcess(String processId) throws JSONException, IOException {

58.

59. String uri = REST_URI + "/repository/process-definitions/" + processId;

60. JSONObject updateData = new JSONObject();

61. updateData.put("category", "Updated Category");

62. Representation response = getClientResource(uri).put(updateData);

63. JSONObject object = new JSONObject(response.getText());

64. if (object != null) {

65. return object;

66. }

67. return null;

}

68. Create a test method update in the Test REST API class file to perform the testing of the update method, as shown in the following code:

69.@Test

70. public void update() throws Exception{

71.System.out.println("Before Updating Category");

72.JSONObject object1 = Process_Deployment. getProcessDefinitionById ("vacationRequest:1:33");

73. assertNotNull(object1);

74. System.out.println(object1.toString());

75. System.out.println("After Updating Category");

76.JSONObject object2 = Process_Deployment.UpdateProcess("vacationRequest:1:33");

77. assertNotNull(object2);

78. System.out.println(object2.toString());

}

79. On executing the test class, the Category parameter should be updated with the value provided in the updateProcess method, which will produce the output as shown in the following screenshot:

Time for action – working with processes

80. As we can update the process by setting the Method value to PUT, we can also manage the action parameter using the PUT method as follows:

81.public static JSONObject suspendProcessDefination(String id) throws JSONException, IOException {

82. String uri = REST_URI + "/repository/process-definitions/" + id;

83.

84. JSONObject my_data = new JSONObject();

85. my_data.put("action", "suspend");

86. my_data.put("includeProcessInstances", "false");

87.

88. Representation response = getClientResource(uri).put(my_data);

89. JSONObject object = new JSONObject(response.getText());

90. if (object != null) {

91. return object;

92. }

93. return null;

}

94. In the preceding code, we have set the action parameter to suspend the process. This action will suspend the process so that it cannot be executed. If you want to activate the process, you need to change the value of the action parameter from suspend toactivate, as shown in the following code:

95.public static JSONObject activateProcessDefination(String id) throws JSONException, IOException {

96. String uri = REST_URI + "/repository/process-definitions/"+ id;

97. JSONObject my_data = new JSONObject();

98. my_data.put("action", "activate");

99. my_data.put("includeProcessInstances", "true");

100.

101. Representation response = getClientResource(uri).put(my_data);

102. JSONObject object = new JSONObject(response.getText());

103. if (object != null) {

104. return object;

105. }

106. return null;

}

107. Using the REST API, we can also assign users to the business process. Now, we will see how to assign users to the business process. We will create the addCandidate method in the Process_Deployment class as follows:

108. public static JSONObject addCandidate(String id) throws JSONException, IOException {

109. String uri = REST_URI + "/repository/process-definitions/" + id + "/identitylinks";

110. JSONObject my_data = new JSONObject();

111. my_data.put("user", "kermit");

112. Representation response = getClientResource(uri).post(my_data);

113. JSONObject object = new JSONObject(response.getText());

114. if (object != null) {

115. return object;

116. }

117. return null;

}

What just happened?

So far, we have used the REST API to retrieve and edit the process using the PUT method. We have also gone through updating the action parameter by specifying an action to the process, such as suspending and activating the process or adding users to it.

Time for action – working with tasks

We have used the REST API for fetching, updating, and adding actions to the business process and will also use it when we want to work with tasks. To access a task, we have to use the GET method with the URL /runtime/tasks and perform the following steps:

1. First, we have to fetch processes by setting the Method field to GET and the URL field to /runtime/tasks, as shown in the following screenshot:

Time for action – working with tasks

2. By navigating to the preceding URL, we will get a list of the tasks available in the repository with all the details, as shown in the following screenshot:

Time for action – working with tasks

3. We can also use the REST API to access the tasks from the repository by adding the getTasks method in the Process_Deployment class, which will provide the list of tasks, as shown in the following code:

4. public static JSONArray getTasks() throws JSONException, IOException {

5. String uri = REST_URI + "/runtime/tasks";

6. Representation response = getClientResource(uri).get(MediaType.APPLICATION_JSON);

7. JSONObject object = new JSONObject(response.getText());

8. if (object != null) {

9. JSONArray taskArray = (JSONArray) object.get("data");

10. return taskArray;

11. }

12. return null;

}

13. We can fetch a specific process based on its process ID; similarly, we can retrieve a specific task based on its task ID. For this, we have to specify this URL .. /runtime/tasks/{taskId} in combination with the GET method. To execute the task, we need it to be claimed by a specific person using the POST method. For that, we need to set values for the action and assignee parameters. We can create the claimTask method in the Process_Deployment class as follows:

14.public static void claimTask(int id) throws JSONException, IOException {

15. String uri = REST_URI + "/runtime/tasks/" + id;

16. JSONObject my_data = new JSONObject();

17. my_data.put("action", "claim");

18. my_data.put("assignee", "kermit");

19. Representation response = getClientResource(uri).post(my_data);

}

20. After claiming the task, we have to make sure that it is completed to make the process work smoothly. We need to set the action parameter to complete along with the POST method, as shown in the following code:

21.public static void completeTask(int id) throws JSONException, IOException {

22. String uri = REST_URI + "/runtime/tasks/" + id;

23. JSONObject my_data = new JSONObject();

24. my_data.put("action", "complete");

25. Representation response = getClientResource(uri).post(my_data);

}

What just happened?

We are done learning about the Task API: we got the list of tasks available within the repository, fetched a specific task based on the task ID, and then changed the action parameter for claiming the task. After claiming the task, the task was completed.

Time for action – working with users

To access user-related information, we have to use an identity-related URL, that is, /identity/users. Now, we will use the identity service. Perform the following steps to archive the identity service:

1. Add a user to the Activiti Engine using the createUser method in the Process_Deployment class through which we will pass the details of the user as follows:

2. public static JSONObject createUser() throws JSONException, IOException {

3. String uri = REST_URI + "/identity/users";

4. JSONObject my_data = new JSONObject();

5. my_data.put("id", "irshad");

6. my_data.put("firstName", "Irshad");

7. my_data.put("lastName", "Mansuri");

8. my_data.put("email", "irshad@localhost.com");

9. my_data.put("password", "irshad");

10. Representation response = getClientResource(uri).post(my_data);

11. JSONObject object = new JSONObject(response.getText());

12. if (object != null) {

13. return object;

14. }

15. return null;

}

16. We can create a user using the REST API and also update and delete them with it.

17. To update a user, we can use the same method as we did for creating a user, except that we have to provide a user id for the update. The URL for the update is /identity/users/{userId}, as shown in the following code:

18.public static JSONObject updateUser(String id) throws JSONException, IOException {

19. String uri = REST_URI + "/identity/users/" + id;

20. JSONObject my_data = new JSONObject();

21. my_data.put("email", "irshad.mansuri@localhost.com");

22. Representation response = getClientResource(uri).put(my_data);

23. JSONObject object = new JSONObject(response.getText());

24. if (object != null) {

25. return object;

26. }

27. return null;

}

28. To delete a user from the repository, we have to use the deleteUser method. We will add the following method to archive the delete functionality:

29.public static void deleteUser(String id) throws JSONException, IOException {

30. String uri = REST_URI + "/identity/users/" + id;

31. Representation response = getClientResource(uri).delete(MediaType.ALL);

}

32. With the help of the identity service, we can not only access the users but also create and update groups. For groups, we have to provide the URL /identity/groups.

33. Now we will use the createGroup method to create a group in the repository, which will contain information regarding the group, as shown in the following code:

34. public static JSONObject createGroup() throws JSONException, IOException {

35. String uri = REST_URI + "/identity/groups";

36.

37. JSONObject my_data = new JSONObject();

38. my_data.put("id", "attunegroup");

39. my_data.put("name", "Attune Group");

40. my_data.put("type", "security-role");

41. Representation response = getClientResource(uri).post(my_data);

42. JSONObject object = new JSONObject(response.getText());

43. if (object != null) {

44. return object;

45. }

46. return null;

}

47. As we know how to update a user using identity services, we can also update a group.

48. Once we have created a group, we need to add some users to it. For that, we need to create the addGroupMember method where we will provide the group ID as the parameter in which we want to add users, as follows:

49. public static JSONObject addGroupMember(String id) throws JSONException, IOException {

50. String uri = REST_URI + "/identity/groups/" + id + "/members";

51.

52. JSONObject my_data = new JSONObject();

53. my_data.put("userId", "irshad");

54.

55. Representation response = getClientResource(uri).post(my_data);

56. JSONObject object = new JSONObject(response.getText());

57. if (object != null) {

58. return object;

59. }

60. return null;

}

61. Just as we can add a member to a group, we can also delete them from it. For that, we need to provide the groupID as well as userId values that we want to delete, as shown in the following code:

62. public static void deleteGroupMember(String groupId, String userId) throws JSONException, IOException {

63. String uri = REST_URI + "/identity/groups/" + groupId + "/members/" + userId;

64. Representation response = getClientResource(uri).delete(MediaType.ALL);

}

What just happened?

By the end of this section, you should be able to list, add, and manage users and groups in the repository.

Time for action – working with management

Now we are familiar with all the APIs for fetching processes from the repository, updating and assigning users to a business process, creating and managing users with the Identity API, and creating groups and assigning users to it. We have used the Task API to claim it and complete a task. Now we will have a look at the Management API, which we will use to manage our process engine tables. To work with management, we have to provide the URL /management/tables using the GET method and perform the following steps to archive the management-related information:

1. We will access the management-related tables in the process engine by providing the URL value /management/tables with the GET method, as shown in the following screenshot:

Time for action – working with management

2. The response generated by the preceding URL will be the list of tables available within the process engine, as shown in the following screenshot:

Time for action – working with management

3. To fetch a specific table from the list, we need to provide its name with the help of the URL /management/tables/{tableName} and the get method, as shown in the following code:

4. public static JSONObject getDbTables(String tableName) throws JSONException, IOException {

5. String uri = REST_URI + "/management/tables/" + tableName;

6. Representation response = getClientResource(uri).get( MediaType.APPLICATION_JSON);

7.

8. JSONObject object = new JSONObject(response.getText());

9. if (object != null) {

10. return object;

11. }

12. return null;

}

13. We need to create the getSpecificTable method to fetch a specific table from the list in the Test REST API class. The following code fetches the ACT_RU_VARIABLE value, which contains the list of variables that are currently running:

14.@Test

15. public void getSpecificTable() throws Exception {

16. JSONObject object = Process_Deployment.getDbTables("ACT_RU_VARIABLE");

17. assertNotNull(object);

18. System.out.println(object.toString());

}

19. The Management API is not limited to only fetching tables; it can also fetch columns from a given table. The URL for fetching columns is /management/tables/{tableName}/columns and is used along with the GET method.

20. We will create the getDbTablesColumn method in the Process_Deployment class to fetch the columns of a given table, as shown in the following code:

21.public static JSONObject getDbTablesColumn(String name) throws JSONException, IOException {

22. String uri = REST_URI + "/management/tables/" + name + "/columns";

23. Representation response = getClientResource(uri).get( MediaType.APPLICATION_JSON);

24.

25. JSONObject object = new JSONObject(response.getText());

26. if (object != null) {

27. return object;

28. }

29. return null;

}

30. With the help of the Management API, we can also get the properties of the Activiti Process Engine that is running. For that, we can use the URL /management/properties in combination with the get method.

31. We will create the getEngineProperty method to fetch the engine properties in the Process_Deployment class as follows:

32.public static JSONObject getEngineProperty() throws JSONException, IOException {

33. String uri = REST_URI + "/management/properties";

34. Representation response = getClientResource(uri).get( MediaType.APPLICATION_JSON);

35.

36. JSONObject object = new JSONObject(response.getText());

37. if (object != null) {

38. return object;

39. }

40. return null;

}

41. In the Test REST API, we will create the getProperties method, which will test the fetching of the engine properties, as shown in the following code:

42.@Test

43. public void getProperties() throws Exception {

44. JSONObject object = Process_Deployment.getEngineProperty();

45. assertNotNull(object);

46. System.out.println(object.toString());

}

47. The response generated on the execution of the Test class will give us the details of our database history and the version of the tables that we are using for our process engine, as shown in the following screenshot:

Time for action – working with management

48. We can also fetch engine-related information using the Management API; the URL to access the information is /management/engine, as shown in the following code:

49.public static JSONObject getEngineInfo() throws JSONException, IOException {

50. String uri = REST_URI + "/management/engine";

51. Representation response = getClientResource(uri).get( MediaType.APPLICATION_JSON);

52. JSONObject object = new JSONObject(response.getText());

53. if (object != null) {

54. return object;

55. }

56. return null;

}

57. To test our getEngineInfo method, we need to create the test method getEngineInfo in the Test REST API class as follows:

58.@Test

59. public void getEngineInfo() throws Exception {

60. JSONObject object = Process_Deployment.getEngineInfo();

61. assertNotNull(object);

62. System.out.println(object.toString());

}

63. The output of our test class will provide information regarding the version of Activiti and the path of the activiti-context.xml file, as shown in the following screenshot:

Time for action – working with management

What just happened?

We learned the use of the Management REST API by fetching tables from the repository and columns from a specific table—all this using the same REST API from which we can fetch the properties and engine-related information.

Have a go hero

Now that we have completed the chapter, you should be able to attempt the following tasks using the REST APIs:

· Perform delete, add, and update operations for variables on the tasks.

· Using the Process API, we can start a process instance and execute queries using the Process API. Using the Identity API, we can update and delete users and groups.

Pop quiz – the REST API

Q1. Which API is used to fetch deployment information from the repository?

1. The Repository API

2. The Task API

3. The Process API

4. None of the above

Q2. The Process API is used for fetching which of the following information from the repository?

1. process-definitions

2. deployments

3. users

4. groups

Q3. Which method is used in combination with the Process API to update any information?

1. GET

2. PUT

3. POST

4. DELETE

Summary

In this chapter, we have gained knowledge regarding the use of the REST API. Using the REST API, we can perform all the operations that we can perform using the ProcessEngine API. We have understood the use of the Task, Process, Identity, and Management REST APIs and used them for developing our process. In the next chapter, we will learn how to integrate various third-party tools with Activiti, such as the Liferay Portal, Business Rules, and OSGI.