24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
27
- import jakarta .servlet .http .HttpServletRequest ;
28
- import jakarta .servlet .http .HttpServletResponse ;
29
-
30
27
import org .apache .commons .io .IOUtils ;
31
28
import org .flowable .cmmn .api .runtime .CaseInstance ;
32
29
import org .flowable .cmmn .api .runtime .PlanItemInstance ;
48
45
49
46
import com .fasterxml .jackson .databind .ObjectMapper ;
50
47
48
+ import jakarta .servlet .http .HttpServletRequest ;
49
+ import jakarta .servlet .http .HttpServletResponse ;
50
+
51
51
/**
52
52
* @author Tijs Rademakers
53
53
*/
@@ -180,21 +180,22 @@ protected List<RestVariable> processCaseVariables(CaseInstance caseInstance) {
180
180
return result ;
181
181
}
182
182
183
- protected Object createVariable (CaseInstance caseInstance , HttpServletRequest request , HttpServletResponse response ) {
184
- return createVariable (caseInstance .getId (), CmmnRestResponseFactory .VARIABLE_CASE , request , response , RestVariableScope .GLOBAL ,
183
+ protected Object createVariable (CaseInstance caseInstance , boolean async , HttpServletRequest request , HttpServletResponse response ) {
184
+ return createVariable (caseInstance .getId (), CmmnRestResponseFactory .VARIABLE_CASE , async , request , response , RestVariableScope .GLOBAL ,
185
185
createVariableInterceptor (caseInstance ));
186
186
}
187
187
188
- protected Object createVariable (PlanItemInstance planItemInstance , HttpServletRequest request , HttpServletResponse response ) {
189
- return createVariable (planItemInstance .getId (), CmmnRestResponseFactory .VARIABLE_PLAN_ITEM , request , response , RestVariableScope .LOCAL ,
188
+ protected Object createVariable (PlanItemInstance planItemInstance , boolean async , HttpServletRequest request , HttpServletResponse response ) {
189
+ return createVariable (planItemInstance .getId (), CmmnRestResponseFactory .VARIABLE_PLAN_ITEM , async , request , response , RestVariableScope .LOCAL ,
190
190
createVariableInterceptor (planItemInstance ));
191
191
}
192
192
193
- protected Object createVariable (String instanceId , int variableType , HttpServletRequest request , HttpServletResponse response , RestVariableScope scope ,
194
- VariableInterceptor variableInterceptor ) {
193
+ protected Object createVariable (String instanceId , int variableType , boolean async , HttpServletRequest request , HttpServletResponse response ,
194
+ RestVariableScope scope , VariableInterceptor variableInterceptor ) {
195
+
195
196
Object result = null ;
196
197
if (request instanceof MultipartHttpServletRequest ) {
197
- result = setBinaryVariable ((MultipartHttpServletRequest ) request , instanceId , variableType , true , scope , variableInterceptor );
198
+ result = setBinaryVariable ((MultipartHttpServletRequest ) request , instanceId , variableType , true , async , scope , variableInterceptor );
198
199
} else {
199
200
200
201
List <RestVariable > inputVariables = new ArrayList <>();
@@ -229,19 +230,32 @@ protected Object createVariable(String instanceId, int variableType, HttpServlet
229
230
230
231
if (!variablesToSet .isEmpty ()) {
231
232
variableInterceptor .createVariables (variablesToSet );
232
- Map <String , Object > setVariables ;
233
+ Map <String , Object > setVariables = null ;
233
234
if (variableType == CmmnRestResponseFactory .VARIABLE_PLAN_ITEM || scope == RestVariableScope .LOCAL ) {
234
- runtimeService .setLocalVariables (instanceId , variablesToSet );
235
- setVariables = runtimeService .getLocalVariables (instanceId , variablesToSet .keySet ());
235
+ if (async ) {
236
+ runtimeService .setLocalVariablesAsync (instanceId , variablesToSet );
237
+
238
+ } else {
239
+ runtimeService .setLocalVariables (instanceId , variablesToSet );
240
+ setVariables = runtimeService .getLocalVariables (instanceId , variablesToSet .keySet ());
241
+ }
242
+
236
243
} else {
237
- runtimeService .setVariables (instanceId , variablesToSet );
238
- setVariables = runtimeService .getVariables (instanceId , variablesToSet .keySet ());
244
+ if (async ) {
245
+ runtimeService .setVariablesAsync (instanceId , variablesToSet );
246
+
247
+ } else {
248
+ runtimeService .setVariables (instanceId , variablesToSet );
249
+ setVariables = runtimeService .getVariables (instanceId , variablesToSet .keySet ());
250
+ }
239
251
}
240
252
241
- for (RestVariable inputVariable : inputVariables ) {
242
- String variableName = inputVariable .getName ();
243
- Object variableValue = setVariables .get (variableName );
244
- resultVariables .add (restResponseFactory .createRestVariable (variableName , variableValue , scope , instanceId , variableType , false ));
253
+ if (!async ) {
254
+ for (RestVariable inputVariable : inputVariables ) {
255
+ String variableName = inputVariable .getName ();
256
+ Object variableValue = setVariables .get (variableName );
257
+ resultVariables .add (restResponseFactory .createRestVariable (variableName , variableValue , scope , instanceId , variableType , false ));
258
+ }
245
259
}
246
260
}
247
261
}
@@ -265,23 +279,29 @@ public void deleteAllVariables(CaseInstance caseInstance) {
265
279
runtimeService .removeVariables (caseInstance .getId (), currentVariables );
266
280
}
267
281
268
- protected RestVariable setSimpleVariable (RestVariable restVariable , String instanceId , boolean isNew , RestVariableScope scope , int variableType , VariableInterceptor variableInterceptor ) {
282
+ protected RestVariable setSimpleVariable (RestVariable restVariable , String instanceId , boolean isNew , boolean async , RestVariableScope scope , int variableType , VariableInterceptor variableInterceptor ) {
269
283
if (restVariable .getName () == null ) {
270
284
throw new FlowableIllegalArgumentException ("Variable name is required" );
271
285
}
272
286
273
287
Object actualVariableValue = restResponseFactory .getVariableValue (restVariable );
274
288
275
- setVariable (instanceId , restVariable .getName (), actualVariableValue , scope , isNew , variableInterceptor );
289
+ setVariable (instanceId , restVariable .getName (), actualVariableValue , scope , isNew , async , variableInterceptor );
276
290
277
- RestVariable variable = getVariableFromRequestWithoutAccessCheck (instanceId , restVariable .getName (), variableType , false );
278
- // We are setting the scope because the fetched variable does not have it
279
- variable .setVariableScope (scope );
291
+ RestVariable variable = null ;
292
+
293
+ if (!async ) {
294
+ variable = getVariableFromRequestWithoutAccessCheck (instanceId , restVariable .getName (), variableType , false );
295
+
296
+ // We are setting the scope because the fetched variable does not have it
297
+ variable .setVariableScope (scope );
298
+ }
299
+
280
300
return variable ;
281
301
}
282
302
283
303
protected RestVariable setBinaryVariable (MultipartHttpServletRequest request , String instanceId , int responseVariableType , boolean isNew ,
284
- RestVariableScope scope , VariableInterceptor variableInterceptor ) {
304
+ boolean async , RestVariableScope scope , VariableInterceptor variableInterceptor ) {
285
305
286
306
// Validate input and set defaults
287
307
if (request .getFileMap ().size () == 0 ) {
@@ -338,31 +358,39 @@ protected RestVariable setBinaryVariable(MultipartHttpServletRequest request, St
338
358
if (variableType .equals (CmmnRestResponseFactory .BYTE_ARRAY_VARIABLE_TYPE )) {
339
359
// Use raw bytes as variable value
340
360
byte [] variableBytes = IOUtils .toByteArray (file .getInputStream ());
341
- setVariable (instanceId , variableName , variableBytes , scope , isNew , variableInterceptor );
361
+ setVariable (instanceId , variableName , variableBytes , scope , isNew , async , variableInterceptor );
342
362
343
363
} else if (isSerializableVariableAllowed ) {
344
364
// Try deserializing the object
345
365
ObjectInputStream stream = new ObjectInputStream (file .getInputStream ());
346
366
Object value = stream .readObject ();
347
- setVariable (instanceId , variableName , value , scope , isNew , variableInterceptor );
367
+ setVariable (instanceId , variableName , value , scope , isNew , async , variableInterceptor );
348
368
stream .close ();
349
369
} else {
350
370
throw new FlowableContentNotSupportedException ("Serialized objects are not allowed" );
351
371
}
352
372
353
- RestVariable restVariable = getVariableFromRequestWithoutAccessCheck (instanceId , variableName , responseVariableType , false );
354
- // We are setting the scope because the fetched variable does not have it
355
- restVariable .setVariableScope (scope );
373
+ RestVariable restVariable = null ;
374
+
375
+ if (!async ) {
376
+ restVariable = getVariableFromRequestWithoutAccessCheck (instanceId , variableName , responseVariableType , false );
377
+
378
+ // We are setting the scope because the fetched variable does not have it
379
+ restVariable .setVariableScope (scope );
380
+ }
381
+
356
382
return restVariable ;
383
+
357
384
} catch (IOException ioe ) {
358
385
throw new FlowableIllegalArgumentException ("Could not process multipart content" , ioe );
386
+
359
387
} catch (ClassNotFoundException ioe ) {
360
388
throw new FlowableContentNotSupportedException (
361
389
"The provided body contains a serialized object for which the class was not found: " + ioe .getMessage ());
362
390
}
363
391
}
364
392
365
- protected void setVariable (String instanceId , String name , Object value , RestVariableScope scope , boolean isNew , VariableInterceptor variableInterceptor ) {
393
+ protected void setVariable (String instanceId , String name , Object value , RestVariableScope scope , boolean isNew , boolean async , VariableInterceptor variableInterceptor ) {
366
394
if (isNew ) {
367
395
variableInterceptor .createVariables (Collections .singletonMap (name , value ));
368
396
} else {
@@ -374,9 +402,19 @@ protected void setVariable(String instanceId, String name, Object value, RestVar
374
402
if (isNew && runtimeService .hasLocalVariable (instanceId , name )) {
375
403
throw new FlowableConflictException ("Local variable '" + name + "' is already present on plan item instance '" + instanceId + "'." );
376
404
}
377
- runtimeService .setLocalVariable (instanceId , name , value );
405
+
406
+ if (async ) {
407
+ runtimeService .setLocalVariableAsync (instanceId , name , value );
408
+ } else {
409
+ runtimeService .setLocalVariable (instanceId , name , value );
410
+ }
411
+
378
412
} else {
379
- runtimeService .setVariable (instanceId , name , value );
413
+ if (async ) {
414
+ runtimeService .setVariableAsync (instanceId , name , value );
415
+ } else {
416
+ runtimeService .setVariable (instanceId , name , value );
417
+ }
380
418
}
381
419
}
382
420
0 commit comments