use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class ProcessInstanceService method startInstance.
@POST
@Path("/")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response startInstance(ProcessInstanceCreateRequest processInstanceCreateRequest) {
if (log.isDebugEnabled()) {
log.debug("ProcessInstanceCreateRequest:" + processInstanceCreateRequest.getProcessDefinitionId());
log.debug(" processInstanceCreateRequest.getVariables().size():" + processInstanceCreateRequest.getVariables().size());
}
if (processInstanceCreateRequest.getProcessDefinitionId() == null && processInstanceCreateRequest.getProcessDefinitionKey() == null && processInstanceCreateRequest.getMessage() == null) {
throw new ActivitiIllegalArgumentException("Either processDefinitionId, processDefinitionKey or message is required.");
}
int paramsSet = ((processInstanceCreateRequest.getProcessDefinitionId() != null) ? 1 : 0) + ((processInstanceCreateRequest.getProcessDefinitionKey() != null) ? 1 : 0) + ((processInstanceCreateRequest.getMessage() != null) ? 1 : 0);
if (paramsSet > 1) {
throw new ActivitiIllegalArgumentException("Only one of processDefinitionId, processDefinitionKey or message should be set.");
}
if (processInstanceCreateRequest.isCustomTenantSet()) {
// Tenant-id can only be used with either key or message
if (processInstanceCreateRequest.getProcessDefinitionId() != null) {
throw new ActivitiIllegalArgumentException("TenantId can only be used with either processDefinitionKey or message.");
}
} else {
// if no tenantId, it must be from definitionId
if (processInstanceCreateRequest.getProcessDefinitionId() == null) {
throw new ActivitiIllegalArgumentException("TenantId should be specified to be used with either " + "processDefinitionKey or message.");
}
}
// Have to add the validation part here
if (!isValidUserToStartProcess(processInstanceCreateRequest)) {
throw new RestApiBasicAuthenticationException("User doesn't have the necessary permission to start the process");
}
if (processInstanceCreateRequest.getSkipInstanceCreation() || processInstanceCreateRequest.getSkipInstanceCreationIfExist()) {
ProcessInstanceQueryRequest processInstanceQueryRequest = processInstanceCreateRequest.cloneInstanceCreationRequest();
Map<String, String> allRequestParams = allRequestParams(uriInfo);
DataResponse dataResponse = getQueryResponse(processInstanceQueryRequest, allRequestParams, uriInfo);
if (log.isDebugEnabled()) {
log.debug("ProcessInstanceCreation check:" + dataResponse.getSize());
}
int dataResponseSize = dataResponse.getSize();
if (dataResponseSize > 0) {
if (processInstanceCreateRequest.getCorrelate()) {
if (dataResponseSize != 1) {
String responseMessage = "Correlation matching failed as there are more than one matching instance with " + "given variables state";
throw new NotFoundException(Response.ok().entity(responseMessage).status(Response.Status.NOT_FOUND).build());
}
if (processInstanceCreateRequest.getMessageName() == null) {
String responseMessage = "Correlation matching failed as messageName property is not specified";
throw new ActivitiIllegalArgumentException(responseMessage);
}
return performCorrelation(processInstanceCreateRequest);
} else {
dataResponse.setMessage("Instance information corresponding to the request");
return Response.ok().entity(dataResponse).build();
}
}
}
RestResponseFactory restResponseFactory = new RestResponseFactory();
Map<String, Object> startVariables = null;
if (processInstanceCreateRequest.getVariables() != null) {
startVariables = new HashMap<>();
for (RestVariable variable : processInstanceCreateRequest.getVariables()) {
if (variable.getName() == null) {
throw new ActivitiIllegalArgumentException("Variable name is required.");
}
startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
}
}
// updated the additional variables
if (processInstanceCreateRequest.getAdditionalVariables() != null) {
if (startVariables == null) {
startVariables = new HashMap<>();
}
for (RestVariable variable : processInstanceCreateRequest.getAdditionalVariables()) {
if (variable.getName() == null) {
throw new ActivitiIllegalArgumentException("Additional Variable name is required.");
}
startVariables.put(variable.getName(), restResponseFactory.getVariableValue(variable));
}
}
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
IdentityService identityService = BPMNOSGIService.getIdentityService();
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String userName = carbonContext.getUsername();
ProcessInstanceResponse processInstanceResponse;
// Actually start the instance based on key or id
try {
ProcessInstance instance;
identityService.setAuthenticatedUserId(userName);
if (processInstanceCreateRequest.getProcessDefinitionId() != null) {
instance = runtimeService.startProcessInstanceById(processInstanceCreateRequest.getProcessDefinitionId(), processInstanceCreateRequest.getBusinessKey(), startVariables);
} else if (processInstanceCreateRequest.getProcessDefinitionKey() != null) {
if (processInstanceCreateRequest.isCustomTenantSet()) {
instance = runtimeService.startProcessInstanceByKeyAndTenantId(processInstanceCreateRequest.getProcessDefinitionKey(), processInstanceCreateRequest.getBusinessKey(), startVariables, processInstanceCreateRequest.getTenantId());
} else {
instance = runtimeService.startProcessInstanceByKey(processInstanceCreateRequest.getProcessDefinitionKey(), processInstanceCreateRequest.getBusinessKey(), startVariables);
}
} else {
if (processInstanceCreateRequest.isCustomTenantSet()) {
instance = runtimeService.startProcessInstanceByMessageAndTenantId(processInstanceCreateRequest.getMessage(), processInstanceCreateRequest.getBusinessKey(), startVariables, processInstanceCreateRequest.getTenantId());
} else {
instance = runtimeService.startProcessInstanceByMessage(processInstanceCreateRequest.getMessage(), processInstanceCreateRequest.getBusinessKey(), startVariables);
}
}
HistoryService historyService = BPMNOSGIService.getHistoryService();
if (processInstanceCreateRequest.getReturnVariables()) {
Map<String, Object> runtimeVariableMap = null;
List<HistoricVariableInstance> historicVariableList = null;
if (instance.isEnded()) {
historicVariableList = historyService.createHistoricVariableInstanceQuery().processInstanceId(instance.getId()).list();
} else {
runtimeVariableMap = runtimeService.getVariables(instance.getId());
}
processInstanceResponse = restResponseFactory.createProcessInstanceResponse(instance, true, runtimeVariableMap, historicVariableList, uriInfo.getBaseUri().toString());
} else {
processInstanceResponse = restResponseFactory.createProcessInstanceResponse(instance, uriInfo.getBaseUri().toString());
}
} catch (ActivitiObjectNotFoundException aonfe) {
throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe);
} finally {
identityService.setAuthenticatedUserId(null);
}
return Response.ok().status(Response.Status.CREATED).entity(processInstanceResponse).build();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class ProcessInstanceService method getProcessInstance.
@GET
@Path("/{processInstanceId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getProcessInstance(@PathParam("processInstanceId") String processInstanceId) {
ProcessInstance processInstance = getProcessInstanceFromRequest(processInstanceId);
RestResponseFactory restResponseFactory = new RestResponseFactory();
ProcessInstanceResponse processInstanceResponse = restResponseFactory.createProcessInstanceResponse(processInstance, uriInfo.getBaseUri().toString());
return Response.ok().entity(processInstanceResponse).build();
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory in project carbon-business-process by wso2.
the class ProcessInstanceService method getIdentityLinks.
@GET
@Path("/{processInstanceId}/identitylinks")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<RestIdentityLink> getIdentityLinks(@PathParam("processInstanceId") String processInstanceId) {
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
ProcessInstance processInstance = getProcessInstanceFromRequest(processInstanceId);
return new RestResponseFactory().createRestIdentityLinks(runtimeService.getIdentityLinksForProcessInstance(processInstance.getId()), uriInfo.getBaseUri().toString());
}
use of org.wso2.carbon.bpmn.rest.common.RestResponseFactory 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.common.RestResponseFactory in project carbon-business-process by wso2.
the class ExecutionService method performExecutionAction.
/**
* Execute an action on an execution
* @param executionId
* @param actionRequest
* @return Response
*/
@PUT
@Path("/{executionId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response performExecutionAction(@PathParam("executionId") String executionId, ExecutionActionRequest actionRequest) {
Execution execution = getExecutionFromRequest(executionId);
RuntimeService runtimeService = BPMNOSGIService.getRuntimeService();
if (ExecutionActionRequest.ACTION_SIGNAL.equals(actionRequest.getAction())) {
if (actionRequest.getVariables() != null) {
runtimeService.signal(execution.getId(), getVariablesToSet(actionRequest));
} else {
runtimeService.signal(execution.getId());
}
} else if (ExecutionActionRequest.ACTION_SIGNAL_EVENT_RECEIVED.equals(actionRequest.getAction())) {
if (actionRequest.getSignalName() == null) {
throw new ActivitiIllegalArgumentException("Signal name is required");
}
if (actionRequest.getVariables() != null) {
runtimeService.signalEventReceived(actionRequest.getSignalName(), execution.getId(), getVariablesToSet(actionRequest));
} else {
runtimeService.signalEventReceived(actionRequest.getSignalName(), execution.getId());
}
} else if (ExecutionActionRequest.ACTION_MESSAGE_EVENT_RECEIVED.equals(actionRequest.getAction())) {
if (actionRequest.getMessageName() == null) {
throw new ActivitiIllegalArgumentException("Message name is required");
}
if (actionRequest.getVariables() != null) {
runtimeService.messageEventReceived(actionRequest.getMessageName(), execution.getId(), getVariablesToSet(actionRequest));
} else {
runtimeService.messageEventReceived(actionRequest.getMessageName(), execution.getId());
}
} else {
throw new ActivitiIllegalArgumentException("Invalid action: '" + actionRequest.getAction() + "'.");
}
Response.ResponseBuilder response = Response.ok();
// Re-fetch the execution, could have changed due to action or even completed
execution = runtimeService.createExecutionQuery().executionId(execution.getId()).singleResult();
if (execution == null) {
// Execution is finished, return empty body to inform user
response.status(Response.Status.NO_CONTENT);
} else {
response.entity(new RestResponseFactory().createExecutionResponse(execution, uriInfo.getBaseUri().toString())).build();
}
return response.build();
}
Aggregations