Search in sources :

Example 41 with Artifact

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

the class MetadataUpdaterConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    // Ignore paths like .index etc
    if (!path.startsWith(".")) {
        try {
            BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
            Artifact artifact = layout.getArtifact(path);
            updateVersionMetadata(artifact, path);
            updateProjectMetadata(artifact, path);
        } catch (LayoutException e) {
            log.info("Not processing path that is not an artifact: {} ({})", path, e.getMessage());
        }
    }
}
Also used : LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Artifact(org.apache.archiva.repository.content.Artifact)

Example 42 with Artifact

use of org.apache.archiva.repository.content.Artifact 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<Artifact> references) {
    if (references != null && !references.isEmpty()) {
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Map<ArtifactInfo, ArtifactMetadata> metaRemovalList = new HashMap<>();
        Map<String, Collection<ArtifactMetadata>> metaResolved = new HashMap<>();
        for (Artifact reference : references) {
            String baseVersion = reference.getVersion().getId();
            String namespace = reference.getVersion().getProject().getNamespace().getId();
            // Needed for tracking in the hashmap
            String metaBaseId = reference.toKey();
            if (!metaResolved.containsKey(metaBaseId)) {
                try {
                    metaResolved.put(metaBaseId, metadataRepository.getArtifacts(repositorySession, repository.getId(), namespace, reference.getId(), baseVersion));
                } catch (MetadataResolutionException e) {
                    log.error("Error during metadata retrieval {}: {}", metaBaseId, e.getMessage());
                }
            }
            StorageAsset artifactFile = reference.getAsset();
            for (RepositoryListener listener : listeners) {
                listener.deleteArtifact(metadataRepository, repository.getId(), namespace, reference.getId(), reference.getVersion().getId(), artifactFile.getName());
            }
            if (reference.exists()) {
                try {
                    repository.deleteItem(reference);
                } catch (ContentAccessException e) {
                    log.error("Error while trying to delete artifact {}: {}", reference.toString(), e.getMessage(), e);
                } catch (ItemNotFoundException e) {
                    log.error("Asset deleted from background other thread: {}", e.getMessage());
                }
            }
            boolean snapshotVersion = VersionUtil.isSnapshot(baseVersion);
            // 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.getArtifactVersion())) {
                            ArtifactInfo info = new ArtifactInfo(artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getProjectVersion(), artifactMetadata.getVersion());
                            if (StringUtils.isNotBlank(reference.getClassifier())) {
                                info.setClassifier(reference.getClassifier());
                            } else {
                                info.setClassifier("");
                            }
                            metaRemovalList.put(info, artifactMetadata);
                        }
                    }
                }
            } else // otherwise we delete the artifact version
            {
                ArtifactInfo info = new ArtifactInfo(namespace, reference.getId(), baseVersion, reference.getArtifactVersion());
                for (ArtifactMetadata metadata : metaResolved.get(metaBaseId)) {
                    metaRemovalList.put(info, metadata);
                }
            }
            triggerAuditEvent(repository.getRepository().getId(), reference.toKey(), AuditEvent.PURGE_ARTIFACT);
        // purgeSupportFiles( artifactFile );
        }
        purgeMetadata(metadataRepository, metaRemovalList);
        try {
            repositorySession.save();
        } catch (org.apache.archiva.metadata.repository.MetadataSessionException e) {
            e.printStackTrace();
        }
    }
}
Also used : RepositoryListener(org.apache.archiva.metadata.audit.RepositoryListener) HashMap(java.util.HashMap) org.apache.archiva.metadata.repository(org.apache.archiva.metadata.repository) Artifact(org.apache.archiva.repository.content.Artifact) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Collection(java.util.Collection) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 43 with Artifact

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

the class ManagedDefaultRepositoryContent method newArtifactStream.

/**
 * Returns all related artifacts that match the given artifact. That means all artifacts that have
 * the same filename plus an additional extension, e.g. ${fileName}.sha2
 *
 * @param item the artifact
 * @return the stream of artifacts
 * @throws ContentAccessException if access to the underlying storage failed
 */
