Bonitasoft is an open-source technology which is used for automating business processes. It also provides widgets to develop the UI separately while allowing customizations as needed by a developer.

Bonita Studio has features to compile, develop, and deploy a Bonita application. It extends a graphical environment for creating processes, applications, data models, and user’s views (pages and forms).

If you want to develop UI with other technologies like Angular or any other UI framework other than Bonita UI, it can be achieved using REST API. Bonita provides rest APIs to integrate UI with Bonita. By leveraging these REST APIs, one can access Bonita BDM and process data.

Rest API

REST is an acronym for Representational State Transfer. It defines how applications communicate over the Hypertext Transfer Protocol (HTTP). While REST doesn’t define data formats, it’s usually associated with exchanging JSON or XML documents between a client and a server.

It is defined set of rules in order to create web service. In client server communication REST will create an object of data request by the user and send this data as response. To perform CRUD operations, we will use the http methods which are nothing but REST API methods.

By using Bonita Rest API, we can access all Bonita objects (such as, processes, tasks, users) and perform operations like create, update and delete on processes and tasks. We can perform below tasks using REST APIs:

  • Instantiation of process
  • Display the list of pending tasks of user
  • Update the values of the variables in a process
  • Login and logout

Introduction-Custom REST API Extn

Along with Bonita Rest API, we can also create our own custom REST API as per our requirement. We can call it REST API Extension. Rest API Extensions are used to fetch data from third-party systems like Data base, Web Services, Bonita Engine, etc. They also help to keep a clean separation between the front-end and the back-end. REST API extension can be leveraged to query business data, Bonita Engine APIs, or an external information system such as data base and web service.

When Rest API extensions are created, it will have only one handler, which will limit us to map only one request for that specific handler. Whereas there is no option to accustom different request with different handlers.

By configuration, we can map all GET request to one handler and other one is post handler which will update data in the database.

In configuration, we will mention the different handlers for different methods. Based on the http method the corresponding handler will execute. It comes with the following benefits:

  • No need of multiple rest APIs or projects for each http method
  • Helps avoid code duplication
  • Code separation of different http methods
  • Clean code

Create a new REST API extension

REST API extension includes two different types of files:

The Groovy script file per API extension. The script file has code which tells what exactly the API will do.

  • A page.properties file. It defines information such as REST API extension name but also for mapping between URL and API extension Groovy script file.
  • The Groovy class must implement the interface org.bonitasoft.web.extension.rest.RestApiController

page.properties

1 ) contentType=apiExtension

The above line tells you the type of resource.

2) name=custompage_TrackerCustDbAP

The name of the REST API extension. The name must start with custompage_ and should not include spaces or special characters.

3) You can optionally define a display name that will be used in Portal administration view

displaynName=User information REST API Extension

4) Also, optionally you can include a description.

description = REST API description

5) We will define the list of API extensions defined by your REST API extension.

apiExtensions=GetMethod,PostMethod

GetMethod.method=GET

GetMethod.method=POST

6) Define the URL path template

GetMethod.pathTemplate=example , PostMethod.pathTemplate = example

7) Declare the associated RestAPIController Groovy file

GetMethod.classFileName=com/commoninf/rest/api/GetMethod.groovy

PostMethod.classFileName=com/commoninf/rest/api/PostMethod.groovy

8) Declare the permission required to call the API

GetMethod.permissions=task_visualization

PostMethod.permissions=task_visualization

Steps to create Rest API Extension

Groovy File:

class GetMethod implements RestApiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetMethod.class)
    @Override
    RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder responseBuilder,
