diff --git a/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDecorator.java b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDecorator.java new file mode 100644 index 00000000..66daf474 --- /dev/null +++ b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDecorator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2020-Present The Serverless Workflow Specification Authors + * + * 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 io.serverlessworkflow.impl; + +/** + * Parent interface for decorating object requests with additional modifications.
+ * It is expected that this class is inherited by specializations that indicate the type to be + * decorated. + * + *

Children interfaces should be loaded via ServiceLoader and are sorted by priority in ascending + * order (lower priority numbers executed first). + * + * @see ServicePriority + */ +public interface WorkflowDecorator extends ServicePriority { + + /** + * Decorates an object with information extracted from the workflow and task context + * + * @param decorated + * @param workflowContext + * @param taskContext + */ + void decorate(T decorated, WorkflowContext workflowContext, TaskContext taskContext); +} diff --git a/impl/core/src/main/java/io/serverlessworkflow/impl/events/EmittedEventDecorator.java b/impl/core/src/main/java/io/serverlessworkflow/impl/events/EmittedEventDecorator.java index c73dce1a..840cf008 100644 --- a/impl/core/src/main/java/io/serverlessworkflow/impl/events/EmittedEventDecorator.java +++ b/impl/core/src/main/java/io/serverlessworkflow/impl/events/EmittedEventDecorator.java @@ -16,21 +16,7 @@ package io.serverlessworkflow.impl.events; import io.cloudevents.core.builder.CloudEventBuilder; -import io.serverlessworkflow.impl.ServicePriority; -import io.serverlessworkflow.impl.TaskContext; -import io.serverlessworkflow.impl.WorkflowContext; +import io.serverlessworkflow.impl.WorkflowDecorator; -/** - * Interface for decorating {@link CloudEventBuilder} objects. - * - *

Implementations should be loaded via ServiceLoader and are sorted by priority in ascending - * order (lower priority numbers executed first). Decorators are applied in sequence, where later - * decorators can override configurations set by earlier decorators. - * - * @see ServicePriority - */ -public interface EmittedEventDecorator extends ServicePriority { - - void decorate( - CloudEventBuilder ceBuilder, WorkflowContext workflowContext, TaskContext taskContext); -} +/** Interface for decorating {@link CloudEventBuilder} objects. */ +public interface EmittedEventDecorator extends WorkflowDecorator {} diff --git a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java index 7c40d5a9..c787cd2b 100644 --- a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java +++ b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java @@ -24,7 +24,7 @@ import jakarta.ws.rs.client.Invocation.Builder; import jakarta.ws.rs.client.WebTarget; import java.net.URI; -import java.util.List; +import java.util.Collection; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; @@ -38,7 +38,7 @@ public class HttpExecutor implements CallableTask { private final Optional>> headersMap; private final Optional>> queryMap; private final RequestExecutor requestFunction; - private final List requestDecorators; + private final Collection requestDecorators; HttpExecutor( WorkflowValueResolver uriSupplier, @@ -52,7 +52,7 @@ public class HttpExecutor implements CallableTask { this.requestFunction = requestFunction; this.pathSupplier = pathSupplier; this.requestDecorators = - ServiceLoader.load(RequestDecorator.class).stream() + ServiceLoader.load(HttpRequestDecorator.class).stream() .map(ServiceLoader.Provider::get) .sorted() .toList(); @@ -75,11 +75,7 @@ public CompletableFuture apply( target = target.queryParam(entry.getKey(), entry.getValue()); } Builder request = target.request(); - - for (RequestDecorator requestDecorator : requestDecorators) { - requestDecorator.decorate(request, workflow, taskContext); - } - + requestDecorators.forEach(d -> d.decorate(request, workflow, taskContext)); headersMap.ifPresent(h -> h.apply(workflow, taskContext, input).forEach(request::header)); return CompletableFuture.supplyAsync( () -> requestFunction.apply(request, uri, workflow, taskContext, input), diff --git a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpRequestDecorator.java b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpRequestDecorator.java new file mode 100644 index 00000000..ffab3723 --- /dev/null +++ b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpRequestDecorator.java @@ -0,0 +1,22 @@ +/* + * Copyright 2020-Present The Serverless Workflow Specification Authors + * + * 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 io.serverlessworkflow.impl.executors.http; + +import io.serverlessworkflow.impl.WorkflowDecorator; +import jakarta.ws.rs.client.Invocation; + +/** Interface for decorating HTTP requests with additional modifications. */ +public interface HttpRequestDecorator extends WorkflowDecorator {} diff --git a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/RequestDecorator.java b/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/RequestDecorator.java deleted file mode 100644 index c770e2cf..00000000 --- a/impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/RequestDecorator.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020-Present The Serverless Workflow Specification Authors - * - * 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 io.serverlessworkflow.impl.executors.http; - -import io.serverlessworkflow.impl.ServicePriority; -import io.serverlessworkflow.impl.TaskContext; -import io.serverlessworkflow.impl.WorkflowContext; -import jakarta.ws.rs.client.Invocation; - -/** - * Interface for decorating HTTP requests with additional modifications. - * - *

Implementations should be loaded via ServiceLoader and are sorted by priority in ascending - * order (lower priority numbers executed first). Decorators are applied in sequence, where later - * decorators can override headers set by earlier decorators with the same header name. - * - * @see ServicePriority - */ -public interface RequestDecorator extends ServicePriority { - - /** - * Decorate the HTTP request builder with additional headers or modifications. - * - * @param requestBuilder the request builder to decorate - * @param workflowContext the workflow context - * @param taskContext the task context - */ - void decorate( - Invocation.Builder requestBuilder, WorkflowContext workflowContext, TaskContext taskContext); -}