Skip to content

Commit 968e6b9

Browse files
committed
Moving buildTargetUrl to WorkflowUtils
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent 65333de commit 968e6b9

File tree

5 files changed

+54
-55
lines changed

5 files changed

+54
-55
lines changed

impl/core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@
2828
<groupId>com.cronutils</groupId>
2929
<artifactId>cron-utils</artifactId>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-engine</artifactId>
34+
<scope>test</scope>
35+
</dependency>
3136
</dependencies>
3237
</project>

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowUtils.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,42 @@ public static final String checkSecret(
221221
.orElseThrow(() -> new IllegalStateException("Secret " + secretName + " does not exist"));
222222
}
223223

224-
public static URI concatURI(URI uri, String pathToAppend) {
225-
return uri.getPath().endsWith("/")
226-
? uri.resolve(pathToAppend)
227-
: URI.create(
228-
uri.toString() + (pathToAppend.startsWith("/") ? pathToAppend : "/" + pathToAppend));
224+
public static URI concatURI(URI base, String pathToAppend) {
225+
if (pathToAppend == null || pathToAppend.isEmpty()) {
226+
return base;
227+
}
228+
229+
final URI child = URI.create(pathToAppend);
230+
if (child.isAbsolute()) {
231+
return child;
232+
}
233+
234+
String basePath = base.getPath();
235+
if (basePath == null || basePath.isEmpty()) {
236+
basePath = "/";
237+
} else if (!basePath.endsWith("/")) {
238+
basePath = basePath + "/";
239+
}
240+
241+
String relPath = child.getPath();
242+
if (relPath == null) {
243+
relPath = "";
244+
}
245+
while (relPath.startsWith("/")) {
246+
relPath = relPath.substring(1);
247+
}
248+
249+
String finalPath = basePath + relPath;
250+
251+
String query = child.getQuery();
252+
String fragment = child.getFragment();
253+
254+
try {
255+
return new URI(base.getScheme(), base.getAuthority(), finalPath, query, fragment);
256+
} catch (Exception e) {
257+
throw new IllegalArgumentException(
258+
"Failed to build combined URI from base=" + base + " and path=" + pathToAppend, e);
259+
}
229260
}
230261

231262
public static WorkflowValueResolver<URI> getURISupplier(
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.serverlessworkflow.impl.executors.http;
16+
package io.serverlessworkflow.impl;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919

2020
import java.net.URI;
2121
import org.junit.jupiter.api.Test;
2222

23-
class HttpExecutorTargetSupplierTest {
24-
23+
public class WorkflowUtilsTest {
2524
@Test
2625
void openApiServerWithTrailingSlashAndRootPath() {
2726
URI base = URI.create("https://petstore3.swagger.io/api/v3/");
2827
URI path = URI.create("/pet/findByStatus");
2928

30-
URI result = HttpExecutor.buildTargetUri(base, path);
29+
URI result = WorkflowUtils.concatURI(base, path.toString());
3130

3231
assertEquals("https://petstore3.swagger.io/api/v3/pet/findByStatus", result.toString());
3332
}
@@ -37,7 +36,7 @@ void openApiServerWithoutTrailingSlashAndRootPath() {
3736
URI base = URI.create("https://petstore3.swagger.io/api/v3");
3837
URI path = URI.create("/pet/findByStatus");
3938

40-
URI result = HttpExecutor.buildTargetUri(base, path);
39+
URI result = WorkflowUtils.concatURI(base, path.toString());
4140

4241
assertEquals("https://petstore3.swagger.io/api/v3/pet/findByStatus", result.toString());
4342
}
@@ -47,7 +46,7 @@ void baseWithSlashAndRelativePath() {
4746
URI base = URI.create("https://example.com/api/v1/");
4847
URI path = URI.create("pets");
4948

50-
URI result = HttpExecutor.buildTargetUri(base, path);
49+
URI result = WorkflowUtils.concatURI(base, path.toString());
5150

5251
assertEquals("https://example.com/api/v1/pets", result.toString());
5352
}
@@ -57,7 +56,7 @@ void baseWithoutPathAndRootPath() {
5756
URI base = URI.create("https://example.com");
5857
URI path = URI.create("/pets");
5958

60-
URI result = HttpExecutor.buildTargetUri(base, path);
59+
URI result = WorkflowUtils.concatURI(base, path.toString());
6160

6261
assertEquals("https://example.com/pets", result.toString());
6362
}
@@ -67,7 +66,7 @@ void absolutePathOverridesBase() {
6766
URI base = URI.create("https://example.com/api");
6867
URI path = URI.create("https://other.example.com/foo");
6968

70-
URI result = HttpExecutor.buildTargetUri(base, path);
69+
URI result = WorkflowUtils.concatURI(base, path.toString());
7170

7271
assertEquals("https://other.example.com/foo", result.toString());
7372
}
@@ -77,7 +76,7 @@ void queryAndFragmentAreTakenFromPath() {
7776
URI base = URI.create("https://example.com/api/v1");
7877
URI path = URI.create("/pets?status=available#top");
7978

80-
URI result = HttpExecutor.buildTargetUri(base, path);
79+
URI result = WorkflowUtils.concatURI(base, path.toString());
8180

8281
assertEquals("https://example.com/api/v1/pets?status=available#top", result.toString());
8382
}

impl/http/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,5 @@
1313
<groupId>io.serverlessworkflow</groupId>
1414
<artifactId>serverlessworkflow-impl-template-resolver</artifactId>
1515
</dependency>
16-
<dependency>
17-
<groupId>org.junit.jupiter</groupId>
18-
<artifactId>junit-jupiter-engine</artifactId>
19-
<scope>test</scope>
20-
</dependency>
2116
</dependencies>
2217
</project>

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

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -254,41 +254,10 @@ private static WorkflowValueResolver<WebTarget> getTargetSupplier(
254254

255255
private static WorkflowValueResolver<WebTarget> getTargetSupplier(
256256
WorkflowValueResolver<URI> uriSupplier, WorkflowValueResolver<URI> pathSupplier) {
257-
return (w, t, n) -> {
258-
URI base = uriSupplier.apply(w, t, n);
259-
URI path = pathSupplier.apply(w, t, n);
260-
URI combined = buildTargetUri(base, path);
261-
return HttpClientResolver.client(w, t).target(combined);
262-
};
263-
}
264-
265-
static URI buildTargetUri(URI base, URI path) {
266-
if (path.isAbsolute()) {
267-
return path;
268-
}
269-
270-
String basePath = base.getPath();
271-
if (basePath == null || basePath.isEmpty()) {
272-
basePath = "/";
273-
} else if (!basePath.endsWith("/")) {
274-
basePath = basePath + "/";
275-
}
276-
277-
String relPath = path.getPath() == null ? "" : path.getPath();
278-
while (relPath.startsWith("/")) {
279-
relPath = relPath.substring(1);
280-
}
281-
282-
try {
283-
return new URI(
284-
base.getScheme(),
285-
base.getAuthority(),
286-
basePath + relPath,
287-
path.getQuery(),
288-
path.getFragment());
289-
} catch (Exception e) {
290-
throw new IllegalArgumentException(
291-
"Failed to build combined URI from base=" + base + " and path=" + path, e);
292-
}
257+
return (w, t, n) ->
258+
HttpClientResolver.client(w, t)
259+
.target(
260+
WorkflowUtils.concatURI(
261+
uriSupplier.apply(w, t, n), pathSupplier.apply(w, t, n).toString()));
293262
}
294263
}

0 commit comments

Comments
 (0)