Search in sources :

Example 1 with ResourceException

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

the class HttpResourceLister method list.

public List<String> list(final URI directory) {
    final ExternalResourceReadResponse response = accessor.openResource(directory, true);
    if (response == null) {
        return null;
    }
    try {
        try {
            String contentType = response.getMetaData().getContentType();
            ApacheDirectoryListingParser directoryListingParser = new ApacheDirectoryListingParser();
            InputStream inputStream = response.openStream();
            try {
                return directoryListingParser.parse(directory, inputStream, contentType);
            } catch (Exception e) {
                throw new ResourceException(directory, String.format("Unable to parse HTTP directory listing for '%s'.", directory), e);
            }
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(directory, e);
    }
}
Also used : InputStream(java.io.InputStream) ResourceException(org.gradle.api.resources.ResourceException) ExternalResourceReadResponse(org.gradle.internal.resource.transfer.ExternalResourceReadResponse) IOException(java.io.IOException) ResourceException(org.gradle.api.resources.ResourceException) IOException(java.io.IOException)

Example 2 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 URI location, boolean revalidate) throws ResourceException {
    try {
        URL url = location.toURL();
        final URLConnection connection = url.openConnection();
        final InputStream inputStream = connection.getInputStream();
        return new ExternalResourceReadResponse() {

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

            @Override
            public ExternalResourceMetaData getMetaData() {
                return new DefaultExternalResourceMetaData(location, 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, 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 3 with ResourceException

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

the class UrlExternalResource method getMetaData.

@Nullable
@Override
public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) throws ResourceException {
    try {
        URL url = location.toURL();
        URLConnection connection = url.openConnection();
        try {
            return new DefaultExternalResourceMetaData(location, connection.getLastModified(), connection.getContentLength(), connection.getContentType(), null, null);
        } finally {
            connection.getInputStream().close();
        }
    } catch (FileNotFoundException e) {
        return null;
    } catch (Exception e) {
        throw ResourceExceptions.getFailed(location, 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 4 with ResourceException

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

the class ApiTextResourceAdapter method asFile.

@Override
public File asFile(String targetCharset) {
    try {
        File file = getWrappedTextResource().getFile();
        if (file == null) {
            file = tempFileProvider.createTemporaryFile("wrappedInternalText", ".txt", "resource");
            Files.asCharSink(file, Charset.forName(targetCharset)).write(getWrappedTextResource().getText());
            return file;
        }
        Charset sourceCharset = getWrappedTextResource().getCharset();
        Charset targetCharsetObj = Charset.forName(targetCharset);
        if (targetCharsetObj.equals(sourceCharset)) {
            return file;
        }
        File targetFile = tempFileProvider.createTemporaryFile("uriTextResource", ".txt", "resource");
        try {
            Files.asCharSource(file, sourceCharset).copyTo(Files.asCharSink(targetFile, targetCharsetObj));
            return targetFile;
        } catch (IOException e) {
            throw new ResourceException("Could not write " + getDisplayName() + " content to " + targetFile + ".", e);
        }
    } catch (Exception e) {
        throw ResourceExceptions.readFailed(getDisplayName(), e);
    }
}
Also used : Charset(java.nio.charset.Charset) ResourceException(org.gradle.api.resources.ResourceException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) ResourceException(org.gradle.api.resources.ResourceException)

Example 5 with ResourceException

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

the class FileCollectionBackedTextResource method asFile.

@Override
public File asFile(String targetCharset) {
    try {
        Charset targetCharsetObj = Charset.forName(targetCharset);
        if (targetCharsetObj.equals(charset)) {
            return fileCollection.getSingleFile();
        }
        File targetFile = tempFileProvider.createTemporaryFile("fileCollection", ".txt", "resource");
        try {
            Files.asCharSource(fileCollection.getSingleFile(), charset).copyTo(Files.asCharSink(targetFile, targetCharsetObj));
        } catch (IOException e) {
            throw new ResourceException("Could not write " + getDisplayName() + " content to " + targetFile + ".", e);
        }
        return targetFile;
    } catch (Exception e) {
        throw ResourceExceptions.readFailed(getDisplayName(), e);
    }
}
Also used : Charset(java.nio.charset.Charset) ResourceException(org.gradle.api.resources.ResourceException) IOException(java.io.IOException) File(java.io.File) ResourceException(org.gradle.api.resources.ResourceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

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