RestAPIContext context) {
		// Retrieve p parameter
        def p = request.getParameter "p"
        if (p == null) {
            return buildResponse(responseBuilder, HttpServletResponse.SC_BAD_REQUEST,"""{"error" :
"the parameter p is missing"}""")
        }
        // Retrieve c parameter
        def c = request.getParameter "c"
        if (c == null) {
            return buildResponse(responseBuilder, HttpServletResponse.SC_BAD_REQUEST,"""{"error" :
"the parameter c is missing"}""")
        }
        Properties props = loadProperties("configuration.properties", context.resourceProvider)
        String paramValue = props["myParameterKey"]
        def result = [ "p" : p ,"c" : c , "myParameterKey" : paramValue ,"method" :
"Executing GetMethod REST Extension." ]
		LOGGER.info("Executing GetMethod REST Extension.");
        return buildResponse(responseBuilder, HttpServletResponse.SC_OK, new JsonBuilder(result).toString())
    }
    RestApiResponse buildResponse(RestApiResponseBuilder responseBuilder, int httpStatus, Serializable body) {
        return responseBuilder.with {
            withResponseStatus(httpStatus)
            withResponse(body)
            build()
        }
    }
    RestApiResponse buildPagedResponse(RestApiResponseBuilder responseBuilder, Serializable body, int p, int c,
long total) {
        return responseBuilder.with {
            withContentRange(p,c,total)
            withResponse(body)
            build()
        }
    }
    Properties loadProperties(String fileName, ResourceProvider resourceProvider) {
        Properties props = new Properties()
        resourceProvider.getResourceAsStream(fileName).withStream { InputStream s ->
            props.load s
        }
        props
    }
}

Get Method Result:

{“p”:”0″,”c”:”-1″,”myParameterKey”:”bonitasoft.com”,”method”:”Executing GetMethod REST Extension.”}

class PostMethod implements RestApiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PostMethod.class)
    @Override
    RestApiResponse doHandle(HttpServletRequest request, RestApiResponseBuilder responseBuilder,
RestAPIContext context) {
        def p = request.getParameter "p"
        if (p == null) {
            return buildResponse(responseBuilder, HttpServletResponse.SC_BAD_REQUEST,"""{"error" :
"the parameter p is missing"}""")
        }
        def c = request.getParameter "c"
        if (c == null) {
            return buildResponse(responseBuilder, HttpServletResponse.SC_BAD_REQUEST,"""{"error" :
"the parameter c is missing"}""")
        }
        Properties props = loadProperties("configuration.properties", context.resourceProvider)
        String paramValue = props["myParameterKey"]
        def result = [ "p" : p ,"c" : c , "myParameterKey" : paramValue ,"method" :
"Executing PostMethod REST Extension." ]
		LOGGER.info("Executing PostMethod REST Extension.");
        return buildResponse(responseBuilder, HttpServletResponse.SC_OK, new JsonBuilder(result).toString())
    }
    RestApiResponse buildResponse(RestApiResponseBuilder responseBuilder, int httpStatus, Serializable body) {
        return responseBuilder.with {
            withResponseStatus(httpStatus)
            withResponse(body)
            build()
        }
    }
    RestApiResponse buildPagedResponse(RestApiResponseBuilder responseBuilder, Serializable body, int p, int c,
long total) {
        return responseBuilder.with {
            withContentRange(p,c,total)
            withResponse(body)
            build()
        }
    }
    Properties loadProperties(String fileName, ResourceProvider resourceProvider) {
        Properties props = new Properties()
        resourceProvider.getResourceAsStream(fileName).withStream { InputStream s ->
            props.load s
        }
        props
    }
}

Post Method Result:

{“p”:”0″,”c”:”-1″,”myParameterKey”:”bonitasoft.com”,”method”:”Executing PostMethod REST Extension.”}

Summing up

Bonita BPM native UIs (Portal and forms) are generic, and they often lack the richness or extensibility that is required to build complex business applications. Rest API extensions are most useful in creating UIs leveraging Angular.

I am sure if we follow the above-mentioned steps, we can configure multiple http methods in a single REST API extension.

This brings us to the end of this article on Different HTTP Methods in Bonita REST Extensions. Feel free to leave your question below. We’ll get back to you in the shortest possible time.

 

Author

  Vijay Kumar Nalagonda is a Senior Technical Associate at Evoke Technologies. He is technically adept on various technologies including Java, Spring MVC, Spring AOP, Hibernate, REST Web Services, and AngularJS. Vijay likes to read about emerging technologies and is keen to explore Hadoop and NoSQL databases.
Please follow and share

Leave a comment