Search in sources :

Example 11 with ExternalResourceName

use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.

the class MavenVersionLister method listVersions.

public void listVersions(ModuleIdentifier module, List<ResourcePattern> patterns, BuildableModuleVersionListingResolveResult result) {
    final Set<ExternalResourceName> searched = Sets.newHashSet();
    List<String> versions = Lists.newArrayList();
    boolean hasResult = false;
    for (ResourcePattern pattern : patterns) {
        ExternalResourceName metadataLocation = pattern.toModulePath(module).resolve("maven-metadata.xml");
        if (searched.add(metadataLocation)) {
            result.attempted(metadataLocation);
            try {
                MavenMetadata mavenMetaData = mavenMetadataLoader.load(metadataLocation);
                versions.addAll(mavenMetaData.versions);
                hasResult = true;
            } catch (MissingResourceException e) {
            // Continue
            }
        }
    }
    if (hasResult) {
        result.listed(versions);
    }
}
Also used : ExternalResourceName(org.gradle.internal.resource.ExternalResourceName) ResourcePattern(org.gradle.api.internal.artifacts.repositories.resolver.ResourcePattern) MissingResourceException(org.gradle.api.resources.MissingResourceException)

Example 12 with ExternalResourceName

use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.

the class UrlExternalResource method open.

