Search in sources :

Example 1 with ReadMetadataRequest

use of org.apache.archiva.metadata.repository.storage.ReadMetadataRequest in project archiva by apache.

the class Maven2RepositoryStorage method readArtifactsMetadata.

@Override
public Collection<ArtifactMetadata> readArtifactsMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageRuntimeException {
    Path dir = pathTranslator.toFile(getRepositoryBasedir(readMetadataRequest.getRepositoryId()), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion());
    if (!(Files.exists(dir) && Files.isDirectory(dir))) {
        return Collections.emptyList();
    }
    // all files that are not metadata and not a checksum / signature are considered artifacts
    final Predicate<Path> dFilter = new ArtifactDirectoryFilter(readMetadataRequest.getFilter());
    try (Stream<Path> stream = Files.list(dir)) {
        // Returns a map TRUE -> (success values), FALSE -> (Exceptions)
        Map<Boolean, List<Try<ArtifactMetadata>>> result = stream.filter(dFilter).map(path -> {
            try {
                return Try.success(getArtifactFromFile(readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), path));
            } catch (Exception e) {
                LOGGER.debug("Could not create metadata for {}:  {}", path, e.getMessage(), e);
                return Try.<ArtifactMetadata>failure(e);
            }
        }).collect(Collectors.groupingBy(Try::isSuccess));
        if (result.containsKey(Boolean.FALSE) && result.get(Boolean.FALSE).size() > 0 && (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE).size() == 0)) {
            LOGGER.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
            Try<ArtifactMetadata> failure = result.get(Boolean.FALSE).get(0);
            LOGGER.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
            throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
        } else {
            if (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE) == null) {
                return Collections.emptyList();
            }
            return result.get(Boolean.TRUE).stream().map(tr -> tr.get()).collect(Collectors.toList());
        }
    } catch (IOException e) {
        LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) StringUtils(org.apache.commons.lang.StringUtils) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Date(java.util.Date) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) LoggerFactory(org.slf4j.LoggerFactory) RepositoryProxyConnectors(org.apache.archiva.proxy.model.RepositoryProxyConnectors) DefaultModelBuilderFactory(org.apache.maven.model.building.DefaultModelBuilderFactory) XMLException(org.apache.archiva.xml.XMLException) RepositoryProblemFacet(org.apache.archiva.metadata.model.facets.RepositoryProblemFacet) ReleaseScheme(org.apache.archiva.repository.ReleaseScheme) NetworkProxyAdmin(org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin) ModelBuilder(org.apache.maven.model.building.ModelBuilder) PathParser(org.apache.archiva.repository.content.PathParser) Map(java.util.Map) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) Path(java.nio.file.Path) ProxyConnectorAdmin(org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin) Try(org.apache.archiva.common.Try) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Predicate(java.util.function.Predicate) Collection(java.util.Collection) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Filter(org.apache.archiva.metadata.repository.filter.Filter) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) Scm(org.apache.maven.model.Scm) FileNotFoundException(java.io.FileNotFoundException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) Relocation(org.apache.maven.model.Relocation) Organization(org.apache.maven.model.Organization) List(java.util.List) DistributionManagement(org.apache.maven.model.DistributionManagement) Stream(java.util.stream.Stream) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) PostConstruct(javax.annotation.PostConstruct) ArtifactReference(org.apache.archiva.model.ArtifactReference) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Model(org.apache.maven.model.Model) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) CiManagement(org.apache.maven.model.CiManagement) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) RepositoryPathTranslator(org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator) RelocationException(org.apache.archiva.metadata.repository.storage.RelocationException) Dependency(org.apache.maven.model.Dependency) HashMap(java.util.HashMap) WagonFactory(org.apache.archiva.proxy.common.WagonFactory) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) RemoteRepository(org.apache.archiva.repository.RemoteRepository) Charset(java.nio.charset.Charset) Service(org.springframework.stereotype.Service) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) MavenMetadataReader(org.apache.archiva.maven2.metadata.MavenMetadataReader) Named(javax.inject.Named) LayoutException(org.apache.archiva.repository.LayoutException) IssueManagement(org.apache.maven.model.IssueManagement) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) VersionUtil(org.apache.archiva.common.utils.VersionUtil) IOException(java.io.IOException) License(org.apache.maven.model.License) ApplicationContext(org.springframework.context.ApplicationContext) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) ModelProblem(org.apache.maven.model.building.ModelProblem) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) Paths(java.nio.file.Paths) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) MailingList(org.apache.maven.model.MailingList) Collections(java.util.Collections) RepositoryStorage(org.apache.archiva.metadata.repository.storage.RepositoryStorage) ArrayUtils(org.apache.commons.lang.ArrayUtils) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) XMLException(org.apache.archiva.xml.XMLException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) RelocationException(org.apache.archiva.metadata.repository.storage.RelocationException) LayoutException(org.apache.archiva.repository.LayoutException) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) IOException(java.io.IOException) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) List(java.util.List) ArrayList(java.util.ArrayList) MailingList(org.apache.maven.model.MailingList) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 2 with ReadMetadataRequest

