Search in sources :

Example 21 with LayoutException

use of org.apache.archiva.repository.LayoutException in project archiva by apache.

the class ManagedDefaultRepositoryContent method getRelatedArtifacts.

@Override
public Set<ArtifactReference> getRelatedArtifacts(ArtifactReference reference) throws ContentNotFoundException {
    Path artifactFile = toFile(reference);
    Path repoBase = PathUtil.getPathFromUri(repository.getLocation()).toAbsolutePath();
    Path repoDir = artifactFile.getParent().toAbsolutePath();
    if (!Files.exists(repoDir)) {
        throw new ContentNotFoundException("Unable to get related artifacts using a non-existant directory: " + repoDir.toAbsolutePath());
    }
    if (!Files.isDirectory(repoDir)) {
        throw new ContentNotFoundException("Unable to get related artifacts using a non-directory: " + repoDir.toAbsolutePath());
    }
    Set<ArtifactReference> foundArtifacts;
    try (Stream<Path> stream = Files.list(repoDir)) {
        foundArtifacts = stream.filter(Files::isRegularFile).map(path -> {
            try {
                ArtifactReference artifact = toArtifactReference(repoBase.relativize(path).toString());
                if (artifact.getGroupId().equals(reference.getGroupId()) && artifact.getArtifactId().equals(reference.getArtifactId()) && artifact.getVersion().equals(reference.getVersion())) {
                    return artifact;
                } else {
                    return null;
                }
            } catch (LayoutException e) {
                log.debug("Not processing file that is not an artifact: {}", e.getMessage());
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toSet());
    } catch (IOException e) {
        log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
        return Collections.emptySet();
    }
    return foundArtifacts;
}
Also used : Path(java.nio.file.Path) StringUtils(org.apache.commons.lang.StringUtils) PathUtil(org.apache.archiva.common.utils.PathUtil) ArtifactMappingProvider(org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider) DefaultArtifactMappingProvider(org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider) ProjectReference(org.apache.archiva.model.ProjectReference) HashSet(java.util.HashSet) URI(java.net.URI) LayoutException(org.apache.archiva.repository.LayoutException) Path(java.nio.file.Path) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) VersionedReference(org.apache.archiva.model.VersionedReference) Files(java.nio.file.Files) RepositoryException(org.apache.archiva.repository.RepositoryException) Set(java.util.Set) FileTypes(org.apache.archiva.configuration.FileTypes) IOException(java.io.IOException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) ArtifactReference(org.apache.archiva.model.ArtifactReference) Collections(java.util.Collections) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) LayoutException(org.apache.archiva.repository.LayoutException) Objects(java.util.Objects) IOException(java.io.IOException) Files(java.nio.file.Files) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

LayoutException (org.apache.archiva.repository.LayoutException)21 ArtifactReference (org.apache.archiva.model.ArtifactReference)15 Path (java.nio.file.Path)11 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)10 IOException (java.io.IOException)8 VersionedReference (org.apache.archiva.model.VersionedReference)8 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)8 HashSet (java.util.HashSet)6 ProjectReference (org.apache.archiva.model.ProjectReference)6 Files (java.nio.file.Files)4 Paths (java.nio.file.Paths)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 PathUtil (org.apache.archiva.common.utils.PathUtil)4 FileTypes (org.apache.archiva.configuration.FileTypes)4 RepositoryException (org.apache.archiva.repository.RepositoryException)4 StringUtils (org.apache.commons.lang.StringUtils)4