Search in sources :

Example 6 with ContentItem

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);
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Artifact(org.apache.archiva.repository.content.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ContentItem(org.apache.archiva.repository.content.ContentItem) HashSet(java.util.HashSet) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Example 7 with ContentItem

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);
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) Version(org.apache.archiva.repository.content.Version) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Test(org.junit.Test)

Example 8 with ContentItem

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")));
}
Also used : Path(java.nio.file.Path) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) MavenManagedRepository(org.apache.archiva.maven.repository.MavenManagedRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Namespace(org.apache.archiva.repository.content.Namespace) Test(org.junit.Test)

Example 9 with ContentItem

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());
}
Also used : Project(org.apache.archiva.repository.content.Project) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) Version(org.apache.archiva.repository.content.Version) Namespace(org.apache.archiva.repository.content.Namespace) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Test(org.junit.Test)

Example 10 with ContentItem

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);
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Namespace(org.apache.archiva.repository.content.Namespace) Test(org.junit.Test)

Aggregations

ContentItem (org.apache.archiva.repository.content.ContentItem)29 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)16 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)15 Test (org.junit.Test)14 Path (java.nio.file.Path)12 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)12 Artifact (org.apache.archiva.repository.content.Artifact)10 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)10 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)9 Project (org.apache.archiva.repository.content.Project)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 LayoutException (org.apache.archiva.repository.content.LayoutException)8 Version (org.apache.archiva.repository.content.Version)8 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)7 ItemSelector (org.apache.archiva.repository.content.ItemSelector)7 MavenManagedRepository (org.apache.archiva.maven.repository.MavenManagedRepository)6 DataItem (org.apache.archiva.repository.content.DataItem)6 Namespace (org.apache.archiva.repository.content.Namespace)6 IOException (java.io.IOException)5 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)5