Web service invocation with Bonita's workflow
Bonita Studio provides connectors which are a specific function to interact with other services. And here, we will see how we can invoke a SOAP 1.2 based web service at an activity using the connector. In this tutorial, I focus on the WS connector creation instead of the details about workflow design. Hence, you shoud practice the previous TP to be clear with the workflow design before working on this tutorial.
We will design a simple “Displaying a weather report” workflow. At the Get Weather step, we will call a web service to display the weather in your city.
Design the "Displaying your city weather" workflow
Using Bonita Studio, create a workflow as following:
Set the Details of the activities as following:
Name |
Description |
Type |
Priority |
Enter your city |
User enter the country and city |
Human |
Normal |
Get weather |
Get the weather report using a web service |
Service |
|
Gate |
|
XOR |
|
Display weather report |
The weather report is displayed to the user |
Human |
Normal |
no result found |
A message is displayed to the user if no result is found |
Human |
Normal |
Declare three global variables:
Name |
Description |
Data type |
city |
The user's city |
Text |
country |
The user's country |
Text |
weather |
The weather report |
Text |
At the
enter you city activity, we will design a simple form for submitting the city and country names. To do this, click on
Application → Pageflow, then
Add. Name the form as
get_city_form and the Description as
The form used for getting the city and country. Uncheck the global variable
weather and click
Finish.
Click on the
submit button in
get_city_form and fill in the
General description as following
Save and close the get_city_form Form.
Now, we set the transition conditions on the gateway. Select the transition from the gateway to display weather report and check default flow in General tab.
Select the transition from the
gateway to
no result found and fill in as following:
Now, we add the forms for the activities Display weather report and no result found. Select Display weather report, then Application–>Pageflow–>Add. Name the form as display_form and leave the global variables checked. click Finish. Remove the Weather variable from the form and drag a Text Area from the palette.
Select on the text area, then rename its label as
weather data in
General tab.
In General–>data, set the Initial value as weather. Save the display_form.
By the same way, add a no_result_form form to no result found task that contains a Message element whose Initial value is No result found and a Submit button.
Save the no_result_form form and the process is completely designed now. However, our main task which is invoking a web service at the activity Get weather has not been done yet. Now, it's time do it.
Invoking the Web service
Get the
WSDL file of the weather service. Open it and have a look on its content to get the data for the web service connector configuration.
At the Get weather activity, select General –> Connectors –> add. Choose SOAP web services –> Web services SOAP 1.2 from the list and click Next.
Name it as
WeatherReport_Connector. Set
Select event as
enter. Click
Next.
Fill in the
Connection parameters from the wsdl file as following and click
Next:
By the same way, fill in the
Request parameters from the wsdl file as following:
for your project, we may need to use SOAPUI to have the correct message structure
The code for the
Envelope request is as following:
<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<tns:GetWeather xmlns:tns="http://www.webserviceX.NET">
<tns:CityName>${city}</tns:CityName>
<tns:CountryName>${country}</tns:CountryName>
</tns:GetWeather>
</env:Body>
</env:Envelope>
Click Next –> Next. In Response Configuration, check the Returns body box. click Next.
In
Output operations, choose
weather variable from the first list. Click on the
Pencil icon in the second list to assign a value for the
weather variable. Choose
script in the
Edit operation form and add the following code:
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
// Clean response xml document
responseDocumentBody.normalizeDocument();
// Get result node
NodeList resultList = responseDocumentBody.getElementsByTagName("GetWeatherResult");
Element resultElement = (Element) resultList.item(0);
String weatherData = resultElement.getTextContent();
return weatherData;
we may need to use for your project the function getElementsByTagNameNS instead of getElementsByTagName
Name it
result and in
Return type, choose
java.lang.String and click
Ok Then
Finish.
Save the process and
Run it. Enter the city
Melun and country
France and click
get weather report. Wait a few seconds, and the activity
Get weather will be available. Click on it to get the response from the WS server. You can see now the weather report displayed in
Weather data text area
Done.
Exercices
Can we assign directly the result variable to the weather (steps 8,9)?
Can you find an operation other than
GetWeather provided by the service
GlobalWeather in the
WSDL file?
If yes, can you invoke the corresponding operation?
The response of the GetWeather operation is in the form of an XML file (step 10). Re-configure the connector at Display weather report to get the information from the response.
Can you display the above results in a table?
-
Back to top