Search in sources :

Example 11 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class DefaultRepositoriesService method removeProjectVersion.

@Override
public ActionStatus removeProjectVersion(String repositoryId, String namespace, String projectId, String version) throws ArchivaRestServiceException {
    // if not a generic we can use the standard way to delete artifact
    if (!VersionUtil.isGenericSnapshot(version)) {
        Artifact artifact = new Artifact(namespace, projectId, version);
        artifact.setRepositoryId(repositoryId);
        artifact.setContext(repositoryId);
        return deleteArtifact(artifact);
    }
    if (StringUtils.isEmpty(repositoryId)) {
        throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
    }
    if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
        throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
    }
    if (StringUtils.isEmpty(namespace)) {
        throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(projectId)) {
        throw new ArchivaRestServiceException("artifactId cannot be null", 400, null);
    }
    if (StringUtils.isEmpty(version)) {
        throw new ArchivaRestServiceException("version cannot be null", 400, null);
    }
    RepositorySession repositorySession = null;
    try {
        repositorySession = repositorySessionFactory.createSession();
    } catch (MetadataRepositoryException e) {
        e.printStackTrace();
    }
    try {
        ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
        BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(namespace).withProjectId(projectId).withVersion(version).build();
        Version versionItem = layout.getVersion(selector);
        if (versionItem != null && versionItem.exists()) {
            repository.deleteItem(versionItem);
        }
        MetadataRepository metadataRepository = repositorySession.getRepository();
        Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, namespace, projectId, version);
        for (ArtifactMetadata artifactMetadata : artifacts) {
            metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, version);
        }
        metadataRepository.removeProjectVersion(repositorySession, repositoryId, namespace, projectId, version);
    } catch (MetadataRepositoryException | MetadataResolutionException | RepositoryException | ItemNotFoundException | LayoutException e) {
        throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
    } finally {
        try {
            repositorySession.save();
        } catch (MetadataSessionException e) {
            log.error("Session save failed {}", e.getMessage());
        }
        repositorySession.close();
    }
    return ActionStatus.SUCCESS;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataSessionException(org.apache.archiva.metadata.repository.MetadataSessionException) Artifact(org.apache.archiva.maven.model.Artifact) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Version(org.apache.archiva.repository.content.Version) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException)

