Skip to content

Commit ee6805e

Browse files
authored
Add RequestDecorator to allow SDK clients to modify the request (#1089)
* Add RequestDecorator to allow SDK clients to modify the request Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Apply @fjtirado suggestion Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> --------- Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 1221b12 commit ee6805e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/HttpExecutor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import jakarta.ws.rs.client.Invocation.Builder;
2525
import jakarta.ws.rs.client.WebTarget;
2626
import java.net.URI;
27+
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Map.Entry;
2930
import java.util.Optional;
31+
import java.util.ServiceLoader;
3032
import java.util.concurrent.CompletableFuture;
3133

3234
public class HttpExecutor implements CallableTask {
@@ -36,6 +38,7 @@ public class HttpExecutor implements CallableTask {
3638
private final Optional<WorkflowValueResolver<Map<String, Object>>> headersMap;
3739
private final Optional<WorkflowValueResolver<Map<String, Object>>> queryMap;
3840
private final RequestExecutor requestFunction;
41+
private final List<RequestDecorator> requestDecorators;
3942

4043
HttpExecutor(
4144
WorkflowValueResolver<URI> uriSupplier,
@@ -48,6 +51,11 @@ public class HttpExecutor implements CallableTask {
4851
this.queryMap = queryMap;
4952
this.requestFunction = requestFunction;
5053
this.pathSupplier = pathSupplier;
54+
this.requestDecorators =
55+
ServiceLoader.load(RequestDecorator.class).stream()
56+
.map(ServiceLoader.Provider::get)
57+
.sorted()
58+
.toList();
5159
}
5260

5361
public CompletableFuture<WorkflowModel> apply(
@@ -67,6 +75,11 @@ public CompletableFuture<WorkflowModel> apply(
6775
target = target.queryParam(entry.getKey(), entry.getValue());
6876
}
6977
Builder request = target.request();
78+
79+
for (RequestDecorator requestDecorator : requestDecorators) {
80+
requestDecorator.decorate(request, workflow, taskContext, input);
81+
}
82+
7083
headersMap.ifPresent(
7184
h -> h.apply(workflow, taskContext, input).forEach((k, v) -> request.header(k, v)));
7285
return CompletableFuture.supplyAsync(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.executors.http;
17+
18+
import io.serverlessworkflow.impl.ServicePriority;
19+
import io.serverlessworkflow.impl.TaskContext;
20+
import io.serverlessworkflow.impl.WorkflowContext;
21+
import io.serverlessworkflow.impl.WorkflowModel;
22+
import jakarta.ws.rs.client.Invocation;
23+
24+
/**
25+
* Interface for decorating HTTP requests with additional modifications.
26+
*
27+
* <p>Implementations should be loaded via ServiceLoader and are sorted by priority in ascending
28+
* order (lower priority numbers executed first). Decorators are applied in sequence, where later
29+
* decorators can override headers set by earlier decorators with the same header name.
30+
*
31+
* @see ServicePriority
32+
*/
33+
public interface RequestDecorator extends ServicePriority {
34+
35+
/**
36+
* Decorate the HTTP request builder with additional headers or modifications.
37+
*
38+
* @param requestBuilder the request builder to decorate
39+
* @param workflowContext the workflow context
40+
* @param taskContext the task context
41+
* @param workflowModel the input data
42+
*/
43+
void decorate(
44+
Invocation.Builder requestBuilder,
45+
WorkflowContext workflowContext,
46+
TaskContext taskContext,
47+
WorkflowModel workflowModel);
48+
}

0 commit comments

Comments
 (0)