Search in sources :

Example 11 with ArtifactReference

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

the class AbstractRepositoryPurge method purge.

/**
 * Purge the repo. Update db and index of removed artifacts.
 *
 * @param references
 */
protected void purge(Set<ArtifactReference> references) {
    if (references != null && !references.isEmpty()) {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
        Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
        for (ArtifactReference reference : references) {
            String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
            // Needed for tracking in the hashmap
            String metaBaseId = reference.getGroupId() + "/" + reference.getArtifactId() + "/" + baseVersion;
            if (!metaResolved.containsKey(metaBaseId)) {
                try {
                    metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repository.getId(), reference.getGroupId(), reference.getArtifactId(), baseVersion));
                } catch (MetadataResolutionException e) {
                    log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
                }
            }
            Path artifactFile = repository.toFile(reference);
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), artifactFile.getFileName().toString());
            }
            try {
                Files.delete(artifactFile);
                log.debug("File deleted: {}", artifactFile.toAbsolutePath());
            } catch (IOException e) {
                log.error("Could not delete file {}: {}", artifactFile.toAbsolutePath(), e.getMessage(), e);
                continue;
            }
            try {
                repository.deleteArtifact(reference);
            } catch (ContentNotFoundException e) {
                log.warn("skip error deleting artifact {}: {}", reference, e.getMessage());
            }
            boolean snapshotVersion = VersionUtil.isSnapshot(reference.getVersion());
            // If this is a snapshot we have to search for artifacts with the same version. And remove all of them.
            if (snapshotVersion) {
                Collection<ArtifactMetadata> artifacts = metaResolved.get(metaBaseId);
                if (artifacts != null) {
                    // cleanup snapshots metadata
                    for (ArtifactMetadata artifactMetadata : artifacts) {
                        // Artifact metadata and reference version should match.
                        if (artifactMetadata.getVersion().equals(reference.getVersion())) {
                            ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
                            if (StringUtils.isNotBlank(reference.getClassifier())) {
                                info.setClassifier(reference.getClassifier());
                                metaRemovalList.put(info, artifactMetadata);
                            } else {
                                // metadataRepository.removeArtifact( artifactMetadata, baseVersion );
                                metaRemovalList.put(info, artifactMetadata);
                            }
                        }
                    }
                }
            } else // otherwise we delete the artifact version
            {
                ArtifactInfo info = new ArtifactInfo(reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion());
                for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
                    metaRemovalList.put(info, metadata);
                }
            }
            triggerAuditEvent(repository.getRepository().getId(), ArtifactReference.toKey(reference), AuditEvent.PURGE_ARTIFACT);
            purgeSupportFiles(artifactFile);
        }
        purgeMetadata(metadataRepository, metaRemovalList);
        repositorySession.save();
    }
}
Also used : Path(java.nio.file.Path) RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) HashMap(java.util.HashMap) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) IOException(java.io.IOException) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Collection(java.util.Collection) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 12 with ArtifactReference

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

the class CacheFailuresTransferTest method testGetWhenInBothProxiedButFirstCacheFailure.

@Test
public void testGetWhenInBothProxiedButFirstCacheFailure() throws Exception {
    String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    Files.deleteIfExists(expectedFile);
    assertFalse(Files.exists(expectedFile));
    String url = PathUtil.toUrl(REPOPATH_PROXIED1 + "/" + path);
    // Intentionally set failure on url in proxied1 (for test)
    UrlFailureCache failurlCache = lookupUrlFailureCache();
    failurlCache.cacheFailure(url);
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    saveConnector(ID_DEFAULT_MANAGED, "proxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    // Validate that file actually came from proxied2 (as intended).
    Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
    assertFileEquals(expectedFile, downloadedFile, proxied2File);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) UrlFailureCache(org.apache.archiva.policies.urlcache.UrlFailureCache) ArtifactReference(org.apache.archiva.model.ArtifactReference) Test(org.junit.Test)

Example 13 with ArtifactReference

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

the class CacheFailuresTransferTest method testGetWithCacheFailuresOff.

@Test
public void testGetWithCacheFailuresOff() throws Exception {
    String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
    Path expectedFile = managedDefaultDir.resolve(path);
    setupTestableManagedRepository(path);
    assertNotExistsInManagedDefaultRepo(expectedFile);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    // Configure Repository (usually done within archiva.xml configuration)
    saveRemoteRepositoryConfig("badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default");
    saveRemoteRepositoryConfig("badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default");
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    saveConnector(ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    wagonMock.get(EasyMock.eq(path), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().andThrow(new ResourceDoesNotExistException("resource does not exist.")).times(2);
    wagonMockControl.replay();
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    wagonMockControl.verify();
    // Second attempt to download same artifact DOES NOT use cache
    wagonMockControl.reset();
    wagonMock.get(EasyMock.eq(path), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().andThrow(new ResourceDoesNotExistException("resource does not exist.")).times(2);
    wagonMockControl.replay();
    downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    wagonMockControl.verify();
    assertNotDownloaded(downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) File(java.io.File) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Test(org.junit.Test)

Example 14 with ArtifactReference

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

the class CacheFailuresTransferTest method testGetWithCacheFailuresOn.

@Test
public void testGetWithCacheFailuresOn() throws Exception {
    String path = "org/apache/maven/test/get-in-second-proxy/1.0/get-in-second-proxy-1.0.jar";
    Path expectedFile = managedDefaultDir.resolve(path);
    setupTestableManagedRepository(path);
    assertNotExistsInManagedDefaultRepo(expectedFile);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    // Configure Repository (usually done within archiva.xml configuration)
    saveRemoteRepositoryConfig("badproxied1", "Bad Proxied 1", "test://bad.machine.com/repo/", "default");
    saveRemoteRepositoryConfig("badproxied2", "Bad Proxied 2", "test://bad.machine.com/anotherrepo/", "default");
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "badproxied1", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    saveConnector(ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES, false);
    wagonMock.get(EasyMock.eq(path), EasyMock.anyObject(File.class));
    EasyMock.expectLastCall().andThrow(new ResourceDoesNotExistException("resource does not exist.")).times(2);
    wagonMockControl.replay();
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    wagonMockControl.verify();
    // Second attempt to download same artifact use cache
    wagonMockControl.reset();
    wagonMockControl.replay();
    downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    wagonMockControl.verify();
    assertNotDownloaded(downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) ArtifactReference(org.apache.archiva.model.ArtifactReference) File(java.io.File) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) Test(org.junit.Test)

Example 15 with ArtifactReference

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

the class ChecksumTransferTest method testGetChecksumCorrectSha1NoMd5.

@Test
public void testGetChecksumCorrectSha1NoMd5() throws Exception {
    String path = "org/apache/maven/test/get-checksum-sha1-only/1.0/get-checksum-sha1-only-1.0.jar";
    setupTestableManagedRepository(path);
    Path expectedFile = managedDefaultDir.resolve(path);
    ArtifactReference artifact = managedDefaultRepository.toArtifactReference(path);
    FileUtils.deleteDirectory(expectedFile.getParent());
    assertFalse(Files.exists(expectedFile));
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, "proxied1", ChecksumPolicy.IGNORE, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, false);
    Path downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository, artifact);
    Path proxied1File = Paths.get(REPOPATH_PROXIED1, path);
    assertFileEquals(expectedFile, downloadedFile, proxied1File);
    assertNoTempFiles(expectedFile);
    assertChecksums(expectedFile, "748a3a013bf5eacf2bbb40a2ac7d37889b728837 *get-checksum-sha1-only-1.0.jar", null);
}
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