Search in sources :

Example 11 with Version

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

the class ManagedDefaultRepositoryContent method newItemStream.

@Override
public Stream<? extends ContentItem> newItemStream(ItemSelector selector, boolean parallel) throws ContentAccessException, IllegalArgumentException {
    final Predicate<StorageAsset> filter = getItemFileFilterFromSelector(selector);
    StorageAsset startDir;
    if (selector.getNamespace().contains("*")) {
        startDir = getAsset("");
    } else if (selector.hasProjectId() && selector.getProjectId().contains("*")) {
        startDir = getAsset(selector.getNamespace());
    } else if (selector.hasProjectId() && selector.hasVersion() && selector.getVersion().contains("*")) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId());
    } else if (selector.hasProjectId() && selector.hasVersion()) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
    } else if (selector.hasProjectId()) {
        startDir = getAsset(selector.getNamespace(), selector.getProjectId());
    } else {
        startDir = getAsset(selector.getNamespace());
        if (!selector.recurse()) {
            // We descend into 2 subdirectories (project and version)
            return startDir.list().stream().flatMap(a -> getChildrenDF(a, 1)).map(this::getItemFromPath);
        }
    }
    return StorageUtil.newAssetStream(startDir, parallel).filter(filter).map(this::getItemFromPath);
}
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) StorageAsset(org.apache.archiva.repository.storage.StorageAsset)

Example 12 with Version

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

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

the class MetadataTransferTest method assertFetchVersioned.

/**
 * Transfer the metadata file.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchVersioned(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    ContentItem item = managedDefaultRepository.toItem(requestedResource);
    if (item instanceof DataItem) {
        item = managedDefaultRepository.getParent(item);
    }
    assertNotNull(item);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Version version = layout.adaptItem(Version.class, item);
    assertNotNull(version);
    String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(version));
    StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
    assertNotNull("Should have downloaded a file.", downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) DataItem(org.apache.archiva.repository.content.DataItem) Version(org.apache.archiva.repository.content.Version) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 14 with Version

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

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

Version (org.apache.archiva.repository.content.Version)18 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)13 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)9 ContentItem (org.apache.archiva.repository.content.ContentItem)9 LayoutException (org.apache.archiva.repository.content.LayoutException)8 Project (org.apache.archiva.repository.content.Project)8 Path (java.nio.file.Path)7 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)7 Artifact (org.apache.archiva.repository.content.Artifact)7 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)7 ItemSelector (org.apache.archiva.repository.content.ItemSelector)7 Namespace (org.apache.archiva.repository.content.Namespace)7 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)7 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)7 Test (org.junit.Test)7 ManagedRepository (org.apache.archiva.repository.ManagedRepository)6 DataItem (org.apache.archiva.repository.content.DataItem)6 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)5 ArchivaVersion (org.apache.archiva.repository.content.base.ArchivaVersion)5 IOException (java.io.IOException)3