Chapter 33. REST Task
The REST task performs REST calls and outputs the response as an object.
			RestWorkItemHandler is capable of interacting with the REST service, and supports both types of services:
		
- Secured: requires authentication.
 - Open: does not require authentication.
 
Authentication methods currently supported are:
- 
					
BASIC - 
					
FORM_BASED 
Authentication information can be given on handler initialization and can be overridden using work item parameters. All other configuration options must be given in the work item parameters map:
Input Attributes
- Url
 Target URL to be invoked. This attribute is mandatory.
It is often necessary to configure the URL attribute with an expression. This gives you the ability to change the URL dynamically throughout the runtime. For example:
http://DOMAIN:PORT/restService/getCars?brand=#{carBrand}http://DOMAIN:PORT/restService/getCars?brand=#{carBrand}Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this example, carBrand is replaced by the value of the
carBrandprocess variable during runtime.- Method
 - The method of the request, such as GET, POST, or other. The default method is GET.
 - ContentType
 - 
						The data type if you are sending data. The supported data types are 
application/jsonandapplication/xml. This attribute is mandatory for POST and PUT requests. If you want to use this attribute, map it as a data input variable in the Data I/O dialogue of the task. - Content
 - The data you want to send. This attribute is mandatory for POST and PUT requests. This is an optional parameter. If you want to use it, map it as a data input variable in the Data I/O dialogue of the task.
 - ConnectTimeout
 - The connection timeout. The default value is 60 seconds.
 - ReadTimeout
 - The timeout on response. The default value is 60 seconds.
 - Username
 - The user name for authentication. This attribute overrides the handler initialization user name.
 - Password
 The password for authentication. This attribute overrides the handler initialization password.
User name and password for basic authentication can be passed at construction time using the following:
RESTWorkItemHandler(String username, String password);
RESTWorkItemHandler(String username, String password);Copy to Clipboard Copied! Toggle word wrap Toggle overflow - AuthUrl
 The URL that is handling authentication when using the
AuthenticationType.FORM_BASEDauthentication method.Use the following constructor for
FORM_BASEDauthentication:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following is an example of how the constructor must be used in Deployment descriptor:
new org.jbpm.process.workitem.rest.RESTWorkItemHandler("username","password","http://mydomain.com/my-j-security-check-url",classLoader)new org.jbpm.process.workitem.rest.RESTWorkItemHandler("username","password","http://mydomain.com/my-j-security-check-url",classLoader)Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantAuthUrl configuration requires the typical implementation for
FORM_BASEDauthentication in Java EE servers, and therefore should point to thej_security_checkURL. Similarly, inputs for user name and password must be bound toj_usernameandj_passwordwhen usingFORM_BASEDauthentication, otherwise authentication may fail.- ResultClass
 - 
						This attribute determines the class to which the value from the 
Resultattribute will be converted. If not provided, the default value isString. - HandleResponseErrors
 - An optional parameter that instructs the handler to throw errors in case of unsuccessful response codes. For information on how to handle response errors, see the section called “Handling REST Response Error”.
 
Output Attributes
- Result
 - The result returned by the REST service.
 - Status
 - The variable contains a value from interval 200 to 300 if the REST request was successful, or an error response code if the request was unsuccessful. This variable is not mapped by default. If you want to use this variable, map it manually as an output variable of the REST task.
 - StatusMsg
 - If the service returned an error response, this variable will contain the error response message. This variable is not mapped by default. If you want to use this variable, map it manually as an output variable of the REST task.
 
			All output attributes are String by default.
		
Handling REST Response Error
			HandleResponseErrors can be handled in two ways:
		
In the Process Definition Workflow
Status: WhenRESTWorkItemHandlerproduces aStatusoutput variable that includes an HTTP response code. This can be mapped to a process variable and used in a XOR gateway to determine the service outcome.- 
							
StatusMsg: The output variableStatusMsgincludes additional messages sent by the server, and is filled only when the HTTP Code is not between 200 and 300. 
Using a Boundary Event
To enable this feature, set the REST work item input variable
HandleResponseErrorstotrue.ImportantThe
HandleResponsemust have a valid boolean expression or be left empty, which is equivalent tofalse. Otherwise, the REST task will throw an exception.When the REST work item input variable
HandleResponseErrorsis set totrue, theRESTWorkItemHandlerhandler will, upon receiving an HTTP response code outside of the 200-300 interval, throw the following Java exception:public RESTServiceException(int status, String response, String endpoint) { super("Unsuccessful response from REST server (status " + status +", endpoint " + endoint +", response " + response +"");public RESTServiceException(int status, String response, String endpoint) { super("Unsuccessful response from REST server (status " + status +", endpoint " + endoint +", response " + response +"");Copy to Clipboard Copied! Toggle word wrap Toggle overflow With the
HandleResponseErrorsoption enabled, this error can be caught using a boundary event:The provided example includes:
- 
							A 
WorkItemHandlerRuntimeExceptionrestErrorprocess variable. - 
							A 
WorkItemHandlerRuntimeExceptionBoundaryErrorevent-defined output variable that has been mapped to therestErrorprocess variable. A Script task that includes the following code:
org.jbpm.process.workitem.rest.RESTServiceException x = (org.jbpm.process.workitem.rest.RESTServiceException) restError.getCause().getCause();
org.jbpm.process.workitem.rest.RESTServiceException x = (org.jbpm.process.workitem.rest.RESTServiceException) restError.getCause().getCause();Copy to Clipboard Copied! Toggle word wrap Toggle overflow This code allows
RestServiceExceptionto be extracted fromWorkItemHandlerRuntimeException. UsingRestServiceExceptionprovides access to the following methods:- 
							
getResponse - 
							
getStatus getEndpointThe next line in the Script task is:
System.out.println("response:"+x.getResponse());System.out.println("response:"+x.getResponse());Copy to Clipboard Copied! Toggle word wrap Toggle overflow This provides the full error message as returned by the server.
- 
							A