From 3587c9efd545f5fd8ddfd4e79bd8b2d7fcae2f29 Mon Sep 17 00:00:00 2001 From: Javier Date: Wed, 3 Jul 2024 18:25:34 +0200 Subject: [PATCH] [Fix #3569] Refining for windows --- .../workflow/io/FileContentLoader.java | 19 +++++++++++- .../workflow/io/URIContentLoaderFactory.java | 10 ++----- .../workflow/io/URIContentLoaderType.java | 29 +++++++------------ .../workflow/io/URIContentLoaderTest.java | 17 ++++++----- .../resources/{pepe.txt => pepe a pepa.txt} | 0 .../rpc/WorkflowRPCCodeGenProvider.java | 3 +- .../workflow/io/QuarkusResourceCache.java | 7 ++--- 7 files changed, 46 insertions(+), 39 deletions(-) rename kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/resources/{pepe.txt => pepe a pepa.txt} (100%) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/FileContentLoader.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/FileContentLoader.java index ef86e1d2aeb..aabd9b1258b 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/FileContentLoader.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/FileContentLoader.java @@ -20,17 +20,23 @@ import java.io.IOException; import java.io.UncheckedIOException; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class FileContentLoader extends CachedContentLoader { private final Path path; + private static final Logger logger = LoggerFactory.getLogger(FileContentLoader.class); + FileContentLoader(String uri, URIContentLoader... fallbackContentLoaders) { super(uri, fallbackContentLoaders); - this.path = Path.of(uriToPath(uri)); + this.path = obtainPath(uri); } @Override @@ -38,6 +44,17 @@ public URIContentLoaderType type() { return URIContentLoaderType.FILE; } + private static Path obtainPath(String uri) { + if (uri.startsWith(URIContentLoaderType.FILE.scheme())) { + try { + return Path.of(URI.create(uri)); + } catch (Exception ex) { + logger.info("URI {} is not valid one according to Java, trying alternative approach", uri, ex); + } + } + return Path.of(uriToPath(uri)); + } + @Override protected Optional internalGetPath() { return Files.exists(path) ? Optional.of(path) : Optional.empty(); diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java index 0a284b595f3..9e44087b36f 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java @@ -74,11 +74,11 @@ public static byte[] readBytes(String uriStr, Workflow workflow, Optional scheme(String uri) { public static URIContentLoaderType from(String uri) { return scheme(uri).map(scheme -> { switch (scheme) { + default: case "file": return FILE; case "classpath": @@ -54,17 +55,10 @@ public static URIContentLoaderType from(String uri) { return HTTP; case "https": return HTTPS; - default: - throw new IllegalArgumentException("Unrecognized protocol " + scheme + "for uri " + uri); } }).orElse(FILE); } - public String addScheme(String path) { - String lowerCasePath = path.toLowerCase(); - return lowerCasePath.startsWith(scheme) ? path : scheme + path; - } - public boolean isAbsolutePath(String path) { if (!path.isBlank()) { char firstChar = path.trim().charAt(0); @@ -78,16 +72,19 @@ public boolean isAbsolutePath(String path) { } public String concat(String basePath, String additionalPath) { - char separator = separator(); - if (!basePath.isBlank() && !isAbsolutePath(basePath)) { - basePath = separator + basePath; + if (isAbsolutePath(additionalPath)) { + return keepSchemaIfPresent(basePath, additionalPath); + } + int indexOf = lastIndexOf(basePath); + if (indexOf != -1) { + return keepSchemaIfPresent(basePath, basePath.substring(0, indexOf + 1) + additionalPath); + } else { + return keepSchemaIfPresent(basePath, additionalPath); } - return basePath + separator + additionalPath; } - public String trimLast(String path) { - int indexOf = lastIndexOf(path); - return indexOf != -1 ? path.substring(0, indexOf) : ""; + private String keepSchemaIfPresent(String basePath, String resultPath) { + return basePath.startsWith(scheme) && !resultPath.startsWith(scheme) ? scheme + resultPath : resultPath; } public String lastPart(String path) { @@ -112,8 +109,4 @@ private int lastIndexOf(String path) { return indexOf; } - private char separator() { - return additionalSeparators.length > 0 ? additionalSeparators[0] : '/'; - } - } diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderTest.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderTest.java index 33ff48c3351..551b9c22fbc 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderTest.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderTest.java @@ -34,12 +34,12 @@ class URIContentLoaderTest { @Test void testExistingFile() throws URISyntaxException { - assertThat(readString(builder(Thread.currentThread().getContextClassLoader().getResource("pepe.txt").toURI()))).isEqualTo("my name is javierito"); + assertThat(readString(builder(Thread.currentThread().getContextClassLoader().getResource("pepe a pepa.txt").toURI()))).isEqualTo("my name is javierito"); } @Test void testExistingClasspath() { - assertThat(readString(builder("classpath:pepe.txt"))).isEqualTo("my name is javierito"); + assertThat(readString(builder("classpath:pepe a pepa.txt"))).isEqualTo("my name is javierito"); } @Test @@ -56,13 +56,14 @@ void testNotExistingClasspath() { @Test void testCompoundURI() { - assertThat(compoundURI("classpath:pepe.json", "pepa.json")).isEqualTo("classpath:/pepa.json"); - assertThat(compoundURI("classpath:pepe.json", "file:///pepa.json")).isEqualTo("file:///pepa.json"); + assertThat(compoundURI("classpath:pepe.json", "pepa.json")).isEqualTo("classpath:pepa.json"); + assertThat(compoundURI("classpath:pepe.json", "file:/pepa.json")).isEqualTo("file:/pepa.json"); assertThat(compoundURI("classpath:schema/pepe.json", "/pepa.json")).isEqualTo("classpath:/pepa.json"); - assertThat(compoundURI("classpath:schema/pepe.json", "pepa.json")).isEqualTo("classpath:/schema/pepa.json"); - assertThat(compoundURI("pepe.json", "pepa.json")).isEqualTo("file:/pepa.json"); - assertThat(compoundURI("schema/pepe.json", "pepa.json")).isEqualTo("file:/schema/pepa.json"); - assertThat(compoundURI("schema/pepe.json", "/pepa.json")).isEqualTo("file:/pepa.json"); + assertThat(compoundURI("classpath:schema/pepe.json", "pepa.json")).isEqualTo("classpath:schema/pepa.json"); + assertThat(compoundURI("pepe.json", "pepa.json")).isEqualTo("pepa.json"); + assertThat(compoundURI("schema/pepe.json", "pepa.json")).isEqualTo("schema/pepa.json"); + assertThat(compoundURI("schema/pepe.json", "/pepa.json")).isEqualTo("/pepa.json"); assertThat(compoundURI("pepe.json", "classpath:pepa.json")).isEqualTo("classpath:pepa.json"); } + } diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/resources/pepe.txt b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/resources/pepe a pepa.txt similarity index 100% rename from kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/resources/pepe.txt rename to kogito-serverless-workflow/kogito-serverless-workflow-builder/src/test/resources/pepe a pepa.txt diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/rpc/WorkflowRPCCodeGenProvider.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/rpc/WorkflowRPCCodeGenProvider.java index 8c35dcc7bf7..dd96cbde940 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/rpc/WorkflowRPCCodeGenProvider.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/rpc/WorkflowRPCCodeGenProvider.java @@ -72,7 +72,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException { } ProtocUtils.generateDescriptor(protoFiles, context); return true; - } catch (IOException io) { + } catch (Exception io) { + logger.error("Exception generating RPC code", io); throw new CodeGenException(io); } } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/io/QuarkusResourceCache.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/io/QuarkusResourceCache.java index 310dedb1d6b..17beb1d815b 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/io/QuarkusResourceCache.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/io/QuarkusResourceCache.java @@ -18,8 +18,7 @@ */ package org.kie.kogito.serverless.workflow.io; -import java.net.URI; -import java.util.function.Function; +import java.util.function.Supplier; import io.quarkus.cache.Cache; import io.quarkus.cache.CacheName; @@ -40,7 +39,7 @@ void init() { ResourceCacheFactory.setResourceCache(this::get); } - private byte[] get(URI uri, Function retrieveCall) { - return cache.get(uri, retrieveCall).await().indefinitely(); + private byte[] get(String uri, Supplier retrieveCall) { + return cache.get(uri, u -> retrieveCall.get()).await().indefinitely(); } }