|
13 | 13 | package org.flowable.http.common.impl;
|
14 | 14 |
|
15 | 15 | import java.io.IOException;
|
| 16 | +import java.util.Base64; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Locale; |
16 | 19 | import java.util.Set;
|
17 | 20 | import java.util.concurrent.CompletableFuture;
|
18 | 21 |
|
|
25 | 28 | import org.flowable.http.common.api.HttpHeaders;
|
26 | 29 | import org.flowable.http.common.api.HttpRequest;
|
27 | 30 | import org.flowable.http.common.api.HttpResponse;
|
| 31 | +import org.flowable.http.common.api.HttpResponseVariableType; |
28 | 32 | import org.flowable.http.common.api.client.AsyncExecutableHttpRequest;
|
29 | 33 | import org.flowable.http.common.api.client.ExecutableHttpRequest;
|
30 | 34 | import org.flowable.http.common.api.client.FlowableHttpClient;
|
31 | 35 |
|
| 36 | +import com.fasterxml.jackson.core.JsonProcessingException; |
32 | 37 | import com.fasterxml.jackson.databind.ObjectMapper;
|
33 | 38 | import com.fasterxml.jackson.databind.node.MissingNode;
|
| 39 | +import com.fasterxml.jackson.databind.node.NullNode; |
34 | 40 |
|
35 | 41 | /**
|
36 | 42 | * @author Filip Hrisafov
|
@@ -73,7 +79,14 @@ public abstract class BaseHttpActivityDelegate {
|
73 | 79 | // Flag to save the response variables as a transient variable. Default is false (Optional).
|
74 | 80 | protected Expression saveResponseParametersTransient;
|
75 | 81 | // 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 |
76 | 87 | protected Expression saveResponseVariableAsJson;
|
| 88 | + |
| 89 | + protected Expression responseVariableType; |
77 | 90 | // Prefix for the execution variable names (Optional)
|
78 | 91 | protected Expression resultVariablePrefix;
|
79 | 92 |
|
@@ -105,7 +118,7 @@ protected RequestData createRequest(VariableContainer variableContainer, String
|
105 | 118 | requestData.setSaveRequest(ExpressionUtils.getBooleanFromField(saveRequestVariables, variableContainer));
|
106 | 119 | requestData.setSaveResponse(ExpressionUtils.getBooleanFromField(saveResponseParameters, variableContainer));
|
107 | 120 | requestData.setSaveResponseTransient(ExpressionUtils.getBooleanFromField(saveResponseParametersTransient, variableContainer));
|
108 |
| - requestData.setSaveResponseAsJson(ExpressionUtils.getBooleanFromField(saveResponseVariableAsJson, variableContainer)); |
| 121 | + requestData.setResponseVariableType(determineResponseVariableType(variableContainer)); |
109 | 122 | requestData.setPrefix(ExpressionUtils.getStringFromField(resultVariablePrefix, variableContainer));
|
110 | 123 |
|
111 | 124 | String failCodes = ExpressionUtils.getStringFromField(failStatusCodes, variableContainer);
|
@@ -163,10 +176,8 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
|
163 | 176 | if (!response.isBodyResponseHandled()) {
|
164 | 177 | String responseVariableName = ExpressionUtils.getStringFromField(this.responseVariableName, variableContainer);
|
165 | 178 | 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 | + |
170 | 181 | if (request.isSaveResponseTransient()) {
|
171 | 182 | variableContainer.setTransientVariable(varName, varValue);
|
172 | 183 | } else {
|
@@ -205,6 +216,101 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
|
205 | 216 | }
|
206 | 217 | }
|
207 | 218 |
|
| 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 | + |
208 | 314 | protected CompletableFuture<ExecutionData> prepareAndExecuteRequest(RequestData request, boolean parallelInSameTransaction, AsyncTaskInvoker taskInvoker) {
|
209 | 315 | ExecutableHttpRequest httpRequest = httpClient.prepareRequest(request.getHttpRequest());
|
210 | 316 |
|
@@ -303,7 +409,8 @@ public static class RequestData {
|
303 | 409 | protected boolean saveRequest;
|
304 | 410 | protected boolean saveResponse;
|
305 | 411 | protected boolean saveResponseTransient;
|
306 |
| - protected boolean saveResponseAsJson; |
| 412 | + |
| 413 | + protected HttpResponseVariableType responseVariableType; |
307 | 414 | protected String prefix;
|
308 | 415 |
|
309 | 416 | public HttpRequest getHttpRequest() {
|
@@ -362,12 +469,12 @@ public void setSaveResponseTransient(boolean saveResponseTransient) {
|
362 | 469 | this.saveResponseTransient = saveResponseTransient;
|
363 | 470 | }
|
364 | 471 |
|
365 |
| - public boolean isSaveResponseAsJson() { |
366 |
| - return saveResponseAsJson; |
| 472 | + public HttpResponseVariableType getResponseVariableType() { |
| 473 | + return responseVariableType; |
367 | 474 | }
|
368 | 475 |
|
369 |
| - public void setSaveResponseAsJson(boolean saveResponseAsJson) { |
370 |
| - this.saveResponseAsJson = saveResponseAsJson; |
| 476 | + public void setResponseVariableType(HttpResponseVariableType responseVariableType) { |
| 477 | + this.responseVariableType = responseVariableType; |
371 | 478 | }
|
372 | 479 |
|
373 | 480 | public String getPrefix() {
|
|
0 commit comments