Search in sources :

Example 1 with DefaultExternalResourceMetaData

use of org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData 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 2 with DefaultExternalResourceMetaData

use of org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData 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 3 with DefaultExternalResourceMetaData

use of org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData 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 4 with DefaultExternalResourceMetaData

use of org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData 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 5 with DefaultExternalResourceMetaData

use of org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData in project gradle by gradle.

the class S3ResourceConnector method getMetaData.

public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) {
    LOGGER.debug("Attempting to get resource metadata: {}", location);
    S3Object s3Object = s3Client.getMetaData(location);
    if (s3Object == null) {
        return null;
    }
    try {
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        return new DefaultExternalResourceMetaData(location, objectMetadata.getLastModified().getTime(), objectMetadata.getContentLength(), objectMetadata.getContentType(), objectMetadata.getETag(), // Passing null for sha1 - TODO - consider using the etag which is an MD5 hash of the file (when less than 5Gb)
        null);
    } finally {
        IoActions.closeQuietly(s3Object);
    }
}
Also used : S3Object(com.amazonaws.services.s3.model.S3Object) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DefaultExternalResourceMetaData(org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData)

Aggregations

DefaultExternalResourceMetaData (org.gradle.internal.resource.metadata.DefaultExternalResourceMetaData)7 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 Nullable (javax.annotation.Nullable)4 ResourceException (org.gradle.api.resources.ResourceException)4 UncheckedException (org.gradle.internal.UncheckedException)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)3 S3Object (com.amazonaws.services.s3.model.S3Object)2 InputStream (java.io.InputStream)2 Date (java.util.Date)1