Skip to content

Commit

Permalink
Add variable async option to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsrademakers committed Oct 29, 2024
1 parent 5f1f990 commit 0d5eda0
Show file tree
Hide file tree
Showing 20 changed files with 1,101 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class CmmnRestUrls {
public static final String SEGMENT_CASE_INSTANCE_RESOURCE = "case-instances";
public static final String SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE = "plan-item-instances";
public static final String SEGMENT_VARIABLES = "variables";
public static final String SEGMENT_VARIABLES_ASYNC = "variables-async";
public static final String SEGMENT_VARIABLE_INSTANCE_RESOURCE = "variable-instances";
public static final String SEGMENT_EVENT_SUBSCRIPTIONS = "event-subscriptions";
public static final String SEGMENT_SUBTASKS = "subtasks";
Expand Down Expand Up @@ -242,11 +243,21 @@ public final class CmmnRestUrls {
* URL template for case instance variable collection: <i>cmmn-runtime/case-instances/{0:processInstanceId}/variables</i>
*/
public static final String[] URL_CASE_INSTANCE_VARIABLE_COLLECTION = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_CASE_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES };

/**
* URL template for case instance variable collection: <i>cmmn-runtime/case-instances/{0:processInstanceId}/variables-async</i>
*/
public static final String[] URL_CASE_INSTANCE_VARIABLE_ASYNC_COLLECTION = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_CASE_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES_ASYNC };

/**
* URL template for a single case instance variable: <i>cmmn-runtime/case-instances /{0:caseInstanceId}/variables/{1:variableName}</i>
*/
public static final String[] URL_CASE_INSTANCE_VARIABLE = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_CASE_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES, "{1}" };

/**
* URL template for a single case instance variable: <i>cmmn-runtime/case-instances /{0:caseInstanceId}/variables-async/{1:variableName}</i>
*/
public static final String[] URL_CASE_INSTANCE_VARIABLE_ASYNC = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_CASE_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES_ASYNC, "{1}" };

/**
* URL template for a single case instance variable data: <i>cmmn-runtime/case-instances/{0:processInstanceId}/variables/{1:variableName}/data</i>
Expand Down Expand Up @@ -289,17 +300,25 @@ public final class CmmnRestUrls {
*/
public static final String[] URL_PLAN_ITEM_INSTANCE = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}" };


/**
* URL template for plan item instance variables: <i>cmmn-runtime/plan-item-instances/{0:planItemInstanceId}/variables</i>
*/
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLES = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}",
SEGMENT_VARIABLES };
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLES = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES };

/**
* URL template for plan item instance variables: <i>cmmn-runtime/plan-item-instances/{0:planItemInstanceId}/variables-async</i>
*/
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLES_ASYNC = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES_ASYNC };

/**
* URL template for a single plan item instance variable: <i>cmmn-runtime/plan-item-instances/{0:planItemInstanceId}/variables/{1:variableName}</i>
*/
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLE = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES,
"{1}" };
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLE = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES, "{1}" };

/**
* URL template for a single plan item instance variable: <i>cmmn-runtime/plan-item-instances/{0:planItemInstanceId}/variables-async/{1:variableName}</i>
*/
public static final String[] URL_PLAN_ITEM_INSTANCE_VARIABLE_ASYNC = { SEGMENT_RUNTIME_RESOURCES, SEGMENT_PLAN_ITEM_INSTANCE_RESOURCE, "{0}", SEGMENT_VARIABLES_ASYNC, "{1}" };

/**
* URL template for a single case instance: <i>cmmn-runtime/plan-item-instances/{0:planItemInstanceId}/variables/{1:variableName}/data</i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.api.runtime.PlanItemInstance;
Expand All @@ -48,6 +45,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
* @author Tijs Rademakers
*/
Expand Down Expand Up @@ -180,21 +180,22 @@ protected List<RestVariable> processCaseVariables(CaseInstance caseInstance) {
return result;
}

