Skip to content

Commit 4abdc2c

Browse files
committed
Allow to define the response variable type of HttpServiceTask
Allows to download binary content and store it as byte array to the variable or base64 encoded string.
1 parent eeb859f commit 4abdc2c

9 files changed

+519
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Licensed under the Apache License, Version 2.0 (the "License");
2+
* you may not use this file except in compliance with the License.
3+
* You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
package org.flowable.http.common.api;
14+
15+
public enum HttpResponseVariableType {
16+
17+
AUTO,
18+
STRING,
19+
JSON,
20+
BASE64,
21+
BYTES
22+
}

modules/flowable-http-common/src/main/java/org/flowable/http/common/impl/BaseHttpActivityDelegate.java

+116-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
package org.flowable.http.common.impl;
1414

1515
import java.io.IOException;
16+
import java.util.Base64;
17+
import java.util.List;
18+
import java.util.Locale;
1619
import java.util.Set;
1720
import java.util.concurrent.CompletableFuture;
1821

@@ -25,12 +28,15 @@
2528
import org.flowable.http.common.api.HttpHeaders;
2629
import org.flowable.http.common.api.HttpRequest;
2730
import org.flowable.http.common.api.HttpResponse;
31+
import org.flowable.http.common.api.HttpResponseVariableType;
2832
import org.flowable.http.common.api.client.AsyncExecutableHttpRequest;
2933
import org.flowable.http.common.api.client.ExecutableHttpRequest;
3034
import org.flowable.http.common.api.client.FlowableHttpClient;
3135

36+
import com.fasterxml.jackson.core.JsonProcessingException;
3237
import com.fasterxml.jackson.databind.ObjectMapper;
3338
import com.fasterxml.jackson.databind.node.MissingNode;
39+
import com.fasterxml.jackson.databind.node.NullNode;
3440

3541
/**
3642
* @author Filip Hrisafov
@@ -72,8 +78,14 @@ public abstract class BaseHttpActivityDelegate {
7278
protected Expression responseVariableName;
7379
// Flag to save the response variables as a transient variable. Default is false (Optional).
7480
protected Expression saveResponseParametersTransient;
81+
/**
82+
* @deprecated use {@link #responseVariableType} instead
83+
*/
7584
// Flag to save the response variable as an ObjectNode instead of a String
85+
@Deprecated
7686
protected Expression saveResponseVariableAsJson;
87+
88+
protected Expression responseVariableType;
7789
// Prefix for the execution variable names (Optional)
7890
protected Expression resultVariablePrefix;
7991

