use of org.activiti.rest.exception.ActivitiContentNotSupportedException in project Activiti by Activiti.
the class TaskVariableBaseResource method setBinaryVariable.
protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, Task task, 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 {
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(task, variableName, variableBytes, scope, isNew);
} else if (isSerializableVariableAllowed) {
// Try deserializing the object
ObjectInputStream stream = new ObjectInputStream(file.getInputStream());
Object value = stream.readObject();
setVariable(task, variableName, value, scope, isNew);
stream.close();
} else {
throw new ActivitiContentNotSupportedException("Serialized objects are not allowed");
}
return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, task.getId(), null, null);
} catch (IOException ioe) {
throw new ActivitiIllegalArgumentException("Error getting binary variable", 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.exception.ActivitiContentNotSupportedException 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());
}
}
Aggregations