Skip to content

Commit 25eb876

Browse files
committed
Using jersey as web client
Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent aba0408 commit 25eb876

File tree

5 files changed

+140
-68
lines changed

5 files changed

+140
-68
lines changed

impl/pom.xml

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,68 @@
66
<version>7.0.0-SNAPSHOT</version>
77
</parent>
88
<artifactId>serverlessworkflow-impl</artifactId>
9+
<properties>
10+
<version.org.glassfish.jersey>3.1.9</version.org.glassfish.jersey>
11+
</properties>
912
<dependencies>
1013
<dependency>
1114
<groupId>io.serverlessworkflow</groupId>
1215
<artifactId>serverlessworkflow-api</artifactId>
1316
<version>7.0.0-SNAPSHOT</version>
1417
</dependency>
18+
<dependency>
19+
<groupId>org.glassfish.jersey.core</groupId>
20+
<artifactId>jersey-client</artifactId>
21+
<version>${version.org.glassfish.jersey}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.glassfish.jersey.media</groupId>
25+
<artifactId>jersey-media-json-jackson</artifactId>
26+
<version>${version.org.glassfish.jersey}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.junit.jupiter</groupId>
30+
<artifactId>junit-jupiter-api</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.junit.jupiter</groupId>
35+
<artifactId>junit-jupiter-engine</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-params</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.assertj</groupId>
45+
<artifactId>assertj-core</artifactId>
46+
<scope>test</scope>
47+
</dependency>
1548
</dependencies>
16-
<build>
17-
<plugins>
18-
<plugin>
19-
<groupId>com.spotify.fmt</groupId>
20-
<artifactId>fmt-maven-plugin</artifactId>
21-
<configuration>
22-
<sourceDirectory>src/main/java</sourceDirectory>
23-
<testSourceDirectory>src/test/java</testSourceDirectory>
24-
<verbose>false</verbose>
25-
<filesNamePattern>.*\.java</filesNamePattern>
26-
<skip>false</skip>
27-
<skipSortingImports>false</skipSortingImports>
28-
<style>google</style>
29-
</configuration>
30-
<executions>
31-
<execution>
32-
<goals>
33-
<goal>format</goal>
34-
</goals>
35-
</execution>
36-
</executions>
37-
</plugin>
38-
<plugin>
39-
<groupId>org.apache.maven.plugins</groupId>
40-
<artifactId>maven-jar-plugin</artifactId>
41-
<executions>
42-
<execution>
43-
<goals>
44-
<goal>test-jar</goal>
45-
</goals>
46-
</execution>
47-
</executions>
48-
</plugin>
49-
</plugins>
50-
</build>
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>com.spotify.fmt</groupId>
53+
<artifactId>fmt-maven-plugin</artifactId>
54+
<configuration>
55+
<sourceDirectory>src/main/java</sourceDirectory>
56+
<testSourceDirectory>src/test/java</testSourceDirectory>
57+
<verbose>false</verbose>
58+
<filesNamePattern>.*\.java</filesNamePattern>
59+
<skip>false</skip>
60+
<skipSortingImports>false</skipSortingImports>
61+
<style>google</style>
62+
</configuration>
63+
<executions>
64+
<execution>
65+
<goals>
66+
<goal>format</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
5173
</project>

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,62 @@
1515
*/
1616
package io.serverlessworkflow.impl;
1717

18+
import com.fasterxml.jackson.core.type.TypeReference;
1819
import com.fasterxml.jackson.databind.JsonNode;
1920
import io.serverlessworkflow.api.types.CallHTTP;
2021
import io.serverlessworkflow.api.types.HTTPArguments;
21-
import java.io.BufferedInputStream;
22-
import java.io.IOException;
23-
import java.io.InputStream;
24-
import java.io.UncheckedIOException;
25-
import java.net.HttpURLConnection;
26-
import java.net.MalformedURLException;
27-
import java.net.URL;
22+
import io.serverlessworkflow.api.types.WithHTTPHeaders;
23+
import io.serverlessworkflow.api.types.WithHTTPQuery;
24+
import jakarta.ws.rs.client.Client;
25+
import jakarta.ws.rs.client.ClientBuilder;
26+
import jakarta.ws.rs.client.Entity;
27+
import jakarta.ws.rs.client.Invocation.Builder;
28+
import jakarta.ws.rs.client.WebTarget;
29+
import jakarta.ws.rs.core.MediaType;
2830
import java.util.Map;
31+
import java.util.Map.Entry;
2932

