Skip to content

Commit 3557b8d

Browse files
fjtiradorgdoliveira
authored andcommitted
* [Fix apache#3569] Fixing windows path issue * [Fix apache#3569] Switching from URI to String * [Fix apache#3569] Refining for windows * [Fix_#3569] Refining uriToPath usage * [Fix apache#3569] Temporary commit to debug CI issue * Revert "[Fix apache#3569] Temporary commit to debug CI issue" This reverts commit 24eb3c0. * [Fix apache#3569] Disable reproducible comparison for spring boot integration * [Fix apache#3569] Skip module * Revert "[Fix apache#3569] Skip module" This reverts commit 118ff4b. * Revert "[Fix apache#3569] Disable reproducible comparison for spring boot integration" This reverts commit c2565a9.
1 parent 425c6e3 commit 3557b8d

File tree

27 files changed

+179
-198
lines changed

27 files changed

+179
-198
lines changed

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/CachedContentLoader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.ByteArrayInputStream;
2222
import java.io.InputStream;
23-
import java.net.URI;
2423
import java.nio.file.Path;
2524
import java.util.Optional;
2625

@@ -48,10 +47,10 @@ public synchronized byte[] readAllBytes() {
4847
}
4948
}
5049

51-
private final URI uri;
50+
protected final String uri;
5251
private URIContentLoader[] fallbackContentLoaders;
5352

54-
protected CachedContentLoader(URI uri, URIContentLoader... fallbackContentLoaders) {
53+
protected CachedContentLoader(String uri, URIContentLoader... fallbackContentLoaders) {
5554
this.uri = uri;
5655
this.fallbackContentLoaders = fallbackContentLoaders;
5756
}
@@ -91,10 +90,13 @@ public InputStream getInputStream() {
9190
}
9291
}
9392

94-
protected abstract byte[] loadURI(URI uri);
95-
96-
@Override
97-
public URI uri() {
98-
return uri;
93+
protected static String trimScheme(String uri, String scheme) {
94+
String str = uri;
95+
if (str.toLowerCase().startsWith(scheme)) {
96+
str = str.substring(scheme.length());
97+
}
98+
return str;
9999
}
100+
101+
protected abstract byte[] loadURI();
100102
}

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/ClassPathContentLoader.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.UncheckedIOException;
24-
import java.net.URI;
2524
import java.net.URISyntaxException;
2625
import java.net.URL;
27-
import java.net.URLDecoder;
28-
import java.nio.charset.Charset;
2926
import java.nio.file.Path;
3027
import java.util.Optional;
3128

