use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory 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();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class WorkflowTaskService method getEvent.
@GET
@Path("/{taskId}/events/{eventId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getEvent(@PathParam("taskId") String taskId, @PathParam("eventId") String eventId) {
HistoricTaskInstance task = getHistoricTaskFromRequest(taskId);
TaskService taskService = BPMNOSGIService.getTaskService();
Event event = taskService.getEvent(eventId);
if (event == null || !task.getId().equals(event.getTaskId())) {
throw new ActivitiObjectNotFoundException("Task '" + task.getId() + "' doesn't have an event with id '" + eventId + "'.", Event.class);
}
EventResponse eventResponse = new RestResponseFactory().createEventResponse(event, uriInfo.getBaseUri().toString());
return Response.ok().entity(eventResponse).build();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class WorkflowTaskService method getComments.
@GET
@Path("/{taskId}/comments")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getComments(@PathParam("taskId") String taskId) {
HistoricTaskInstance task = getHistoricTaskFromRequest(taskId);
TaskService taskService = BPMNOSGIService.getTaskService();
List<CommentResponse> commentResponseList = new RestResponseFactory().createRestCommentList(taskService.getTaskComments(task.getId()), uriInfo.getBaseUri().toString());
CommentResponseCollection commentResponseCollection = new CommentResponseCollection();
commentResponseCollection.setCommentResponseList(commentResponseList);
return Response.ok().entity(commentResponseCollection).build();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class WorkflowTaskService method updateTask.
@PUT
@Path("/{taskId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateTask(@PathParam("taskId") String taskId, TaskRequest taskRequest) {
if (taskRequest == null) {
throw new ActivitiException("A request body was expected when updating the task.");
}
Task task = getTaskFromRequest(taskId);
// Populate the task properties based on the request
populateTaskFromRequest(task, taskRequest);
TaskService taskService = BPMNOSGIService.getTaskService();
// Save the task and fetch agian, it's possible that an assignment-listener has updated
// fields after it was saved so we can't use the in-memory task
taskService.saveTask(task);
task = taskService.createTaskQuery().taskId(task.getId()).singleResult();
return Response.ok().entity(new RestResponseFactory().createTaskResponse(task, uriInfo.getBaseUri().toString())).build();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class WorkflowTaskService method createIdentityLink.
@POST
@Path("/{taskId}/identitylinks")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createIdentityLink(@PathParam("taskId") String taskId, RestIdentityLink identityLink) {
Task task = getTaskFromRequest(taskId);
if (identityLink.getGroup() == null && identityLink.getUser() == null) {
throw new ActivitiIllegalArgumentException("A group or a user is required to create an identity link.");
}
if (identityLink.getGroup() != null && identityLink.getUser() != null) {
throw new ActivitiIllegalArgumentException("Only one of user or group can be used to create an identity link.");
}
if (identityLink.getType() == null) {
throw new ActivitiIllegalArgumentException("The identity link type is required.");
}
TaskService taskService = BPMNOSGIService.getTaskService();
if (identityLink.getGroup() != null) {
taskService.addGroupIdentityLink(task.getId(), identityLink.getGroup(), identityLink.getType());
} else {
taskService.addUserIdentityLink(task.getId(), identityLink.getUser(), identityLink.getType());
}
RestIdentityLink restIdentityLink = new RestResponseFactory().createRestIdentityLink(identityLink.getType(), identityLink.getUser(), identityLink.getGroup(), task.getId(), null, null, uriInfo.getBaseUri().toString());
return Response.ok().status(Response.Status.CREATED).entity(restIdentityLink).build();
}
Aggregations