use of org.apache.archiva.repository.content.ItemNotFoundException 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.ItemNotFoundException in project archiva by apache.
the class DefaultRepositoriesService method deleteProject.
@Override
public ActionStatus deleteProject(String groupId, String projectId, String repositoryId) throws ArchivaRestServiceException {
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
}
if (StringUtils.isEmpty(groupId)) {
throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(projectId)) {
throw new ArchivaRestServiceException("artifactId cannot be null", 400, null);
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(projectId).build();
ContentItem item = repository.getItem(itemSelector);
repository.deleteItem(item);
} catch (ContentNotFoundException e) {
log.warn("skip ContentNotFoundException: {}", e.getMessage());
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (ItemNotFoundException e) {
log.error("Item not found {}", e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
}
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeProject(repositorySession, repositoryId, groupId, projectId);
repositorySession.save();
} catch (MetadataRepositoryException | MetadataSessionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.content.ItemNotFoundException 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.ItemNotFoundException in project archiva by apache.
the class DefaultRepositoriesService method deleteArtifact.
@Override
public ActionStatus deleteArtifact(Artifact artifact) throws ArchivaRestServiceException {
String repositoryId = artifact.getContext();
// so try both!!
if (StringUtils.isEmpty(repositoryId)) {
repositoryId = artifact.getRepositoryId();
}
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
}
if (artifact == null) {
throw new ArchivaRestServiceException("artifact cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getGroupId())) {
throw new ArchivaRestServiceException("artifact.groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getArtifactId())) {
throw new ArchivaRestServiceException("artifact.artifactId cannot be null", 400, null);
}
// TODO more control on artifact fields
boolean snapshotVersion = VersionUtil.isSnapshot(artifact.getVersion()) | VersionUtil.isGenericSnapshot(artifact.getVersion());
String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
TimeZone timezone = TimeZone.getTimeZone("UTC");
DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
fmt.setTimeZone(timezone);
ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).build();
Version version1 = layout.getVersion(versionSelector);
String path = repository.toPath(version1);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).withClassifier(artifact.getClassifier()).withArtifactId(artifact.getArtifactId()).withType(artifact.getType()).includeRelatedArtifacts().build();
MetadataRepository metadataRepository = repositorySession.getRepository();
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = layout.getArtifacts(selector);
for (org.apache.archiva.repository.content.Artifact aRef : artifactItems) {
try {
repository.deleteItem(aRef);
} catch (ItemNotFoundException e) {
log.error("Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage());
}
}
} else {
int index = path.lastIndexOf('/');
path = path.substring(0, index);
StorageAsset targetPath = repo.getAsset(path);
if (!targetPath.exists()) {
// throw new ContentNotFoundException(
// artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
log.warn("targetPath {} not found skip file deletion", targetPath);
return ActionStatus.FAIL;
}
// delete from file system
if (!snapshotVersion && version1.exists()) {
try {
repository.deleteItem(version1);
} catch (ItemNotFoundException e) {
log.error("Could not delete version item {}", e.getMessage());
}
} else {
// We are deleting all version related artifacts for a snapshot version
for (org.apache.archiva.repository.content.Artifact delArtifact : layout.getArtifacts(selector)) {
try {
repository.deleteItem(delArtifact);
} catch (ItemNotFoundException e) {
log.warn("Artifact that should be deleted, was not found: {}", delArtifact);
}
}
StorageAsset metadataFile = getMetadata(repo, targetPath.getPath());
ArchivaRepositoryMetadata metadata = getMetadata(repository.getRepository().getType(), metadataFile);
updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
}
}
Collection<ArtifactMetadata> artifacts = Collections.emptyList();
if (snapshotVersion) {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
} else {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
log.debug("artifacts: {}", artifacts);
if (artifacts.isEmpty()) {
if (!snapshotVersion) {
// verify metata repository doesn't contains anymore the version
Collection<String> projectVersions = metadataRepository.getProjectVersions(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId());
if (projectVersions.contains(artifact.getVersion())) {
log.warn("artifact not found when deleted but version still here ! so force cleanup");
metadataRepository.removeProjectVersion(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
}
}
for (ArtifactMetadata artifactMetadata : artifacts) {
// TODO: mismatch between artifact (snapshot) version and project (base) version here
if (artifactMetadata.getVersion().equals(artifact.getVersion())) {
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
// cleanup facet which contains classifier information
MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
if (StringUtils.equals(artifact.getClassifier(), mavenArtifactFacet.getClassifier())) {
artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version = artifact.getVersion();
MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
mavenArtifactFacetToCompare.setClassifier(artifact.getClassifier());
metadataRepository.removeFacetFromArtifact(repositorySession, repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
repositorySession.save();
}
} else {
if (snapshotVersion) {
metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
} else {
metadataRepository.removeArtifact(repositorySession, artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifact.getVersion(), artifactMetadata.getId());
}
}
// repository metadata to an artifact
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), artifactMetadata.getId());
}
triggerAuditEvent(repositoryId, path, AuditEvent.REMOVE_FILE);
}
}
} catch (ContentNotFoundException e) {
throw new ArchivaRestServiceException("Artifact does not exist: " + e.getMessage(), 400, e);
} catch (RepositoryNotFoundException e) {
throw new ArchivaRestServiceException("Target repository cannot be found: " + e.getMessage(), 400, e);
} catch (RepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (MetadataResolutionException | MetadataSessionException | MetadataRepositoryException | LayoutException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
try {
repositorySession.save();
} catch (MetadataSessionException e) {
log.error("Could not save sesion {}", e.getMessage());
}
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.content.ItemNotFoundException 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();
}
}
}
Aggregations