@@ -105,7 +117,7 @@ protected RequestData createRequest(VariableContainer variableContainer, String
105117
requestData.setSaveRequest(ExpressionUtils.getBooleanFromField(saveRequestVariables, variableContainer));
106118
requestData.setSaveResponse(ExpressionUtils.getBooleanFromField(saveResponseParameters, variableContainer));
107119
requestData.setSaveResponseTransient(ExpressionUtils.getBooleanFromField(saveResponseParametersTransient, variableContainer));
108-
requestData.setSaveResponseAsJson(ExpressionUtils.getBooleanFromField(saveResponseVariableAsJson, variableContainer));
120+
requestData.setResponseVariableType(determineResponseVariableType(variableContainer));
109121
requestData.setPrefix(ExpressionUtils.getStringFromField(resultVariablePrefix, variableContainer));
110122

111123
String failCodes = ExpressionUtils.getStringFromField(failStatusCodes, variableContainer);
@@ -163,10 +175,8 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
163175
if (!response.isBodyResponseHandled()) {
164176
String responseVariableName = ExpressionUtils.getStringFromField(this.responseVariableName, variableContainer);
165177
String varName = StringUtils.isNotEmpty(responseVariableName) ? responseVariableName : request.getPrefix() + "ResponseBody";
166-
Object varValue = request.isSaveResponseAsJson() && response.getBody() != null ? objectMapper.readTree(response.getBody()) : response.getBody();
167-
if (varValue instanceof MissingNode) {
168-
varValue = null;
169-
}
178+
Object varValue = determineResponseVariableValue(request, response, objectMapper);
179+
170180
if (request.isSaveResponseTransient()) {
171181
variableContainer.setTransientVariable(varName, varValue);
172182
} else {
@@ -205,6 +215,101 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
205215
}
206216
}
207217

218+
protected Object determineResponseVariableValue(RequestData request, HttpResponse response, ObjectMapper objectMapper) throws IOException {
219+
HttpResponseVariableType responseVariableType = request.getResponseVariableType();
220+
String contentType = getContentTypeWithoutCharset(response);
221+
switch (responseVariableType) {
222+
case AUTO:
223+
if (isTextContentType(contentType)) {
224+
return response.getBody();
225+
} else if (isJsonContentType(contentType)) {
226+
return readJsonTree(response, objectMapper);
227+
} else {
228+
return response.getBodyBytes();
229+
}
230+
case STRING: {
231+
return response.getBody();
232+
}
233+
case JSON: {
234+
return readJsonTree(response, objectMapper);
235+
}
236+
case BYTES: {
237+
return response.getBodyBytes();
238+
}
239+
case BASE64: {
240+
byte[] bodyBytes = response.getBodyBytes();
241+
return Base64.getEncoder().encodeToString(bodyBytes);
242+
}
243+
default: {
244+
throw new FlowableException("Unsupported response variable type: " + responseVariableType);
245+
}
246+
}
247+
}
248+
249+
protected Object readJsonTree(HttpResponse response, ObjectMapper objectMapper) throws JsonProcessingException {
250+
Object varValue;
251+
if (response.getBody() != null) {
252+
varValue = objectMapper.readTree(response.getBody());
253+
} else {
254+
varValue = response.getBody();
255+
}
256+
if (varValue instanceof MissingNode || varValue instanceof NullNode) {
257+
varValue = null;
258+
}
259+
return varValue;
260+
}
261+
262+
protected boolean isJsonContentType(String contentType) {
263+
if (contentType != null) {
264+
return contentType != null && contentType.endsWith("/json") || contentType.endsWith("+json");
265+
}
266+
return false;
267+
}
268+
269+
protected boolean isTextContentType(String contentType) {
270+
if (contentType != null) {
271+
return contentType.startsWith("text/") || contentType.endsWith("/xml") || contentType.endsWith("+xml");
272+
}
273+
return false;
274+
}
275+
276+
protected String getContentTypeWithoutCharset(HttpResponse response) {
277+
List<String> contentTypeHeader = response.getHttpHeaders().get("Content-Type");
278+
if (contentTypeHeader != null && !contentTypeHeader.isEmpty()) {
279+
String contentType = contentTypeHeader.get(0);
280+
if (contentType.indexOf(';') >= 0) {
281+
contentType = contentType.split(";")[0]; // remove charset if present
282+
}
283+
return contentType;
284+
}
285+
return null;
286+
}
287+
288+
protected HttpResponseVariableType determineResponseVariableType(VariableContainer variableContainer) {
289+
// We use string as default, when neither is set, for backwards compatibility reasons.
290+
HttpResponseVariableType effectiveResponseVariableType = HttpResponseVariableType.STRING;
291+
if (responseVariableType != null && saveResponseVariableAsJson != null) {
292+
throw new FlowableException(
293+
"Only one of responseVariableType or saveResponseVariableAsJson can be set, not both. saveResponseVariableAsJson is deprecated, please use responseVariableType instead.");
294+
}
295+
if (responseVariableType != null) {
296+
String responseVariableTypeString = ExpressionUtils.getStringFromField(responseVariableType, variableContainer).toUpperCase(Locale.ROOT);
297+
if (responseVariableTypeString == null) {
298+
effectiveResponseVariableType = HttpResponseVariableType.AUTO;
299+
} else if (responseVariableTypeString.equalsIgnoreCase("true")) {
300+
// Backwards compatibility - if the responseVariableType is set to true, then we assume it's JSON
301+
effectiveResponseVariableType = HttpResponseVariableType.JSON;
302+
} else {
303+
effectiveResponseVariableType = HttpResponseVariableType.valueOf(responseVariableTypeString.toUpperCase(Locale.ROOT));
304+
}
305+
} else if (saveResponseVariableAsJson != null) {
306+
// Backwards compatibility - if the saveResponseVariableAsJson is set, then we assume it's JSON otherwise it's a string (old default).
307+
boolean saveResponseAsJson = ExpressionUtils.getBooleanFromField(saveResponseVariableAsJson, variableContainer);
308+
effectiveResponseVariableType = saveResponseAsJson ? HttpResponseVariableType.JSON : HttpResponseVariableType.STRING;
309+
}
310+
return effectiveResponseVariableType;
311+
}
312+
208313
protected CompletableFuture<ExecutionData> prepareAndExecuteRequest(RequestData request, boolean parallelInSameTransaction, AsyncTaskInvoker taskInvoker) {
209314
ExecutableHttpRequest httpRequest = httpClient.prepareRequest(request.getHttpRequest());
210315

@@ -303,7 +408,8 @@ public static class RequestData {
303408
protected boolean saveRequest;
304409
protected boolean saveResponse;
305410
protected boolean saveResponseTransient;
306-
protected boolean saveResponseAsJson;
411+
412+
protected HttpResponseVariableType responseVariableType;
307413
protected String prefix;
308414

309415
public HttpRequest getHttpRequest() {
@@ -362,12 +468,12 @@ public void setSaveResponseTransient(boolean saveResponseTransient) {
362468
this.saveResponseTransient = saveResponseTransient;
363469
}
364470

365-
public boolean isSaveResponseAsJson() {
366-
return saveResponseAsJson;
471+
public HttpResponseVariableType getResponseVariableType() {
472+
return responseVariableType;
367473
}
368474

369-
public void setSaveResponseAsJson(boolean saveResponseAsJson) {
370-
this.saveResponseAsJson = saveResponseAsJson;
475+
public void setResponseVariableType(HttpResponseVariableType responseVariableType) {
476+
this.responseVariableType = responseVariableType;
371477
}
372478

373479
public String getPrefix() {

modules/flowable-http/src/test/java/org/flowable/http/bpmn/HttpServiceTaskTest.java

+132
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.net.SocketException;
2323
import java.net.SocketTimeoutException;
24+
import java.nio.charset.StandardCharsets;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -51,6 +52,7 @@
5152

5253
import com.fasterxml.jackson.databind.JsonNode;
5354
import com.fasterxml.jackson.databind.ObjectMapper;
55+
import com.fasterxml.jackson.databind.node.ObjectNode;
5456

5557
import net.javacrumbs.jsonunit.core.Option;
5658

@@ -675,6 +677,136 @@ public void testGetWithVariableParameters(String requestParam, String expectedRe
675677
assertProcessEnded(procId);
676678
}
677679

680+
@Test
681+
@Deployment(resources = "org/flowable/http/bpmn/HttpServiceTaskTest.testGetWithSaveResponseVariableAndVariableType.bpmn20.xml")
682+
public void testGetWithSaveResponseVariablePdfIsPutAsByteArrayForAutoType() {
683+
String procId = runtimeService.createProcessInstanceBuilder()
684+
.processDefinitionKey("simpleGetOnly")
685+
.transientVariable("requestUrl","http://localhost:9798/binary/pdf")
686+
.transientVariable("responseVariableType","auto")
687+
.start()
688+
.getId();
689+
List<HistoricVariableInstance> variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
690+
assertThat(variables)
691+
.extracting(HistoricVariableInstance::getVariableName)
692+
.containsExactly("responseVariable");
693+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(byte[].class, value -> assertThat(value).isNotEmpty());
694+
assertProcessEnded(procId);
695+
696+
697+
// request byte array
698+
procId = runtimeService.createProcessInstanceBuilder()
699+
.processDefinitionKey("simpleGetOnly")
700+
.transientVariable("requestUrl","http://localhost:9798/binary/pdf")
701+
.transientVariable("responseVariableType","bytes")
702+
.start()
703+
.getId();
704+
variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
705+
assertThat(variables)
706+
.extracting(HistoricVariableInstance::getVariableName)
707+
.containsExactly("responseVariable");
708+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(byte[].class, value -> assertThat(value).isNotEmpty());
709+
assertProcessEnded(procId);
710+
}
711+
712+
@Test
713+
@Deployment(resources = "org/flowable/http/bpmn/HttpServiceTaskTest.testGetWithSaveResponseVariableAndVariableType.bpmn20.xml")
714+
public void testGetWithSaveResponseVariableAndVariableTypeStringOctetStream() {
715+
String procId = runtimeService.createProcessInstanceBuilder()
716+
.processDefinitionKey("simpleGetOnly")
717+
.transientVariable("requestUrl","http://localhost:9798/binary/octet-stream-string")
718+
.transientVariable("responseVariableType","string")
719+
.start()
720+
.getId();
721+
List<HistoricVariableInstance> variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
722+
assertThat(variables)
723+
.extracting(HistoricVariableInstance::getVariableName)
724+
.containsExactly("responseVariable");
725+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(String.class,
726+
value -> assertThat(value).isEqualTo("Content-Type is octet-stream, but still a string"));
727+
assertProcessEnded(procId);
728+
}
729+
730+
@Test
731+
@Deployment(resources = "org/flowable/http/bpmn/HttpServiceTaskTest.testGetWithSaveResponseVariableAndVariableType.bpmn20.xml")
732+
public void testGetWithSaveResponseVariableAndVariableTypeJsonContent() {
733+
// AUTO
734+
String procId = runtimeService.createProcessInstanceBuilder()
735+
.processDefinitionKey("simpleGetOnly")
736+
.transientVariable("requestUrl","http://localhost:9798/test")
737+
.transientVariable("responseVariableType","auto")
738+
.start()
739+
.getId();
740+
List<HistoricVariableInstance> variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
741+
assertThat(variables)
742+
.extracting(HistoricVariableInstance::getVariableName)
743+
.containsExactly("responseVariable");
744+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(ObjectNode.class,
745+
value -> assertThatJson(value).isEqualTo("{name:{firstName:'John', lastName:'Doe'}}"));
746+
assertProcessEnded(procId);
747+
748+
// BASE64
749+
procId = runtimeService.createProcessInstanceBuilder()
750+
.processDefinitionKey("simpleGetOnly")
751+
.transientVariable("requestUrl","http://localhost:9798/test")
752+
.transientVariable("responseVariableType","base64")
753+
.start()
754+
.getId();
755+
variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
756+
assertThat(variables)
757+
.extracting(HistoricVariableInstance::getVariableName)
758+
.containsExactly("responseVariable");
759+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(String.class,
760+
value -> assertThat(value).isEqualTo("eyJuYW1lIjp7ImZpcnN0TmFtZSI6IkpvaG4iLCJsYXN0TmFtZSI6IkRvZSJ9fQo="));
761+
assertProcessEnded(procId);
762+
763+
// BYTES
764+
procId = runtimeService.createProcessInstanceBuilder()
765+
.processDefinitionKey("simpleGetOnly")
766+
.transientVariable("requestUrl","http://localhost:9798/test")
767+
.transientVariable("responseVariableType","bytes")
768+
.start()
769+
.getId();
770+
variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
771+
assertThat(variables)
772+
.extracting(HistoricVariableInstance::getVariableName)
773+
.containsExactly("responseVariable");
774+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(byte[].class,
775+
value -> assertThat(value).asString(StandardCharsets.UTF_8).isEqualToIgnoringWhitespace("{\"name\":{\"firstName\":\"John\",\"lastName\":\"Doe\"}}"));
776+
assertProcessEnded(procId);
777+
778+
// STRING
779+
procId = runtimeService.createProcessInstanceBuilder()
780+
.processDefinitionKey("simpleGetOnly")
781+
.transientVariable("requestUrl","http://localhost:9798/test")
782+
.transientVariable("responseVariableType","string")
783+
.start()
784+
.getId();
785+
variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
786+
assertThat(variables)
787+
.extracting(HistoricVariableInstance::getVariableName)
788+
.containsExactly("responseVariable");
789+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(String.class,
790+
value -> assertThat(value).isEqualToIgnoringWhitespace("{\"name\":{\"firstName\":\"John\",\"lastName\":\"Doe\"}}"));
791+
assertProcessEnded(procId);
792+
793+
// JSON
794+
procId = runtimeService.createProcessInstanceBuilder()
795+
.processDefinitionKey("simpleGetOnly")
796+
.transientVariable("requestUrl","http://localhost:9798/test")
797+
.transientVariable("responseVariableType","json")
798+
.start()
799+
.getId();
800+
variables = historyService.createHistoricVariableInstanceQuery().processInstanceId(procId).list();
801+
assertThat(variables)
802+
.extracting(HistoricVariableInstance::getVariableName)
803+
.containsExactly("responseVariable");
804+
assertThat(variables.get(0).getValue()).isInstanceOfSatisfying(ObjectNode.class,
805+
value -> assertThatJson(value).isEqualTo("{name:{firstName:'John',lastName:'Doe'}}"));
806+
assertProcessEnded(procId);
807+
}
808+
809+
678810
static Stream<Arguments> parametersForGetWithVariableParameters() {
679811
return Stream.of(
680812
Arguments.arguments("Test+ Plus", "Test+ Plus"),

0 commit comments

Comments
 (0)