Search in sources :

Example 66 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class DaysOldRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            return;
        }
        ArtifactReference artifact = repository.toArtifactReference(path);
        Calendar olderThanThisDate = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
        olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
        // respect retention count
        VersionedReference reference = new VersionedReference();
        reference.setGroupId(artifact.getGroupId());
        reference.setArtifactId(artifact.getArtifactId());
        reference.setVersion(artifact.getVersion());
        List<String> versions = new ArrayList<>(repository.getVersions(reference));
        Collections.sort(versions, VersionComparator.getInstance());
        if (retentionCount > versions.size()) {
            // Done. nothing to do here. skip it.
            return;
        }
        int countToPurge = versions.size() - retentionCount;
        Set<ArtifactReference> artifactsToDelete = new HashSet<>();
        for (String version : versions) {
            if (countToPurge-- <= 0) {
                break;
            }
            ArtifactReference newArtifactReference = repository.toArtifactReference(artifactFile.toAbsolutePath().toString());
            newArtifactReference.setVersion(version);
            Path newArtifactFile = repository.toFile(newArtifactReference);
            // Is this a generic snapshot "1.0-SNAPSHOT" ?
            if (VersionUtil.isGenericSnapshot(newArtifactReference.getVersion())) {
                if (Files.getLastModifiedTime(newArtifactFile).toMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            } else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
            if (VersionUtil.isUniqueSnapshot(newArtifactReference.getVersion())) {
                Calendar timestampCal = uniqueSnapshotToCalendar(newArtifactReference.getVersion());
                if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            }
        }
        purge(artifactsToDelete);
    } catch (ContentNotFoundException | IOException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ArtifactReference(org.apache.archiva.model.ArtifactReference) HashSet(java.util.HashSet)

Example 67 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class SnapshotTransferTest method testMetadataDrivenSnapshotNotPresentAlready.

@Test
public void testMetadataDrivenSnapshotNotPresentAlready() throws Exception {
    String path = "org/apache/maven/test/get-metadata-snapshot/1.0-SNAPSHOT/get-metadata-snapshot-1.0-20050831.101112-1.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    Files.deleteIfExists(expectedFile);
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile, proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 68 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class SnapshotTransferTest method testNewerTimestampDrivenSnapshotOnFirstRepo.

@Test
public void testNewerTimestampDrivenSnapshotOnFirstRepo() 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);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    assertTrue(Files.exists(expectedFile));
    Files.setLastModifiedTime(expectedFile, FileTime.from(getPastDate().getTime(), TimeUnit.MILLISECONDS));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    Path proxiedFile = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile, proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 69 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference 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);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(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);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    assertFileEquals(expectedFile, downloadedFile, proxiedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 70 with ArtifactReference

use of org.apache.archiva.model.ArtifactReference in project archiva by apache.

the class SnapshotTransferTest method testTimestampDrivenSnapshotNotUpdated.

@Test
public void testTimestampDrivenSnapshotNotUpdated() 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);
    Path remoteFile = Paths.get(REPOPATH_PROXIED1, path);
    setManagedNewerThanRemote(expectedFile, remoteFile, 12000000);
    long expectedTimestamp = Files.getLastModifiedTime(expectedFile).toMillis();
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    assertNotDownloaded(downloadedFile);
    assertNotModified(expectedFile, expectedTimestamp);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Aggregations

ArtifactReference (org.apache.archiva.model.ArtifactReference)77 Path (java.nio.file.Path)62 Test (org.junit.Test)50 LayoutException (org.apache.archiva.repository.LayoutException)13 IOException (java.io.IOException)10 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)9 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)8 RepositoryException (org.apache.archiva.repository.RepositoryException)8 VersionedReference (org.apache.archiva.model.VersionedReference)7 File (java.io.File)5 Date (java.util.Date)5 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)5 RepositoryTask (org.apache.archiva.scheduler.repository.model.RepositoryTask)5 ResourceDoesNotExistException (org.apache.maven.wagon.ResourceDoesNotExistException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)4 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)4 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)4 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)4