use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.
the class BaseExecutionVariableResource method setBinaryVariable.
protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, Execution execution, int responseVariableType, boolean isNew) {
// Validate input and set defaults
if (request.getFileMap().size() == 0) {
throw new ActivitiIllegalArgumentException("No file content was found in request body.");
}
// Get first file in the map, ignore possible other files
MultipartFile file = request.getFile(request.getFileMap().keySet().iterator().next());
if (file == null) {
throw new ActivitiIllegalArgumentException("No file content was found in request body.");
}
String variableScope = null;
String variableName = null;
String variableType = null;
Map<String, String[]> paramMap = request.getParameterMap();
for (String parameterName : paramMap.keySet()) {
if (paramMap.get(parameterName).length > 0) {
if (parameterName.equalsIgnoreCase("scope")) {
variableScope = paramMap.get(parameterName)[0];
} else if (parameterName.equalsIgnoreCase("name")) {
variableName = paramMap.get(parameterName)[0];
} else if (parameterName.equalsIgnoreCase("type")) {
variableType = paramMap.get(parameterName)[0];
}
}
}
try {
// Validate input and set defaults
if (variableName == null) {
throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
}
if (variableType != null) {
if (!RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType) && !RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variableType)) {
throw new ActivitiIllegalArgumentException("Only 'binary' and 'serializable' are supported as variable type.");
}
} else {
variableType = RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE;
}
RestVariableScope scope = RestVariableScope.LOCAL;
if (variableScope != null) {
scope = RestVariable.getScopeFromString(variableScope);
}
if (variableType.equals(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
// Use raw bytes as variable value
byte[] variableBytes = IOUtils.toByteArray(file.getInputStream());
setVariable(execution, variableName, variableBytes, scope, isNew);
} else if (isSerializableVariableAllowed) {
// Try deserializing the object
ObjectInputStream stream = new ObjectInputStream(file.getInputStream());
Object value = stream.readObject();
setVariable(execution, variableName, value, scope, isNew);
stream.close();
} else {
throw new ActivitiContentNotSupportedException("Serialized objects are not allowed");
}
if (responseVariableType == RestResponseFactory.VARIABLE_PROCESS) {
return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, null, null, execution.getId());
} else {
return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, null, execution.getId(), null);
}
} catch (IOException ioe) {
throw new ActivitiIllegalArgumentException("Could not process multipart content", ioe);
} catch (ClassNotFoundException ioe) {
throw new ActivitiContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
}
}
use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.
the class BaseVariableCollectionResource method createExecutionVariable.
protected Object createExecutionVariable(Execution execution, boolean override, int variableType, HttpServletRequest request, HttpServletResponse response) {
Object result = null;
if (request instanceof MultipartHttpServletRequest) {
result = setBinaryVariable((MultipartHttpServletRequest) request, execution, variableType, true);
} else {
List<RestVariable> inputVariables = new ArrayList<RestVariable>();
List<RestVariable> resultVariables = new ArrayList<RestVariable>();
result = resultVariables;
try {
@SuppressWarnings("unchecked") List<Object> variableObjects = (List<Object>) objectMapper.readValue(request.getInputStream(), List.class);
for (Object restObject : variableObjects) {
RestVariable restVariable = objectMapper.convertValue(restObject, RestVariable.class);
inputVariables.add(restVariable);
}
} catch (Exception e) {
throw new ActivitiIllegalArgumentException("Failed to serialize to a RestVariable instance", e);
}
if (inputVariables == null || inputVariables.size() == 0) {
throw new ActivitiIllegalArgumentException("Request didn't contain a list of variables to create.");
}
RestVariableScope sharedScope = null;
RestVariableScope varScope = null;
Map<String, Object> variablesToSet = new HashMap<String, Object>();
for (RestVariable var : inputVariables) {
// Validate if scopes match
varScope = var.getVariableScope();
if (var.getName() == null) {
throw new ActivitiIllegalArgumentException("Variable name is required");
}
if (varScope == null) {
varScope = RestVariableScope.LOCAL;
}
if (sharedScope == null) {
sharedScope = varScope;
}
if (varScope != sharedScope) {
throw new ActivitiIllegalArgumentException("Only allowed to update multiple variables in the same scope.");
}
if (!override && hasVariableOnScope(execution, var.getName(), varScope)) {
throw new ActivitiConflictException("Variable '" + var.getName() + "' is already present on execution '" + execution.getId() + "'.");
}
Object actualVariableValue = restResponseFactory.getVariableValue(var);
variablesToSet.put(var.getName(), actualVariableValue);
resultVariables.add(restResponseFactory.createRestVariable(var.getName(), actualVariableValue, varScope, execution.getId(), variableType, false));
}
if (!variablesToSet.isEmpty()) {
if (sharedScope == RestVariableScope.LOCAL) {
runtimeService.setVariablesLocal(execution.getId(), variablesToSet);
} else {
if (execution.getParentId() != null) {
// Explicitly set on parent, setting non-local variables on execution itself will override local-variables if exists
runtimeService.setVariables(execution.getParentId(), variablesToSet);
} else {
// Standalone task, no global variables possible
throw new ActivitiIllegalArgumentException("Cannot set global variables on execution '" + execution.getId() + "', task is not part of process.");
}
}
}
}
response.setStatus(HttpStatus.CREATED.value());
return result;
}
use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.
the class ProcessInstanceVariableResource method deleteVariable.
@RequestMapping(value = "/runtime/process-instances/{processInstanceId}/variables/{variableName}", method = RequestMethod.DELETE)
public void deleteVariable(@PathVariable("processInstanceId") String processInstanceId, @PathVariable("variableName") String variableName, @RequestParam(value = "scope", required = false) String scope, HttpServletResponse response) {
Execution execution = getProcessInstanceFromRequest(processInstanceId);
// Determine scope
RestVariableScope variableScope = RestVariableScope.LOCAL;
if (scope != null) {
variableScope = RestVariable.getScopeFromString(scope);
}
if (!hasVariableOnScope(execution, variableName, variableScope)) {
throw new ActivitiObjectNotFoundException("Execution '" + execution.getId() + "' doesn't have a variable '" + variableName + "' in scope " + variableScope.name().toLowerCase(), VariableInstanceEntity.class);
}
if (variableScope == RestVariableScope.LOCAL) {
runtimeService.removeVariableLocal(execution.getId(), variableName);
} else {
// Safe to use parentId, as the hasVariableOnScope would have stopped a global-var update on a root-execution
runtimeService.removeVariable(execution.getParentId(), variableName);
}
response.setStatus(HttpStatus.NO_CONTENT.value());
}
use of org.activiti.rest.service.api.engine.variable.RestVariable.RestVariableScope in project Activiti by Activiti.
the class TaskVariableBaseResource method getVariableFromRequest.
public RestVariable getVariableFromRequest(String taskId, String variableName, String scope, boolean includeBinary) {
boolean variableFound = false;
Object value = null;
RestVariableScope variableScope = RestVariable.getScopeFromString(scope);
if (variableScope == null) {
// First, check local variables (which have precedence when no scope is supplied)
if (taskService.hasVariableLocal(taskId, variableName)) {
value = taskService.getVariableLocal(taskId, variableName);
variableScope = RestVariableScope.LOCAL;
variableFound = true;
} else {
// Revert to execution-variable when not present local on the task
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task.getExecutionId() != null && runtimeService.hasVariable(task.getExecutionId(), variableName)) {
value = runtimeService.getVariable(task.getExecutionId(), variableName);
variableScope = RestVariableScope.GLOBAL;
variableFound = true;
}
}
} else if (variableScope == RestVariableScope.GLOBAL) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task.getExecutionId() != null && runtimeService.hasVariable(task.getExecutionId(), variableName)) {
value = runtimeService.getVariable(task.getExecutionId(), variableName);
variableFound = true;
}
} else if (variableScope == RestVariableScope.LOCAL) {
if (taskService.hasVariableLocal(taskId, variableName)) {
value = taskService.getVariableLocal(taskId, variableName);
variableFound = true;
}
}
if (!variableFound) {
throw new ActivitiObjectNotFoundException("Task '" + taskId + "' doesn't have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class);
} else {
return restResponseFactory.createRestVariable(variableName, value, variableScope, taskId, RestResponseFactory.VARIABLE_TASK, includeBinary);
}
}
Aggregations