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