Skip to content

Commit 4dc20bd

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 4dc20bd

9 files changed

+520
-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

+117-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
@@ -73,7 +79,14 @@ public abstract class BaseHttpActivityDelegate {
7379
// Flag to save the response variables as a transient variable. Default is false (Optional).
7480
protected Expression saveResponseParametersTransient;
7581
// Flag to save the response variable as an ObjectNode instead of a String
82+
/**
83+
* @deprecated use {@link #responseVariableType} instead
84+
*/
85+
// Flag to save the response variable as an ObjectNode instead of a String
86+
@Deprecated
7687
protected Expression saveResponseVariableAsJson;
88+
89+
protected Expression responseVariableType;
7790
// Prefix for the execution variable names (Optional)
7891
protected Expression resultVariablePrefix;
7992

@@ -105,7 +118,7 @@ protected RequestData createRequest(VariableContainer variableContainer, String
105118
requestData.setSaveRequest(ExpressionUtils.getBooleanFromField(saveRequestVariables, variableContainer));
106119
requestData.setSaveResponse(ExpressionUtils.getBooleanFromField(saveResponseParameters, variableContainer));
107120
requestData.setSaveResponseTransient(ExpressionUtils.getBooleanFromField(saveResponseParametersTransient, variableContainer));
108-
requestData.setSaveResponseAsJson(ExpressionUtils.getBooleanFromField(saveResponseVariableAsJson, variableContainer));
121+
requestData.setResponseVariableType(determineResponseVariableType(variableContainer));
109122
requestData.setPrefix(ExpressionUtils.getStringFromField(resultVariablePrefix, variableContainer));
110123

111124
String failCodes = ExpressionUtils.getStringFromField(failStatusCodes, variableContainer);
@@ -163,10 +176,8 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
163176
if (!response.isBodyResponseHandled()) {
164177
String responseVariableName = ExpressionUtils.getStringFromField(this.responseVariableName, variableContainer);
165178
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-
}
179+
Object varValue = determineResponseVariableValue(request, response, objectMapper);
180+
170181
if (request.isSaveResponseTransient()) {
171182
variableContainer.setTransientVariable(varName, varValue);
172183
} else {
@@ -205,6 +216,101 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
205216
}
206217
}
207218

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

@@ -303,7 +409,8 @@ public static class RequestData {
303409
protected boolean saveRequest;
304410
protected boolean saveResponse;
305411
protected boolean saveResponseTransient;
306-
protected boolean saveResponseAsJson;
412+
413+
protected HttpResponseVariableType responseVariableType;
307414
protected String prefix;
308415

309416
public HttpRequest getHttpRequest() {
@@ -362,12 +469,12 @@ public void setSaveResponseTransient(boolean saveResponseTransient) {
362469
this.saveResponseTransient = saveResponseTransient;
363470
}
364471

365-
public boolean isSaveResponseAsJson() {
366-
return saveResponseAsJson;
472+
public HttpResponseVariableType getResponseVariableType() {
473+
return responseVariableType;
367474
}
368475

369-
public void setSaveResponseAsJson(boolean saveResponseAsJson) {
370-
this.saveResponseAsJson = saveResponseAsJson;
476+
public void setResponseVariableType(HttpResponseVariableType responseVariableType) {
477+
this.responseVariableType = responseVariableType;
371478
}
372479

373480
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)