REST API / Start a case with a contract for a given user

Hello.

I’ve tried the REST API to start cases sending the contract attributes (/API/bpm/process/processId/instantiation).
It works as expected.

Now, I’d like to do the same process creation, but for a user that is not the REST session owner (an application account), and without opening new sessions for each user. Indeed, the job is exposed as a web service (on an ESB) and may be called with high performance requirements, and some operations may imply several users. Opening and closing sessions for each operation should be a performance issue.

The java API as an operation that allows to set the user at the process instantiation.

What is the best way to do that using the REST API ?

Thanks for your help.

Regards.

Hi,

For what I know, the REST API doesn’t offer this kind of operation out of the box. However, you can create your own REST API extension to do the job.

See the REST API extensions documentation for more information

Hello.
Thanks for your answer.
I’ve already started to build and test a very simple REST extension.
But I’ll need documentation about :

  • how set a template path with a variable part (like the path with the process definition id) ?
  • how retrieve this pat in the groovy class
  • how retrieve the contract from the request body, in the legacy API format, and transform it to pass it to the java API
  • how get the java Process API in the extension
    Have you examples with some kind of java API processing, not just the simple examples that come with the studio ?
    Thanks again.
    Regards.

You can’t define a path template, you’ll have to parse the path yourself to extract the variable. You can retrieve the path from the HttpServletRequest parameter (which is a javax.servlet.http.HttpServletRequest)

The request body is accessible from the HttpServletRequest parameter. Here is a very simple example that shows how to read the request body and parse the Json to a Map. Note that you’ll need to import groovy.json.JsonSlurper

String joinRequest = request.reader.readLines().join("\n")
def slurper = new JsonSlurper()
Map requestParams = slurper.parseText(joinRequest)

Finally, in order to use the java API you simply need to import the classes you need at the top of your groovy file. For example :

import org.bonitasoft.engine.api.ProcessAPI

Hello.

In the meantime I’ve done some tests on this way.

Now, a first version of my extension only fails when retrieving a Date field in the contract.

The fields of the contract were used before without any problem when the Map was processed by the Bonita REST API. But now, in my extension, the JSonSlurper seems to decode the Date with a bad format, and the ProcessAPI call throws an exception.

In both case, the client uses Jackson and Spring RestTemplate, and I suppose the problems comes from the extension contract parsing.

The received JSON body, encoded by Jackson, is :

{"theAAInput":{"firstname":"Juan","nationality":"ES","surname":"RODRIGUEZ","birthDate":-251513880000}}

I use the JsonSlurper to get my contract back :
def contract = new JsonSlurper().parseText(json)

And when I call the engine : processAPI.startProcessWithInputs(userId, processDefinitionId, contract)

I get the following exception :
org.bonitasoft.engine.core.process.instance.api.exceptions.SContractViolationException: Error while validating expected inputs: [-251513880000 cannot be assigned to DATE, -251513880000 cannot be assigned to DATE]

An idea about date formats and conversions ?

Thanks again.