fjtirado commented on code in PR #3937:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3937#discussion_r2379192188


##########
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;
+

Review Comment:
   You need to keep the empty parametes constructor for compatibility. Declare 
the three new fields as final and initilizaze them to defaults in the empty 
parameter constructor



##########
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;

Review Comment:
   Here you should use the utility method I mentiones in another comment



##########
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));

Review Comment:
   If renposes is already a map, you just clone it using new HashMap(map)



##########
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);

Review Comment:
   This sohuld be "response" to be aligned with the case is which the returned 
value is not a json



##########
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());

Review Comment:
   This paremeter is not really neeeded
   Actually the status code is handled in a different way, take a look to 
https://github.com/apache/incubator-kie-kogito-runtimes/issues/4013



##########
kogito-workitems/kogito-rest-workitem/src/test/java/org/kogito/workitem/rest/resulthandlers/DefaultRestWorkItemHandlerResultTest.java:
##########


Review Comment:
   Too many test here, but I would like to see a IT test with a worklow 
definition that makes use of this funcionality



-- 
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]

Reply via email to