Search in sources :

Example 26 with ProjectVersionMetadata

use of org.apache.archiva.metadata.model.ProjectVersionMetadata in project archiva by apache.

the class DefaultBrowseService method getProjectVersionMetadata.

@Override
public ProjectVersionMetadata getProjectVersionMetadata(String groupId, String artifactId, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    RepositorySession repositorySession = null;
    try {
        Collection<String> projectVersions = getVersions(selectedRepos, groupId, artifactId);
        repositorySession = repositorySessionFactory.createSession();
        MetadataResolver metadataResolver = repositorySession.getResolver();
        ProjectVersionMetadata sharedModel = new ProjectVersionMetadata();
        MavenProjectFacet mavenFacet = new MavenProjectFacet();
        mavenFacet.setGroupId(groupId);
        mavenFacet.setArtifactId(artifactId);
        sharedModel.addFacet(mavenFacet);
        boolean isFirstVersion = true;
        for (String version : projectVersions) {
            ProjectVersionMetadata versionMetadata = null;
            for (String repoId : selectedRepos) {
                if (versionMetadata == null || versionMetadata.isIncomplete()) {
                    try {
                        ProjectVersionMetadata projectVersionMetadataResolved = null;
                        boolean useCache = !StringUtils.endsWith(version, VersionUtil.SNAPSHOT);
                        String cacheKey = null;
                        boolean cacheToUpdate = false;
                        // not a snapshot so get it from cache
                        if (useCache) {
                            cacheKey = repoId + groupId + artifactId + version;
                            projectVersionMetadataResolved = versionMetadataCache.get(cacheKey);
                        }
                        if (useCache && projectVersionMetadataResolved != null) {
                            versionMetadata = projectVersionMetadataResolved;
                        } else {
                            projectVersionMetadataResolved = metadataResolver.resolveProjectVersion(repositorySession, repoId, groupId, artifactId, version);
                            versionMetadata = projectVersionMetadataResolved;
                            cacheToUpdate = true;
                        }
                        if (useCache && cacheToUpdate) {
                            versionMetadataCache.put(cacheKey, projectVersionMetadataResolved);
                        }
                    } catch (MetadataResolutionException e) {
                        log.error("Skipping invalid metadata while compiling shared model for " + groupId + ":" + artifactId + " in repo " + repoId + ": " + e.getMessage());
                    }
                }
            }
            if (versionMetadata == null) {
                continue;
            }
            if (isFirstVersion) {
                sharedModel = versionMetadata;
                sharedModel.setId(null);
            } else {
                MavenProjectFacet versionMetadataMavenFacet = (MavenProjectFacet) versionMetadata.getFacet(MavenProjectFacet.FACET_ID);
                if (versionMetadataMavenFacet != null) {
                    if (// 
                    mavenFacet.getPackaging() != null && !StringUtils.equalsIgnoreCase(mavenFacet.getPackaging(), versionMetadataMavenFacet.getPackaging())) {
                        mavenFacet.setPackaging(null);
                    }
                }
                if (// 
                StringUtils.isEmpty(sharedModel.getName()) && !StringUtils.isEmpty(versionMetadata.getName())) {
                    sharedModel.setName(versionMetadata.getName());
                }
                if (// 
                sharedModel.getDescription() != null && !StringUtils.equalsIgnoreCase(sharedModel.getDescription(), versionMetadata.getDescription())) {
                    sharedModel.setDescription(StringUtils.isNotEmpty(versionMetadata.getDescription()) ? versionMetadata.getDescription() : "");
                }
                if (// 
                sharedModel.getIssueManagement() != null && // 
                versionMetadata.getIssueManagement() != null && !StringUtils.equalsIgnoreCase(sharedModel.getIssueManagement().getUrl(), versionMetadata.getIssueManagement().getUrl())) {
                    sharedModel.setIssueManagement(versionMetadata.getIssueManagement());
                }
                if (// 
                sharedModel.getCiManagement() != null && // 
                versionMetadata.getCiManagement() != null && !StringUtils.equalsIgnoreCase(sharedModel.getCiManagement().getUrl(), versionMetadata.getCiManagement().getUrl())) {
                    sharedModel.setCiManagement(versionMetadata.getCiManagement());
                }
                if (// 
                sharedModel.getOrganization() != null && // 
                versionMetadata.getOrganization() != null && !StringUtils.equalsIgnoreCase(sharedModel.getOrganization().getName(), versionMetadata.getOrganization().getName())) {
                    sharedModel.setOrganization(versionMetadata.getOrganization());
                }
                if (// 
                sharedModel.getUrl() != null && !StringUtils.equalsIgnoreCase(sharedModel.getUrl(), versionMetadata.getUrl())) {
                    sharedModel.setUrl(versionMetadata.getUrl());
                }
            }
            isFirstVersion = false;
        }
        return sharedModel;
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        if (repositorySession != null) {
            repositorySession.close();
        }
    }
}
Also used : ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) MavenProjectFacet(org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 27 with ProjectVersionMetadata

use of org.apache.archiva.metadata.model.ProjectVersionMetadata in project archiva by apache.

the class DefaultBrowseService method getProjectMetadata.

@Override
public ProjectVersionMetadata getProjectMetadata(String groupId, String artifactId, String version, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    RepositorySession repositorySession = null;
    try {
        repositorySession = repositorySessionFactory.createSession();
        MetadataResolver metadataResolver = repositorySession.getResolver();
        ProjectVersionMetadata versionMetadata = null;
        for (String repoId : selectedRepos) {
            if (versionMetadata == null || versionMetadata.isIncomplete()) {
                try {
                    ProjectVersionMetadata versionMetadataTmp = metadataResolver.resolveProjectVersion(repositorySession, repoId, groupId, artifactId, version);
                    if (versionMetadata == null && versionMetadataTmp != null) {
                        versionMetadata = versionMetadataTmp;
                    }
                } catch (MetadataResolutionException e) {
                    log.warn("Skipping invalid metadata while compiling shared model for {}:{} in repo {}: {}", groupId, artifactId, repoId, e.getMessage());
                }
            }
        }
        return versionMetadata;
    } finally {
        if (repositorySession != null) {
            repositorySession.close();
        }
    }
}
Also used : ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 28 with ProjectVersionMetadata

use of org.apache.archiva.metadata.model.ProjectVersionMetadata in project archiva by apache.

the class DefaultBrowseService method getMetadatas.

@Override
public List<Entry> getMetadatas(String groupId, String artifactId, String version, String repositoryId) throws ArchivaRestServiceException {
    ProjectVersionMetadata projectVersionMetadata = getProjectMetadata(groupId, artifactId, version, repositoryId);
    if (projectVersionMetadata == null) {
        return Collections.emptyList();
    }
    MetadataFacet metadataFacet = projectVersionMetadata.getFacet(GenericMetadataFacet.FACET_ID);
    if (metadataFacet == null) {
        return Collections.emptyList();
    }
    Map<String, String> map = metadataFacet.toProperties();
    List<Entry> entries = new ArrayList<>(map.size());
    for (Map.Entry<String, String> entry : map.entrySet()) {
        entries.add(new Entry(entry.getKey(), entry.getValue()));
    }
    return entries;
}
Also used : BrowseResultEntry(org.apache.archiva.rest.api.model.BrowseResultEntry) ZipEntry(java.util.zip.ZipEntry) TreeEntry(org.apache.archiva.maven2.model.TreeEntry) JarEntry(java.util.jar.JarEntry) Entry(org.apache.archiva.rest.api.model.Entry) ArtifactContentEntry(org.apache.archiva.rest.api.model.ArtifactContentEntry) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) GenericMetadataFacet(org.apache.archiva.metadata.generic.GenericMetadataFacet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with ProjectVersionMetadata

use of org.apache.archiva.metadata.model.ProjectVersionMetadata in project archiva by apache.

the class AbstractMetadataRepositoryTest method testGetNamespacesWithProjectsPresent.

@Test
public void testGetNamespacesWithProjectsPresent() throws Exception {
    String namespace = "org.apache.maven.shared";
    repository.updateNamespace(TEST_REPO_ID, namespace);
    ProjectVersionMetadata metadata = new ProjectVersionMetadata();
    metadata.setId(TEST_PROJECT_VERSION);
    repository.updateProjectVersion(TEST_REPO_ID, namespace, TEST_PROJECT, metadata);
    Collection<String> namespaces = repository.getNamespaces(TEST_REPO_ID, namespace);
    assertThat(namespaces).isNotNull().isEmpty();
}
Also used : ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) Test(org.junit.Test)

