Search in sources :

Example 26 with Artifact

use of org.apache.archiva.repository.content.Artifact in project archiva by apache.

the class SnapshotTransferTest method testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure.

@Test
public void testTimestampDrivenSnapshotNotPresentAlreadyExpiredCacheFailure() throws Exception {
    String path = "org/apache/maven/test/get-timestamped-snapshot/1.0-SNAPSHOT/get-timestamped-snapshot-1.0-SNAPSHOT.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(path);
    Files.deleteIfExists(expectedFile);
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile.getFilePath(), proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Example 27 with Artifact

use of org.apache.archiva.repository.content.Artifact in project archiva by apache.

the class SnapshotTransferTest method testTimestampDrivenSnapshotNotExpired.

/**
 * TODO: Has problems with wagon implementation not preserving timestamp.
 */
/*
    public void testNewerTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
        throws Exception
    {
        String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
        setupTestableManagedRepository( path );
        
        Path expectedFile = managedDefaultDir.resolve(path);
        ArtifactReference artifact = createArtifactReference( "default", path );

        Files.delete(expectedFile);
        assertFalse( Files.exists(expectedFile) );

        // Create customized proxy / target repository
        File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED1_TARGET, REPOPATH_PROXIED1,
                                                            REPOPATH_PROXIED1_TARGET, "default" );

        new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );

        // Configure Connector (usually done within archiva.xml configuration)
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );

        File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );

        // Should have downloaded the content from proxy2, as proxy1 has an old (by file.lastModified check) version.
        Path proxiedFile = Paths.get(REPOPATH_PROXIED2, path);
        assertFileEquals( expectedFile, downloadedFile, proxiedFile );
        assertNoTempFiles( expectedFile );
    } 

    public void testOlderTimestampDrivenSnapshotOnSecondRepoThanFirstNotPresentAlready()
        throws Exception
    {
        String path = "org/apache/maven/test/get-timestamped-snapshot-in-both/1.0-SNAPSHOT/get-timestamped-snapshot-in-both-1.0-SNAPSHOT.jar";
        setupTestableManagedRepository( path );
        
        Path expectedFile = managedDefaultDir.resolve(path);
        ArtifactReference artifact = createArtifactReference( "default", path );

        Files.delete(expectedFile);
        assertFalse( Files.exists(expectedFile) );

        // Create customized proxy / target repository
        File targetProxyDir = saveTargetedRepositoryConfig( ID_PROXIED2_TARGET, REPOPATH_PROXIED2,
                                                            REPOPATH_PROXIED2_TARGET, "default" );

        new File( targetProxyDir, path ).setLastModified( getPastDate().getTime() );

        // Configure Connector (usually done within archiva.xml configuration)
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );
        saveConnector( ID_DEFAULT_MANAGED, ID_PROXIED2_TARGET, ChecksumPolicy.IGNORED, ReleasesPolicy.IGNORED,
                       SnapshotsPolicy.IGNORED, CachedFailuresPolicy.IGNORED );

        File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );

        File proxiedFile = new File( REPOPATH_PROXIED1_TARGET, path );
        assertFileEquals( expectedFile, downloadedFile, proxiedFile );
        assertNoTempFiles( expectedFile );
    } */
@Test
public void testTimestampDrivenSnapshotNotExpired() throws Exception {
    String path = "org/apache/maven/test/get-present-timestamped-snapshot/1.0-SNAPSHOT/get-present-timestamped-snapshot-1.0-SNAPSHOT.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(path);
    assertTrue(Files.exists(expectedFile));
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    Files.setLastModifiedTime(proxiedFile, FileTime.from(getFutureDate().getTime(), TimeUnit.MILLISECONDS));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    assertFileEquals(expectedFile, downloadedFile.getFilePath(), proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Artifact(org.apache.archiva.repository.content.Artifact) Test(org.junit.Test)

Example 28 with Artifact

use of org.apache.archiva.repository.content.Artifact in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        StorageAsset artifactFile = repository.getRepository().getRoot().resolve(path);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        if (!artifactFile.exists()) {
            // Nothing to do here, file doesn't exist, skip it.
            return;
        }
        Artifact artifactRef = layout.getArtifact(path);
        if (!VersionUtil.isSnapshot(artifactRef.getVersion().getId())) {
            // Nothing to do here, not a snapshot, skip it.
            return;
        }
        ItemSelector projectSelector = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).build();
        // 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)) {
                BaseRepositoryContentLayout repoContent = repo.getContent().getLayout(BaseRepositoryContentLayout.class);
                Project proj = repoContent.getProject(projectSelector);
                for (Version version : repoContent.getVersions(proj)) {
                    if (!VersionUtil.isSnapshot(version.getId())) {
                        releasedVersions.add(version.getId());
                    }
                }
            }
        }
        Collections.sort(releasedVersions, VersionComparator.getInstance());
        // Now clean out any version that is earlier than the highest released version.
        boolean needsMetadataUpdate = false;
        ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactRef.getNamespace().getId()).withProjectId(artifactRef.getId()).withArtifactId(artifactRef.getId());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion().getId()))) {
            ArchivaItemSelector selector = versionSelectorBuilder.withVersion(artifactRef.getVersion().getId()).build();
            Version version = layout.getVersion(selector);
            if (version.exists()) {
                repository.deleteItem(version);
            }
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId(), artifactFile.getName());
            }
            metadataRepository.removeProjectVersion(repositorySession, repository.getId(), artifactRef.getNamespace().getId(), artifactRef.getId(), artifactRef.getVersion().getId());
            needsMetadataUpdate = true;
        }
        if (needsMetadataUpdate) {
            updateMetadata(artifactRef);
        }
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (MetadataRepositoryException e) {
        log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
    } catch (ContentAccessException e) {
        e.printStackTrace();
    } catch (ItemNotFoundException e) {
        log.error("Could not find item to delete {}", e.getMessage(), e);
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ArrayList(java.util.ArrayList) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Artifact(org.apache.archiva.repository.content.Artifact) Project(org.apache.archiva.repository.content.Project) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) ItemSelector(org.apache.archiva.repository.content.ItemSelector) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 29 with Artifact

