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