@@ -34,24 +31,12 @@ public class ClassPathContentLoader extends CachedContentLoader {
3431
private final Optional<URL> resource;
3532
private final String classpath;
3633

37-
ClassPathContentLoader(URI uri, Optional<ClassLoader> cl, URIContentLoader... fallbackContentLoaders) {
34+
ClassPathContentLoader(String uri, Optional<ClassLoader> cl, URIContentLoader... fallbackContentLoaders) {
3835
super(uri, fallbackContentLoaders);
39-
this.classpath = getPath(uri);
36+
this.classpath = uriToPath(uri);
4037
this.resource = Optional.ofNullable(cl.orElse(Thread.currentThread().getContextClassLoader()).getResource(classpath));
4138
}
4239

43-
static String getPath(URI uri) {
44-
final String classPathPrefix = "classpath:";
45-
String str = URLDecoder.decode(uri.toString(), Charset.defaultCharset());
46-
if (str.toLowerCase().startsWith(classPathPrefix)) {
47-
str = str.substring(classPathPrefix.length());
48-
while (str.startsWith("/")) {
49-
str = str.substring(1);
50-
}
51-
}
52-
return str;
53-
}
54-
5540
public Optional<URL> getResource() {
5641
return resource;
5742
}
@@ -74,7 +59,7 @@ private static Path fromURL(URL url) {
7459
}
7560

7661
@Override
77-
protected byte[] loadURI(URI uri) {
62+
protected byte[] loadURI() {
7863
return resource.map(this::loadBytes).orElseThrow(() -> new IllegalArgumentException("cannot find classpath resource " + classpath));
7964
}
8065

@@ -86,6 +71,17 @@ private byte[] loadBytes(URL r) {
8671
}
8772
}
8873

74+
static String uriToPath(String uri) {
75+
return removeSlash(trimScheme(uri, URIContentLoaderType.CLASSPATH.scheme()));
76+
}
77+
78+
private static String removeSlash(String str) {
79+
while (str.startsWith("/")) {
80+
str = str.substring(1);
81+
}
82+
return str;
83+
}
84+
8985
@Override
9086
public URIContentLoaderType type() {
9187
return URIContentLoaderType.CLASSPATH;

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/FileContentLoader.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,51 @@
2525
import java.nio.file.Path;
2626
import java.util.Optional;
2727

28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
2831
public class FileContentLoader extends CachedContentLoader {
2932

3033
private final Path path;
3134

32-
FileContentLoader(URI uri, URIContentLoader... fallbackContentLoaders) {
35+
private static final Logger logger = LoggerFactory.getLogger(FileContentLoader.class);
36+
37+
FileContentLoader(String uri, URIContentLoader... fallbackContentLoaders) {
3338
super(uri, fallbackContentLoaders);
34-
this.path = Path.of(getPath(uri));
39+
this.path = obtainPath(uri);
3540
}
3641

3742
@Override
3843
public URIContentLoaderType type() {
3944
return URIContentLoaderType.FILE;
4045
}
4146

47+
private static Path obtainPath(String uri) {
48+
if (uri.startsWith(URIContentLoaderType.FILE.scheme())) {
49+
try {
50+
return Path.of(URI.create(uri));
51+
} catch (Exception ex) {
52+
logger.info("URI {} is not valid one according to Java, trying alternative approach", uri, ex);
53+
}
54+
}
55+
return Path.of(uriToPath(uri));
56+
}
57+
4258
@Override
4359
protected Optional<Path> internalGetPath() {
4460
return Files.exists(path) ? Optional.of(path) : Optional.empty();
4561
}
4662

4763
@Override
48-
protected byte[] loadURI(URI uri) {
64+
protected byte[] loadURI() {
4965
try {
5066
return Files.readAllBytes(path);
5167
} catch (IOException io) {
5268
throw new UncheckedIOException(io);
5369
}
5470
}
5571

56-
static String getPath(URI uri) {
57-
return uri.getPath();
72+
static String uriToPath(String uri) {
73+
return trimScheme(uri, URIContentLoaderType.FILE.scheme());
5874
}
5975
}

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/HttpContentLoader.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.InputStream;
2323
import java.net.HttpURLConnection;
2424
import java.net.URI;
25+
import java.net.URL;
2526
import java.util.Base64;
2627
import java.util.Collection;
2728
import java.util.HashMap;
@@ -50,17 +51,19 @@ class HttpContentLoader extends CachedContentLoader {
5051

5152
private Optional<Workflow> workflow;
5253
private String authRef;
54+
private final URIContentLoaderType type;
5355

54-
HttpContentLoader(URI uri, Optional<Workflow> workflow, String authRef) {
56+
HttpContentLoader(String uri, Optional<Workflow> workflow, String authRef, URIContentLoaderType type) {
5557
super(uri);
5658
this.workflow = workflow;
5759
this.authRef = authRef;
60+
this.type = type;
5861
}
5962

6063
@Override
61-
protected byte[] loadURI(URI u) {
64+
protected byte[] loadURI() {
6265
try {
63-
HttpURLConnection conn = (HttpURLConnection) u.toURL().openConnection();
66+
HttpURLConnection conn = (HttpURLConnection) new URL(uri).openConnection();
6467
// some http servers required specific accept header (*/* is specified for those we do not care about accept)
6568
conn.setRequestProperty("Accept", "application/json,application/yaml,application/yml,application/text,text/*,*/*");
6669
workflow.map(Workflow::getAuth).map(Auth::getAuthDefs).stream().flatMap(Collection::stream)
@@ -74,7 +77,7 @@ protected byte[] loadURI(URI u) {
7477
} else {
7578
try (InputStream is = conn.getErrorStream()) {
7679
throw new IllegalArgumentException(String.format(
77-
"Failed to fetch remote file: %s. Status code is %d and response: %n %s", u, code, is == null ? "" : new String(is.readAllBytes())));
80+
"Failed to fetch remote file: %s. Status code is %d and response: %n %s", uri, code, is == null ? "" : new String(is.readAllBytes())));
7881
}
7982
}
8083
} catch (IOException io) {
@@ -147,6 +150,11 @@ private String encode(String str) {
147150

148151
@Override
149152
public URIContentLoaderType type() {
150-
return URIContentLoaderType.HTTP;
153+
return type;
151154
}
155+
156+
static String uriToPath(String uri) {
157+
return URI.create(uri).getPath();
158+
}
159+
152160
}

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/ResourceCache.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
*/
1919
package org.kie.kogito.serverless.workflow.io;
2020

21-
import java.net.URI;
22-
import java.util.function.Function;
21+
import java.util.function.Supplier;
2322

2423
public interface ResourceCache {
25-
byte[] get(URI uri, Function<URI, byte[]> retrieveCall);
24+
byte[] get(String uri, Supplier<byte[]> retrieveCall);
2625
}

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/ResourceCacheFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
*/
1919
package org.kie.kogito.serverless.workflow.io;
2020

21-
import java.net.URI;
2221
import java.util.Collections;
2322
import java.util.Map;
2423
import java.util.WeakHashMap;
2524
import java.util.concurrent.atomic.AtomicReference;
26-
import java.util.function.Function;
25+
import java.util.function.Supplier;
2726

2827
public class ResourceCacheFactory {
2928
private static final AtomicReference<ResourceCache> cache = new AtomicReference<>(new LocalResourceCache());
@@ -33,11 +32,11 @@ public static ResourceCache getCache() {
3332
}
3433

3534
private static class LocalResourceCache implements ResourceCache {
36-
private final Map<URI, byte[]> map = Collections.synchronizedMap(new WeakHashMap<>());
35+
private final Map<String, byte[]> map = Collections.synchronizedMap(new WeakHashMap<>());
3736

3837
@Override
39-
public byte[] get(URI uri, Function<URI, byte[]> retrieveCall) {
40-
return map.computeIfAbsent(uri, retrieveCall);
38+
public byte[] get(String uri, Supplier<byte[]> retrieveCall) {
39+
return map.computeIfAbsent(uri, u -> retrieveCall.get());
4140
}
4241

4342
}

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoader.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
package org.kie.kogito.serverless.workflow.io;
2020

2121
import java.io.InputStream;
22-
import java.net.URI;
2322
import java.nio.file.Path;
2423
import java.util.Optional;
2524

2625
public interface URIContentLoader {
2726

28-
URI uri();
29-
3027
InputStream getInputStream();
3128

3229
URIContentLoaderType type();

kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import java.io.InputStream;
2323
import java.io.UncheckedIOException;
2424
import java.net.URI;
25-
import java.net.URISyntaxException;
26-
import java.net.URLEncoder;
27-
import java.nio.charset.Charset;
2825
import java.util.Optional;
2926

3027
import org.kie.kogito.serverless.workflow.parser.ParserContext;
@@ -51,29 +48,11 @@ public static String readString(URIContentLoader loader) {
5148
return new String(readAllBytes(loader));
5249
}
5350

54-
public static String getFileName(URI uri) {
55-
URIContentLoaderType type = URIContentLoaderType.from(uri);
56-
String path = uriToPath(type, uri);
57-
return type.lastPart(path);
58-
}
59-
60-
private static String uriToPath(URIContentLoaderType type, URI uri) {
61-
switch (type) {
62-
case CLASSPATH:
63-
return ClassPathContentLoader.getPath(uri);
64-
case FILE:
65-
return FileContentLoader.getPath(uri);
66-
case HTTP:
67-
default:
68-
return uri.getPath();
69-
}
70-
}
71-
7251
public static String readString(Builder builder) {
7352
return readString(builder.build());
7453
}
7554

76-
public static URIContentLoader buildLoader(URI uri, Workflow workflow, Optional<ParserContext> context, String authRef) {
55+
public static URIContentLoader buildLoader(String uri, Workflow workflow, Optional<ParserContext> context, String authRef) {
7756
Builder builder = new Builder(uri).withWorkflow(workflow).withAuthRef(authRef);
7857
context.map(c -> c.getContext().getClassLoader()).ifPresent(builder::withClassloader);
7958
getBaseURI(workflow).ifPresent(builder::withBaseURI);
@@ -85,25 +64,29 @@ public static byte[] readBytes(String uriStr, Workflow workflow, ParserContext p
8564
}
8665

8766
public static byte[] readBytes(String uriStr, Workflow workflow, Optional<ParserContext> parserContext) {
88-
return readAllBytes(buildLoader(URI.create(uriStr), workflow, parserContext, null));
67+
return readAllBytes(buildLoader(uriStr, workflow, parserContext, null));
8968
}
9069

9170
public static Builder builder(URI uri) {
92-
return new Builder(uri);
71+
return new Builder(uri.toString());
9372
}
9473

9574
public static Builder builder(String uri) {
96-
return new Builder(URI.create(URLEncoder.encode(uri, Charset.defaultCharset())));
75+
return new Builder(uri);
9776
}
9877

9978
public static class Builder {
100-
private URI uri;
79+
private String uri;
10180
private ClassLoader cl;
10281
private Workflow workflow;
10382
private String authRef;
104-
private URI baseURI;
83+
private String baseURI;
10584

10685
private Builder(URI uri) {
86+
this.uri = uri.toString();
87+
}
88+
89+
private Builder(String uri) {
10790
this.uri = uri;
10891
}
10992

@@ -122,44 +105,30 @@ public Builder withAuthRef(String authRef) {
122105
return this;
123106
}
124107

125-
public Builder withBaseURI(URI baseURI) {
108+
public Builder withBaseURI(String baseURI) {
126109
this.baseURI = baseURI;
127110
return this;
128111
}
129112

130113
public URIContentLoader build() {
131-
final URI finalURI = baseURI != null ? compoundURI(baseURI, uri) : uri;
114+
final String finalURI = baseURI != null ? compoundURI(baseURI, uri) : uri;
132115
switch (URIContentLoaderType.from(finalURI)) {
133116
default:
134117
case FILE:
135118
return new FileContentLoader(finalURI, new ClassPathContentLoader(uri, Optional.ofNullable(cl)));
136119
case HTTP:
137-
return new HttpContentLoader(finalURI, Optional.ofNullable(workflow), authRef);
120+
return new HttpContentLoader(finalURI, Optional.ofNullable(workflow), authRef, URIContentLoaderType.HTTP);
121+
case HTTPS:
122+
return new HttpContentLoader(finalURI, Optional.ofNullable(workflow), authRef, URIContentLoaderType.HTTPS);
138123
case CLASSPATH:
139124
Optional<ClassLoader> optionalCl = Optional.ofNullable(cl);
140125
return finalURI == uri ? new ClassPathContentLoader(finalURI, optionalCl) : new ClassPathContentLoader(finalURI, optionalCl, new ClassPathContentLoader(uri, optionalCl));
141126
}
142127
}
143128
}
144129

145-
public static URI compoundURI(URI baseURI, URI uri) {
146-
if (uri.getScheme() != null) {
147-
return uri;
148-
}
149-
URIContentLoaderType type = URIContentLoaderType.from(baseURI);
150-
String basePath = type.trimLast(uriToPath(type, baseURI));
151-
String additionalPath = uriToPath(type, uri);
152-
String path;
153-
if (type.isAbsolutePath(additionalPath)) {
154-
path = additionalPath;
155-
} else {
156-
path = type.concat(basePath, additionalPath);
157-
}
158-
try {
159-
return new URI(type.toString().toLowerCase(), baseURI.getAuthority(), path, uri.getQuery(), uri.getFragment());
160-
} catch (URISyntaxException e) {
161-
throw new IllegalArgumentException(e.getMessage(), e);
162-
}
130+
public static String compoundURI(String baseURI, String uri) {
131+
return URIContentLoaderType.scheme(uri).isPresent() ? uri : URIContentLoaderType.from(baseURI).concat(baseURI, uri);
163132
}
164133

165134
private URIContentLoaderFactory() {

0 commit comments

Comments
 (0)