use of org.apache.archiva.repository.events.RepositoryListener 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();
}
}
use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.
the class DefaultRepositoriesService method deleteArtifact.
@Override
public Boolean 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 (!isAuthorizedToDeleteArtifacts(repositoryId)) {
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());
RepositorySession repositorySession = repositorySessionFactory.createSession();
try {
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
TimeZone timezone = TimeZone.getTimeZone("UTC");
DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
fmt.setTimeZone(timezone);
ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository(repositoryId);
VersionedReference ref = new VersionedReference();
ref.setArtifactId(artifact.getArtifactId());
ref.setGroupId(artifact.getGroupId());
ref.setVersion(artifact.getVersion());
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setArtifactId(artifact.getArtifactId());
artifactReference.setGroupId(artifact.getGroupId());
artifactReference.setVersion(artifact.getVersion());
artifactReference.setClassifier(artifact.getClassifier());
artifactReference.setType(artifact.getPackaging());
MetadataRepository metadataRepository = repositorySession.getRepository();
String path = repository.toMetadataPath(ref);
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
repository.deleteArtifact(artifactReference);
} else {
int index = path.lastIndexOf('/');
path = path.substring(0, index);
Path targetPath = Paths.get(repoConfig.getLocation(), path);
if (!Files.exists(targetPath)) {
// throw new ContentNotFoundException(
// artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
log.warn("targetPath {} not found skip file deletion", targetPath);
}
// delete from file system
if (!snapshotVersion) {
repository.deleteVersion(ref);
} else {
Set<ArtifactReference> related = repository.getRelatedArtifacts(artifactReference);
log.debug("related: {}", related);
for (ArtifactReference artifactRef : related) {
repository.deleteArtifact(artifactRef);
}
}
Path metadataFile = getMetadata(targetPath.toAbsolutePath().toString());
ArchivaRepositoryMetadata metadata = getMetadata(metadataFile);
updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
}
Collection<ArtifactMetadata> artifacts = Collections.emptyList();
if (snapshotVersion) {
String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
artifacts = metadataRepository.getArtifacts(repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
} else {
artifacts = metadataRepository.getArtifacts(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(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(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.removeArtifact(repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
metadataRepository.save();
}
} else {
if (snapshotVersion) {
metadataRepository.removeArtifact(artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
} else {
metadataRepository.removeArtifact(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 e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (MetadataRepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (RepositoryAdminException e) {
throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.save();
repositorySession.close();
}
return Boolean.TRUE;
}
use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.
the class DefaultMetadataResolver method resolveProjectVersion.
@Override
public ProjectVersionMetadata resolveProjectVersion(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion) throws MetadataResolutionException {
MetadataRepository metadataRepository = session.getRepository();
ProjectVersionMetadata metadata = metadataRepository.getProjectVersion(repoId, namespace, projectId, projectVersion);
// may then work here and be more efficient than always trying again)
if (metadata == null || metadata.isIncomplete()) {
try {
ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion).browsingRequest(true);
metadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
log.debug("Resolved project version metadata from storage: {}", metadata);
// eg. maven projects should be able to process parent here
if (!metadata.getDependencies().isEmpty()) {
ProjectVersionReference ref = new ProjectVersionReference();
ref.setNamespace(namespace);
ref.setProjectId(projectId);
ref.setProjectVersion(projectVersion);
ref.setReferenceType(ProjectVersionReference.ReferenceType.DEPENDENCY);
}
try {
for (RepositoryListener listener : listeners) {
listener.addArtifact(session, repoId, namespace, projectId, metadata);
}
metadataRepository.updateProjectVersion(repoId, namespace, projectId, metadata);
} catch (MetadataRepositoryException e) {
log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
}
session.markDirty();
} catch (RepositoryStorageMetadataInvalidException e) {
for (RepositoryListener listener : listeners) {
listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
}
throw new MetadataResolutionException(e.getMessage(), e);
} catch (RepositoryStorageMetadataNotFoundException e) {
for (RepositoryListener listener : listeners) {
listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
}
// no need to rethrow - return null
} catch (RepositoryStorageRuntimeException e) {
for (RepositoryListener listener : listeners) {
listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
}
throw new MetadataResolutionException(e.getMessage(), e);
}
}
return metadata;
}
use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.
the class DefaultMetadataResolver method resolveProjectVersions.
@Override
public Collection<String> resolveProjectVersions(RepositorySession session, String repoId, String namespace, String projectId) throws MetadataResolutionException {
try {
MetadataRepository metadataRepository = session.getRepository();
Collection<String> projectVersions = metadataRepository.getProjectVersions(repoId, namespace, projectId);
Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions(repoId, namespace, projectId, new ExcludesFilter<String>(projectVersions));
if (storageProjectVersions != null && !storageProjectVersions.isEmpty()) {
log.debug("Resolved project versions from storage: {}", storageProjectVersions);
for (String projectVersion : storageProjectVersions) {
try {
ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion);
ProjectVersionMetadata versionMetadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
for (RepositoryListener listener : listeners) {
listener.addArtifact(session, repoId, namespace, projectId, versionMetadata);
}
metadataRepository.updateProjectVersion(repoId, namespace, projectId, versionMetadata);
} catch (MetadataRepositoryException e) {
log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
} catch (RepositoryStorageMetadataInvalidException e) {
log.warn("Not update project in metadata repository due to an error resolving it from storage: {}", e.getMessage());
for (RepositoryListener listener : listeners) {
listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
}
} catch (RepositoryStorageMetadataNotFoundException e) {
for (RepositoryListener listener : listeners) {
listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
}
}
}
session.markDirty();
projectVersions = new ArrayList<>(projectVersions);
projectVersions.addAll(storageProjectVersions);
}
return projectVersions;
} catch (RepositoryStorageRuntimeException e) {
throw new MetadataResolutionException(e.getMessage(), e);
}
}
use of org.apache.archiva.repository.events.RepositoryListener in project archiva by apache.
the class DaysOldRepositoryPurgeTest method testMetadataDrivenSnapshots.
@Test
public void testMetadataDrivenSnapshots() throws Exception {
org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME);
ArtifactCleanupFeature atf = repoConfiguration.getFeature(ArtifactCleanupFeature.class).get();
List<RepositoryListener> listeners = Collections.singletonList(listener);
repoPurge = new DaysOldRepositoryPurge(getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, listeners);
String repoRoot = prepareTestRepos();
String projectNs = "org.codehaus.plexus";
String projectPath = projectNs.replaceAll("\\.", "/");
String projectName = "plexus-utils";
String projectVersion = "1.4.3-SNAPSHOT";
String projectRoot = repoRoot + "/" + projectPath + "/" + projectName;
Path repo = getTestRepoRootPath();
Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
Set<String> deletedVersions = new HashSet<>();
deletedVersions.add("1.4.3-20070113.163208-4");
String versionRoot = projectRoot + "/" + projectVersion;
Calendar currentDate = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
setLastModified(versionRoot, currentDate.getTimeInMillis());
String timestamp = new SimpleDateFormat("yyyyMMdd.HHmmss").format(currentDate.getTime());
for (int i = 5; i <= 7; i++) {
Path jarFile = Paths.get(versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar");
Files.createFile(jarFile);
Path pomFile = Paths.get(versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom");
Files.createFile(pomFile);
// set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
if (i == 5) {
Files.setLastModifiedTime(jarFile, FileTime.fromMillis(OLD_TIMESTAMP));
Files.setLastModifiedTime(pomFile, FileTime.fromMillis(OLD_TIMESTAMP));
}
}
// test listeners for the correct artifacts
String[] exts = { ".sha1", "" };
for (int i = 0; i < exts.length; i++) {
listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils", "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" + exts[i]);
listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils", "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" + exts[i]);
}
listenerControl.replay();
// Provide the metadata list
List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
repoPurge.process(PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT);
listenerControl.verify();
// Verify the metadataRepository invocations
verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
List<ArtifactMetadata> metaL = metadataArg.getAllValues();
for (ArtifactMetadata meta : metaL) {
assertTrue(meta.getId().startsWith(projectName));
assertTrue(deletedVersions.contains(meta.getVersion()));
}
// this should be deleted since the filename version (timestamp) is older than
// 100 days even if the last modified date was <100 days ago
assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar");
assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1");
assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom");
assertDeleted(versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1");
// this should not be deleted because last modified date is <100 days ago
assertExists(versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar");
assertExists(versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom");
for (int i = 5; i <= 7; i++) {
assertExists(versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar");
assertExists(versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom");
}
}
Aggregations