26
26
import io .serverlessworkflow .impl .WorkflowApplication ;
27
27
import io .serverlessworkflow .impl .WorkflowContext ;
28
28
import io .serverlessworkflow .impl .WorkflowDefinition ;
29
+ import io .serverlessworkflow .impl .WorkflowException ;
29
30
import io .serverlessworkflow .impl .WorkflowModel ;
30
- import io .serverlessworkflow .impl .WorkflowValueResolver ;
31
31
import io .serverlessworkflow .impl .executors .CallableTask ;
32
32
import io .serverlessworkflow .impl .executors .http .HttpExecutor ;
33
33
import io .serverlessworkflow .impl .expressions .ExpressionDescriptor ;
34
34
import io .serverlessworkflow .impl .expressions .ExpressionFactory ;
35
35
import io .serverlessworkflow .impl .resources .ResourceLoader ;
36
36
import jakarta .ws .rs .core .UriBuilder ;
37
37
import java .net .URI ;
38
- import java .util .Objects ;
39
38
import java .util .concurrent .CompletableFuture ;
40
39
import java .util .stream .Collectors ;
41
40
@@ -49,39 +48,6 @@ public class OpenAPIExecutor implements CallableTask<CallOpenAPI> {
49
48
50
49
private ResourceLoader resourceLoader ;
51
50
52
- private static TargetSupplier getTargetSupplier (
53
- Endpoint endpoint , ExpressionFactory expressionFactory ) {
54
- if (endpoint .getEndpointConfiguration () != null ) {
55
- EndpointUri uri = endpoint .getEndpointConfiguration ().getUri ();
56
- if (uri .getLiteralEndpointURI () != null ) {
57
- return getURISupplier (uri .getLiteralEndpointURI ());
58
- } else if (uri .getExpressionEndpointURI () != null ) {
59
- return new ExpressionURISupplier (
60
- expressionFactory .resolveString (
61
- ExpressionDescriptor .from (uri .getExpressionEndpointURI ())));
62
- }
63
- } else if (endpoint .getRuntimeExpression () != null ) {
64
- return new ExpressionURISupplier (
65
- expressionFactory .resolveString (
66
- ExpressionDescriptor .from (endpoint .getRuntimeExpression ())));
67
- } else if (endpoint .getUriTemplate () != null ) {
68
- return getURISupplier (endpoint .getUriTemplate ());
69
- }
70
- throw new IllegalArgumentException ("Invalid endpoint definition " + endpoint );
71
- }
72
-
73
- private static TargetSupplier getURISupplier (UriTemplate template ) {
74
- if (template .getLiteralUri () != null ) {
75
- return (w , t , n ) -> template .getLiteralUri ();
76
- } else if (template .getLiteralUriTemplate () != null ) {
77
- return (w , t , n ) ->
78
- UriBuilder .fromUri (template .getLiteralUriTemplate ())
79
- .resolveTemplates (n .asMap ().orElseThrow (), false )
80
- .build ();
81
- }
82
- throw new IllegalArgumentException ("Invalid uritemplate definition " + template );
83
- }
84
-
85
51
@ Override
86
52
public boolean accept (Class <? extends TaskBase > clazz ) {
87
53
return clazz .equals (CallOpenAPI .class );
@@ -90,52 +56,53 @@ public boolean accept(Class<? extends TaskBase> clazz) {
90
56
@ Override
91
57
public CompletableFuture <WorkflowModel > apply (
92
58
WorkflowContext workflowContext , TaskContext taskContext , WorkflowModel input ) {
93
-
94
59
String operationId = task .getWith ().getOperationId ();
95
60
URI openAPIEndpoint = targetSupplier .apply (workflowContext , taskContext , input );
96
61
OpenAPIProcessor processor = new OpenAPIProcessor (operationId , openAPIEndpoint );
97
62
OperationDefinition operation = processor .parse ();
98
63
99
64
OperationPathResolver pathResolver =
100
- new OperationPathResolver (operation .getPath (), input .asMap ().orElseThrow ());
101
- URI resolvedPath = pathResolver .passPathParams ().apply (workflowContext , taskContext , input );
102
-
103
- HttpCallAdapter httpCallAdapter =
104
- new HttpCallAdapter ()
105
- .auth (task .getWith ().getAuthentication ())
106
- .body (operation .getBody ())
107
- .contentType (operation .getContentType ())
108
- .headers (
109
- operation .getParameters ().stream ()
110
- .filter (p -> "header" .equals (p .getIn ()))
111
- .collect (Collectors .toUnmodifiableSet ()))
112
- .method (operation .getMethod ())
113
- .query (
114
- operation .getParameters ().stream ()
115
- .filter (p -> "query" .equals (p .getIn ()))
116
- .collect (Collectors .toUnmodifiableSet ()))
117
- .redirect (task .getWith ().isRedirect ())
118
- .target (resolvedPath )
119
- .workflowParams (task .getWith ().getParameters ().getAdditionalProperties ());
65
+ new OperationPathResolver (
66
+ operation .getPath (),
67
+ application ,
68
+ task .getWith ().getParameters ().getAdditionalProperties ());
120
69
121
70
return CompletableFuture .supplyAsync (
122
71
() -> {
123
- RuntimeException ex = null ;
72
+ 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 ());
89
+
90
+ WorkflowException workflowException = null ;
91
+
124
92
for (var server : operation .getServers ()) {
125
93
CallHTTP callHTTP = httpCallAdapter .server (server ).build ();
126
94
HttpExecutor executor = new HttpExecutor ();
127
95
executor .init (callHTTP , definition );
128
96
129
97
try {
130
98
return executor .apply (workflowContext , taskContext , input ).get ();
99
+ } catch (WorkflowException e ) {
100
+ workflowException = e ;
131
101
} catch (Exception e ) {
132
-
133
- System .out .println ("Call to " + server + " failed: " + e .getMessage ());
134
- ex = new RuntimeException (e );
102
+ throw new RuntimeException (e );
135
103
}
136
104
}
137
- Objects .requireNonNull (ex , "Should have at least one exception" );
138
- throw ex ; // if we there, we failed all servers and ex is not null
105
+ throw workflowException ; // if we there, we failed all servers and ex is not null
139
106
},
140
107
workflowContext .definition ().application ().executorService ());
141
108
}
@@ -153,20 +120,35 @@ public void init(CallOpenAPI task, WorkflowDefinition definition) {
153
120
task .getWith ().getDocument ().getEndpoint (), application .expressionFactory ());
154
121
}
155
122
156
- public interface TargetSupplier {
157
- URI apply (WorkflowContext workflow , TaskContext taskContext , WorkflowModel input );
158
- }
159
-
160
- private static class ExpressionURISupplier implements TargetSupplier {
161
- private WorkflowValueResolver <String > expr ;
162
-
163
- public ExpressionURISupplier (WorkflowValueResolver <String > expr ) {
164
- this .expr = expr ;
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 ());
165
139
}
140
+ throw new IllegalArgumentException ("Invalid endpoint definition " + endpoint );
141
+ }
166
142
167
- @ Override
168
- public URI apply (WorkflowContext workflow , TaskContext task , WorkflowModel node ) {
169
- return URI .create (expr .apply (workflow , task , node ));
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 ();
170
151
}
152
+ throw new IllegalArgumentException ("Invalid uri template definition " + template );
171
153
}
172
154
}
0 commit comments