Search in sources :

Example 1 with ContentNotFoundException

use of org.apache.archiva.repository.ContentNotFoundException 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();
}
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) IOException(java.io.IOException) Files(java.nio.file.Files)

Example 2 with ContentNotFoundException

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

the class CleanupReleasedSnapshotsRepositoryPurge method updateMetadata.

/*
     * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
     * -> not sure what needs to be changed here.
     */
@SuppressWarnings("deprecation")
private void updateMetadata(ArtifactReference artifact) {
    VersionedReference versionRef = new VersionedReference();
    versionRef.setGroupId(artifact.getGroupId());
    versionRef.setArtifactId(artifact.getArtifactId());
    versionRef.setVersion(artifact.getVersion());
    ProjectReference projectRef = new ProjectReference();
    projectRef.setGroupId(artifact.getGroupId());
    projectRef.setArtifactId(artifact.getArtifactId());
    try {
        metadataTools.updateMetadata(repository, versionRef);
    } catch (ContentNotFoundException e) {
    // Ignore. (Just means we have no snapshot versions left to reference).
    } catch (RepositoryMetadataException e) {
    // Ignore.
    } catch (IOException e) {
    // Ignore.
    } catch (LayoutException e) {
    // Ignore.
    }
    try {
        metadataTools.updateMetadata(repository, projectRef);
    } catch (ContentNotFoundException e) {
    // Ignore. (Just means we have no snapshot versions left to reference).
    } catch (RepositoryMetadataException e) {
    // Ignore.
    } catch (IOException e) {
    // Ignore.
    } catch (LayoutException e) {
    // Ignore.
    }
}
Also used : ProjectReference(org.apache.archiva.model.ProjectReference) VersionedReference(org.apache.archiva.model.VersionedReference) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) LayoutException(org.apache.archiva.repository.LayoutException) IOException(java.io.IOException)

Example 3 with ContentNotFoundException

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

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        ArtifactReference artifactRef = repository.toArtifactReference(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ProjectReference reference = new ProjectReference();
        reference.setGroupId(artifactRef.getGroupId());
        reference.setArtifactId(artifactRef.getArtifactId());
        // Gether the released versions
        List<String> releasedVersions = new ArrayList<>();
        Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
        for (org.apache.archiva.repository.ManagedRepository repo : repos) {
            if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
                try {
                    ManagedRepositoryContent repoContent = repo.getContent();
                    for (String version : repoContent.getVersions(reference)) {
                        if (!VersionUtil.isSnapshot(version)) {
                            releasedVersions.add(version);
                        }
                    }
                } catch (RepositoryException e) {
                // swallow
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        VersionedReference versionRef = new VersionedReference();
        versionRef.setGroupId(artifactRef.getGroupId());
        versionRef.setArtifactId(artifactRef.getArtifactId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion()))) {
            versionRef.setVersion(artifactRef.getVersion());
            repository.deleteVersion(versionRef);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), artifactFile.getFileName().toString());
            }
            metadataRepository.removeProjectVersion(repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (ContentNotFoundException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ProjectReference(org.apache.archiva.model.ProjectReference) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 4 with ContentNotFoundException

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

the class RetentionCountRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            return;
        }
        ArtifactReference artifact = repository.toArtifactReference(path);
        if (VersionUtil.isSnapshot(artifact.getVersion())) {
            VersionedReference reference = new VersionedReference();
            reference.setGroupId(artifact.getGroupId());
            reference.setArtifactId(artifact.getArtifactId());
            reference.setVersion(artifact.getVersion());
            List<String> versions = new ArrayList<>(repository.getVersions(reference));
            Collections.sort(versions, VersionComparator.getInstance());
            if (retentionCount > versions.size()) {
                log.trace("No deletion, because retention count is higher than actual number of artifacts.");
                // Done. nothing to do here. skip it.
                return;
            }
            int countToPurge = versions.size() - retentionCount;
            Set<ArtifactReference> artifactsToDelete = new HashSet<>();
            for (String version : versions) {
                if (countToPurge-- <= 0) {
                    break;
                }
                artifactsToDelete.addAll(repository.getRelatedArtifacts(getNewArtifactReference(artifact, version)));
            }
            purge(artifactsToDelete);
        }
    } catch (LayoutException le) {
        throw new RepositoryPurgeException(le.getMessage(), le);
    } catch (ContentNotFoundException e) {
        log.error("Repostory artifact not found {}", path);
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) ArtifactReference(org.apache.archiva.model.ArtifactReference) HashSet(java.util.HashSet)

Example 5 with ContentNotFoundException

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

the class AbstractRepositoryPurge method purge.

/**
 * Purge the repo. Update db and index of removed artifacts.
 *
 * @param references
 */
protected void purge(Set<ArtifactReference> references) {
    if (references != null && !references.isEmpty()) {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
        Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
        for (ArtifactReference reference : references) {
            String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
            // Needed for tracking in the hashmap
            String metaBaseId = reference.getGroupId() + "/" + reference.getArtifactId() + "/" + baseVersion;
            if (!metaResolved.containsKey(metaBaseId)) {
                try {
                    metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repository.getId(), reference.getGroupId(), reference.getArtifactId(), baseVersion));
                } catch (MetadataResolutionException e) {
                    log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
                }
            }
            Path artifactFile = repository.toFile(reference);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), artifactFile.getFileName().toString());
            }
            try {
                Files.delete(artifactFile);
                log.debug("File deleted: {}", artifactFile.toAbsolutePath());
            } catch (IOException e) {
                log.error("Could not delete file {}: {}", artifactFile.toAbsolutePath(), e.getMessage(), e);
                continue;
            }
            try {
                repository.deleteArtifact(reference);
            } catch (ContentNotFoundException e) {
                log.warn("skip error deleting artifact {}: {}", reference, e.getMessage());
            }
            boolean snapshotVersion = VersionUtil.isSnapshot(reference.getVersion());
            // If this is a snapshot we have to search for artifacts with the same version. And remove all of them.
            if (snapshotVersion) {
                Collection<ArtifactMetadata> artifacts = metaResolved.get(metaBaseId);
                if (artifacts != null) {
                    // cleanup snapshots metadata
                    for (ArtifactMetadata artifactMetadata : artifacts) {
                        // Artifact metadata and reference version should match.
                        if (artifactMetadata.getVersion().equals(reference.getVersion())) {
                            ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
                            if (StringUtils.isNotBlank(reference.getClassifier())) {
                                info.setClassifier(reference.getClassifier());
                                metaRemovalList.put(info, artifactMetadata);
                            } else {
                                // metadataRepository.removeArtifact( artifactMetadata, baseVersion );
                                metaRemovalList.put(info, artifactMetadata);
                            }
                        }
                    }
                }
            } else // otherwise we delete the artifact version
            {
                ArtifactInfo info = new ArtifactInfo(reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion());
                for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
                    metaRemovalList.put(info, metadata);
                }
            }
            triggerAuditEvent(repository.getRepository().getId(), ArtifactReference.toKey(reference), AuditEvent.PURGE_ARTIFACT);
            purgeSupportFiles(artifactFile);
        }
        purgeMetadata(metadataRepository, metaRemovalList);
        repositorySession.save();
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) HashMap(java.util.HashMap) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) IOException(java.io.IOException) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Collection(java.util.Collection) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) 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