use of org.apache.archiva.metadata.repository.storage.ReadMetadataRequest in project archiva by apache.

the class DefaultMetadataResolver method resolveArtifacts.

@Override
public Collection<ArtifactMetadata> resolveArtifacts(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion) throws MetadataResolutionException {
    try {
        MetadataRepository metadataRepository = session.getRepository();
        Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts(repoId, namespace, projectId, projectVersion);
        ExcludesFilter<String> filter = new ExcludesFilter<String>(createArtifactIdList(artifacts));
        ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion).filter(filter);
        Collection<ArtifactMetadata> storageArtifacts = repositoryStorage.readArtifactsMetadata(readMetadataRequest);
        if (storageArtifacts != null && !storageArtifacts.isEmpty()) {
            log.debug("Resolved artifacts from storage: {}", storageArtifacts);
            for (ArtifactMetadata artifact : storageArtifacts) {
                try {
                    metadataRepository.updateArtifact(repoId, namespace, projectId, projectVersion, artifact);
                } catch (MetadataRepositoryException e) {
                    log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
                }
            }
            session.markDirty();
            artifacts = new ArrayList<>(artifacts);
            artifacts.addAll(storageArtifacts);
        }
        return artifacts;
    } catch (RepositoryStorageRuntimeException e) {
        for (RepositoryListener listener : listeners) {
            listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
        }
        throw new MetadataResolutionException(e.getMessage(), e);
    }
}
Also used : RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ExcludesFilter(org.apache.archiva.metadata.repository.filter.ExcludesFilter) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 3 with ReadMetadataRequest

use of org.apache.archiva.metadata.repository.storage.ReadMetadataRequest in project archiva by apache.

the class Maven2RepositoryMetadataResolverMRM1411RepoGroupTest method testGetProjectVersionMetadataWithParentSuccessful.

// Tests for MRM-1411 - START
@Test
public void testGetProjectVersionMetadataWithParentSuccessful() throws Exception {
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-module-a", "target/test-repository/com/example/test/test-artifact-module-a");
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-root", "target/test-repository/com/example/test/test-artifact-root");
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-parent", "target/test-repository/com/example/test/test-artifact-parent");
    ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(TEST_REPO_ID).namespace("com.example.test").projectId("test-artifact-module-a").projectVersion("1.0");
    ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(readMetadataRequest);
    MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet(MavenProjectFacet.FACET_ID);
    assertEquals("jar", facet.getPackaging());
    assertEquals("http://maven.apache.org", metadata.getUrl());
    assertEquals("com.example.test", facet.getParent().getGroupId());
    assertEquals("test-artifact-root", facet.getParent().getArtifactId());
    assertEquals("1.0", facet.getParent().getVersion());
    assertEquals("test-artifact-module-a", facet.getArtifactId());
    assertEquals("com.example.test", facet.getGroupId());
    assertNull(metadata.getCiManagement());
    assertNotNull(metadata.getDescription());
    checkApacheLicense(metadata);
    assertEquals("1.0", metadata.getId());
    assertEquals("Test Artifact :: Module A", metadata.getName());
    String path = "test-artifact/trunk/test-artifact-module-a";
    assertEquals(TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection());
    assertEquals(TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection());
    assertEquals(TEST_SCM_URL_BASE + path, metadata.getScm().getUrl());
    List<Dependency> dependencies = metadata.getDependencies();
    assertEquals(2, dependencies.size());
    assertDependency(dependencies.get(0), "commons-io", "commons-io", "1.4");
    assertDependency(dependencies.get(1), "junit", "junit", "3.8.1", "test");
    List<String> paths = new ArrayList<>();
    paths.add("target/test-repository/com/example/test/test-artifact-module-a");
    paths.add("target/test-repository/com/example/test/test-artifact-parent");
    paths.add("target/test-repository/com/example/test/test-artifact-root");
    deleteTestArtifactWithParent(paths);
}
Also used : ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) Dependency(org.apache.archiva.metadata.model.Dependency) Test(org.junit.Test)

Example 4 with ReadMetadataRequest

use of org.apache.archiva.metadata.repository.storage.ReadMetadataRequest in project archiva by apache.

the class Maven2RepositoryMetadataResolverMRM1411RepoGroupTest method testGetProjectVersionMetadataWithParentNoRemoteReposConfigured.