public Stream<? extends Artifact> newArtifactStream(Artifact item) throws ContentAccessException {
    final Version v = item.getVersion();
    final String fileName = item.getFileName();
    final Predicate<StorageAsset> filter = (StorageAsset a) -> a.getName().startsWith(fileName + ".");
    return v.getAsset().list().stream().filter(filter).map(a -> {
        try {
            return getArtifactFromPath(a);
        } catch (LayoutException e) {
            log.error("Not a valid artifact path " + a.getPath(), e);
            return null;
        }
    }).filter(Objects::nonNull);
}
Also used : Arrays(java.util.Arrays) ArchivaNamespace(org.apache.archiva.repository.content.base.ArchivaNamespace) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Namespace(org.apache.archiva.repository.content.Namespace) StringUtils(org.apache.commons.lang3.StringUtils) LayoutRuntimeException(org.apache.archiva.repository.content.LayoutRuntimeException) Matcher(java.util.regex.Matcher) FileUtils(org.apache.archiva.common.utils.FileUtils) MavenMetadataReader(org.apache.archiva.maven.metadata.MavenMetadataReader) ItemSelector(org.apache.archiva.repository.content.ItemSelector) Path(java.nio.file.Path) ContentItem(org.apache.archiva.repository.content.ContentItem) RepositoryStorage(org.apache.archiva.repository.storage.RepositoryStorage) FileTypes(org.apache.archiva.configuration.provider.FileTypes) ArtifactOptBuilder(org.apache.archiva.repository.content.base.builder.ArtifactOptBuilder) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Predicate(java.util.function.Predicate) BaseArtifactTypes(org.apache.archiva.repository.content.BaseArtifactTypes) Collectors(java.util.stream.Collectors) ManagedRepository(org.apache.archiva.repository.ManagedRepository) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Pattern(java.util.regex.Pattern) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) Artifact(org.apache.archiva.repository.content.Artifact) ItemDeleteStatus(org.apache.archiva.repository.ItemDeleteStatus) ArchivaProject(org.apache.archiva.repository.content.base.ArchivaProject) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Version(org.apache.archiva.repository.content.Version) ReferenceMap(org.apache.commons.collections4.map.ReferenceMap) Inject(javax.inject.Inject) Named(javax.inject.Named) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) Project(org.apache.archiva.repository.content.Project) Files(java.nio.file.Files) FileLockManager(org.apache.archiva.common.filelock.FileLockManager) VersionUtil(org.apache.archiva.common.utils.VersionUtil) ManagedRepositoryContentLayout(org.apache.archiva.repository.content.ManagedRepositoryContentLayout) IOException(java.io.IOException) DataItem(org.apache.archiva.repository.content.DataItem) Consumer(java.util.function.Consumer) StorageUtil(org.apache.archiva.repository.storage.util.StorageUtil) ArchivaVersion(org.apache.archiva.repository.content.base.ArchivaVersion) LayoutException(org.apache.archiva.repository.content.LayoutException) Collections(java.util.Collections) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException) Version(org.apache.archiva.repository.content.Version) ArchivaVersion(org.apache.archiva.repository.content.base.ArchivaVersion) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) Objects(java.util.Objects)

Example 44 with Artifact

use of org.apache.archiva.repository.content.Artifact 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);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(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);
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    // Validate that file actually came from proxied2 (as intended).
    Path proxied2File = Paths.get(REPOPATH_PROXIED2, path);
    assertNotNull(downloadedFile);
    assertFileEquals(expectedFile, downloadedFile.getFilePath(), proxied2File);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) UrlFailureCache(org.apache.archiva.policies.urlcache.UrlFailureCache) 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 45 with Artifact

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

the class SnapshotTransferTest method testOlderTimestampDrivenSnapshotOnFirstRepo.

@Test
public void testOlderTimestampDrivenSnapshotOnFirstRepo() 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);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Artifact artifact = layout.getArtifact(path);
    // Configure Connector (usually done within archiva.xml configuration)
    saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, false);
    // Attempt to download.
    StorageAsset downloadedFile = proxyHandler.fetchFromProxies(managedDefaultRepository.getRepository(), artifact);
    // Should not have downloaded as managed is newer than remote.
    assertNotDownloaded(downloadedFile);
    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)

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