I have done an implementation to invoke the http REST endpoints from camunda bpmn via camunda http connector.
In this case, first, the BpmnInvoker
REST end point will be called providing the bpmnProcessId
as a path param and Authorization
key as header param with the requestBody
Then within the BpmnInvoker
,relevant bpmn diagram will be invoked passing the requestBody values. And within these bpmn tasks there will be some service tasks that will invoke the REST endpoints.
To call the REST endpoints from BPMN diagrams, I use camunda http connector. And in this case, I need to pass the Authorization key as a http header to the REST endpoint.
For that I add the Authorization key to the variable map and take it as a variable in the bpmn diagram and add to the header Map as mentioned in the image and bpmn diagram.
But the problem is since this Authorization is normally more than 4000 characters in length. So, it can't be passed as a String via camunda engine. I tried converting it to the another object (eg : byte[] / StringBuffer etc) assuming it will be stored as a blob, but in that case it gives Class Cast Exception when it is going to assign to the http header.
BPMNInvoke.java
@Path("/base")
public class BpmnInvoker {
ProcessEngine defaultProcessEngine = BpmPlatform.getProcessEngineService().getDefaultProcessEngine();
RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
@POST
@Path("/path/{bpmnProcessId}")
public Response start(@PathParam("bpmnProcessId") String bpmnProcessId, String requestBody, @HeaderParam("Authorization") String authorization) {
Map<String, Object> variableMap = new HashMap<String, Object>();
variableMap = IntegrationUtility.convertJSONStringToMap(requestBody);
// Set the Authorization token to the variable map.
// Normally this is more than 4000 characters long dynamic key.
// It must be set as a http header value when invoking REST endpoint via http connector.
variableMap.put("Authorization", authorization);
// Invoke the bpmn diagram by calling the startProcessInstanceByKey on runtimeService.
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnProcessId, variableMap);
// rest of the work after completing the process engine
}
}
----
----
<bpmn:serviceTask id="Task_1ujngj7" name="Dummy Service 2">
<bpmn:extensionElements>
<camunda:connector>
<camunda:inputOutput>
<camunda:inputParameter name="url">http://localhost:8080/x-services/path/to/rest/service</camunda:inputParameter>
<camunda:inputParameter name="method">POST</camunda:inputParameter>
<camunda:inputParameter name="headers">
<camunda:map>
<camunda:entry key="Authorization">${Authorization}</camunda:entry>
<camunda:entry key="Content-Type">application/json</camunda:entry>
</camunda:map>
</camunda:inputParameter>
<camunda:inputParameter name="payload">${payload}</camunda:inputParameter>
<camunda:outputParameter name="payload">${response}</camunda:outputParameter>
</camunda:inputOutput>
<camunda:connectorId>http-connector</camunda:connectorId>
</camunda:connector>
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_0w4wfbo</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_153ti9s</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:sequenceFlow id="SequenceFlow_1kvov2r" sourceRef="Task_0i9s02e" targetRef="EndEvent_0o062nq" />
<bpmn:serviceTask id="Task_0i9s02e" name="DummyServiceClear" camunda:delegateExpression="${dummyServiceClear}">
<bpmn:incoming>SequenceFlow_153ti9s</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_1kvov2r</bpmn:outgoing>
</bpmn:serviceTask>
----
----
try this:
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats;
variableMap.put("Authorization", Variables
.objectValue(authorization)
// tells the engine to use java serialization for persisting the value
.serializationDataFormat(SerializationDataFormats.JAVA)
.create());