use of org.apache.archiva.repository.content.ContentAccessException in project archiva by apache.
the class ManagedDefaultRepositoryContent method deleteItem.
/**
* Removes the item from the filesystem. For namespaces, projects and versions it deletes
* recursively.
* For namespaces you have to be careful, because maven repositories may have sub namespaces
* parallel to projects. Which means deleting a namespaces also deletes the sub namespaces and
* not only the projects of the given namespace. Better run the delete for each project of
* a namespace.
* <p>
* Artifacts are deleted as provided. No related artifacts will be deleted.
*
* @param item the item that should be removed
* @throws ItemNotFoundException if the item does not exist
* @throws ContentAccessException if some error occurred while accessing the filesystem
*/
@Override
public void deleteItem(ContentItem item) throws ItemNotFoundException, ContentAccessException {
final Path baseDirectory = getRepoDir();
final Path itemPath = item.getAsset().getFilePath();
if (!Files.exists(itemPath)) {
throw new ItemNotFoundException("The item " + item.toString() + "does not exist in the repository " + getId());
}
if (!itemPath.toAbsolutePath().startsWith(baseDirectory.toAbsolutePath())) {
log.error("The namespace {} to delete from repository {} is not a subdirectory of the repository base.", item, getId());
log.error("Namespace directory: {}", itemPath);
log.error("Repository directory: {}", baseDirectory);
throw new ContentAccessException("Inconsistent directories found. Could not delete namespace.");
}
try {
if (Files.isDirectory(itemPath)) {
FileUtils.deleteDirectory(itemPath);
} else {
Files.deleteIfExists(itemPath);
}
} catch (IOException e) {
log.error("Could not delete item from path {}: {}", itemPath, e.getMessage(), e);
throw new ContentAccessException("Error occured while deleting item " + item + ": " + e.getMessage(), e);
}
}
use of org.apache.archiva.repository.content.ContentAccessException in project archiva by apache.
the class CleanupReleasedSnapshotsRepositoryPurge method process.
@Override
public void process(String path) throws RepositoryPurgeException {
try {
StorageAsset artifactFile = repository.getRepository().getRoot().resolve(path);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
if (!artifactFile.exists()) {
// Nothing to do here, file doesn't exist, skip it.
return;
}
Artifact artifactRef = layout.getArtifact(path);
if (!VersionUtil.isSnapshot(artifactRef.getVersion().getId())) {
// Nothing to do here, not a snapshot, skip it.
return;
}
ItemSelector projectSelector = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).build();
// 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)) {
BaseRepositoryContentLayout repoContent = repo.getContent().getLayout(BaseRepositoryContentLayout.class);
Project proj = repoContent.getProject(projectSelector);
for (Version version : repoContent.getVersions(proj)) {
if (!VersionUtil.isSnapshot(version.getId())) {
releasedVersions.add(version.getId());
}
}
}
}
Collections.sort(releasedVersions, VersionComparator.getInstance());
// Now clean out any version that is earlier than the highest released version.
boolean needsMetadataUpdate = false;
ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).withArtifactId(artifactRef.getId());
MetadataRepository metadataRepository = repositorySession.getRepository();
if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion().getId()))) {
ArchivaItemSelector selector = versionSelectorBuilder.withVersion(artifactRef.getVersion().getId()).build();
Version version = layout.getVersion(selector);
if (version.exists()) {
repository.deleteItem(version);
}
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId(), artifactFile.getName());
}
metadataRepository.removeProjectVersion(repositorySession, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId());
needsMetadataUpdate = true;
}
if (needsMetadataUpdate) {
updateMetadata(artifactRef);
}
} catch (LayoutException e) {
log.debug("Not processing file that is not an artifact: {}", e.getMessage());
} catch (MetadataRepositoryException e) {
log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
} catch (ContentAccessException e) {
e.printStackTrace();
} catch (ItemNotFoundException e) {
log.error("Could not find item to delete {}", e.getMessage(), e);
}
}
use of org.apache.archiva.repository.content.ContentAccessException in project archiva by apache.
the class RetentionCountRepositoryPurge method process.
@Override
public void process(String path) throws RepositoryPurgeException {
try {
ContentItem item = repository.toItem(path);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
Artifact artifact = layout.adaptItem(Artifact.class, item);
if (!artifact.exists()) {
return;
}
if (VersionUtil.isSnapshot(artifact.getVersion().getId())) {
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getVersion().getProject().getNamespace().getId()).withProjectId(artifact.getVersion().getProject().getId()).withArtifactId(artifact.getId()).withVersion(artifact.getVersion().getId()).withClassifier("*").includeRelatedArtifacts().build();
List<String> versions;
try (Stream<? extends Artifact> stream = repository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
versions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
}
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;
}
ArchivaItemSelector.Builder selectorBuilder = ArchivaItemSelector.builder().withNamespace(artifact.getVersion().getProject().getNamespace().getId()).withProjectId(artifact.getVersion().getProject().getId()).withArtifactId(artifact.getId()).withClassifier("*").includeRelatedArtifacts().withVersion(artifact.getVersion().getId());
int countToPurge = versions.size() - retentionCount;
Set<Artifact> artifactsToDelete = new HashSet<>();
for (String version : versions) {
if (countToPurge-- <= 0) {
break;
}
List<? extends Artifact> delArtifacts = repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(selectorBuilder.withArtifactVersion(version).build());
if (delArtifacts != null && delArtifacts.size() > 0) {
artifactsToDelete.addAll(delArtifacts);
}
}
purge(artifactsToDelete);
}
} catch (LayoutException le) {
throw new RepositoryPurgeException(le.getMessage(), le);
} catch (ContentAccessException e) {
log.error("Error while accessing the repository data: {}", e.getMessage(), e);
throw new RepositoryPurgeException(e.getMessage(), e);
}
}
use of org.apache.archiva.repository.content.ContentAccessException 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<Artifact> references) {
if (references != null && !references.isEmpty()) {
MetadataRepository metadataRepository = repositorySession.getRepository();
Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
for (Artifact reference : references) {
String baseVersion = reference.getVersion().getId();
String namespace = reference.getVersion().getProject().getNamespace().getId();
// Needed for tracking in the hashmap
String metaBaseId = reference.toKey();
if (!metaResolved.containsKey(metaBaseId)) {
try {
metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repositorySession, repository.getId(), namespace, reference.getId(), baseVersion));
} catch (MetadataResolutionException e) {
log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
}
}
StorageAsset artifactFile = reference.getAsset();
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), namespace, reference.getId(), reference.getVersion().getId(), artifactFile.getName());
}
if (reference.exists()) {
try {
repository.deleteItem(reference);
} catch (ContentAccessException e) {
log.error("Error while trying to delete artifact {}: {}", reference.toString(), e.getMessage(), e);
} catch (ItemNotFoundException e) {
log.error("Asset deleted from background other thread: {}", e.getMessage());
}
}
boolean snapshotVersion = VersionUtil.isSnapshot(baseVersion);
// 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.getArtifactVersion())) {
ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
if (StringUtils.isNotBlank(reference.getClassifier())) {
info.setClassifier(reference.getClassifier());
} else {
info.setClassifier("");
}
metaRemovalList.put(info, artifactMetadata);
}
}
}
} else // otherwise we delete the artifact version
{
ArtifactInfo info = new ArtifactInfo(namespace, reference.getId(), baseVersion, reference.getArtifactVersion());
for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
metaRemovalList.put(info, metadata);
}
}
triggerAuditEvent(repository.getRepository().getId(), reference.toKey(), AuditEvent.PURGE_ARTIFACT);
// purgeSupportFiles( artifactFile );
}
purgeMetadata(metadataRepository, metaRemovalList);
try {
repositorySession.save();
} catch (org.apache.archiva.metadata.repository.MetadataSessionException e) {
e.printStackTrace();
}
}
}
use of org.apache.archiva.repository.content.ContentAccessException in project archiva by apache.
the class ManagedDefaultRepositoryContent method deleteAllItems.
@Override
public void deleteAllItems(ItemSelector selector, Consumer<ItemDeleteStatus> consumer) throws ContentAccessException, IllegalArgumentException {
try (Stream<? extends ContentItem> stream = newItemStream(selector, false)) {
stream.forEach(item -> {
try {
deleteItem(item);
consumer.accept(new ItemDeleteStatus(item));
} catch (ItemNotFoundException e) {
consumer.accept(new ItemDeleteStatus(item, ItemDeleteStatus.ITEM_NOT_FOUND, e));
} catch (Exception e) {
consumer.accept(new ItemDeleteStatus(item, ItemDeleteStatus.DELETION_FAILED, e));
} catch (Throwable e) {
consumer.accept(new ItemDeleteStatus(item, ItemDeleteStatus.UNKNOWN, e));
}
});
}
}
Aggregations