@Test
public void testGetProjectVersionMetadataWithParentNoRemoteReposConfigured() throws Exception {
    // remove configuration
    Configuration config = configuration.getConfiguration();
    RemoteRepositoryConfiguration remoteRepo = config.findRemoteRepositoryById(TEST_REMOTE_REPO_ID);
    config.removeRemoteRepository(remoteRepo);
    configuration.save(config);
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-module-a", "target/test-repository/com/example/test/test-artifact-module-a");
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-root", "target/test-repository/com/example/test/test-artifact-root");
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-parent", "target/test-repository/com/example/test/test-artifact-parent");
    ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(TEST_REPO_ID).namespace("com.example.test").projectId("test-artifact-module-a").projectVersion("1.0");
    ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(readMetadataRequest);
    assertEquals("1.0", metadata.getId());
    MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet(MavenProjectFacet.FACET_ID);
    assertNotNull(facet);
    assertEquals("com.example.test", facet.getGroupId());
    assertEquals("test-artifact-module-a", facet.getArtifactId());
    assertEquals("jar", facet.getPackaging());
    List<String> paths = new ArrayList<>();
    paths.add("target/test-repository/com/example/test/test-artifact-module-a");
    paths.add("target/test-repository/com/example/test/test-artifact-parent");
    paths.add("target/test-repository/com/example/test/test-artifact-root");
    deleteTestArtifactWithParent(paths);
}
Also used : ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) RepositoryGroupConfiguration(org.apache.archiva.configuration.RepositoryGroupConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.ManagedRepositoryConfiguration) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Configuration(org.apache.archiva.configuration.Configuration) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) Test(org.junit.Test)

Example 5 with ReadMetadataRequest

use of org.apache.archiva.metadata.repository.storage.ReadMetadataRequest in project archiva by apache.

the class Maven2RepositoryMetadataResolverMRM1411RepoGroupTest method testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed2.

@Test
public void testGetProjectVersionMetadataWithParentSnapshotVersionAndSnapNotAllowed2() throws Exception {
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-artifact-module-b", "target/test-repository/com/example/test/test-artifact-module-b");
    copyTestArtifactWithParent("src/test/resources/com/example/test/test-snapshot-artifact-root", "target/test-repositorys/com/example/test/test-snapshot-artifact-root");
    ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(TEST_REPO_ID).namespace("com.example.test").projectId("test-artifact-module-b").projectVersion("1.0");
    ProjectVersionMetadata metadata = storage.readProjectVersionMetadata(readMetadataRequest);
    MavenProjectFacet facet = (MavenProjectFacet) metadata.getFacet(MavenProjectFacet.FACET_ID);
    assertEquals("jar", facet.getPackaging());
    assertEquals("com.example.test", facet.getParent().getGroupId());
    assertEquals("test-snapshot-artifact-root", facet.getParent().getArtifactId());
    assertEquals("1.1-SNAPSHOT", facet.getParent().getVersion());
    assertEquals("test-artifact-module-b", facet.getArtifactId());
    assertEquals("com.example.test", facet.getGroupId());
    assertNull(metadata.getCiManagement());
    assertNotNull(metadata.getDescription());
    checkApacheLicense(metadata);
    assertEquals("1.0", metadata.getId());
    assertEquals("Test Artifact :: Module B", metadata.getName());
    String path = "test-snapshot-artifact/trunk/test-artifact-module-b";
    assertEquals(TEST_SCM_CONN_BASE + path, metadata.getScm().getConnection());
    assertEquals(TEST_SCM_DEV_CONN_BASE + path, metadata.getScm().getDeveloperConnection());
    assertEquals(TEST_SCM_URL_BASE + path, metadata.getScm().getUrl());
    List<Dependency> dependencies = metadata.getDependencies();
    assertEquals(2, dependencies.size());
    assertDependency(dependencies.get(0), "commons-io", "commons-io", "1.4");
    assertDependency(dependencies.get(1), "junit", "junit", "3.8.1", "test");
    List<String> paths = new ArrayList<>();
    paths.add("target/test-repository/com/example/test/test-artifact-module-b");
    paths.add("target/test-repository/com/example/test/test-snapshot-artifact-root");
    deleteTestArtifactWithParent(paths);
}
Also used : ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) Dependency(org.apache.archiva.metadata.model.Dependency) Test(org.junit.Test)

Aggregations

ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)36 Test (org.junit.Test)31 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)20 ArrayList (java.util.ArrayList)16 RepositoryStorageMetadataNotFoundException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)8 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)7 Dependency (org.apache.archiva.metadata.model.Dependency)7 RepositoryStorageMetadataInvalidException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)6 RepositoryStorageRuntimeException (org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException)6 MavenArtifactFacet (org.apache.archiva.metadata.model.maven2.MavenArtifactFacet)3 Path (java.nio.file.Path)2 ProjectMetadata (org.apache.archiva.metadata.model.ProjectMetadata)2 RepositoryListener (org.apache.archiva.repository.events.RepositoryListener)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1