Search in sources :

Example 6 with ManagedRepositoryContent

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);
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) MavenManagedRepository(org.apache.archiva.repository.maven2.MavenManagedRepository) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArrayList(java.util.ArrayList) VersionComparator(org.apache.archiva.common.utils.VersionComparator)

Example 7 with ManagedRepositoryContent

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);
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ProjectReference(org.apache.archiva.model.ProjectReference) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArrayList(java.util.ArrayList) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 8 with ManagedRepositoryContent

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);
}
Also used : ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ConsumerException(org.apache.archiva.consumers.ConsumerException) ArtifactCleanupFeature(org.apache.archiva.repository.features.ArtifactCleanupFeature)

Example 9 with ManagedRepositoryContent

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);
    }
}
Also used : LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ConsumerException(org.apache.archiva.consumers.ConsumerException) ArtifactReference(org.apache.archiva.model.ArtifactReference) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 10 with ManagedRepositoryContent

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;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession)

Aggregations

ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)41 Path (java.nio.file.Path)16 Test (org.junit.Test)13 RepositoryException (org.apache.archiva.repository.RepositoryException)12 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)11 DavException (org.apache.jackrabbit.webdav.DavException)10 ArrayList (java.util.ArrayList)9 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)9 IOException (java.io.IOException)8 ArtifactReference (org.apache.archiva.model.ArtifactReference)8 VersionedReference (org.apache.archiva.model.VersionedReference)7 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)6 LayoutException (org.apache.archiva.repository.LayoutException)6 RepositoryContentProvider (org.apache.archiva.repository.RepositoryContentProvider)6 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)6 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)5 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)5 ManagedRepository (org.apache.archiva.repository.ManagedRepository)5 MavenManagedRepository (org.apache.archiva.repository.maven2.MavenManagedRepository)5