public static ExternalResource open(URL url) throws IOException {
    URI uri;
    try {
        uri = url.toURI();
    } catch (URISyntaxException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
    UrlExternalResource connector = new UrlExternalResource();
    return new AccessorBackedExternalResource(new ExternalResourceName(uri), connector, connector, connector, false);
}
Also used : ExternalResourceName(org.gradle.internal.resource.ExternalResourceName) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 13 with ExternalResourceName

use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.

the class PublicKeyDownloadService method toQuery.

private ExternalResourceName toQuery(URI baseUri, String fingerprint) throws URISyntaxException {
    String scheme = baseUri.getScheme();
    int port = baseUri.getPort();
    if ("hkp".equals(scheme)) {
        scheme = "http";
        port = 11371;
    }
    return new ExternalResourceName(new URI(scheme, null, baseUri.getHost(), port, "/pks/lookup", "op=get&options=mr&search=0x" + fingerprint, null));
}
Also used : ExternalResourceName(org.gradle.internal.resource.ExternalResourceName) SecuritySupport.toLongIdHexString(org.gradle.security.internal.SecuritySupport.toLongIdHexString) URI(java.net.URI)

Example 14 with ExternalResourceName

use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.

the class PublicKeyDownloadService method tryDownloadKeyFromServer.

@SuppressWarnings("OptionalAssignedToNull")
private void tryDownloadKeyFromServer(String fingerprint, List<URI> baseUris, PublicKeyResultBuilder builder, Consumer<? super PGPPublicKeyRing> onKeyring) {
    Deque<URI> serversLeft = new ArrayDeque<>(baseUris);
    try {
        ExponentialBackoff<ExponentialBackoff.Signal> backoff = ExponentialBackoff.of(5, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS);
        backoff.retryUntil(() -> {
            URI baseUri = serversLeft.poll();
            if (baseUri == null) {
                // no more servers left despite retries
                return IOQuery.Result.successful(false);
            }
            try {
                ExternalResourceName query = toQuery(baseUri, fingerprint);
                ExternalResourceReadResult<IOQuery.Result<Boolean>> response = client.resource(query).withContentIfPresent(inputStream -> {
                    extractKeyRing(inputStream, builder, onKeyring);
                    return IOQuery.Result.successful(true);
                });
                if (response != null) {
                    return response.getResult();
                } else {
                    logKeyDownloadAttempt(fingerprint, baseUri);
                // null means the resource is missing from this repo
                }
            } catch (Exception e) {
                logKeyDownloadAttempt(fingerprint, baseUri);
                // add for retry
                serversLeft.add(baseUri);
            }
            // retry
            return IOQuery.Result.notSuccessful(false);
        });
    } catch (InterruptedException | IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : ExternalResourceName(org.gradle.internal.resource.ExternalResourceName) IOException(java.io.IOException) URI(java.net.URI) ArrayDeque(java.util.ArrayDeque) URISyntaxException(java.net.URISyntaxException) UncheckedException(org.gradle.internal.UncheckedException) IOException(java.io.IOException) ExternalResourceReadResult(org.gradle.internal.resource.ExternalResourceReadResult)

Example 15 with ExternalResourceName

use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.

the class ResourceVersionLister method newVisitor.

public VersionPatternVisitor newVisitor(final ModuleIdentifier module, final Collection<String> dest, final ResourceAwareResolveResult result) {
    return new VersionPatternVisitor() {

        final Set<ExternalResourceName> directories = new HashSet<ExternalResourceName>();

        public void visit(ResourcePattern pattern, IvyArtifactName artifact) throws ResourceException {
            ExternalResourceName versionListPattern = pattern.toVersionListPattern(module, artifact);
            LOGGER.debug("Listing all in {}", versionListPattern);
            try {
                List<String> versionStrings = listRevisionToken(versionListPattern);
                for (String versionString : versionStrings) {
                    dest.add(versionString);
                }
            } catch (Exception e) {
                throw ResourceExceptions.failure(versionListPattern.getUri(), String.format("Could not list versions using %s.", pattern), e);
            }
        }

        // lists all the values a revision token listed by a given url lister
        private List<String> listRevisionToken(ExternalResourceName versionListPattern) {
            String pattern = versionListPattern.getPath();
            if (!pattern.contains(REVISION_TOKEN)) {
                LOGGER.debug("revision token not defined in pattern {}.", pattern);
                return Collections.emptyList();
            }
            String prefix = pattern.substring(0, pattern.indexOf(REVISION_TOKEN));
            if (revisionMatchesDirectoryName(pattern)) {
                ExternalResourceName parent = versionListPattern.getRoot().resolve(prefix);
                return listAll(parent);
            } else {
                int parentFolderSlashIndex = prefix.lastIndexOf(fileSeparator);
                String revisionParentFolder = parentFolderSlashIndex == -1 ? "" : prefix.substring(0, parentFolderSlashIndex + 1);
                ExternalResourceName parent = versionListPattern.getRoot().resolve(revisionParentFolder);
                LOGGER.debug("using {} to list all in {} ", repository, revisionParentFolder);
                if (!directories.add(parent)) {
                    return Collections.emptyList();
                }
                result.attempted(parent);
                List<String> all = repository.list(parent.getUri());
                if (all == null) {
                    return Collections.emptyList();
                }
                LOGGER.debug("found {} urls", all.size());
                Pattern regexPattern = createRegexPattern(pattern, parentFolderSlashIndex);
                List<String> ret = filterMatchedValues(all, regexPattern);
                LOGGER.debug("{} matched {}", ret.size(), pattern);
                return ret;
            }
        }

        private List<String> filterMatchedValues(List<String> all, final Pattern p) {
            List<String> ret = new ArrayList<String>(all.size());
            for (String path : all) {
                Matcher m = p.matcher(path);
                if (m.matches()) {
                    String value = m.group(1);
                    ret.add(value);
                }
            }
            return ret;
        }

        private Pattern createRegexPattern(String pattern, int prefixLastSlashIndex) {
            int endNameIndex = pattern.indexOf(fileSeparator, prefixLastSlashIndex + 1);
            String namePattern;
            if (endNameIndex != -1) {
                namePattern = pattern.substring(prefixLastSlashIndex + 1, endNameIndex);
            } else {
                namePattern = pattern.substring(prefixLastSlashIndex + 1);
            }
            namePattern = namePattern.replaceAll("\\.", "\\\\.");
            String acceptNamePattern = namePattern.replaceAll("\\[revision\\]", "(.+)");
            return Pattern.compile(acceptNamePattern);
        }

        private boolean revisionMatchesDirectoryName(String pattern) {
            int startToken = pattern.indexOf(REVISION_TOKEN);
            if (startToken > 0 && !pattern.substring(startToken - 1, startToken).equals(fileSeparator)) {
                // previous character is not a separator
                return false;
            }
            int endToken = startToken + REV_TOKEN_LENGTH;
            if (endToken < pattern.length() && !pattern.substring(endToken, endToken + 1).equals(fileSeparator)) {
                // next character is not a separator
                return false;
            }
            return true;
        }

        private List<String> listAll(ExternalResourceName parent) {
            if (!directories.add(parent)) {
                return Collections.emptyList();
            }
            LOGGER.debug("using {} to list all in {}", repository, parent);
            result.attempted(parent.toString());
            List<String> paths = repository.list(parent.getUri());
            if (paths == null) {
                return Collections.emptyList();
            }
            LOGGER.debug("found {} resources", paths.size());
            return paths;
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) ExternalResourceName(org.gradle.internal.resource.ExternalResourceName) Matcher(java.util.regex.Matcher) ResourceException(org.gradle.api.resources.ResourceException) IvyArtifactName(org.gradle.internal.component.model.IvyArtifactName)

Aggregations

ExternalResourceName (org.gradle.internal.resource.ExternalResourceName)25 LocallyAvailableExternalResource (org.gradle.internal.resource.local.LocallyAvailableExternalResource)6 Pattern (java.util.regex.Pattern)4 URI (java.net.URI)3 Nullable (javax.annotation.Nullable)3 IvyArtifactName (org.gradle.internal.component.model.IvyArtifactName)3 LocallyAvailableResourceCandidates (org.gradle.internal.resource.local.LocallyAvailableResourceCandidates)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 Matcher (java.util.regex.Matcher)2 ExternalResource (org.gradle.internal.resource.ExternalResource)2 ByteArrayReadableContent (org.gradle.internal.resource.local.ByteArrayReadableContent)2 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1