use of org.apache.archiva.common.Try in project archiva by apache.
the class Maven2RepositoryStorage method readArtifactsMetadata.
@Override
public Collection<ArtifactMetadata> readArtifactsMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageRuntimeException {
Path dir = pathTranslator.toFile(getRepositoryBasedir(readMetadataRequest.getRepositoryId()), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion());
if (!(Files.exists(dir) && Files.isDirectory(dir))) {
return Collections.emptyList();
}
// all files that are not metadata and not a checksum / signature are considered artifacts
final Predicate<Path> dFilter = new ArtifactDirectoryFilter(readMetadataRequest.getFilter());
try (Stream<Path> stream = Files.list(dir)) {
// Returns a map TRUE -> (success values), FALSE -> (Exceptions)
Map<Boolean, List<Try<ArtifactMetadata>>> result = stream.filter(dFilter).map(path -> {
try {
return Try.success(getArtifactFromFile(readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), path));
} catch (Exception e) {
LOGGER.debug("Could not create metadata for {}: {}", path, e.getMessage(), e);
return Try.<ArtifactMetadata>failure(e);
}
}).collect(Collectors.groupingBy(Try::isSuccess));
if (result.containsKey(Boolean.FALSE) && result.get(Boolean.FALSE).size() > 0 && (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE).size() == 0)) {
LOGGER.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
Try<ArtifactMetadata> failure = result.get(Boolean.FALSE).get(0);
LOGGER.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
} else {
if (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE) == null) {
return Collections.emptyList();
}
return result.get(Boolean.TRUE).stream().map(tr -> tr.get()).collect(Collectors.toList());
}
} catch (IOException e) {
LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
}
return Collections.emptyList();
}
use of org.apache.archiva.common.Try in project archiva by apache.
the class Maven2RepositoryStorage method listProjects.
@Override
public Collection<String> listProjects(String repoId, String namespace, Filter<String> filter) throws RepositoryStorageRuntimeException {
Path dir = pathTranslator.toFile(getRepositoryBasedir(repoId), namespace);
if (!(Files.exists(dir) && Files.isDirectory(dir))) {
return Collections.emptyList();
}
// scan all directories in the namespace, and only include those that are known to be projects
final Predicate<Path> dFilter = new DirectoryFilter(filter);
try (Stream<Path> stream = Files.list(dir)) {
return stream.filter(dFilter).filter(path -> isProject(path, filter)).map(path -> path.getFileName().toString()).sorted().collect(Collectors.toList());
} catch (IOException e) {
LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
return Collections.emptyList();
}
}
use of org.apache.archiva.common.Try in project archiva by apache.
the class Maven2RepositoryStorage method listNamespaces.
@Override
public Collection<String> listNamespaces(String repoId, String namespace, Filter<String> filter) throws RepositoryStorageRuntimeException {
Path dir = pathTranslator.toFile(getRepositoryBasedir(repoId), namespace);
if (!(Files.exists(dir) && Files.isDirectory(dir))) {
return Collections.emptyList();
}
// scan all the directories which are potential namespaces. Any directories known to be projects are excluded
Predicate<Path> dFilter = new DirectoryFilter(filter);
try (Stream<Path> stream = Files.list(dir)) {
return stream.filter(dFilter).filter(path -> !isProject(path, filter)).map(path -> path.getFileName().toString()).sorted().collect(Collectors.toList());
} catch (IOException e) {
LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
return Collections.emptyList();
}
}
Aggregations