fjtirado commented on code in PR #3937:
URL:
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3937#discussion_r2379185211
##########
kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/resulthandlers/DefaultRestWorkItemHandlerResult.java:
##########
@@ -18,18 +18,100 @@
*/
package org.kogito.workitem.rest.resulthandlers;
+import java.util.HashMap;
import java.util.Map;
+import java.util.Spliterators;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.kogito.workitem.rest.decorators.PrefixParamsDecorator;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import io.vertx.core.json.DecodeException;
import io.vertx.mutiny.core.buffer.Buffer;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import static
org.kogito.workitem.rest.RestWorkItemHandlerUtils.checkStatusCode;
public class DefaultRestWorkItemHandlerResult implements
RestWorkItemHandlerResult {
+ public static final String STATUS_CODE_PARAM = "STATUS_CODE";
+
+ private boolean returnHeaders = false;
+ private boolean returnStatusCode = false;
+ private boolean failOnStatusError = true;
+
+ public DefaultRestWorkItemHandlerResult(boolean returnHeaders, boolean
returnStatusCode, boolean failOnStatusError) {
+ this.returnHeaders = returnHeaders;
+ this.returnStatusCode = returnStatusCode;
+ this.failOnStatusError = failOnStatusError;
+ }
+
@Override
public Object apply(HttpResponse<Buffer> response, Class<?> target) {
- checkStatusCode(response);
- return target == null ? response.bodyAsJson(Map.class) :
response.bodyAsJson(target);
+ if (this.failOnStatusError) {
+ checkStatusCode(response);
+ }
+
+ Map<String, Object> result = new HashMap<>();
+
+ try {
+ Object body = target == null ? response.bodyAsJson(Map.class) :
response.bodyAsJson(target);
+
+ if (!this.returnHeaders && !this.returnStatusCode) {
+ return body;
+ }
+
+ if (body instanceof Map) {
+ ((Map<?, ?>) body).forEach((key, value) ->
result.put(String.valueOf(key), value));
+ } else if (body instanceof JsonNode && ((JsonNode)
body).isObject()) {
+ JsonNode node = (JsonNode) body;
+ node.fields().forEachRemaining(entry ->
result.put(entry.getKey(), extractJsonNodeValue(entry.getValue())));
+ } else {
+ result.put("body", body);
+ }
+ } catch (DecodeException e) {
+ result.put("body", response.bodyAsString());
+ }
+
+ if (this.returnHeaders) {
+ response.headers().forEach(entry ->
result.put(PrefixParamsDecorator.HEADER_PREFIX + entry.getKey(),
entry.getValue()));
+ }
+ if (this.returnStatusCode) {
+ result.put(STATUS_CODE_PARAM, response.statusCode());
+ }
+
+ return result;
+ }
+
+ private static Object extractJsonNodeValue(JsonNode node) {
+ if (node.isTextual())
+ return node.textValue();
+ if (node.isInt())
+ return node.intValue();
+ if (node.isLong())
+ return node.longValue();
+ if (node.isDouble())
+ return node.doubleValue();
+ if (node.isBoolean())
+ return node.booleanValue();
+ if (node.isNull())
+ return null;
+ if (node.isArray()) {
+ // Wrap the Iterator in a Spliterator and create a Stream
+ return StreamSupport.stream(
+ Spliterators.spliteratorUnknownSize(node.elements(), 0),
+ false)
+
.map(DefaultRestWorkItemHandlerResult::extractJsonNodeValue)
+ .collect(Collectors.toList());
+ }
+ if (node.isObject()) {
+ // Handle objects by recursively processing each field
+ Map<String, Object> result = new HashMap<>();
+ node.fields().forEachRemaining(entry -> result.put(entry.getKey(),
extractJsonNodeValue(entry.getValue())));
+ return result;
+ }
+ return node.toString();
}
Review Comment:
Better to reuse this function
https://github.com/apache/incubator-kie-kogito-runtimes/blob/main/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/JsonObjectUtils.java#L112
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]