This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git
The following commit(s) were added to refs/heads/master by this push:
new 2dfbcd8 SLING-10586 prevent java.nio.file.FileSystemNotFoundException
during DefaultArtifactHandler.getArtifact(...) (#29)
2dfbcd8 is described below
commit 2dfbcd89a2a12f5d76d222df1af0fa9afa434679
Author: Konrad Windszus <[email protected]>
AuthorDate: Tue Jul 6 18:16:32 2021 +0200
SLING-10586 prevent java.nio.file.FileSystemNotFoundException during
DefaultArtifactHandler.getArtifact(...) (#29)
Co-authored-by: Karl Pauls <[email protected]>
---
.../feature/io/artifacts/ArtifactManager.java | 17 ++++++----
.../feature/io/artifacts/ArtifactManagerTest.java | 37 +++++++++++++++++++++
.../7.0.1/org.apache.felix.framework-7.0.1.jar | Bin 0 -> 773772 bytes
3 files changed, 47 insertions(+), 7 deletions(-)
diff --git
a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
index a71c7df..fff8b06 100644
--- a/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
@@ -24,10 +24,12 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.ProcessBuilder.Redirect;
import java.net.MalformedURLException;
+import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
+import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -383,16 +385,17 @@ public class ArtifactManager
logger.debug("Checking url to be local file {}", url);
// check if this is already a local file
try {
- final Path f = Paths.get(new URL(url).toURI());
- if (Files.exists(f)) {
- this.config.incLocalArtifacts();
- return f.toUri().toURL();
+ URI uri = new URI(url);
+ if
(FileSystems.getDefault().provider().getScheme().equals(uri.getScheme())) {
+ final Path f = Paths.get(uri);
+ if (Files.exists(f)) {
+ this.config.incLocalArtifacts();
+ return f.toUri().toURL();
+ }
+ return null;
}
- return null;
} catch ( final URISyntaxException ise) {
// ignore
- } catch ( final IllegalArgumentException iae) {
- // ignore
} catch ( final MalformedURLException mue) {
// ignore
}
diff --git
a/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
b/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
index 8bdc9cb..c59434a 100644
---
a/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
+++
b/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
@@ -22,12 +22,17 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.io.File;
import java.io.IOException;
import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.net.URLStreamHandlerFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
+import java.util.Random;
import org.apache.sling.feature.io.artifacts.spi.ArtifactProvider;
import org.junit.Test;
@@ -115,4 +120,36 @@ public class ArtifactManagerTest {
assertTrue(tempDir.toFile().isDirectory());
assertTrue(tempDir.toFile().delete());
}
+
+ @Test public void testGetArtifactHandler() throws Exception {
+ ArtifactManagerConfig cfg = new ArtifactManagerConfig();
+ cfg.setRepositoryUrls(new
String[]{"https://repo.maven.apache.org/maven2"});
+
+ ArtifactManager am = ArtifactManager.getArtifactManager(cfg);
+
+
assertNotNull(am.getArtifactHandler(":org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar"));
+
assertNotNull(am.getArtifactHandler("mvn:org.apache.felix/org.apache.felix.framework/7.0.1"));
+
assertNotNull(am.getArtifactHandler("src/test/resources/m2/org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar"));
+
+ String fooProtocol = "foo" + new Random().nextLong();
+ URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
+ @Override
+ public URLStreamHandler createURLStreamHandler(String protocol) {
+ if (fooProtocol.equals(protocol)) {
+ return new URLStreamHandler() {
+ @Override
+ protected URLConnection openConnection(URL u) throws
IOException {
+ return new
File("src/test/resources/m2/org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar").toURL().openConnection();
+ }
+ };
+ }
+ else {
+ return null;
+ }
+ }
+ });
+ assertNotNull(am.getArtifactHandler(fooProtocol + ":/felix.jar"));
+
+ am.shutdown();
+ }
}
diff --git
a/src/test/resources/m2/org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar
b/src/test/resources/m2/org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar
new file mode 100644
index 0000000..e676324
Binary files /dev/null and
b/src/test/resources/m2/org/apache/felix/org.apache.felix.framework/7.0.1/org.apache.felix.framework-7.0.1.jar
differ