protected Object createVariable(CaseInstance caseInstance, HttpServletRequest request, HttpServletResponse response) {
return createVariable(caseInstance.getId(), CmmnRestResponseFactory.VARIABLE_CASE, request, response, RestVariableScope.GLOBAL,
protected Object createVariable(CaseInstance caseInstance, boolean async, HttpServletRequest request, HttpServletResponse response) {
return createVariable(caseInstance.getId(), CmmnRestResponseFactory.VARIABLE_CASE, async, request, response, RestVariableScope.GLOBAL,
createVariableInterceptor(caseInstance));
}

protected Object createVariable(PlanItemInstance planItemInstance, HttpServletRequest request, HttpServletResponse response) {
return createVariable(planItemInstance.getId(), CmmnRestResponseFactory.VARIABLE_PLAN_ITEM, request, response, RestVariableScope.LOCAL,
protected Object createVariable(PlanItemInstance planItemInstance, boolean async, HttpServletRequest request, HttpServletResponse response) {
return createVariable(planItemInstance.getId(), CmmnRestResponseFactory.VARIABLE_PLAN_ITEM, async, request, response, RestVariableScope.LOCAL,
createVariableInterceptor(planItemInstance));
}

protected Object createVariable(String instanceId, int variableType, HttpServletRequest request, HttpServletResponse response, RestVariableScope scope,
VariableInterceptor variableInterceptor) {
protected Object createVariable(String instanceId, int variableType, boolean async, HttpServletRequest request, HttpServletResponse response,
RestVariableScope scope, VariableInterceptor variableInterceptor) {

Object result = null;
if (request instanceof MultipartHttpServletRequest) {
result = setBinaryVariable((MultipartHttpServletRequest) request, instanceId, variableType, true, scope, variableInterceptor);
result = setBinaryVariable((MultipartHttpServletRequest) request, instanceId, variableType, true, async, scope, variableInterceptor);
} else {

List<RestVariable> inputVariables = new ArrayList<>();
Expand Down Expand Up @@ -229,19 +230,32 @@ protected Object createVariable(String instanceId, int variableType, HttpServlet

if (!variablesToSet.isEmpty()) {
variableInterceptor.createVariables(variablesToSet);
Map<String, Object> setVariables;
Map<String, Object> setVariables = null;
if (variableType == CmmnRestResponseFactory.VARIABLE_PLAN_ITEM || scope == RestVariableScope.LOCAL) {
runtimeService.setLocalVariables(instanceId, variablesToSet);
setVariables = runtimeService.getLocalVariables(instanceId, variablesToSet.keySet());
if (async) {
runtimeService.setLocalVariablesAsync(instanceId, variablesToSet);

} else {
runtimeService.setLocalVariables(instanceId, variablesToSet);
setVariables = runtimeService.getLocalVariables(instanceId, variablesToSet.keySet());
}

} else {
runtimeService.setVariables(instanceId, variablesToSet);
setVariables = runtimeService.getVariables(instanceId, variablesToSet.keySet());
if (async) {
runtimeService.setVariablesAsync(instanceId, variablesToSet);

} else {
runtimeService.setVariables(instanceId, variablesToSet);
setVariables = runtimeService.getVariables(instanceId, variablesToSet.keySet());
}
}

for (RestVariable inputVariable : inputVariables) {
String variableName = inputVariable.getName();
Object variableValue = setVariables.get(variableName);
resultVariables.add(restResponseFactory.createRestVariable(variableName, variableValue, scope, instanceId, variableType, false));
if (!async) {
for (RestVariable inputVariable : inputVariables) {
String variableName = inputVariable.getName();
Object variableValue = setVariables.get(variableName);
resultVariables.add(restResponseFactory.createRestVariable(variableName, variableValue, scope, instanceId, variableType, false));
}
}
}
}
Expand All @@ -265,23 +279,29 @@ public void deleteAllVariables(CaseInstance caseInstance) {
runtimeService.removeVariables(caseInstance.getId(), currentVariables);
}

protected RestVariable setSimpleVariable(RestVariable restVariable, String instanceId, boolean isNew, RestVariableScope scope, int variableType, VariableInterceptor variableInterceptor) {
protected RestVariable setSimpleVariable(RestVariable restVariable, String instanceId, boolean isNew, boolean async, RestVariableScope scope, int variableType, VariableInterceptor variableInterceptor) {
if (restVariable.getName() == null) {
throw new FlowableIllegalArgumentException("Variable name is required");
}

Object actualVariableValue = restResponseFactory.getVariableValue(restVariable);

setVariable(instanceId, restVariable.getName(), actualVariableValue, scope, isNew, variableInterceptor);
setVariable(instanceId, restVariable.getName(), actualVariableValue, scope, isNew, async, variableInterceptor);

RestVariable variable = getVariableFromRequestWithoutAccessCheck(instanceId, restVariable.getName(), variableType, false);
// We are setting the scope because the fetched variable does not have it
variable.setVariableScope(scope);
RestVariable variable = null;

if (!async) {
variable = getVariableFromRequestWithoutAccessCheck(instanceId, restVariable.getName(), variableType, false);

// We are setting the scope because the fetched variable does not have it
variable.setVariableScope(scope);
}

return variable;
}

protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, String instanceId, int responseVariableType, boolean isNew,
RestVariableScope scope, VariableInterceptor variableInterceptor) {
boolean async, RestVariableScope scope, VariableInterceptor variableInterceptor) {

// Validate input and set defaults
if (request.getFileMap().size() == 0) {
Expand Down Expand Up @@ -338,31 +358,39 @@ protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, St
if (variableType.equals(CmmnRestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
// Use raw bytes as variable value
byte[] variableBytes = IOUtils.toByteArray(file.getInputStream());
setVariable(instanceId, variableName, variableBytes, scope, isNew, variableInterceptor);
setVariable(instanceId, variableName, variableBytes, scope, isNew, async, variableInterceptor);

} else if (isSerializableVariableAllowed) {
// Try deserializing the object
ObjectInputStream stream = new ObjectInputStream(file.getInputStream());
Object value = stream.readObject();
setVariable(instanceId, variableName, value, scope, isNew, variableInterceptor);
setVariable(instanceId, variableName, value, scope, isNew, async, variableInterceptor);
stream.close();
} else {
throw new FlowableContentNotSupportedException("Serialized objects are not allowed");
}

RestVariable restVariable = getVariableFromRequestWithoutAccessCheck(instanceId, variableName, responseVariableType, false);
// We are setting the scope because the fetched variable does not have it
restVariable.setVariableScope(scope);
RestVariable restVariable = null;

if (!async) {
restVariable = getVariableFromRequestWithoutAccessCheck(instanceId, variableName, responseVariableType, false);

// We are setting the scope because the fetched variable does not have it
restVariable.setVariableScope(scope);
}

return restVariable;

} catch (IOException ioe) {
throw new FlowableIllegalArgumentException("Could not process multipart content", ioe);

} catch (ClassNotFoundException ioe) {
throw new FlowableContentNotSupportedException(
"The provided body contains a serialized object for which the class was not found: " + ioe.getMessage());
}
}

protected void setVariable(String instanceId, String name, Object value, RestVariableScope scope, boolean isNew, VariableInterceptor variableInterceptor) {
protected void setVariable(String instanceId, String name, Object value, RestVariableScope scope, boolean isNew, boolean async, VariableInterceptor variableInterceptor) {
if (isNew) {
variableInterceptor.createVariables(Collections.singletonMap(name, value));
} else {
Expand All @@ -374,9 +402,19 @@ protected void setVariable(String instanceId, String name, Object value, RestVar
if (isNew && runtimeService.hasLocalVariable(instanceId, name)) {
throw new FlowableConflictException("Local variable '" + name + "' is already present on plan item instance '" + instanceId + "'.");
}
runtimeService.setLocalVariable(instanceId, name, value);

if (async) {
runtimeService.setLocalVariableAsync(instanceId, name, value);
} else {
runtimeService.setLocalVariable(instanceId, name, value);
}

} else {
runtimeService.setVariable(instanceId, name, value);
if (async) {
runtimeService.setVariableAsync(instanceId, name, value);
} else {
runtimeService.setVariable(instanceId, name, value);
}
}
}

Expand Down
Loading

0 comments on commit 0d5eda0

Please sign in to comment.