Getting data from a simple REST service

In this tutorial, we try to call a public REST service from a task in Bonita. We will modify the tutorial Getting a target resource based on the client request to create a simple service that return the user's information in XML format. Then, we'll create a business process using Bonita with a simple task. On this task, we create a connector that calls this service and parser the user's information.

Create the REST service

  1. Open the class UserResource.java file. Modify the code as following:

    package forthREST;
     
    import org.restlet.resource.Get;
    import org.restlet.resource.ServerResource;
     
    public class UserResource extends ServerResource {  	
    	@Get  
    	public String toString() {
    		String uid = (String) getRequestAttributes().get("uid");
    		if (uid.equals("123")){
    			return "<users>" +
    					"<user>" +
    					"<id>"+uid+"</id>" +
    					"<name>John</name>" +
    					"</user>" +
    					"</users>";
    		}
    		return "No information found.";  
     
    	}  
    }
  2. We suppose that the REST service will return the user's information in XML format. To simplify the example, we return a string in XML format for only the user whose ID is 123 as given in the source codes above.
  3. Now, run the modified REST service by right clicking on the RESTDistributor.java file, select Run As → Java Application.
  4. Open http://localhost:8182/users/123 by your web browser. You should get the following:

    <users><user><id>123</id><name>John</name></user></users>

Create business process to access this REST resource

On this part, we'll create a simple business process that accesses to the user resource provided by the REST service above. We'll use the Groovy script connector to retrieved the name of user 123.

  1. Open Bonita Studio. Click on New button to create a new diagram.
  2. Design a process as following, where Step2 is a service task and Step3 is a human and is just for previewing the result returned from the REST service.


  3. create the global variable name of type text.
  4. Click on Step2. In the Execution→Connectors in tab, click on Access the Bonita Marketplace.


  5. Select the REST connector and click Next.
  6. Select REST from the left panel and GET from the right panel, then Next.


  7. Name the connector as RestService_Connector, then Next.
  8. Set the GET URL to http://localhost:8182/users/123, then Next.


  9. Keep clicking Next until reaching the Output Operations step shown below.


  10. Choose the variable name from the first list to store the connector output.


  11. Select the edit pencil icon in the second list.
  12. Add the following code in the REST editor:

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
     
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
     
    //the response is in XML, so we parse the XML bodyAsString
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource inputSource = new InputSource();
    inputSource.setCharacterStream(new StringReader(bodyAsString));
    Document userDataDocument = documentBuilder.parse(inputSource);
    //get the root element users
    Node usersNode = userDataDocument.getDocumentElement();
    //get the childs nodes of the node users (the list contains one child which is user)
    NodeList usersChildNodes  = usersNode.getChildNodes();
    //get the first child (which is user)
    Node userNode = usersChildNodes.item(0);
    //get the childs of user
    NodeList userChildNodes = userNode.getChildNodes();
    String userName="";
    //search for the child node "name"
    for (int i=0; i<userChildNodes.getLength(); i++)
    {
    		Node node = userChildNodes.item(i);
    		if (node.getNodeType() == Node.ELEMENT_NODE)
    		{
    				String key = node.getNodeName();
    				if(key.equals("name"))
    				{
    					String value = node.getTextContent();
    					userName=value;
    					break;
    				}
     
    		}
    }
    return userName;
  13. Click Ok.


  14. Click Finish.
  15. Save the process.
  16. Let's design a simple form for Step3.
  17. Click on Step3. Click on Execution→Form. In Target Form, create new Form called outputForm.


  18. In the Form page, drag and drop a Text field, then name it Result.
  19. The Result text field will display the result of the web service which will be stored in the variable name. To do so, click on create a new variable in the form. Enter the details as shown below.


  20. Click on Save, then click on the Result text field. In the right side panel, go to Value and fill it with result.value.


  21. Save and close the form.
  22. Make sure that the web service is still listening on port 8182.
  23. Click on the Configure tag. Map the Employee actor to your organization Staff group and Employee role.
  24. Save changes, then Run the process.
  25. Start the pool process.
  26. The result of the web service will be displayed in the Result text field as shown below.


Exercises

  1. Modify the business process above to allow inputting the user's ID to request instead of embedding it in the URI. For example, instead of requesting the resource at http://localhost:8182/users/123/, the process allows requesting flexibly resources at http://localhost:8182/users/[userID]/ where the [userID] is provided by the initiator of the business process.
  2. Add more user items to the REST service. Then, modify the business process to receive list of existing users' names.
  3. Modify the REST service to return the user's information in JSON format. Then, update the business process's connector to achieve the provided user's names.
teaching_assistant/workflow/getting_data_from_a_simple_rest_service.txt · Last modified: 2022/01/06 17:14 by Nour
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0