18
18
import com .fasterxml .jackson .core .type .TypeReference ;
19
19
import com .fasterxml .jackson .databind .JsonNode ;
20
20
import io .serverlessworkflow .api .types .CallHTTP ;
21
+ import io .serverlessworkflow .api .types .Endpoint ;
22
+ import io .serverlessworkflow .api .types .EndpointUri ;
21
23
import io .serverlessworkflow .api .types .HTTPArguments ;
24
+ import io .serverlessworkflow .api .types .UriTemplate ;
22
25
import io .serverlessworkflow .api .types .WithHTTPHeaders ;
23
26
import io .serverlessworkflow .api .types .WithHTTPQuery ;
24
27
import jakarta .ws .rs .HttpMethod ;
27
30
import jakarta .ws .rs .client .Entity ;
28
31
import jakarta .ws .rs .client .Invocation .Builder ;
29
32
import jakarta .ws .rs .client .WebTarget ;
33
+ import java .net .URI ;
30
34
import java .util .Map ;
31
35
import java .util .Map .Entry ;
36
+ import java .util .function .Function ;
32
37
33
38
public class HttpExecutor extends AbstractTaskExecutor <CallHTTP > {
34
39
35
40
private static final Client client = ClientBuilder .newClient ();
36
41
37
- public HttpExecutor (CallHTTP task ) {
38
- super (task );
42
+ private final Function <JsonNode , WebTarget > targetSupplier ;
43
+
44
+ public HttpExecutor (CallHTTP task , ExpressionFactory factory ) {
45
+ super (task , factory );
46
+ this .targetSupplier = getTargetSupplier (task .getWith ().getEndpoint ());
39
47
}
40
48
41
49
@ Override
42
50
protected JsonNode internalExecute (JsonNode node ) {
43
51
HTTPArguments httpArgs = task .getWith ();
44
- // missing checks
45
- String uri =
46
- httpArgs
47
- .getEndpoint ()
48
- .getEndpointConfiguration ()
49
- .getUri ()
50
- .getLiteralEndpointURI ()
51
- .getLiteralUriTemplate ();
52
- WebTarget target = client .target (uri );
53
52
WithHTTPQuery query = httpArgs .getQuery ();
53
+ WebTarget target = targetSupplier .apply (node );
54
54
if (query != null ) {
55
55
for (Entry <String , Object > entry : query .getAdditionalProperties ().entrySet ()) {
56
56
target = target .queryParam (entry .getKey (), entry .getValue ());
57
57
}
58
58
}
59
- Builder request =
60
- target
61
- .resolveTemplates (
62
- JsonUtils .mapper ().convertValue (node , new TypeReference <Map <String , Object >>() {}))
63
- .request ();
59
+ Builder request = target .request ();
64
60
WithHTTPHeaders headers = httpArgs .getHeaders ();
65
61
if (headers != null ) {
66
62
headers .getAdditionalProperties ().forEach (request ::header );
@@ -73,4 +69,71 @@ protected JsonNode internalExecute(JsonNode node) {
73
69
return request .post (Entity .json (httpArgs .getBody ()), JsonNode .class );
74
70
}
75
71
}
72
+
73
+ private Function <JsonNode , WebTarget > getTargetSupplier (Endpoint endpoint ) {
74
+ if (endpoint .getEndpointConfiguration () != null ) {
75
+ EndpointUri uri = endpoint .getEndpointConfiguration ().getUri ();
76
+ if (uri .getLiteralEndpointURI () != null ) {
77
+ return getURISupplier (uri .getLiteralEndpointURI ());
78
+ } else if (uri .getExpressionEndpointURI () != null ) {
79
+ return new ExpressionURISupplier (uri .getExpressionEndpointURI ());
80
+ }
81
+ } else if (endpoint .getRuntimeExpression () != null ) {
82
+ return new ExpressionURISupplier (endpoint .getRuntimeExpression ());
83
+ } else if (endpoint .getUriTemplate () != null ) {
84
+ return getURISupplier (endpoint .getUriTemplate ());
85
+ }
86
+ throw new IllegalArgumentException ("Invalid endpoint definition " + endpoint );
87
+ }
88
+
89
+ private Function <JsonNode , WebTarget > getURISupplier (UriTemplate template ) {
90
+ if (template .getLiteralUri () != null ) {
91
+ return new URISupplier (template .getLiteralUri ());
92
+ } else if (template .getLiteralUriTemplate () != null ) {
93
+ return new URITemplateSupplier (template .getLiteralUriTemplate ());
94
+ }
95
+ throw new IllegalArgumentException ("Invalid uritemplate definition " + template );
96
+ }
97
+
98
+ private class URISupplier implements Function <JsonNode , WebTarget > {
99
+ private final URI uri ;
100
+
101
+ public URISupplier (URI uri ) {
102
+ this .uri = uri ;
103
+ }
104
+
105
+ @ Override
106
+ public WebTarget apply (JsonNode input ) {
107
+ return client .target (uri );
108
+ }
109
+ }
110
+
111
+ private class URITemplateSupplier implements Function <JsonNode , WebTarget > {
112
+ private final String uri ;
113
+
114
+ public URITemplateSupplier (String uri ) {
115
+ this .uri = uri ;
116
+ }
117
+
118
+ @ Override
119
+ public WebTarget apply (JsonNode input ) {
120
+ return client
121
+ .target (uri )
122
+ .resolveTemplates (
123
+ JsonUtils .mapper ().convertValue (input , new TypeReference <Map <String , Object >>() {}));
124
+ }
125
+ }
126
+
127
+ private class ExpressionURISupplier implements Function <JsonNode , WebTarget > {
128
+ private Expression expr ;
129
+
130
+ public ExpressionURISupplier (String expr ) {
131
+ this .expr = exprFactory .getExpression (expr );
132
+ }
133
+
134
+ @ Override
135
+ public WebTarget apply (JsonNode input ) {
136
+ return client .target (expr .eval (input ).asText ());
137
+ }
138
+ }
76
139
}
0 commit comments