3033
public class HttpExecutor extends AbstractTaskExecutor<CallHTTP> {
3134

35+
private static final Client client = ClientBuilder.newClient();
36+
3237
public HttpExecutor(CallHTTP task) {
3338
super(task);
3439
}
3540

3641
@Override
3742
protected JsonNode internalExecute(JsonNode node) {
38-
try {
39-
HTTPArguments httpArgs = task.getWith();
40-
// todo think on how to solve this oneOf in an smarter way
41-
// URL url = new URL(((Endpoint) httpArgs.getEndpoint()).getUri().toString());
42-
URL url = new URL(((Map) httpArgs.getEndpoint()).get("uri").toString());
43-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
44-
conn.setRequestMethod(httpArgs.getMethod().toUpperCase());
45-
int responseCode = conn.getResponseCode();
46-
if (responseCode == HttpURLConnection.HTTP_OK) {
47-
try (InputStream in = new BufferedInputStream(conn.getInputStream())) {
48-
return JsonUtils.mapper().readValue(in, JsonNode.class);
49-
}
50-
}
51-
throw new IllegalArgumentException("Respose code is " + responseCode);
5243

53-
} catch (MalformedURLException ex) {
54-
throw new IllegalArgumentException(ex);
55-
} catch (IOException ex) {
56-
throw new UncheckedIOException(ex);
44+
HTTPArguments httpArgs = task.getWith();
45+
String uri =
46+
httpArgs
47+
.getEndpoint()
48+
.getEndpointConfiguration()
49+
.getUri()
50+
.getLiteralEndpointURI()
51+
.getLiteralUriTemplate();
52+
WebTarget target = client.target(uri);
53+
WithHTTPQuery query = httpArgs.getQuery();
54+
if (query != null) {
55+
for (Entry<String, Object> entry : query.getAdditionalProperties().entrySet()) {
56+
target = target.queryParam(entry.getKey(), entry.getValue());
57+
}
58+
}
59+
Builder request =
60+
target
61+
.resolveTemplates(
62+
JsonUtils.mapper().convertValue(node, new TypeReference<Map<String, Object>>() {}))
63+
.request(MediaType.APPLICATION_JSON);
64+
WithHTTPHeaders headers = httpArgs.getHeaders();
65+
if (headers != null) {
66+
headers.getAdditionalProperties().forEach(request::header);
67+
}
68+
switch (httpArgs.getMethod().toLowerCase()) {
69+
case "get":
70+
default:
71+
return request.get(JsonNode.class);
72+
case "post":
73+
return request.post(Entity.json(httpArgs.getBody()), JsonNode.class);
5774
}
5875
}
5976
}

impl/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public State state() {
138138
return state;
139139
}
140140

141-
public Object outputAsJavaObject() {
141+
public Object output() {
142142
return toJavaValue(output);
143143
}
144144

impl/src/main/resources/callHttp.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ do:
99
with:
1010
method: get
1111
endpoint:
12-
uri: https://petstore.swagger.io/v2/pet/10
12+
uri: https://petstore.swagger.io/v2/pet/{petId}
1313
output: response
Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
package io.serverlessworkflow.impl;
22

3+
/*
4+
* Copyright 2020-Present The Serverless Workflow Specification Authors
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
318
import static io.serverlessworkflow.api.WorkflowReader.readWorkflowFromClasspath;
19+
import static org.assertj.core.api.Assertions.assertThat;
420

521
import java.io.IOException;
622
import java.util.Map;
23+
import java.util.stream.Stream;
24+
import org.assertj.core.api.Condition;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
728

829
public class WorkflowDefinitionTest {
930

10-
public static void main(String[] args) throws IOException {
31+
@ParameterizedTest
32+
@MethodSource("provideParameters")
33+
void testWorkflowExecution(String fileName, Object input, Condition<Object> condition)
34+
throws IOException {
35+
assertThat(
36+
WorkflowDefinition.builder(readWorkflowFromClasspath(fileName))
37+
.build()
38+
.execute(input)
39+
.output())
40+
.is(condition);
41+
}
1142

12-
System.out.println(
13-
WorkflowDefinition.builder(readWorkflowFromClasspath("callHttp.yaml"))
14-
.build()
15-
.execute(Map.of("petId", 10))
16-
.outputAsJavaObject());
43+
private static Stream<Arguments> provideParameters() {
44+
return Stream.of(
45+
Arguments.of(
46+
"callHttp.yaml",
47+
Map.of("petId", 10),
48+
new Condition<>(
49+
o -> ((Map<String, Object>) o).containsKey("photoUrls"), "callHttpCondition")));
1750
}
1851
}

0 commit comments

Comments
 (0)