Writing a REST Interface with JSON schema-validation and some Java transformations with Mule ESB is really easy and can be developed in a very short time. We want to show a sample application to demonstrate how easy Camunda can be used with Mule in an approver scenario for Slack.
In Mule Anypoint Studio we first define a simple flow (in our example Mule2Camunda-GatewayFlow) that receives incoming JSON messages via a REST-Interface and validates incoming data. This can be done with a HTTPListener Configuration and an APIkit Router straight behind. The APIkit Router expects a REST API definition file based on RAML. We place our RAML File in the APIkit Router component which maps all incoming and valid (!) JSON messages to the TransformerFlow.
The RAML (RESTful API Modeling Language) is the heart of our small interface as it defines resource, request methods as well as body and response messages. The body part includes an example JSON message and a helpful JSON schema, so we have the possibility to check every JSON message before processing it. For every client-submitted data we currently check for required objects and data types. This actually helps a lot as it prohibits invalid requests to be transported into background systems or logic. Invalid requests are rejected at the border to the application network.
In our case, both fields “name” and “text” are defined as Strings. As we always need both fields to successfully post a slack message they are set to be required. Using a JSON Schema validation even allows us to check against regex patterns, so this kind of validation can block non-secure JSON requests if needed. Sending any invalid message will be denied with a http status 500 – BadRequestException response.
After successfully checking incoming JSON data we pass the JSON to our TransformerFlow which also happens in the APIKitrouter (you remember the mapping?). The TransformerFlow then converts JSON values to a HashMap and we convert it again in plain Java Code to JSON format and send it directly to Camunda via HttpRequest. This transformation part is needed when we have to add or aggregate data before sending the JSON request against Camunda. In this case we just put the data into a Camunda-readable JSON format and can send it with the HttpRequest-Handler.
So, that’s it. We have a running REST/JSON Interface with JSON validation and could easily do more fancy stuff with it.