Example 30 with ProjectVersionMetadata

use of org.apache.archiva.metadata.model.ProjectVersionMetadata in project archiva by apache.

the class AbstractMetadataRepositoryTest method testGetArtifactOnly.

@Test
public void testGetArtifactOnly() throws Exception {
    assertThat(new ArrayList<>(repository.getArtifacts(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION))).isNotNull().isEmpty();
    assertThat(repository.getProjectVersion(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION)).isNull();
    assertThat(repository.getProject(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT)).isNull();
    assertThat(repository.getRootNamespaces(TEST_REPO_ID)).isNotNull().isEmpty();
    ArtifactMetadata metadata = createArtifact();
    repository.updateArtifact(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata);
    Collection<ArtifactMetadata> artifacts = repository.getArtifacts(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
    // assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
    assertThat(artifacts).containsExactly(metadata);
    // test that namespace, project and project version is also constructed
    assertThat(repository.getRootNamespaces(TEST_REPO_ID)).isNotNull().isNotEmpty().contains(TEST_NAMESPACE).hasSize(1);
    ProjectMetadata projectMetadata = repository.getProject(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT);
    assertEquals(TEST_PROJECT, projectMetadata.getId());
    assertEquals(TEST_NAMESPACE, projectMetadata.getNamespace());
    ProjectVersionMetadata projectVersionMetadata = repository.getProjectVersion(TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION);
    assertEquals(TEST_PROJECT_VERSION, projectVersionMetadata.getId());
}
Also used : ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Test(org.junit.Test)

Aggregations

ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)44 Test (org.junit.Test)29 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)19 ArrayList (java.util.ArrayList)14 Dependency (org.apache.archiva.metadata.model.Dependency)11 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)8 HashMap (java.util.HashMap)7 GenericMetadataFacet (org.apache.archiva.metadata.generic.GenericMetadataFacet)6 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)5 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)5 RepositoryStorageRuntimeException (org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException)5 CiManagement (org.apache.archiva.metadata.model.CiManagement)4 IssueManagement (org.apache.archiva.metadata.model.IssueManagement)4 MailingList (org.apache.archiva.metadata.model.MailingList)4 Scm (org.apache.archiva.metadata.model.Scm)4 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)4 RepositoryStorageMetadataInvalidException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)4 RepositoryStorageMetadataNotFoundException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)4 Path (java.nio.file.Path)3 License (org.apache.archiva.metadata.model.License)3