Skip to content

Commit aba0408

Browse files
committed
Adding reference implementation
Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent 7038b7d commit aba0408

File tree

14 files changed

+778
-1
lines changed

14 files changed

+778
-1
lines changed

api/src/test/java/io/serverlessworkflow/api/FeaturesTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public class FeaturesTest {
4444
"features/switch.yaml",
4545
"features/try.yaml",
4646
"features/listen.yaml",
47-
"features/callFunction.yaml"
47+
"features/callFunction.yaml",
48+
"features/callCustomFunction.json"
4849
})
4950
public void testSpecFeaturesParsing(String workflowLocation) throws IOException {
5051
Workflow workflow = readWorkflowFromClasspath(workflowLocation);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"document": {
3+
"dsl": "1.0.0-alpha5",
4+
"namespace": "test",
5+
"name": "call-example",
6+
"version": "0.1.0"
7+
},
8+
"schedule": {
9+
"cron": "0 8 * * *"
10+
},
11+
"do": [
12+
{
13+
"getData": {
14+
"call": "http",
15+
"with": {
16+
"method": "get",
17+
"endpoint": "https://api.agify.io?name=meelad"
18+
}
19+
},
20+
"output": {
21+
"as": ".data.reading"
22+
}
23+
},
24+
{
25+
"filterData": {
26+
"for": {
27+
"in": ".data.reading",
28+
"each": "reading"
29+
},
30+
"do": [
31+
{
32+
"log": {
33+
"call": "https://github.com/raw/serverlessworkflow/catalog/main/functions/log/1.0.0/function.yaml",
34+
"with": {
35+
"level": "information",
36+
"format": "{TIMESTAMP} [{LEVEL}] ({CONTEXT}): {MESSAGE}",
37+
"message": "Hello, world!",
38+
"timestamp": true
39+
}
40+
}
41+
}
42+
]
43+
}
44+
}
45+
]
46+
}

impl/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>io.serverlessworkflow</groupId>
5+
<artifactId>serverlessworkflow-parent</artifactId>
6+
<version>7.0.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>serverlessworkflow-impl</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>io.serverlessworkflow</groupId>
12+
<artifactId>serverlessworkflow-api</artifactId>
13+
<version>7.0.0-SNAPSHOT</version>
14+
</dependency>
15+
</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>
51+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import io.serverlessworkflow.api.types.TaskBase;
20+
21+
public abstract class AbstractTaskExecutor<T extends TaskBase> implements TaskExecutor<T> {
22+
23+
protected final T task;
24+
25+
protected AbstractTaskExecutor(T task) {
26+
this.task = task;
27+
}
28+
29+
@Override
30+
public JsonNode apply(JsonNode node) {
31+
32+
// do input filtering
33+
return internalExecute(node);
34+
// do output filtering
35+
36+
}
37+
38+
protected abstract JsonNode internalExecute(JsonNode node);
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import io.serverlessworkflow.api.types.CallHTTP;
20+
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;
28+
import java.util.Map;
29+
30+
public class HttpExecutor extends AbstractTaskExecutor<CallHTTP> {
31+
32+
public HttpExecutor(CallHTTP task) {
33+
super(task);
34+
}
35+
36+
@Override
37+
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);
52+
53+
} catch (MalformedURLException ex) {
54+
throw new IllegalArgumentException(ex);
55+
} catch (IOException ex) {
56+
throw new UncheckedIOException(ex);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)