use of org.apache.archiva.repository.content.Artifact 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 30 with Artifact

use of org.apache.archiva.repository.content.Artifact 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();
        BaseRepositoryContentLayout layout = repositoryContent.getLayout(BaseRepositoryContentLayout.class);
        Artifact artifact = layout.getArtifact(path);
        repositorySession.getRepository().getArtifacts(repositorySession, repository.getId(), artifact.getNamespace().getId(), artifact.getId(), artifact.getVersion().getId());
    } catch (LayoutException | MetadataResolutionException e) {
        throw new ConsumerException(e.getLocalizedMessage(), e);
    }
}
Also used : LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ConsumerException(org.apache.archiva.consumers.ConsumerException) Artifact(org.apache.archiva.repository.content.Artifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Aggregations

Artifact (org.apache.archiva.repository.content.Artifact)78 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)63 Test (org.junit.Test)60 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)59 Path (java.nio.file.Path)54 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)26 ItemSelector (org.apache.archiva.repository.content.ItemSelector)21 LayoutException (org.apache.archiva.repository.content.LayoutException)21 ContentItem (org.apache.archiva.repository.content.ContentItem)19 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)15 IOException (java.io.IOException)13 ManagedRepository (org.apache.archiva.repository.ManagedRepository)13 Project (org.apache.archiva.repository.content.Project)13 MavenMetadataReader (org.apache.archiva.maven.metadata.MavenMetadataReader)12 Version (org.apache.archiva.repository.content.Version)12 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)12 Reader (java.io.Reader)11 List (java.util.List)11 Collectors (java.util.stream.Collectors)11 Inject (javax.inject.Inject)11