From 24e5d9d7c197e0d595c618674deb1a109549a749 Mon Sep 17 00:00:00 2001 From: Helber Belmiro Date: Tue, 22 Aug 2023 11:41:52 -0300 Subject: [PATCH] KOGITO-8410 Replaced PrefixParamsDecorator with CollectionParamsDecorator Signed-off-by: Helber Belmiro --- .../CollectionParamsDecoratorSupplier.java | 0 .../decorators/PrefixParamsDecorator.java | 2 +- .../customfunctions/KnativeTypeHandler.java | 54 ++++++++++--------- .../KnativeFunctionPayloadSupplier.java | 12 ++--- .../KnativeWorkItemHandler.java | 18 ------- 5 files changed, 35 insertions(+), 51 deletions(-) rename kogito-serverless-workflow/{kogito-serverless-workflow-openapi-parser => kogito-serverless-workflow-rest-parser}/src/main/java/org/kie/kogito/serverless/workflow/suppliers/CollectionParamsDecoratorSupplier.java (100%) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-openapi-parser/src/main/java/org/kie/kogito/serverless/workflow/suppliers/CollectionParamsDecoratorSupplier.java b/kogito-serverless-workflow/kogito-serverless-workflow-rest-parser/src/main/java/org/kie/kogito/serverless/workflow/suppliers/CollectionParamsDecoratorSupplier.java similarity index 100% rename from kogito-serverless-workflow/kogito-serverless-workflow-openapi-parser/src/main/java/org/kie/kogito/serverless/workflow/suppliers/CollectionParamsDecoratorSupplier.java rename to kogito-serverless-workflow/kogito-serverless-workflow-rest-parser/src/main/java/org/kie/kogito/serverless/workflow/suppliers/CollectionParamsDecoratorSupplier.java diff --git a/kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/decorators/PrefixParamsDecorator.java b/kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/decorators/PrefixParamsDecorator.java index 3f449962ce9..6bc6c447c17 100644 --- a/kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/decorators/PrefixParamsDecorator.java +++ b/kogito-workitems/kogito-rest-workitem/src/main/java/org/kogito/workitem/rest/decorators/PrefixParamsDecorator.java @@ -18,7 +18,7 @@ public class PrefixParamsDecorator extends AbstractParamsDecorator { private static final String HEADER_PREFIX = "HEADER_"; - public static final String QUERY_PREFIX = "QUERY_"; + private static final String QUERY_PREFIX = "QUERY_"; @Override protected boolean isHeaderParameter(String key) { diff --git a/quarkus/addons/knative/serving/deployment/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/deployment/customfunctions/KnativeTypeHandler.java b/quarkus/addons/knative/serving/deployment/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/deployment/customfunctions/KnativeTypeHandler.java index 0d0600b8567..b46269a3871 100644 --- a/quarkus/addons/knative/serving/deployment/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/deployment/customfunctions/KnativeTypeHandler.java +++ b/quarkus/addons/knative/serving/deployment/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/deployment/customfunctions/KnativeTypeHandler.java @@ -23,6 +23,7 @@ import org.jbpm.ruleflow.core.RuleFlowNodeContainerFactory; import org.jbpm.ruleflow.core.factory.NodeFactory; import org.jbpm.ruleflow.core.factory.WorkItemNodeFactory; +import org.jetbrains.annotations.NotNull; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.CloudEventKnativeParamsDecorator; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation; @@ -30,10 +31,9 @@ import org.kie.kogito.serverless.workflow.parser.ParserContext; import org.kie.kogito.serverless.workflow.parser.VariableInfo; import org.kie.kogito.serverless.workflow.parser.types.WorkItemTypeHandler; +import org.kie.kogito.serverless.workflow.suppliers.CollectionParamsDecoratorSupplier; import org.kie.kogito.serverless.workflow.suppliers.ParamsRestBodyBuilderSupplier; import org.kogito.workitem.rest.RestWorkItemHandler; -import org.kogito.workitem.rest.decorators.ParamsDecorator; -import org.kogito.workitem.rest.decorators.PrefixParamsDecorator; import com.github.javaparser.ast.expr.Expression; @@ -42,7 +42,6 @@ import io.serverlessworkflow.api.functions.FunctionRef; import io.vertx.core.http.HttpMethod; -import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_DELIMITER; import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_PROPERTY_NAME; import static org.kie.kogito.serverless.workflow.parser.FunctionTypeHandlerFactory.trimCustomOperation; import static org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils.runtimeRestApi; @@ -60,19 +59,42 @@ public class KnativeTypeHandler extends WorkItemTypeHandler { WorkItemNodeFactory node = buildWorkItem(embeddedSubProcess, context, varInfo.getInputVar(), varInfo.getOutputVar()) .name(functionDef.getName()); - if (functionRef.getArguments() != null && !functionRef.getArguments().isEmpty()) { - List payloadFields = new ArrayList<>(); - functionRef.getArguments().fieldNames().forEachRemaining(payloadFields::add); + List payloadFields = getPayloadFields(functionRef); + + Operation operation = Operation.parse(trimCustomOperation(functionDef)); + + if (HttpMethod.GET.equals(operation.getHttpMethod())) { + node.workParameter(RestWorkItemHandler.PARAMS_DECORATOR, new CollectionParamsDecoratorSupplier(List.of(), payloadFields)); + } else { if (!payloadFields.isEmpty()) { - node.workParameter(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, payloadFields)); + node.workParameter(PAYLOAD_FIELDS_PROPERTY_NAME, payloadFields); + } + if (operation.isCloudEvent()) { + node.workParameter(RestWorkItemHandler.PARAMS_DECORATOR, CloudEventKnativeParamsDecorator.class.getName()); + } else { + node.workParameter(RestWorkItemHandler.PARAMS_DECORATOR, PlainJsonKnativeParamsDecorator.class.getName()); } } + node.workParameter(KnativeWorkItemHandler.SERVICE_PROPERTY_NAME, operation.getService()) + .workParameter(KnativeWorkItemHandler.PATH_PROPERTY_NAME, operation.getPath()) + .workParameter(RestWorkItemHandler.METHOD, operation.getHttpMethod()); + return addFunctionArgs(workflow, fillWorkItemHandler(workflow, context, node, functionDef), functionRef); } + @NotNull + private static List getPayloadFields(FunctionRef functionRef) { + List payloadFields = new ArrayList<>(); + + if (functionRef.getArguments() != null && !functionRef.getArguments().isEmpty()) { + functionRef.getArguments().fieldNames().forEachRemaining(payloadFields::add); + } + return payloadFields; + } + @Override protected > WorkItemNodeFactory fillWorkItemHandler( Workflow workflow, ParserContext context, WorkItemNodeFactory node, FunctionDefinition functionDef) { @@ -80,31 +102,15 @@ public class KnativeTypeHandler extends WorkItemTypeHandler { functionDef.getMetadata().forEach(node::metaData); } - Operation operation = Operation.parse(trimCustomOperation(functionDef)); - Supplier requestTimeout = runtimeRestApi(functionDef, "timeout", context.getContext(), String.class, DEFAULT_REQUEST_TIMEOUT_VALUE); - return node.workParameter(KnativeWorkItemHandler.SERVICE_PROPERTY_NAME, operation.getService()) - .workParameter(KnativeWorkItemHandler.PATH_PROPERTY_NAME, operation.getPath()) - .workParameter(RestWorkItemHandler.BODY_BUILDER, new ParamsRestBodyBuilderSupplier()) - .workParameter(RestWorkItemHandler.PARAMS_DECORATOR, getParamsDecorator(operation).getName()) - .workParameter(RestWorkItemHandler.METHOD, operation.getHttpMethod()) + return node.workParameter(RestWorkItemHandler.BODY_BUILDER, new ParamsRestBodyBuilderSupplier()) .workParameter(RestWorkItemHandler.REQUEST_TIMEOUT_IN_MILLIS, requestTimeout) .metaData(TaskDescriptor.KEY_WORKITEM_TYPE, RestWorkItemHandler.REST_TASK_TYPE) .workName(KnativeWorkItemHandler.NAME); } - private static Class getParamsDecorator(Operation operation) { - if (operation.isCloudEvent()) { - return CloudEventKnativeParamsDecorator.class; - } else if (HttpMethod.GET.equals(operation.getHttpMethod())) { - return PrefixParamsDecorator.class; - } else { - return PlainJsonKnativeParamsDecorator.class; - } - } - @Override public String type() { return KnativeWorkItemHandler.NAME; diff --git a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeFunctionPayloadSupplier.java b/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeFunctionPayloadSupplier.java index a5f2bd5fa9a..bad93914de8 100644 --- a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeFunctionPayloadSupplier.java +++ b/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeFunctionPayloadSupplier.java @@ -15,13 +15,12 @@ */ package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; -import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import static java.util.stream.Collectors.toMap; -import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_DELIMITER; import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.PAYLOAD_FIELDS_PROPERTY_NAME; final class KnativeFunctionPayloadSupplier { @@ -35,11 +34,8 @@ static Map getPayload(Map parameters) { } private static List getPayloadFields(Map parameters) { - String payloadFields = (String) parameters.remove(PAYLOAD_FIELDS_PROPERTY_NAME); - if (payloadFields != null) { - return Arrays.asList(payloadFields.split(PAYLOAD_FIELDS_DELIMITER)); - } else { - return List.of(); - } + @SuppressWarnings("unchecked") + List payloadFields = (List) parameters.remove(PAYLOAD_FIELDS_PROPERTY_NAME); + return Objects.requireNonNullElseGet(payloadFields, List::of); } } diff --git a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeWorkItemHandler.java b/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeWorkItemHandler.java index b7b50c41bc4..8ae77622bf8 100644 --- a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeWorkItemHandler.java +++ b/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/KnativeWorkItemHandler.java @@ -16,7 +16,6 @@ package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; import java.net.URI; -import java.util.Arrays; import java.util.Map; import org.kie.kogito.addons.k8s.resource.catalog.KubernetesServiceCatalog; @@ -26,11 +25,9 @@ import org.kie.kogito.process.workitem.WorkItemExecutionException; import org.kogito.workitem.rest.RestWorkItemHandler; -import io.vertx.core.http.HttpMethod; import io.vertx.mutiny.ext.web.client.WebClient; import static org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol.KNATIVE; -import static org.kogito.workitem.rest.decorators.PrefixParamsDecorator.QUERY_PREFIX; public final class KnativeWorkItemHandler extends RestWorkItemHandler { @@ -46,8 +43,6 @@ public final class KnativeWorkItemHandler extends RestWorkItemHandler { public static final String PAYLOAD_FIELDS_PROPERTY_NAME = "knative_function_payload_fields"; - public static final String PAYLOAD_FIELDS_DELIMITER = ";"; - public static final String CLOUDEVENT_SENT_AS_PLAIN_JSON_ERROR_MESSAGE = "A Knative custom function argument cannot be a CloudEvent when the 'asCloudEvent' property are not set to 'true'"; private final KubernetesServiceCatalog kubernetesServiceCatalog; @@ -61,22 +56,9 @@ public KnativeWorkItemHandler(WebClient httpClient, WebClient httpsClient, Kuber public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manager) { Map parameters = workItem.getParameters(); parameters.put(RestWorkItemHandler.URL, getUrl(parameters)); - - if (HttpMethod.GET.name().equals(workItem.getParameters().get(RestWorkItemHandler.METHOD))) { - addQueryParamsPrefix(workItem); - } - super.executeWorkItem(workItem, manager); } - private static void addQueryParamsPrefix(KogitoWorkItem workItem) { - String payloadFields = workItem.getParameters().remove(PAYLOAD_FIELDS_PROPERTY_NAME).toString(); - if (payloadFields != null) { - Arrays.stream(payloadFields.split(PAYLOAD_FIELDS_DELIMITER)) - .forEach(field -> workItem.getParameters().put(QUERY_PREFIX + field, workItem.getParameters().remove(field))); - } - } - private String getUrl(Map parameters) { return getServiceAddress(parameters) + parameters.remove(PATH_PROPERTY_NAME); }