use of java.nio.file.Files 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 java.nio.file.Files in project archiva by apache.
the class FileMetadataRepository method getProjects.
@Override
public Collection<String> getProjects(String repoId, String namespace) throws MetadataResolutionException {
try {
List<String> projects;
Path directory = getDirectory(repoId).resolve(namespace);
if (!(Files.exists(directory) && Files.isDirectory(directory))) {
return Collections.emptyList();
}
final String searchFile = PROJECT_METADATA_KEY + ".properties";
try (Stream<Path> fs = Files.list(directory)) {
projects = fs.filter(Files::isDirectory).filter(path -> Files.exists(path.resolve(searchFile))).map(path -> path.getFileName().toString()).collect(Collectors.toList());
}
return projects;
} catch (IOException e) {
throw new MetadataResolutionException(e.getMessage(), e);
}
}
use of java.nio.file.Files in project archiva by apache.
the class FileMetadataRepository method getMetadataFacets.
@Override
public List<String> getMetadataFacets(String repoId, String facetId) throws MetadataRepositoryException {
try {
Path directory = getMetadataDirectory(repoId, facetId);
if (!(Files.exists(directory) && Files.isDirectory(directory))) {
return Collections.emptyList();
}
List<String> facets;
final String searchFile = METADATA_KEY + ".properties";
try (Stream<Path> fs = Files.walk(directory, FileVisitOption.FOLLOW_LINKS)) {
facets = fs.filter(Files::isDirectory).filter(path -> Files.exists(path.resolve(searchFile))).map(path -> directory.relativize(path).toString()).collect(Collectors.toList());
}
return facets;
} catch (IOException e) {
throw new MetadataRepositoryException(e.getMessage(), e);
}
}
use of java.nio.file.Files in project archiva by apache.
the class ManagedDefaultRepositoryContent method getVersions.
/**
* Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
* information.
*
* @return the Set of available versions, based on the project reference.
* @throws LayoutException
* @throws LayoutException
*/
@Override
public Set<String> getVersions(ProjectReference reference) throws ContentNotFoundException, LayoutException {
String path = toMetadataPath(reference);
int idx = path.lastIndexOf('/');
if (idx > 0) {
path = path.substring(0, idx);
}
Path repoDir = PathUtil.getPathFromUri(repository.getLocation()).resolve(path);
if (!Files.exists(repoDir)) {
throw new ContentNotFoundException("Unable to get Versions on a non-existant directory: " + repoDir.toAbsolutePath());
}
if (!Files.isDirectory(repoDir)) {
throw new ContentNotFoundException("Unable to get Versions on a non-directory: " + repoDir.toAbsolutePath());
}
final String groupId = reference.getGroupId();
final String artifactId = reference.getArtifactId();
try (Stream<Path> stream = Files.list(repoDir)) {
return stream.filter(Files::isDirectory).map(p -> newVersionedRef(groupId, artifactId, p.getFileName().toString())).filter(this::hasArtifact).map(ref -> ref.getVersion()).collect(Collectors.toSet());
} catch (IOException e) {
log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
} catch (RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof LayoutException) {
throw (LayoutException) e.getCause();
} else {
throw e;
}
}
return Collections.emptySet();
}
use of java.nio.file.Files in project archiva by apache.
the class MetadataTools method getFirstArtifact.
/**
* Get the first Artifact found in the provided VersionedReference location.
*
* @param managedRepository the repository to search within.
* @param reference the reference to the versioned reference to search within
* @return the ArtifactReference to the first artifact located within the versioned reference. or null if
* no artifact was found within the versioned reference.
* @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
* @throws LayoutException
*/
public ArtifactReference getFirstArtifact(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, IOException {
String path = toPath(reference);
int idx = path.lastIndexOf('/');
if (idx > 0) {
path = path.substring(0, idx);
}
Path repoDir = Paths.get(managedRepository.getRepoRoot(), path);
if (!Files.exists(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-existant directory: " + repoDir.toAbsolutePath());
}
if (!Files.isDirectory(repoDir)) {
throw new IOException("Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath());
}
try (Stream<Path> stream = Files.list(repoDir)) {
String result = stream.filter(Files::isRegularFile).map(path1 -> PathUtil.getRelative(managedRepository.getRepoRoot(), path1)).filter(filetypes::matchesArtifactPattern).findFirst().orElse(null);
if (result != null) {
return managedRepository.toArtifactReference(result);
}
}
// No artifact was found.
return null;
}
Aggregations