Search in sources :

Example 11 with ContentNotFoundException

use of org.apache.archiva.repository.ContentNotFoundException 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)

Example 12 with ContentNotFoundException

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

the class ManagedDefaultRepositoryContent method deleteProject.

@Override
public void deleteProject(String namespace, String projectId) throws RepositoryException {
    ArtifactReference artifactReference = new ArtifactReference();
    artifactReference.setGroupId(namespace);
    artifactReference.setArtifactId(projectId);
    String path = toPath(artifactReference);
    Path directory = Paths.get(getRepoRoot(), path);
    if (!Files.exists(directory)) {
        throw new ContentNotFoundException("cannot found project " + namespace + ":" + projectId);
    }
    if (Files.isDirectory(directory)) {
        try {
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            throw new RepositoryException(e.getMessage(), e);
        }
    } else {
        log.warn("project {}:{} is not a directory", namespace, projectId);
    }
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) IOException(java.io.IOException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)12 Path (java.nio.file.Path)10 ArtifactReference (org.apache.archiva.model.ArtifactReference)9 VersionedReference (org.apache.archiva.model.VersionedReference)8 IOException (java.io.IOException)7 LayoutException (org.apache.archiva.repository.LayoutException)7 RepositoryException (org.apache.archiva.repository.RepositoryException)7 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)6 HashSet (java.util.HashSet)5 ProjectReference (org.apache.archiva.model.ProjectReference)5 ArrayList (java.util.ArrayList)4 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)4 URI (java.net.URI)3 Files (java.nio.file.Files)3 Paths (java.nio.file.Paths)3 Collections (java.util.Collections)3 List (java.util.List)3 Objects (java.util.Objects)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3