Search in sources :

Example 1 with MetadataResolver

use of org.apache.archiva.metadata.repository.MetadataResolver in project archiva by apache.

the class DefaultBrowseService method browseGroupId.

@Override
public BrowseResult browseGroupId(String groupId, String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    Set<String> projects = new LinkedHashSet<>();
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    Set<String> namespaces;
    try {
        MetadataResolver metadataResolver = repositorySession.getResolver();
        Set<String> namespacesToCollapse = new LinkedHashSet<>();
        for (String repoId : selectedRepos) {
            namespacesToCollapse.addAll(metadataResolver.resolveNamespaces(repositorySession, repoId, groupId));
            projects.addAll(metadataResolver.resolveProjects(repositorySession, repoId, groupId));
        }
        // TODO: this logic should be optional, particularly remembering we want to keep this code simple
        // it is located here to avoid the content repository implementation needing to do too much for what
        // is essentially presentation code
        namespaces = new LinkedHashSet<>();
        for (String n : namespacesToCollapse) {
            // TODO: check performance of this
            namespaces.add(collapseNamespaces(repositorySession, metadataResolver, selectedRepos, groupId + "." + n));
        }
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<>(namespaces.size() + projects.size());
    for (String namespace : namespaces) {
        browseGroupResultEntries.add(new BrowseResultEntry(namespace, false).groupId(namespace));
    }
    for (String project : projects) {
        browseGroupResultEntries.add(new BrowseResultEntry(groupId + '.' + project, true).groupId(groupId).artifactId(project));
    }
    Collections.sort(browseGroupResultEntries);
    return new BrowseResult(browseGroupResultEntries);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BrowseResultEntry(org.apache.archiva.rest.api.model.BrowseResultEntry) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) BrowseResult(org.apache.archiva.rest.api.model.BrowseResult)

Example 2 with MetadataResolver

use of org.apache.archiva.metadata.repository.MetadataResolver in project archiva by apache.

the class DefaultBrowseService method getRootGroups.

@Override
public BrowseResult getRootGroups(String repositoryId) throws ArchivaRestServiceException {
    List<String> selectedRepos = getSelectedRepos(repositoryId);
    Set<String> namespaces = new LinkedHashSet<String>();
    // TODO: this logic should be optional, particularly remembering we want to keep this code simple
    // it is located here to avoid the content repository implementation needing to do too much for what
    // is essentially presentation code
    Set<String> namespacesToCollapse = new LinkedHashSet<String>();
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        MetadataResolver metadataResolver = repositorySession.getResolver();
        for (String repoId : selectedRepos) {
            namespacesToCollapse.addAll(metadataResolver.resolveRootNamespaces(repositorySession, repoId));
        }
        for (String n : namespacesToCollapse) {
            // TODO: check performance of this
            namespaces.add(collapseNamespaces(repositorySession, metadataResolver, selectedRepos, n));
        }
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    List<BrowseResultEntry> browseGroupResultEntries = new ArrayList<>(namespaces.size());
    for (String namespace : namespaces) {
        browseGroupResultEntries.add(new BrowseResultEntry(namespace, false));
    }
    Collections.sort(browseGroupResultEntries);
    return new BrowseResult(browseGroupResultEntries);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) BrowseResultEntry(org.apache.archiva.rest.api.model.BrowseResultEntry) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) BrowseResult(org.apache.archiva.rest.api.model.BrowseResult) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 3 with MetadataResolver

use of org.apache.archiva.metadata.repository.MetadataResolver 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 4 with MetadataResolver

use of org.apache.archiva.metadata.repository.MetadataResolver in project archiva by apache.

the class DefaultBrowseService method getDependees.

@Override
public List<Artifact> getDependees(String groupId, String artifactId, String version, String repositoryId) throws ArchivaRestServiceException {
    List<ProjectVersionReference> references = new ArrayList<>();
    // TODO: what if we get duplicates across repositories?
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        MetadataResolver metadataResolver = repositorySession.getResolver();
        for (String repoId : getObservableRepos()) {
            // TODO: what about if we want to see this irrespective of version?
            references.addAll(metadataResolver.resolveProjectReferences(repositorySession, repoId, groupId, artifactId, version));
        }
    } catch (MetadataResolutionException e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    } finally {
        repositorySession.close();
    }
    List<Artifact> artifacts = new ArrayList<>(references.size());
    for (ProjectVersionReference projectVersionReference : references) {
        artifacts.add(new Artifact(projectVersionReference.getNamespace(), projectVersionReference.getProjectId(), projectVersionReference.getProjectVersion()));
    }
    return artifacts;
}
Also used : ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ArrayList(java.util.ArrayList) MetadataResolver(org.apache.archiva.metadata.repository.MetadataResolver) ProjectVersionReference(org.apache.archiva.metadata.model.ProjectVersionReference) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) Artifact(org.apache.archiva.maven2.model.Artifact) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact)

Example 5 with MetadataResolver

use of org.apache.archiva.metadata.repository.MetadataResolver 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)

Aggregations

MetadataResolver (org.apache.archiva.metadata.repository.MetadataResolver)7 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)7 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)6 ArrayList (java.util.ArrayList)5 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)5 LinkedHashSet (java.util.LinkedHashSet)3 Artifact (org.apache.archiva.maven2.model.Artifact)2 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)2 ArchivaArtifact (org.apache.archiva.model.ArchivaArtifact)2 BrowseResult (org.apache.archiva.rest.api.model.BrowseResult)2 BrowseResultEntry (org.apache.archiva.rest.api.model.BrowseResultEntry)2 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)1 ProjectVersionReference (org.apache.archiva.metadata.model.ProjectVersionReference)1 MavenProjectFacet (org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet)1