Search in sources :

Example 6 with ResourceException

use of org.gradle.api.resources.ResourceException in project gradle by gradle.

the class UrlExternalResource method openResource.

@Nullable
@Override
public ExternalResourceReadResponse openResource(final ExternalResourceName location, boolean revalidate) throws ResourceException {
    try {
        URL url = location.getUri().toURL();
        final URLConnection connection = url.openConnection();
        final InputStream inputStream = connection.getInputStream();
        return new ExternalResourceReadResponse() {

            @Override
            public InputStream openStream() {
                return inputStream;
            }

            @Override
            public ExternalResourceMetaData getMetaData() {
                return new DefaultExternalResourceMetaData(location.getUri(), connection.getLastModified(), connection.getContentLength(), connection.getContentType(), null, null);
            }

            @Override
            public void close() throws IOException {
                inputStream.close();
            }
        };
    } catch (FileNotFoundException e) {
        return null;
    } catch (Exception e) {
        throw ResourceExceptions.getFailed(location.getUri(), e);
    }
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) DefaultExternalResourceMetaData(org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData) URL(java.net.URL) URLConnection(java.net.URLConnection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UncheckedException(org.gradle.internal.UncheckedException) ResourceException(org.gradle.api.resources.ResourceException) Nullable(javax.annotation.Nullable)

Example 7 with ResourceException

use of org.gradle.api.resources.ResourceException in project gradle by gradle.

the class UrlExternalResource method getMetaData.

@Nullable
@Override
public ExternalResourceMetaData getMetaData(ExternalResourceName location, boolean revalidate) throws ResourceException {
    try {
        URL url = location.getUri().toURL();
        URLConnection connection = url.openConnection();
        try {
            return new DefaultExternalResourceMetaData(location.getUri(), connection.getLastModified(), connection.getContentLength(), connection.getContentType(), null, null);
        } finally {
            connection.getInputStream().close();
        }
    } catch (FileNotFoundException e) {
        return null;
    } catch (Exception e) {
        throw ResourceExceptions.getFailed(location.getUri(), e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DefaultExternalResourceMetaData(org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData) URL(java.net.URL) URLConnection(java.net.URLConnection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UncheckedException(org.gradle.internal.UncheckedException) ResourceException(org.gradle.api.resources.ResourceException) Nullable(javax.annotation.Nullable)

Example 8 with ResourceException

use of org.gradle.api.resources.ResourceException in project gradle by gradle.

the class HttpResourceLister method list.

@Override
public List<String> list(final ExternalResourceName directory) {
    return accessor.withContent(directory, true, (inputStream, metaData) -> {
        String contentType = metaData.getContentType();
        ApacheDirectoryListingParser directoryListingParser = new ApacheDirectoryListingParser();
        try {
            return directoryListingParser.parse(directory.getUri(), inputStream, contentType);
        } catch (Exception e) {
            throw new ResourceException(directory.getUri(), String.format("Unable to parse HTTP directory listing for '%s'.", directory.getUri()), e);
        }
    });
}
Also used : ResourceException(org.gradle.api.resources.ResourceException) ResourceException(org.gradle.api.resources.ResourceException)

Example 9 with ResourceException

use of org.gradle.api.resources.ResourceException in project gradle by gradle.

the class SftpResourceUploader method ensureParentDirectoryExists.

private void ensureParentDirectoryExists(ChannelSftp channel, URI uri) {
    String parentPath = FilenameUtils.getFullPathNoEndSeparator(uri.getPath());
    if (parentPath.equals("/")) {
        return;
    }
    URI parent = uri.resolve(parentPath);
    try {
        channel.lstat(parentPath);
        return;
    } catch (com.jcraft.jsch.SftpException e) {
        if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            throw new ResourceException(parent, String.format("Could not lstat resource '%s'.", parent), e);
        }
    }
    ensureParentDirectoryExists(channel, parent);
    try {
        channel.mkdir(parentPath);
    } catch (com.jcraft.jsch.SftpException e) {
        throw new ResourceException(parent, String.format("Could not create resource '%s'.", parent), e);
    }
}
Also used : ResourceException(org.gradle.api.resources.ResourceException) URI(java.net.URI)

Example 10 with ResourceException

use of org.gradle.api.resources.ResourceException 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

ResourceException (org.gradle.api.resources.ResourceException)14 IOException (java.io.IOException)8 FileNotFoundException (java.io.FileNotFoundException)5 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 Nullable (javax.annotation.Nullable)4 UncheckedException (org.gradle.internal.UncheckedException)4 DefaultExternalResourceMetaData (org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData)4 URI (java.net.URI)3 Charset (java.nio.charset.Charset)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 SAXParser (org.cyberneko.html.parsers.SAXParser)1