use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class MetadataToolsTest method assertSnapshotVersions.
private void assertSnapshotVersions(String artifactId, String version, String[] expectedVersions) throws Exception {
Path repoRootDir = Paths.get("src/test/repositories/metadata-repository");
VersionedReference reference = new VersionedReference();
reference.setGroupId("org.apache.archiva.metadata.tests");
reference.setArtifactId(artifactId);
reference.setVersion(version);
MavenManagedRepository repo = createRepository("test-repo", "Test Repository: " + name.getMethodName(), repoRootDir);
RepositoryContentProvider provider = applicationContext.getBean("repositoryContentProvider#maven", RepositoryContentProvider.class);
ManagedRepositoryContent repoContent = provider.createManagedContent(repo);
Set<String> testedVersionSet = tools.gatherSnapshotVersions(repoContent, reference);
// Sort the list (for asserts)
List<String> testedVersions = new ArrayList<>();
testedVersions.addAll(testedVersionSet);
Collections.sort(testedVersions, new VersionComparator());
// Test the expected array of versions, to the actual tested versions
assertEquals("Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size());
for (int i = 0; i < expectedVersions.length; i++) {
String actualVersion = testedVersions.get(i);
assertEquals("Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion);
}
}
use of org.apache.archiva.repository.ManagedRepositoryContent 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);
}
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class RepositoryPurgeConsumer method beginScan.
@Override
public void beginScan(ManagedRepository repository, Date whenGathered) throws ConsumerException {
ManagedRepositoryContent repositoryContent;
repositoryContent = repository.getContent();
repositorySession = repositorySessionFactory.createSession();
if (repository.supportsFeature(ArtifactCleanupFeature.class)) {
ArtifactCleanupFeature acf = repository.getFeature(ArtifactCleanupFeature.class).get();
int retentionPeriodInDays = acf.getRetentionPeriod().getDays();
int retentionCount = acf.getRetentionCount();
if (retentionPeriodInDays != 0) {
repoPurge = new DaysOldRepositoryPurge(repositoryContent, retentionPeriodInDays, retentionCount, repositorySession, listeners);
} else {
repoPurge = new RetentionCountRepositoryPurge(repositoryContent, retentionCount, repositorySession, listeners);
}
deleteReleasedSnapshots = acf.isDeleteReleasedSnapshots();
} else {
throw new ConsumerException("The repository does not support the ArtifactCleanup feature " + repository.getId());
}
cleanUp = new CleanupReleasedSnapshotsRepositoryPurge(repositoryContent, metadataTools, repositoryRegistry, repositorySession, listeners);
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class SimpleArtifactConsumer method processFile.
public void processFile(String path, boolean executeOnEntireRepo) throws ConsumerException {
log.info("Processing entry [{}] from repository [{}]", path, this.repository.getId());
try {
ManagedRepositoryContent repositoryContent = repository.getContent();
ArtifactReference artifact = repositoryContent.toArtifactReference(path);
repositorySession.getRepository().getArtifacts(repository.getId(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
} catch (LayoutException | MetadataResolutionException e) {
throw new ConsumerException(e.getLocalizedMessage(), e);
}
}
use of org.apache.archiva.repository.ManagedRepositoryContent in project archiva by apache.
the class DefaultRepositoriesService method deleteProject.
@Override
public Boolean deleteProject(String groupId, String projectId, String repositoryId) throws ArchivaRestServiceException {
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 (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 = repositorySessionFactory.createSession();
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
repository.deleteProject(groupId, projectId);
} 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);
}
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeProject(repositoryId, groupId, projectId);
metadataRepository.save();
} catch (MetadataRepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.close();
}
return true;
}
Aggregations