Skip to content

Commit 6699e3a

Browse files
committed
post review fix
Signed-off-by: Dmitrii Tikhomirov <chani.liet@gmail.com>
1 parent 27bb3c0 commit 6699e3a

File tree

5 files changed

+101
-94
lines changed

5 files changed

+101
-94
lines changed
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.serverlessworkflow.impl.executors.openapi;
16+
package io.serverlessworkflow.impl.executors.http;
17+
18+
import static io.serverlessworkflow.impl.executors.http.HttpExecutor.client;
1719

1820
import io.serverlessworkflow.impl.TaskContext;
1921
import io.serverlessworkflow.impl.WorkflowContext;
2022
import io.serverlessworkflow.impl.WorkflowModel;
2123
import io.serverlessworkflow.impl.WorkflowValueResolver;
22-
import java.net.URI;
24+
import jakarta.ws.rs.client.WebTarget;
2325

24-
class ExpressionURISupplier implements TargetSupplier {
25-
private WorkflowValueResolver<String> resolver;
26+
public class ExpressionURISupplier implements TargetSupplier {
27+
private WorkflowValueResolver<String> expr;
2628

27-
ExpressionURISupplier(WorkflowValueResolver<String> resolver) {
28-
this.resolver = resolver;
29+
public ExpressionURISupplier(WorkflowValueResolver<String> expr) {
30+
this.expr = expr;
2931
}
3032

3133
@Override
32-
public URI apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) {
33-
return URI.create(resolver.apply(workflow, task, node));
34+
public WebTarget apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) {
35+
return client.target(expr.apply(workflow, task, node));
3436
}
3537
}

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
public class HttpExecutor implements CallableTask<CallHTTP> {
4949

50-
private static final Client client = ClientBuilder.newClient();
50+
public static final Client client = ClientBuilder.newClient();
5151

5252
private TargetSupplier targetSupplier;
5353
private Optional<WorkflowValueResolver<Map<String, Object>>> headersMap;
@@ -56,11 +56,6 @@ public class HttpExecutor implements CallableTask<CallHTTP> {
5656
private RequestSupplier requestFunction;
5757
private HttpModelConverter converter = new HttpModelConverter() {};
5858

59-
@FunctionalInterface
60-
private interface TargetSupplier {
61-
WebTarget apply(WorkflowContext workflow, TaskContext task, WorkflowModel node);
62-
}
63-
6459
@FunctionalInterface
6560
private interface RequestSupplier {
6661
WorkflowModel apply(
@@ -172,7 +167,7 @@ public boolean accept(Class<? extends TaskBase> clazz) {
172167
return clazz.equals(CallHTTP.class);
173168
}
174169

175-
private static TargetSupplier getTargetSupplier(
170+
public static TargetSupplier getTargetSupplier(
176171
Endpoint endpoint, ExpressionFactory expressionFactory) {
177172
if (endpoint.getEndpointConfiguration() != null) {
178173
EndpointUri uri = endpoint.getEndpointConfiguration().getUri();
@@ -193,7 +188,7 @@ private static TargetSupplier getTargetSupplier(
193188
throw new IllegalArgumentException("Invalid endpoint definition " + endpoint);
194189
}
195190

196-
private static TargetSupplier getURISupplier(UriTemplate template) {
191+
public static TargetSupplier getURISupplier(UriTemplate template) {
197192
if (template.getLiteralUri() != null) {
198193
return (w, t, n) -> client.target(template.getLiteralUri());
199194
} else if (template.getLiteralUriTemplate() != null) {
@@ -202,17 +197,4 @@ private static TargetSupplier getURISupplier(UriTemplate template) {
202197
}
203198
throw new IllegalArgumentException("Invalid uritemplate definition " + template);
204199
}
205-
206-
private static class ExpressionURISupplier implements TargetSupplier {
207-
private WorkflowValueResolver<String> expr;
208-
209-
public ExpressionURISupplier(WorkflowValueResolver<String> expr) {
210-
this.expr = expr;
211-
}
212-
213-
@Override
214-
public WebTarget apply(WorkflowContext workflow, TaskContext task, WorkflowModel node) {
215-
return client.target(expr.apply(workflow, task, node));
216-
}
217-
}
218200
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.serverlessworkflow.impl.executors.openapi;
16+
package io.serverlessworkflow.impl.executors.http;
1717

1818
import io.serverlessworkflow.impl.TaskContext;
1919
import io.serverlessworkflow.impl.WorkflowContext;
2020
import io.serverlessworkflow.impl.WorkflowModel;
21-
import java.net.URI;
21+
import jakarta.ws.rs.client.WebTarget;
2222

23-
interface TargetSupplier {
24-
URI apply(WorkflowContext workflow, TaskContext taskContext, WorkflowModel input);
23+
public interface TargetSupplier {
24+
WebTarget apply(WorkflowContext workflow, TaskContext task, WorkflowModel node);
2525
}

impl/openapi/src/main/java/io/serverlessworkflow/impl/executors/openapi/OpenAPIExecutor.java

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
*/
1616
package io.serverlessworkflow.impl.executors.openapi;
1717

18+
import static io.serverlessworkflow.impl.executors.http.HttpExecutor.getTargetSupplier;
19+
1820
import io.serverlessworkflow.api.types.CallHTTP;
1921
import io.serverlessworkflow.api.types.CallOpenAPI;
20-
import io.serverlessworkflow.api.types.Endpoint;
21-
import io.serverlessworkflow.api.types.EndpointUri;
2222
import io.serverlessworkflow.api.types.TaskBase;
23-
import io.serverlessworkflow.api.types.UriTemplate;
2423
import io.serverlessworkflow.api.types.Workflow;
2524
import io.serverlessworkflow.impl.TaskContext;
2625
import io.serverlessworkflow.impl.WorkflowApplication;
@@ -30,11 +29,8 @@
3029
import io.serverlessworkflow.impl.WorkflowModel;
3130
import io.serverlessworkflow.impl.executors.CallableTask;
3231
import io.serverlessworkflow.impl.executors.http.HttpExecutor;
33-
import io.serverlessworkflow.impl.expressions.ExpressionDescriptor;
34-
import io.serverlessworkflow.impl.expressions.ExpressionFactory;
32+
import io.serverlessworkflow.impl.executors.http.TargetSupplier;
3533
import io.serverlessworkflow.impl.resources.ResourceLoader;
36-
import jakarta.ws.rs.core.UriBuilder;
37-
import java.net.URI;
3834
import java.util.concurrent.CompletableFuture;
3935
import java.util.stream.Collectors;
4036

@@ -56,36 +52,17 @@ public boolean accept(Class<? extends TaskBase> clazz) {
5652
@Override
5753
public CompletableFuture<WorkflowModel> apply(
5854
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
59-
String operationId = task.getWith().getOperationId();
60-
URI openAPIEndpoint = targetSupplier.apply(workflowContext, taskContext, input);
61-
OpenAPIProcessor processor = new OpenAPIProcessor(operationId, openAPIEndpoint);
62-
OperationDefinition operation = processor.parse();
6355

64-
OperationPathResolver pathResolver =
65-
new OperationPathResolver(
66-
operation.getPath(),
67-
application,
68-
task.getWith().getParameters().getAdditionalProperties());
56+
OperationDefinitionSupplier operationDefinitionSupplier =
57+
new OperationDefinitionSupplier(application, task);
58+
59+
OperationDefinition operation =
60+
operationDefinitionSupplier.get(workflowContext, taskContext, input);
6961

7062
return CompletableFuture.supplyAsync(
7163
() -> {
7264
HttpCallAdapter httpCallAdapter =
73-
new HttpCallAdapter()
74-
.auth(task.getWith().getAuthentication())
75-
.body(operation.getBody())
76-
.contentType(operation.getContentType())
77-
.headers(
78-
operation.getParameters().stream()
79-
.filter(p -> "header".equals(p.getIn()))
80-
.collect(Collectors.toUnmodifiableSet()))
81-
.method(operation.getMethod())
82-
.query(
83-
operation.getParameters().stream()
84-
.filter(p -> "query".equals(p.getIn()))
85-
.collect(Collectors.toUnmodifiableSet()))
86-
.redirect(task.getWith().isRedirect())
87-
.target(pathResolver.resolve(workflowContext, taskContext, input))
88-
.workflowParams(task.getWith().getParameters().getAdditionalProperties());
65+
getHttpCallAdapter(operation, workflowContext, taskContext, input);
8966

9067
WorkflowException workflowException = null;
9168

@@ -120,35 +97,32 @@ public void init(CallOpenAPI task, WorkflowDefinition definition) {
12097
task.getWith().getDocument().getEndpoint(), application.expressionFactory());
12198
}
12299

123-
private TargetSupplier getTargetSupplier(Endpoint endpoint, ExpressionFactory expressionFactory) {
124-
if (endpoint.getEndpointConfiguration() != null) {
125-
EndpointUri uri = endpoint.getEndpointConfiguration().getUri();
126-
if (uri.getLiteralEndpointURI() != null) {
127-
return getURISupplier(uri.getLiteralEndpointURI());
128-
} else if (uri.getExpressionEndpointURI() != null) {
129-
return new ExpressionURISupplier(
130-
expressionFactory.resolveString(
131-
ExpressionDescriptor.from(uri.getExpressionEndpointURI())));
132-
}
133-
} else if (endpoint.getRuntimeExpression() != null) {
134-
return new ExpressionURISupplier(
135-
expressionFactory.resolveString(
136-
ExpressionDescriptor.from(endpoint.getRuntimeExpression())));
137-
} else if (endpoint.getUriTemplate() != null) {
138-
return getURISupplier(endpoint.getUriTemplate());
139-
}
140-
throw new IllegalArgumentException("Invalid endpoint definition " + endpoint);
141-
}
100+
private HttpCallAdapter getHttpCallAdapter(
101+
OperationDefinition operation,
102+
WorkflowContext workflowContext,
103+
TaskContext taskContext,
104+
WorkflowModel input) {
105+
OperationPathResolver pathResolver =
106+
new OperationPathResolver(
107+
operation.getPath(),
108+
application,
109+
task.getWith().getParameters().getAdditionalProperties());
142110

143-
private TargetSupplier getURISupplier(UriTemplate template) {
144-
if (template.getLiteralUri() != null) {
145-
return (w, t, n) -> template.getLiteralUri();
146-
} else if (template.getLiteralUriTemplate() != null) {
147-
return (w, t, n) ->
148-
UriBuilder.fromUri(template.getLiteralUriTemplate())
149-
.resolveTemplates(n.asMap().orElseThrow(), false)
150-
.build();
151-
}
152-
throw new IllegalArgumentException("Invalid uri template definition " + template);
111+
return new HttpCallAdapter()
112+
.auth(task.getWith().getAuthentication())
113+
.body(operation.getBody())
114+
.contentType(operation.getContentType())
115+
.headers(
116+
operation.getParameters().stream()
117+
.filter(p -> "header".equals(p.getIn()))
118+
.collect(Collectors.toUnmodifiableSet()))
119+
.method(operation.getMethod())
120+
.query(
121+
operation.getParameters().stream()
122+
.filter(p -> "query".equals(p.getIn()))
123+
.collect(Collectors.toUnmodifiableSet()))
124+
.redirect(task.getWith().isRedirect())
125+
.target(pathResolver.resolve(workflowContext, taskContext, input))
126+
.workflowParams(task.getWith().getParameters().getAdditionalProperties());
153127
}
154128
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.openapi;
17+
18+
import static io.serverlessworkflow.impl.executors.http.HttpExecutor.getTargetSupplier;
19+
20+
import io.serverlessworkflow.api.types.CallOpenAPI;
21+
import io.serverlessworkflow.impl.TaskContext;
22+
import io.serverlessworkflow.impl.WorkflowApplication;
23+
import io.serverlessworkflow.impl.WorkflowContext;
24+
import io.serverlessworkflow.impl.WorkflowModel;
25+
import io.serverlessworkflow.impl.executors.http.TargetSupplier;
26+
import jakarta.ws.rs.client.WebTarget;
27+
28+
class OperationDefinitionSupplier {
29+
30+
private final WorkflowApplication application;
31+
private final CallOpenAPI task;
32+
33+
OperationDefinitionSupplier(WorkflowApplication application, CallOpenAPI task) {
34+
this.task = task;
35+
this.application = application;
36+
}
37+
38+
OperationDefinition get(
39+
WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) {
40+
TargetSupplier targetSupplier =
41+
getTargetSupplier(
42+
task.getWith().getDocument().getEndpoint(), application.expressionFactory());
43+
44+
String operationId = task.getWith().getOperationId();
45+
WebTarget webTarget = targetSupplier.apply(workflowContext, taskContext, input);
46+
OpenAPIProcessor processor = new OpenAPIProcessor(operationId, webTarget.getUri());
47+
return processor.parse();
48+
}
49+
}

0 commit comments

Comments
 (0)