Skip to content

Commit 85db911

Browse files
authored
Decorator refactor (#1105)
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 194fb1d commit 85db911

5 files changed

Lines changed: 67 additions & 68 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
17+
18+
/**
19+
* Parent interface for decorating object requests with additional modifications.<br>
20+
* It is expected that this class is inherited by specializations that indicate the type to be
21+
* decorated.
22+
*
23+
* <p>Children interfaces should be loaded via ServiceLoader and are sorted by priority in ascending
24+
* order (lower priority numbers executed first).
25+
*
26+
* @see ServicePriority
27+
*/
28+
public interface WorkflowDecorator<T> extends ServicePriority {
29+
30+
/**
31+
* Decorates an object with information extracted from the workflow and task context
32+
*
33+
* @param decorated
34+
* @param workflowContext
35+
* @param taskContext
36+
*/
37+
void decorate(T decorated, WorkflowContext workflowContext, TaskContext taskContext);
38+
}

‎impl/core/src/main/java/io/serverlessworkflow/impl/events/EmittedEventDecorator.java‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,7 @@
1616
package io.serverlessworkflow.impl.events;
1717

1818
import io.cloudevents.core.builder.CloudEventBuilder;
19-
import io.serverlessworkflow.impl.ServicePriority;
20-
import io.serverlessworkflow.impl.TaskContext;
21-
import io.serverlessworkflow.impl.WorkflowContext;
19+
import io.serverlessworkflow.impl.WorkflowDecorator;
2220

23-
/**
24-
* Interface for decorating {@link CloudEventBuilder} objects.
25-
*
26-
* <p>Implementations should be loaded via ServiceLoader and are sorted by priority in ascending
27-
* order (lower priority numbers executed first). Decorators are applied in sequence, where later
28-
* decorators can override configurations set by earlier decorators.
29-
*
30-
* @see ServicePriority
31-
*/
32-
public interface EmittedEventDecorator extends ServicePriority {
33-
34-
void decorate(
35-
CloudEventBuilder ceBuilder, WorkflowContext workflowContext, TaskContext taskContext);
36-
}
21+
/** Interface for decorating {@link CloudEventBuilder} objects. */
22+
public interface EmittedEventDecorator extends WorkflowDecorator<CloudEventBuilder> {}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
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;
27+
import java.util.Collection;
2828
import java.util.Map;
2929
import java.util.Map.Entry;
3030
import java.util.Optional;
@@ -38,7 +38,7 @@ public class HttpExecutor implements CallableTask {
3838
private final Optional<WorkflowValueResolver<Map<String, Object>>> headersMap;
3939
private final Optional<WorkflowValueResolver<Map<String, Object>>> queryMap;
4040
private final RequestExecutor requestFunction;
41-
private final List<RequestDecorator> requestDecorators;
41+
private final Collection<HttpRequestDecorator> requestDecorators;
4242

4343
HttpExecutor(
4444
WorkflowValueResolver<URI> uriSupplier,
@@ -52,7 +52,7 @@ public class HttpExecutor implements CallableTask {
5252
this.requestFunction = requestFunction;
5353
this.pathSupplier = pathSupplier;
5454
this.requestDecorators =
55-
ServiceLoader.load(RequestDecorator.class).stream()
55+
ServiceLoader.load(HttpRequestDecorator.class).stream()
5656
.map(ServiceLoader.Provider::get)
5757
.sorted()
5858
.toList();
@@ -75,11 +75,7 @@ public CompletableFuture<WorkflowModel> apply(
7575
target = target.queryParam(entry.getKey(), entry.getValue());
7676
}
7777
Builder request = target.request();
78-
79-
for (RequestDecorator requestDecorator : requestDecorators) {
80-
requestDecorator.decorate(request, workflow, taskContext);
81-
}
82-
78+
requestDecorators.forEach(d -> d.decorate(request, workflow, taskContext));
8379
headersMap.ifPresent(h -> h.apply(workflow, taskContext, input).forEach(request::header));
8480
return CompletableFuture.supplyAsync(
8581
() -> requestFunction.apply(request, uri, workflow, taskContext, input),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.WorkflowDecorator;
19+
import jakarta.ws.rs.client.Invocation;
20+
21+
/** Interface for decorating HTTP requests with additional modifications. */
22+
public interface HttpRequestDecorator extends WorkflowDecorator<Invocation.Builder> {}

‎impl/http/src/main/java/io/serverlessworkflow/impl/executors/http/RequestDecorator.java‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)