Example 12 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class DaysOldRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        ContentItem item = repository.toItem(path);
        Artifact artifactItem = repository.getLayout(BaseRepositoryContentLayout.class).adaptItem(Artifact.class, item);
        if (!artifactItem.exists()) {
            return;
        }
        // ArtifactReference artifact = repository.toArtifactReference( path );
        Calendar olderThanThisDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withClassifier("*").includeRelatedArtifacts().build();
        List<String> artifactVersions;
        try (Stream<? extends Artifact> stream = repository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
            artifactVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
        }
        Collections.sort(artifactVersions, VersionComparator.getInstance());
        if (retentionCount > artifactVersions.size()) {
            // Done. nothing to do here. skip it.
            return;
        }
        int countToPurge = artifactVersions.size() - retentionCount;
        ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withArtifactId(artifactItem.getId()).withClassifier("*").includeRelatedArtifacts();
        Set<Artifact> artifactsToDelete = new HashSet<>();
        for (String version : artifactVersions) {
            if (countToPurge-- <= 0) {
                break;
            }
            ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion(version).build();
            try {
                // Is this a generic snapshot "1.0-SNAPSHOT" ?
                if (VersionUtil.isGenericSnapshot(version)) {
                    List<? extends Artifact> artifactList = repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector);
                    if (artifactList.size() > 0 && artifactList.get(0).getAsset().getModificationTime().toEpochMilli() < olderThanThisDate.getTimeInMillis()) {
                        artifactsToDelete.addAll(artifactList);
                    }
                } else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
                if (VersionUtil.isUniqueSnapshot(version)) {
                    Calendar timestampCal = uniqueSnapshotToCalendar(version);
                    if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
                        artifactsToDelete.addAll(repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector));
                    }
                }
            } catch (IllegalArgumentException e) {
                log.error("Bad selector for artifact: {}", e.getMessage(), e);
            // continue
            }
        }
        purge(artifactsToDelete);
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (ContentAccessException e) {
        e.printStackTrace();
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Calendar(java.util.Calendar) 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 13 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class MetadataTools method gatherSnapshotVersions.

/**
 * Gather the set of snapshot versions found in a particular versioned reference.
 *
 * @return the Set of snapshot artifact versions found.
 * @throws LayoutException
 * @throws ContentNotFoundException
 */
public Set<String> gatherSnapshotVersions(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, IOException, ContentNotFoundException {
    Set<String> foundVersions = null;
    try {
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(reference.getNamespace()).withProjectId(reference.getArtifactId()).withArtifactId(reference.getArtifactId()).withVersion(reference.getVersion()).build();
        try (Stream<? extends Artifact> stream = managedRepository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
            foundVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
        }
    } catch (ContentAccessException e) {
        log.error("Error while accessing content {}", e.getMessage());
        throw new IOException("Could not access repository content: " + e.getMessage());
    }
    // Next gather up the referenced 'latest' versions found in any proxied repositories
    // maven-metadata-${proxyId}.xml files that may be present.
    // Does this repository have a set of remote proxied repositories?
    Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
    if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
        String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
        baseVersion = baseVersion.substring(0, baseVersion.indexOf(VersionUtil.SNAPSHOT) - 1);
        // Add in the proxied repo version ids too.
        Iterator<String> it = proxiedRepoIds.iterator();
        while (it.hasNext()) {
            String proxyId = it.next();
            ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
            if (proxyMetadata == null) {
                // There is no proxy metadata, skip it.
                continue;
            }
            // Is there some snapshot info?
            SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
            if (snapshot != null) {
                String timestamp = snapshot.getTimestamp();
                int buildNumber = snapshot.getBuildNumber();
                // Only interested in the timestamp + buildnumber.
                if (StringUtils.isNotBlank(timestamp) && (buildNumber > 0)) {
                    foundVersions.add(baseVersion + "-" + timestamp + "-" + buildNumber);
                }
            }
        }
    }
    return foundVersions;
}
Also used : Arrays(java.util.Arrays) ConfigurationListener(org.apache.archiva.configuration.provider.ConfigurationListener) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Date(java.util.Date) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) RegistryListener(org.apache.archiva.components.registry.RegistryListener) Matcher(java.util.regex.Matcher) Map(java.util.Map) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ConfigurationEvent(org.apache.archiva.configuration.provider.ConfigurationEvent) ParseException(java.text.ParseException) ContentItem(org.apache.archiva.repository.content.ContentItem) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) FileTypes(org.apache.archiva.configuration.provider.FileTypes) TimeZone(java.util.TimeZone) Collection(java.util.Collection) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) Set(java.util.Set) Collectors(java.util.stream.Collectors) Plugin(org.apache.archiva.model.Plugin) List(java.util.List) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Artifact(org.apache.archiva.repository.content.Artifact) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ConfigurationNames(org.apache.archiva.configuration.model.ConfigurationNames) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) Calendar(java.util.Calendar) Service(org.springframework.stereotype.Service) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) Named(javax.inject.Named) VersionComparator(org.apache.archiva.common.utils.VersionComparator) LinkedHashSet(java.util.LinkedHashSet) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) Project(org.apache.archiva.repository.content.Project) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ContentNotFoundException(org.apache.archiva.repository.content.ContentNotFoundException) VersionUtil(org.apache.archiva.common.utils.VersionUtil) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) Registry(org.apache.archiva.components.registry.Registry) MetadataReader(org.apache.archiva.repository.metadata.MetadataReader) NumberUtils(org.apache.commons.lang3.math.NumberUtils) LayoutException(org.apache.archiva.repository.content.LayoutException) Collections(java.util.Collections) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) IOException(java.io.IOException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StringUtils(org.apache.commons.lang3.StringUtils) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Example 14 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method deleteItemNotFound.

@Test
public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException {
    ManagedRepository repo = createManagedRepoWithContent("delete-repository");
    ManagedRepositoryContent myRepoContent = repo.getContent();
    Path repoRoot = repo.getRoot().getFilePath();
    ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.test2").build();
    ContentItem item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Namespace);
    try {
        myRepoContent.deleteItem(item);
        assertTrue("ItemNotFoundException expected for non existing namespace", false);
    } catch (ItemNotFoundException e) {
    }
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar2").build();
    item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Project);
    try {
        myRepoContent.deleteItem(item);
        assertTrue("ItemNotFoundException expected for non existing project", false);
    } catch (ItemNotFoundException e) {
    }
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.1").build();
    item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Version);
    try {
        myRepoContent.deleteItem(item);
        assertTrue("ItemNotFoundException expected for non existing version", false);
    } catch (ItemNotFoundException e) {
    }
    selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withExtension("jax").build();
    item = myRepoContent.getItem(selector);
    assertTrue(item instanceof Artifact);
    try {
        myRepoContent.deleteItem(item);
        assertTrue("ItemNotFoundException expected for non existing artifact", false);
    } catch (ItemNotFoundException e) {
    }
}
Also used : Path(java.nio.file.Path) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Project(org.apache.archiva.repository.content.Project) MavenManagedRepository(org.apache.archiva.maven.repository.MavenManagedRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) Version(org.apache.archiva.repository.content.Version) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ContentItem(org.apache.archiva.repository.content.ContentItem) ArchivaContentItem(org.apache.archiva.repository.content.base.ArchivaContentItem) Namespace(org.apache.archiva.repository.content.Namespace) Artifact(org.apache.archiva.repository.content.Artifact) ItemNotFoundException(org.apache.archiva.repository.content.ItemNotFoundException) Test(org.junit.Test)

Example 15 with ArchivaItemSelector

use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method getNonExistingMetadataItem.

@Test
public void getNonExistingMetadataItem() {
    // org/apache/maven/some-ejb/1.0
    ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace("javax.sql").withProjectId("jdbc").withVersion("2.0").build();
    Version version = repoContent.getVersion(versionSelector);
    DataItem metaData = repoContent.getMetadataItem(version);
    assertFalse(metaData.exists());
    assertEquals("/javax/sql/jdbc/2.0/maven-metadata.xml", metaData.getAsset().getPath());
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Version(org.apache.archiva.repository.content.Version) DataItem(org.apache.archiva.repository.content.DataItem) Test(org.junit.Test)

Aggregations

ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)19 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)14 ContentItem (org.apache.archiva.repository.content.ContentItem)10 Test (org.junit.Test)8 ManagedRepository (org.apache.archiva.repository.ManagedRepository)7 RepositoryException (org.apache.archiva.repository.RepositoryException)7 Artifact (org.apache.archiva.repository.content.Artifact)7 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)7 Version (org.apache.archiva.repository.content.Version)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Path (java.nio.file.Path)6 MavenManagedRepository (org.apache.archiva.maven.repository.MavenManagedRepository)6 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)6 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)6 LayoutException (org.apache.archiva.repository.content.LayoutException)6 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)6 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)5 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)5 MetadataSessionException (org.apache.archiva.metadata.repository.MetadataSessionException)4 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)4