Search in sources :

Example 11 with ResourceException

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

the class SftpResourceLister method list.

public List<String> list(URI directory) {
    LockableSftpClient client = sftpClientFactory.createSftpClient(directory, credentials);
    try {
        @SuppressWarnings("unchecked") Vector<ChannelSftp.LsEntry> entries = client.getSftpClient().ls(directory.getPath());
        List<String> list = new ArrayList<String>();
        for (ChannelSftp.LsEntry entry : entries) {
            list.add(entry.getFilename());
        }
        return list;
    } catch (com.jcraft.jsch.SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        }
        throw new ResourceException(directory, String.format("Could not list children for resource '%s'.", directory), e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
Also used : ArrayList(java.util.ArrayList) ChannelSftp(com.jcraft.jsch.ChannelSftp) ResourceException(org.gradle.api.resources.ResourceException)

Example 12 with ResourceException

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

the class DefaultCacheAwareExternalResourceAccessor method getResourceSha1.

private HashValue getResourceSha1(URI location, boolean revalidate) {
    try {
        URI sha1Location = new URI(location.toASCIIString() + ".sha1");
        ExternalResource resource = delegate.getResource(sha1Location, revalidate);
        if (resource == null) {
            return null;
        }
        try {
            return resource.withContent(new Transformer<HashValue, InputStream>() {

                @Override
                public HashValue transform(InputStream inputStream) {
                    try {
                        String sha = IOUtils.toString(inputStream, "us-ascii");
                        return HashValue.parse(sha);
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }
            });
        } finally {
            resource.close();
        }
    } catch (Exception e) {
        throw new ResourceException(location, String.format("Failed to download SHA1 for resource '%s'.", location), e);
    }
}
Also used : InputStream(java.io.InputStream) UncheckedIOException(org.gradle.api.UncheckedIOException) ResourceException(org.gradle.api.resources.ResourceException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) HashValue(org.gradle.internal.hash.HashValue) URI(java.net.URI) DefaultLocallyAvailableExternalResource(org.gradle.internal.resource.local.DefaultLocallyAvailableExternalResource) LocallyAvailableExternalResource(org.gradle.internal.resource.local.LocallyAvailableExternalResource) ExternalResource(org.gradle.internal.resource.ExternalResource) CachedExternalResource(org.gradle.internal.resource.cached.CachedExternalResource) UncheckedIOException(org.gradle.api.UncheckedIOException) ResourceException(org.gradle.api.resources.ResourceException) IOException(java.io.IOException)

Example 13 with ResourceException

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

the class ApacheDirectoryListingParser method parse.

public List<String> parse(URI baseURI, InputStream content, String contentType) throws Exception {
    baseURI = addTrailingSlashes(baseURI);
    if (contentType == null || !contentType.startsWith("text/html")) {
        throw new ResourceException(baseURI, String.format("Unsupported ContentType %s for directory listing '%s'", contentType, baseURI));
    }
    Charset contentEncoding = UriTextResource.extractCharacterEncoding(contentType, Charsets.UTF_8);
    final Reader htmlText = new InputStreamReader(content, contentEncoding);
    final InputSource inputSource = new InputSource(htmlText);
    final SAXParser htmlParser = new SAXParser();
    final AnchorListerHandler anchorListerHandler = new AnchorListerHandler();
    htmlParser.setContentHandler(anchorListerHandler);
    htmlParser.parse(inputSource);
    List<String> hrefs = anchorListerHandler.getHrefs();
    List<URI> uris = resolveURIs(baseURI, hrefs);
    return filterNonDirectChilds(baseURI, uris);
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SAXParser(org.cyberneko.html.parsers.SAXParser) ResourceException(org.gradle.api.resources.ResourceException) URI(java.net.URI)

Example 14 with ResourceException

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

the class SftpResourceLister method list.

@Override
public List<String> list(ExternalResourceName directory) {
    LockableSftpClient client = sftpClientFactory.createSftpClient(directory.getUri(), credentials);
    try {
        @SuppressWarnings("unchecked") Vector<ChannelSftp.LsEntry> entries = client.getSftpClient().ls(directory.getPath());
        List<String> list = new ArrayList<String>();
        for (ChannelSftp.LsEntry entry : entries) {
            list.add(entry.getFilename());
        }
        return list;
    } catch (com.jcraft.jsch.SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        }
        throw new ResourceException(directory.getUri(), String.format("Could not list children for resource '%s'.", directory.getUri()), e);
    } finally {
        sftpClientFactory.releaseSftpClient(client);
    }
}
Also used : ArrayList(java.util.ArrayList) ChannelSftp(com.jcraft.jsch.ChannelSftp) ResourceException(org.gradle.api.resources.ResourceException)

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