|
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
|
@@ -72,8 +78,14 @@ public abstract class BaseHttpActivityDelegate {
|
72 | 78 | protected Expression responseVariableName;
|
73 | 79 | // Flag to save the response variables as a transient variable. Default is false (Optional).
|
74 | 80 | protected Expression saveResponseParametersTransient;
|
| 81 | + /** |
| 82 | + * @deprecated use {@link #responseVariableType} instead |
| 83 | + */ |
75 | 84 | // Flag to save the response variable as an ObjectNode instead of a String
|
| 85 | + @Deprecated |
76 | 86 | protected Expression saveResponseVariableAsJson;
|
| 87 | + |
| 88 | + protected Expression responseVariableType; |
77 | 89 | // Prefix for the execution variable names (Optional)
|
78 | 90 | protected Expression resultVariablePrefix;
|
79 | 91 |
|
@@ -105,7 +117,7 @@ protected RequestData createRequest(VariableContainer variableContainer, String
|
105 | 117 | requestData.setSaveRequest(ExpressionUtils.getBooleanFromField(saveRequestVariables, variableContainer));
|
106 | 118 | requestData.setSaveResponse(ExpressionUtils.getBooleanFromField(saveResponseParameters, variableContainer));
|
107 | 119 | requestData.setSaveResponseTransient(ExpressionUtils.getBooleanFromField(saveResponseParametersTransient, variableContainer));
|
108 |
| - requestData.setSaveResponseAsJson(ExpressionUtils.getBooleanFromField(saveResponseVariableAsJson, variableContainer)); |
| 120 | + requestData.setResponseVariableType(determineResponseVariableType(variableContainer)); |
109 | 121 | requestData.setPrefix(ExpressionUtils.getStringFromField(resultVariablePrefix, variableContainer));
|
110 | 122 |
|
111 | 123 | String failCodes = ExpressionUtils.getStringFromField(failStatusCodes, variableContainer);
|
@@ -163,10 +175,8 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
|
163 | 175 | if (!response.isBodyResponseHandled()) {
|
164 | 176 | String responseVariableName = ExpressionUtils.getStringFromField(this.responseVariableName, variableContainer);
|
165 | 177 | 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 | + |
170 | 180 | if (request.isSaveResponseTransient()) {
|
171 | 181 | variableContainer.setTransientVariable(varName, varValue);
|
172 | 182 | } else {
|
@@ -205,6 +215,101 @@ protected void saveResponseFields(VariableContainer variableContainer, RequestDa
|
205 | 215 | }
|
206 | 216 | }
|
207 | 217 |
|
| 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 | + |
208 | 313 | protected CompletableFuture<ExecutionData> prepareAndExecuteRequest(RequestData request, boolean parallelInSameTransaction, AsyncTaskInvoker taskInvoker) {
|
209 | 314 | ExecutableHttpRequest httpRequest = httpClient.prepareRequest(request.getHttpRequest());
|
210 | 315 |
|
@@ -303,7 +408,8 @@ public static class RequestData {
|
303 | 408 | protected boolean saveRequest;
|
304 | 409 | protected boolean saveResponse;
|
305 | 410 | protected boolean saveResponseTransient;
|
306 |
| - protected boolean saveResponseAsJson; |
| 411 | + |
| 412 | + protected HttpResponseVariableType responseVariableType; |
307 | 413 | protected String prefix;
|
308 | 414 |
|
309 | 415 | public HttpRequest getHttpRequest() {
|
@@ -362,12 +468,12 @@ public void setSaveResponseTransient(boolean saveResponseTransient) {
|
362 | 468 | this.saveResponseTransient = saveResponseTransient;
|
363 | 469 | }
|
364 | 470 |
|
365 |
| - public boolean isSaveResponseAsJson() { |
366 |
| - return saveResponseAsJson; |
| 471 | + public HttpResponseVariableType getResponseVariableType() { |
| 472 | + return responseVariableType; |
367 | 473 | }
|
368 | 474 |
|
369 |
| - public void setSaveResponseAsJson(boolean saveResponseAsJson) { |
370 |
| - this.saveResponseAsJson = saveResponseAsJson; |
| 475 | + public void setResponseVariableType(HttpResponseVariableType responseVariableType) { |
| 476 | + this.responseVariableType = responseVariableType; |
371 | 477 | }
|
372 | 478 |
|
373 | 479 | public String getPrefix() {
|
|
0 commit comments