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 6bc6c447c17..3f449962ce9 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_"; - private static final String QUERY_PREFIX = "QUERY_"; + public 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 9ba69dab93b..0d0600b8567 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 @@ -24,7 +24,6 @@ import org.jbpm.ruleflow.core.factory.NodeFactory; import org.jbpm.ruleflow.core.factory.WorkItemNodeFactory; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.CloudEventKnativeParamsDecorator; -import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.GetRequestKnativeParamsDecorator; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation; import org.kie.kogito.addons.quarkus.knative.serving.customfunctions.PlainJsonKnativeParamsDecorator; @@ -34,6 +33,7 @@ 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,6 +42,7 @@ 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; @@ -63,7 +64,7 @@ public class KnativeTypeHandler extends WorkItemTypeHandler { List payloadFields = new ArrayList<>(); functionRef.getArguments().fieldNames().forEachRemaining(payloadFields::add); if (!payloadFields.isEmpty()) { - node.workParameter(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(";", payloadFields)); + node.workParameter(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, payloadFields)); } } @@ -98,7 +99,7 @@ private static Class getParamsDecorator(Operation ope if (operation.isCloudEvent()) { return CloudEventKnativeParamsDecorator.class; } else if (HttpMethod.GET.equals(operation.getHttpMethod())) { - return GetRequestKnativeParamsDecorator.class; + return PrefixParamsDecorator.class; } else { return PlainJsonKnativeParamsDecorator.class; } diff --git a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecorator.java b/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecorator.java deleted file mode 100644 index 7937e03a03a..00000000000 --- a/quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecorator.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2023 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; - -import java.text.MessageFormat; -import java.util.Map; - -import org.kie.kogito.internal.process.runtime.KogitoWorkItem; -import org.kogito.workitem.rest.decorators.ParamsDecorator; - -import io.vertx.mutiny.ext.web.client.HttpRequest; - -public final class GetRequestKnativeParamsDecorator implements ParamsDecorator { - - @Override - public void decorate(KogitoWorkItem workItem, Map parameters, HttpRequest request) { - KnativeFunctionPayloadSupplier.getPayload(parameters).forEach((key, value) -> { - if (value instanceof String) { - request.addQueryParam(key, (String) value); - } else if (value instanceof Number || value instanceof Boolean) { - request.addQueryParam(key, String.valueOf(value)); - } else { - String message = "Knative functions support only GET requests with String, Number or Boolean attributes. {0} has a {1} value."; - throw new IllegalArgumentException(MessageFormat.format(message, key, value.getClass())); - } - }); - } -} 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 696c2685359..b7b50c41bc4 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,6 +16,7 @@ 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; @@ -25,9 +26,11 @@ 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 { @@ -58,9 +61,22 @@ 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); } diff --git a/quarkus/addons/knative/serving/runtime/src/test/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecoratorTest.java b/quarkus/addons/knative/serving/runtime/src/test/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecoratorTest.java deleted file mode 100644 index 1b3248be032..00000000000 --- a/quarkus/addons/knative/serving/runtime/src/test/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/GetRequestKnativeParamsDecoratorTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2023 Red Hat, Inc. and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.kie.kogito.addons.quarkus.knative.serving.customfunctions; - -import java.util.HashMap; -import java.util.Map; - -import javax.inject.Inject; - -import org.junit.jupiter.api.Test; - -import io.quarkus.test.junit.QuarkusTest; -import io.vertx.core.http.HttpMethod; -import io.vertx.mutiny.core.buffer.Buffer; -import io.vertx.mutiny.ext.web.client.HttpRequest; -import io.vertx.mutiny.ext.web.client.WebClient; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -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; - -@QuarkusTest -class GetRequestKnativeParamsDecoratorTest { - - @Inject - WebClient webClient; - - final GetRequestKnativeParamsDecorator decorator = new GetRequestKnativeParamsDecorator(); - - @Test - void decorate() { - Map expectedParams = Map.of( - "key1", "value1", - "key2", "value2", - "booleanParam", true, - "numberParam", 42); - - HttpRequest request = createRequest(); - - HashMap parameters = new HashMap<>(expectedParams); - parameters.put(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, expectedParams.keySet())); - - decorator.decorate(null, parameters, request); - - assertThat(request.queryParams()).hasSize(4); - expectedParams.forEach((k, v) -> assertThat(request.queryParams().get(k)).isEqualTo(String.valueOf(v))); - } - - @Test - void decorateInvalidTypeShouldThrowException() { - Map expectedParams = Map.of( - "key1", "value1", - "key2", new Object()); - - HttpRequest request = createRequest(); - - HashMap parameters = new HashMap<>(expectedParams); - parameters.put(PAYLOAD_FIELDS_PROPERTY_NAME, String.join(PAYLOAD_FIELDS_DELIMITER, expectedParams.keySet())); - - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> decorator.decorate(null, parameters, request)); - } - - private HttpRequest createRequest() { - return webClient.request(HttpMethod.GET, 8080, "localhost", "/path"); - } -}