public URL getArtifact()

in src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java [384:443]


        public URL getArtifact(final String url, final String relativeCachePath) {
            logger.debug("Checking url to be local file {}", url);
            // check if this is already a local file
            try {
                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;
                }
            } catch ( final URISyntaxException ise) {
                // ignore
            } catch ( final MalformedURLException mue) {
                // ignore
            }
            logger.debug("Checking remote url {}", url);
            try {
                // check for url
                if ( url.indexOf(":") == -1 ) {
                    return null;
                }

                String adjustedRelativePath = relativeCachePath;
                // For Windows we need to remove the drive name from the path
                int pos = adjustedRelativePath.indexOf(":/");
                if(pos >= 0) {
                    adjustedRelativePath = adjustedRelativePath.substring(pos + 2);
                }
                Path cacheFile = cacheDir.resolve(adjustedRelativePath.replace("/", java.nio.file.FileSystems.getDefault().getSeparator()));
                if (!Files.exists(cacheFile) ) {
                    Files.createDirectories(cacheFile.getParent());
                    final URL u = new URL(url);
                    final URLConnection con = u.openConnection();
                    final String userInfo = u.getUserInfo();
                    if (userInfo != null) {
                        con.addRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(u.toURI().getUserInfo().getBytes("UTF-8")));
                    }
                    con.connect();

                    try (InputStream input = con.getInputStream()){
                        Files.copy(input, cacheFile);
                    } catch(IOException e) {
                        //TODO: Remove this logging statement when it settled down
                        logger.debug("Failed to copy file", e);
                        throw e;
                    }
                    this.config.incDownloadedArtifacts();
                } else {
                    this.config.incCachedArtifacts();
                }
                return cacheFile.toUri().toURL();
            } catch ( final Exception e) {
                logger.info("Artifact not found in one repository", e);
                // ignore for now
                return null;
            }
        }