use of org.wso2.carbon.bpmn.rest.model.runtime.AttachmentDataHolder in project carbon-business-process by wso2.
the class BaseExecutionService method createBinaryExecutionVariable.
protected RestVariable createBinaryExecutionVariable(Execution execution, int responseVariableType, UriInfo uriInfo, boolean isNew, MultipartBody multipartBody) {
boolean debugEnabled = log.isDebugEnabled();
Response.ResponseBuilder responseBuilder = Response.ok();
List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments = multipartBody.getAllAttachments();
int attachmentSize = attachments.size();
if (attachmentSize <= 0) {
throw new ActivitiIllegalArgumentException("No Attachments found with the request body");
}
AttachmentDataHolder attachmentDataHolder = new AttachmentDataHolder();
for (int i = 0; i < attachmentSize; i++) {
org.apache.cxf.jaxrs.ext.multipart.Attachment attachment = attachments.get(i);
String contentDispositionHeaderValue = attachment.getHeader("Content-Disposition");
String contentType = attachment.getHeader("Content-Type");
if (debugEnabled) {
log.debug("Going to iterate:" + i);
log.debug("contentDisposition:" + contentDispositionHeaderValue);
}
if (contentDispositionHeaderValue != null) {
contentDispositionHeaderValue = contentDispositionHeaderValue.trim();
Map<String, String> contentDispositionHeaderValueMap = Utils.processContentDispositionHeader(contentDispositionHeaderValue);
String dispositionName = contentDispositionHeaderValueMap.get("name");
DataHandler dataHandler = attachment.getDataHandler();
OutputStream outputStream = null;
if ("name".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Name Reading error occured", e);
}
if (outputStream != null) {
String fileName = outputStream.toString();
attachmentDataHolder.setName(fileName);
}
} else if ("type".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Type Reading error occured", e);
}
if (outputStream != null) {
String typeName = outputStream.toString();
attachmentDataHolder.setType(typeName);
}
} else if ("scope".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Description Reading error occured", e);
}
if (outputStream != null) {
String description = outputStream.toString();
attachmentDataHolder.setScope(description);
}
}
if (contentType != null) {
if ("file".equals(dispositionName)) {
InputStream inputStream = null;
try {
inputStream = dataHandler.getInputStream();
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Error Occured During processing empty body.", e);
}
if (inputStream != null) {
attachmentDataHolder.setContentType(contentType);
byte[] attachmentArray = new byte[0];
try {
attachmentArray = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Processing Attachment Body Failed.", e);
}
attachmentDataHolder.setAttachmentArray(attachmentArray);
}
}
}
}
}
attachmentDataHolder.printDebug();
if (attachmentDataHolder.getName() == null) {
throw new ActivitiIllegalArgumentException("Attachment name is required.");
}
if (attachmentDataHolder.getAttachmentArray() == null) {
throw new ActivitiIllegalArgumentException("Empty attachment body was found in request body after " + "decoding the request" + ".");
}
String variableScope = attachmentDataHolder.getScope();
String variableName = attachmentDataHolder.getName();
String variableType = attachmentDataHolder.getType();
if (log.isDebugEnabled()) {
log.debug("variableScope:" + variableScope + " variableName:" + variableName + " variableType:" + variableType);
}
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;
}
RestVariable.RestVariableScope scope = RestVariable.RestVariableScope.LOCAL;
if (variableScope != null) {
scope = RestVariable.getScopeFromString(variableScope);
}
if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType)) {
// Use raw bytes as variable value
setVariable(execution, variableName, attachmentDataHolder.getAttachmentArray(), scope, isNew);
} else {
// Try deserializing the object
try (InputStream inputStream = new ByteArrayInputStream(attachmentDataHolder.getAttachmentArray());
ObjectInputStream stream = new ObjectInputStream(inputStream)) {
Object value = stream.readObject();
setVariable(execution, variableName, value, scope, isNew);
}
}
if (responseVariableType == RestResponseFactory.VARIABLE_PROCESS) {
return new RestResponseFactory().createBinaryRestVariable(variableName, scope, variableType, null, null, execution.getId(), uriInfo.getBaseUri().toString());
} else {
return new RestResponseFactory().createBinaryRestVariable(variableName, scope, variableType, null, execution.getId(), null, uriInfo.getBaseUri().toString());
}
} catch (IOException ioe) {
throw new ActivitiIllegalArgumentException("Could not process multipart content", ioe);
} catch (ClassNotFoundException ioe) {
throw new BPMNContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
}
}
use of org.wso2.carbon.bpmn.rest.model.runtime.AttachmentDataHolder in project carbon-business-process by wso2.
the class BaseTaskService method setBinaryVariable.
protected RestVariable setBinaryVariable(MultipartBody multipartBody, Task task, boolean isNew, UriInfo uriInfo) throws IOException {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Processing Binary variables");
}
Object result = null;
List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments = multipartBody.getAllAttachments();
int attachmentSize = attachments.size();
if (attachmentSize <= 0) {
throw new ActivitiIllegalArgumentException("No Attachments found with the request body");
}
AttachmentDataHolder attachmentDataHolder = new AttachmentDataHolder();
for (int i = 0; i < attachmentSize; i++) {
org.apache.cxf.jaxrs.ext.multipart.Attachment attachment = attachments.get(i);
String contentDispositionHeaderValue = attachment.getHeader("Content-Disposition");
String contentType = attachment.getHeader("Content-Type");
if (debugEnabled) {
log.debug("Going to iterate:" + i);
log.debug("contentDisposition:" + contentDispositionHeaderValue);
}
if (contentDispositionHeaderValue != null) {
contentDispositionHeaderValue = contentDispositionHeaderValue.trim();
Map<String, String> contentDispositionHeaderValueMap = Utils.processContentDispositionHeader(contentDispositionHeaderValue);
String dispositionName = contentDispositionHeaderValueMap.get("name");
DataHandler dataHandler = attachment.getDataHandler();
OutputStream outputStream = null;
if ("name".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Binary Variable Name Reading error occured", e);
}
if (outputStream != null) {
String fileName = outputStream.toString();
attachmentDataHolder.setName(fileName);
}
} else if ("type".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("\"Binary Variable Type Reading error occured", e);
}
if (outputStream != null) {
String typeName = outputStream.toString();
attachmentDataHolder.setType(typeName);
}
} else if ("scope".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Binary Variable scopeDescription Reading error " + "occured", e);
}
if (outputStream != null) {
String scope = outputStream.toString();
attachmentDataHolder.setScope(scope);
}
}
if (contentType != null) {
if ("file".equals(dispositionName)) {
InputStream inputStream = null;
try {
inputStream = dataHandler.getInputStream();
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Error Occured During processing empty body.", e);
}
if (inputStream != null) {
attachmentDataHolder.setContentType(contentType);
byte[] attachmentArray = new byte[0];
try {
attachmentArray = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Processing BinaryV variable Body Failed.", e);
}
attachmentDataHolder.setAttachmentArray(attachmentArray);
}
}
}
}
}
attachmentDataHolder.printDebug();
String variableScope = attachmentDataHolder.getScope();
String variableName = attachmentDataHolder.getName();
String variableType = attachmentDataHolder.getType();
byte[] attachmentArray = attachmentDataHolder.getAttachmentArray();
try {
if (variableName == null) {
throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
}
if (attachmentArray == null) {
throw new ActivitiIllegalArgumentException("Empty attachment body was found in request body after " + "decoding the request" + ".");
}
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 {
attachmentDataHolder.setType(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE);
}
RestVariable.RestVariableScope scope = RestVariable.RestVariableScope.LOCAL;
if (variableScope != null) {
scope = RestVariable.getScopeFromString(variableScope);
}
if (variableScope != null) {
scope = RestVariable.getScopeFromString(variableScope);
}
if (RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType)) {
// Use raw bytes as variable value
setVariable(task, variableName, attachmentArray, scope, isNew);
} else {
// Try deserializing the object
try (InputStream inputStream = new ByteArrayInputStream(attachmentArray);
ObjectInputStream stream = new ObjectInputStream(inputStream)) {
Object value = stream.readObject();
setVariable(task, variableName, value, scope, isNew);
}
}
return new RestResponseFactory().createBinaryRestVariable(variableName, scope, variableType, task.getId(), null, null, uriInfo.getBaseUri().toString());
} catch (IOException ioe) {
throw new ActivitiIllegalArgumentException("Error getting binary variable", ioe);
} catch (ClassNotFoundException ioe) {
throw new BPMNContentNotSupportedException("The provided body contains a serialized object for which the class is nog found: " + ioe.getMessage());
}
}
use of org.wso2.carbon.bpmn.rest.model.runtime.AttachmentDataHolder in project carbon-business-process by wso2.
the class ProcessInstanceService method setBinaryVariable.
protected RestVariable setBinaryVariable(MultipartBody multipartBody, Execution execution, int responseVariableType, boolean isNew) throws IOException, ServletException {
boolean debugEnabled = log.isDebugEnabled();
List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments = multipartBody.getAllAttachments();
int attachmentSize = attachments.size();
if (attachmentSize <= 0) {
throw new ActivitiIllegalArgumentException("No Attachments found with the request body");
}
AttachmentDataHolder attachmentDataHolder = new AttachmentDataHolder();
for (int i = 0; i < attachmentSize; i++) {
org.apache.cxf.jaxrs.ext.multipart.Attachment attachment = attachments.get(i);
String contentDispositionHeaderValue = attachment.getHeader("Content-Disposition");
String contentType = attachment.getHeader("Content-Type");
if (debugEnabled) {
log.debug("Going to iterate:" + i);
log.debug("contentDisposition:" + contentDispositionHeaderValue);
}
if (contentDispositionHeaderValue != null) {
contentDispositionHeaderValue = contentDispositionHeaderValue.trim();
Map<String, String> contentDispositionHeaderValueMap = Utils.processContentDispositionHeader(contentDispositionHeaderValue);
String dispositionName = contentDispositionHeaderValueMap.get("name");
DataHandler dataHandler = attachment.getDataHandler();
OutputStream outputStream;
if ("name".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Name Reading error occured", e);
}
if (outputStream != null) {
String fileName = outputStream.toString();
attachmentDataHolder.setName(fileName);
}
} else if ("type".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Type Reading error occured", e);
}
if (outputStream != null) {
String typeName = outputStream.toString();
attachmentDataHolder.setType(typeName);
}
} else if ("scope".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Description Reading error occured", e);
}
if (outputStream != null) {
String scope = outputStream.toString();
attachmentDataHolder.setScope(scope);
}
}
if (contentType != null) {
if ("file".equals(dispositionName)) {
InputStream inputStream;
try {
inputStream = dataHandler.getInputStream();
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Error Occured During processing empty body.", e);
}
if (inputStream != null) {
attachmentDataHolder.setContentType(contentType);
byte[] attachmentArray = new byte[0];
try {
attachmentArray = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Processing Attachment Body Failed.", e);
}
attachmentDataHolder.setAttachmentArray(attachmentArray);
}
}
}
}
}
attachmentDataHolder.printDebug();
String variableScope = attachmentDataHolder.getScope();
String variableName = attachmentDataHolder.getName();
String variableType = attachmentDataHolder.getType();
if (attachmentDataHolder.getName() == null) {
throw new ActivitiIllegalArgumentException("Attachment name is required.");
}
if (attachmentDataHolder.getAttachmentArray() == null) {
throw new ActivitiIllegalArgumentException("Empty attachment body was found in request body after " + "decoding the request" + ".");
}
if (debugEnabled) {
log.debug("variableScope:" + variableScope + " variableName:" + variableName + " variableType:" + variableType);
}
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;
}
RestVariable.RestVariableScope scope = RestVariable.RestVariableScope.LOCAL;
if (variableScope != null) {
scope = RestVariable.getScopeFromString(variableScope);
}
if (variableType.equals(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
// Use raw bytes as variable value
setVariable(execution, variableName, attachmentDataHolder.getAttachmentArray(), scope, isNew);
} else {
// Try deserializing the object
try (InputStream inputStream = new ByteArrayInputStream(attachmentDataHolder.getAttachmentArray());
ObjectInputStream stream = new ObjectInputStream(inputStream)) {
Object value = stream.readObject();
setVariable(execution, variableName, value, scope, isNew);
}
}
RestResponseFactory restResponseFactory = new RestResponseFactory();
if (responseVariableType == RestResponseFactory.VARIABLE_PROCESS) {
return new RestResponseFactory().createBinaryRestVariable(variableName, scope, variableType, null, null, execution.getId(), uriInfo.getBaseUri().toString());
} else {
return restResponseFactory.createBinaryRestVariable(variableName, scope, variableType, null, execution.getId(), null, uriInfo.getBaseUri().toString());
}
} catch (IOException ioe) {
throw new ActivitiIllegalArgumentException("Could not process multipart content", ioe);
} catch (ClassNotFoundException ioe) {
throw new BPMNContentNotSupportedException("The provided body contains a serialized object for which the " + "class is not found: " + ioe.getMessage());
}
}
use of org.wso2.carbon.bpmn.rest.model.runtime.AttachmentDataHolder in project carbon-business-process by wso2.
the class WorkflowTaskService method createAttachmentForBinary.
@POST
@Path("/{taskId}/attachments")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response createAttachmentForBinary(@PathParam("taskId") String taskId, MultipartBody multipartBody, @Context HttpServletRequest httpServletRequest) {
boolean debugEnabled = log.isDebugEnabled();
List<org.apache.cxf.jaxrs.ext.multipart.Attachment> attachments = multipartBody.getAllAttachments();
int attachmentSize = attachments.size();
if (attachmentSize <= 0) {
throw new ActivitiIllegalArgumentException("No Attachments found with the request body");
}
AttachmentDataHolder attachmentDataHolder = new AttachmentDataHolder();
for (int i = 0; i < attachmentSize; i++) {
org.apache.cxf.jaxrs.ext.multipart.Attachment attachment = attachments.get(i);
String contentDispositionHeaderValue = attachment.getHeader("Content-Disposition");
String contentType = attachment.getHeader("Content-Type");
if (debugEnabled) {
log.debug("Going to iterate:" + i);
log.debug("contentDisposition:" + contentDispositionHeaderValue);
}
if (contentDispositionHeaderValue != null) {
contentDispositionHeaderValue = contentDispositionHeaderValue.trim();
Map<String, String> contentDispositionHeaderValueMap = Utils.processContentDispositionHeader(contentDispositionHeaderValue);
String dispositionName = contentDispositionHeaderValueMap.get("name");
DataHandler dataHandler = attachment.getDataHandler();
OutputStream outputStream = null;
if ("name".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Name Reading error occured", e);
}
if (outputStream != null) {
String fileName = outputStream.toString();
attachmentDataHolder.setName(fileName);
}
} else if ("type".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Type Reading error occured", e);
}
if (outputStream != null) {
String typeName = outputStream.toString();
attachmentDataHolder.setType(typeName);
}
} else if ("description".equals(dispositionName)) {
try {
outputStream = Utils.getAttachmentStream(dataHandler.getInputStream());
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Attachment Description Reading error occured", e);
}
if (outputStream != null) {
String description = outputStream.toString();
attachmentDataHolder.setDescription(description);
}
}
if (contentType != null) {
if ("file".equals(dispositionName)) {
InputStream inputStream = null;
try {
inputStream = dataHandler.getInputStream();
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Error Occured During processing empty body.", e);
}
if (inputStream != null) {
attachmentDataHolder.setContentType(contentType);
byte[] attachmentArray = new byte[0];
try {
attachmentArray = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new ActivitiIllegalArgumentException("Processing Attachment Body Failed.", e);
}
attachmentDataHolder.setAttachmentArray(attachmentArray);
}
}
}
}
}
attachmentDataHolder.printDebug();
if (attachmentDataHolder.getName() == null) {
throw new ActivitiIllegalArgumentException("Attachment name is required.");
}
if (attachmentDataHolder.getAttachmentArray() == null) {
throw new ActivitiIllegalArgumentException("Empty attachment body was found in request body after " + "decoding the request" + ".");
}
TaskService taskService = BPMNOSGIService.getTaskService();
Task task = getTaskFromRequest(taskId);
Response.ResponseBuilder responseBuilder = Response.ok();
AttachmentResponse result = null;
try {
InputStream inputStream = new ByteArrayInputStream(attachmentDataHolder.getAttachmentArray());
Attachment createdAttachment = taskService.createAttachment(attachmentDataHolder.getContentType(), task.getId(), task.getProcessInstanceId(), attachmentDataHolder.getName(), attachmentDataHolder.getDescription(), inputStream);
responseBuilder.status(Response.Status.CREATED);
result = new RestResponseFactory().createAttachmentResponse(createdAttachment, uriInfo.getBaseUri().toString());
} catch (Exception e) {
throw new ActivitiException("Error creating attachment response", e);
}
return responseBuilder.status(Response.Status.CREATED).entity(result).build();
}
Aggregations