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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations