ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("processOne",
Collections.singletonMap("overlappingVariable", (Object) "processValue"));
runtimeService.setVariableLocal(processInstance.getId(), "binaryVariable", "Initial binary value".getBytes());
Execution childExecution = runtimeService.createExecutionQuery().parentId(processInstance.getId()).singleResult();
assertNotNull(childExecution);
runtimeService.setVariableLocal(childExecution.getId(), "binaryVariable", "Initial binary value child".getBytes());
InputStream binaryContent = new ByteArrayInputStream("This is binary content".getBytes());
// Add name and type
Map<String, String> additionalFields = new HashMap<String, String>();
additionalFields.put("name", "binaryVariable");
additionalFields.put("type", "binary");
HttpPut httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(
RestUrls.URL_EXECUTION_VARIABLE, childExecution.getId(), "binaryVariable"));
httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("value", "application/octet-stream", binaryContent, additionalFields));
CloseableHttpResponse response = executeBinaryRequest(httpPut, HttpStatus.SC_OK);
JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
closeResponse(response);
assertNotNull(responseNode);
assertEquals("binaryVariable", responseNode.get("name").asText());
assertTrue(responseNode.get("value").isNull());
assertEquals("local", responseNode.get("scope").asText());
assertEquals("binary", responseNode.get("type").asText());
assertNotNull(responseNode.get("valueUrl").isNull());
assertTrue(responseNode.get("valueUrl").asText()
.endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION_VARIABLE_DATA, childExecution.getId(), "binaryVariable")));
// Check actual value of variable in engine
Object variableValue = runtimeService.getVariableLocal(childExecution.getId(), "binaryVariable");
assertNotNull(variableValue);
assertTrue(variableValue instanceof byte[]);
assertEquals("This is binary content", new String((byte[]) variableValue));
// Update variable in global scope
additionalFields.put("scope", "global");
binaryContent = new ByteArrayInputStream("This is binary content global".getBytes());
httpPut = new HttpPut(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(
RestUrls.URL_EXECUTION_VARIABLE, childExecution.getId(), "binaryVariable"));
httpPut.setEntity(HttpMultipartHelper.getMultiPartEntity("value", "application/octet-stream", binaryContent, additionalFields));
response = executeBinaryRequest(httpPut, HttpStatus.SC_OK);
responseNode = objectMapper.readTree(response.getEntity().getContent());
closeResponse(response);
assertNotNull(responseNode);
assertEquals("binaryVariable", responseNode.get("name").asText());
assertTrue(responseNode.get("value").isNull());
assertEquals("global", responseNode.get("scope").asText());
assertEquals("binary", responseNode.get("type").asText());
assertNotNull(responseNode.get("valueUrl").isNull());
assertTrue(responseNode.get("valueUrl").asText()
.endsWith(RestUrls.createRelativeResourceUrl(RestUrls.URL_EXECUTION_VARIABLE_DATA, childExecution.getId(), "binaryVariable")));
// Check actual global value of variable in engine
variableValue = runtimeService.getVariableLocal(processInstance.getId(), "binaryVariable");
assertNotNull(variableValue);
assertTrue(variableValue instanceof byte[]);
assertEquals("This is binary content global", new String((byte[]) variableValue));
// local value should remain unchainged
variableValue = runtimeService.getVariableLocal(childExecution.getId(), "binaryVariable");
assertNotNull(variableValue);
assertTrue(variableValue instanceof byte[]);
assertEquals("This is binary content", new String((byte[]) variableValue));
}