use of org.apache.archiva.repository.content.ContentItem 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.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method testGetItemWithVersionSelector.
@Test
public void testGetItemWithVersionSelector() {
ItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").build();
ContentItem item = repoContent.getItem(selector);
assertNotNull(item);
assertTrue(item instanceof Version);
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method deleteNamespaceItem.
@Test
public void deleteNamespaceItem() throws IOException, URISyntaxException, ItemNotFoundException {
ManagedRepository repo = createManagedRepoWithContent("delete-repository");
ManagedRepositoryContent myRepoContent = repo.getContent();
Path repoRoot = repo.getRoot().getFilePath();
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven")));
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").build();
ContentItem item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/maven")));
assertTrue(Files.exists(repoRoot.resolve("org/apache")));
// Sub namespaces are deleted too
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method testGetArtifactFromContentItem.
@Test
public void testGetArtifactFromContentItem() {
ItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").build();
Namespace ns = repoContent.getNamespace(selector);
List<? extends Artifact> artifacts = repoContent.getArtifacts(ns);
assertNotNull(artifacts);
assertEquals(39, artifacts.size());
List<? extends Artifact> artifacts2 = repoContent.getArtifacts((ContentItem) ns);
assertArrayEquals(artifacts.toArray(), artifacts2.toArray());
selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven.shared").withProjectId("maven-downloader").build();
Project project = repoContent.getProject(selector);
artifacts = repoContent.getArtifacts(project);
assertNotNull(artifacts);
assertEquals(27, artifacts.size());
artifacts2 = repoContent.getArtifacts((ContentItem) project);
assertArrayEquals(artifacts.toArray(), artifacts2.toArray());
selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven.shared").withProjectId("maven-downloader").withVersion("1.1").build();
Version version = repoContent.getVersion(selector);
artifacts = repoContent.getArtifacts(version);
assertNotNull(artifacts);
assertEquals(12, artifacts.size());
artifacts2 = repoContent.getArtifacts((ContentItem) version);
assertArrayEquals(artifacts.toArray(), artifacts2.toArray());
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method testGetItemWithNamespaceSelector.
@Test
public void testGetItemWithNamespaceSelector() {
ItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").build();
ContentItem item = repoContent.getItem(selector);
assertNotNull(item);
assertTrue(item instanceof Namespace);
}
Aggregations