Search in sources :

Example 1 with RepositoryStorageMetadataInvalidException

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

the class Maven2RepositoryStorage method readProjectVersionMetadata.

@Override
public ProjectVersionMetadata readProjectVersionMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageMetadataNotFoundException, RepositoryStorageMetadataInvalidException, RepositoryStorageRuntimeException {
    try {
        ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(readMetadataRequest.getRepositoryId());
        boolean isReleases = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE);
        boolean isSnapshots = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT);
        String artifactVersion = readMetadataRequest.getProjectVersion();
        // olamy: in case of browsing via the ui we can mix repos (parent of a SNAPSHOT can come from release repo)
        if (!readMetadataRequest.isBrowsingRequest()) {
            if (VersionUtil.isSnapshot(artifactVersion)) {
                // skygo trying to improve speed by honoring managed configuration MRM-1658
                if (isReleases && !isSnapshots) {
                    throw new RepositoryStorageRuntimeException("lookforsnaponreleaseonly", "managed repo is configured for release only");
                }
            } else {
                if (!isReleases && isSnapshots) {
                    throw new RepositoryStorageRuntimeException("lookforsreleaseonsneponly", "managed repo is configured for snapshot only");
                }
            }
        }
        Path basedir = Paths.get(managedRepository.getLocation());
        if (VersionUtil.isSnapshot(artifactVersion)) {
            Path metadataFile = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), artifactVersion, METADATA_FILENAME);
            try {
                ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
                // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
                SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
                if (snapshotVersion != null) {
                    artifactVersion = // remove SNAPSHOT from end
                    artifactVersion.substring(0, artifactVersion.length() - 8);
                    artifactVersion = artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
                }
            } catch (XMLException e) {
                // unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
                LOGGER.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
            }
        }
        // TODO: won't work well with some other layouts, might need to convert artifact parts to ID by path translator
        String id = readMetadataRequest.getProjectId() + "-" + artifactVersion + ".pom";
        Path file = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), id);
        if (!Files.exists(file)) {
            // metadata could not be resolved
            throw new RepositoryStorageMetadataNotFoundException("The artifact's POM file '" + file.toAbsolutePath() + "' was missing");
        }
        // TODO: this is a workaround until we can properly resolve using proxies as well - this doesn't cache
        // anything locally!
        List<RemoteRepository> remoteRepositories = new ArrayList<>();
        Map<String, NetworkProxy> networkProxies = new HashMap<>();
        Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
        List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(readMetadataRequest.getRepositoryId());
        if (proxyConnectors != null) {
            for (ProxyConnector proxyConnector : proxyConnectors) {
                RemoteRepository remoteRepoConfig = repositoryRegistry.getRemoteRepository(proxyConnector.getTargetRepoId());
                if (remoteRepoConfig != null) {
                    remoteRepositories.add(remoteRepoConfig);
                    NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy(proxyConnector.getProxyId());
                    if (networkProxyConfig != null) {
                        // key/value: remote repo ID/proxy info
                        networkProxies.put(proxyConnector.getTargetRepoId(), networkProxyConfig);
                    }
                }
            }
        }
        // can have released parent pom
        if (readMetadataRequest.isBrowsingRequest()) {
            remoteRepositories.addAll(repositoryRegistry.getRemoteRepositories());
        }
        ModelBuildingRequest req = new DefaultModelBuildingRequest().setProcessPlugins(false).setPomFile(file.toFile()).setTwoPhaseBuilding(false).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
        // MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
        req.setSystemProperties(System.getProperties());
        // MRM-1411
        req.setModelResolver(new RepositoryModelResolver(managedRepository, pathTranslator, wagonFactory, remoteRepositories, networkProxies, managedRepository));
        Model model;
        try {
            model = builder.build(req).getEffectiveModel();
        } catch (ModelBuildingException e) {
            String msg = "The artifact's POM file '" + file + "' was invalid: " + e.getMessage();
            List<ModelProblem> modelProblems = e.getProblems();
            for (ModelProblem problem : modelProblems) {
                // but setTwoPhaseBuilding(true) fix that
                if (((problem.getException() instanceof FileNotFoundException || problem.getException() instanceof NoSuchFileException) && e.getModelId() != null && !e.getModelId().equals(problem.getModelId()))) {
                    LOGGER.warn("The artifact's parent POM file '{}' cannot be resolved. " + "Using defaults for project version metadata..", file);
                    ProjectVersionMetadata metadata = new ProjectVersionMetadata();
                    metadata.setId(readMetadataRequest.getProjectVersion());
                    MavenProjectFacet facet = new MavenProjectFacet();
                    facet.setGroupId(readMetadataRequest.getNamespace());
                    facet.setArtifactId(readMetadataRequest.getProjectId());
                    facet.setPackaging("jar");
                    metadata.addFacet(facet);
                    String errMsg = "Error in resolving artifact's parent POM file. " + (problem.getException() == null ? problem.getMessage() : problem.getException().getMessage());
                    RepositoryProblemFacet repoProblemFacet = new RepositoryProblemFacet();
                    repoProblemFacet.setRepositoryId(readMetadataRequest.getRepositoryId());
                    repoProblemFacet.setId(readMetadataRequest.getRepositoryId());
                    repoProblemFacet.setMessage(errMsg);
                    repoProblemFacet.setProblem(errMsg);
                    repoProblemFacet.setProject(readMetadataRequest.getProjectId());
                    repoProblemFacet.setVersion(readMetadataRequest.getProjectVersion());
                    repoProblemFacet.setNamespace(readMetadataRequest.getNamespace());
                    metadata.addFacet(repoProblemFacet);
                    return metadata;
                }
            }
            throw new RepositoryStorageMetadataInvalidException("invalid-pom", msg, e);
        }
        // Check if the POM is in the correct location
        boolean correctGroupId = readMetadataRequest.getNamespace().equals(model.getGroupId());
        boolean correctArtifactId = readMetadataRequest.getProjectId().equals(model.getArtifactId());
        boolean correctVersion = readMetadataRequest.getProjectVersion().equals(model.getVersion());
        if (!correctGroupId || !correctArtifactId || !correctVersion) {
            StringBuilder message = new StringBuilder("Incorrect POM coordinates in '" + file + "':");
            if (!correctGroupId) {
                message.append("\nIncorrect group ID: ").append(model.getGroupId());
            }
            if (!correctArtifactId) {
                message.append("\nIncorrect artifact ID: ").append(model.getArtifactId());
            }
            if (!correctVersion) {
                message.append("\nIncorrect version: ").append(model.getVersion());
            }
            throw new RepositoryStorageMetadataInvalidException("mislocated-pom", message.toString());
        }
        ProjectVersionMetadata metadata = new ProjectVersionMetadata();
        metadata.setCiManagement(convertCiManagement(model.getCiManagement()));
        metadata.setDescription(model.getDescription());
        metadata.setId(readMetadataRequest.getProjectVersion());
        metadata.setIssueManagement(convertIssueManagement(model.getIssueManagement()));
        metadata.setLicenses(convertLicenses(model.getLicenses()));
        metadata.setMailingLists(convertMailingLists(model.getMailingLists()));
        metadata.setDependencies(convertDependencies(model.getDependencies()));
        metadata.setName(model.getName());
        metadata.setOrganization(convertOrganization(model.getOrganization()));
        metadata.setScm(convertScm(model.getScm()));
        metadata.setUrl(model.getUrl());
        metadata.setProperties(model.getProperties());
        MavenProjectFacet facet = new MavenProjectFacet();
        facet.setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId());
        facet.setArtifactId(model.getArtifactId());
        facet.setPackaging(model.getPackaging());
        if (model.getParent() != null) {
            MavenProjectParent parent = new MavenProjectParent();
            parent.setGroupId(model.getParent().getGroupId());
            parent.setArtifactId(model.getParent().getArtifactId());
            parent.setVersion(model.getParent().getVersion());
            facet.setParent(parent);
        }
        metadata.addFacet(facet);
        return metadata;
    } catch (RepositoryAdminException e) {
        throw new RepositoryStorageRuntimeException("repo-admin", e.getMessage(), e);
    }
}
Also used : ModelBuildingRequest(org.apache.maven.model.building.ModelBuildingRequest) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ManagedRepository(org.apache.archiva.repository.ManagedRepository) HashMap(java.util.HashMap) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) RemoteRepository(org.apache.archiva.repository.RemoteRepository) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) List(java.util.List) ArrayList(java.util.ArrayList) MailingList(org.apache.maven.model.MailingList) ModelProblem(org.apache.maven.model.building.ModelProblem) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Path(java.nio.file.Path) RepositoryProblemFacet(org.apache.archiva.metadata.model.facets.RepositoryProblemFacet) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) XMLException(org.apache.archiva.xml.XMLException) Model(org.apache.maven.model.Model) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 2 with RepositoryStorageMetadataInvalidException

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

the class Maven2RepositoryMetadataResolverTest method testGetProjectVersionMetadataForInvalidPom.

@Test
public void testGetProjectVersionMetadataForInvalidPom() throws Exception {
    try {
        storage.readProjectVersionMetadata(new ReadMetadataRequest(TEST_REPO_ID, "com.example.test", "invalid-pom", "1.0"));
        fail("Should have received an exception due to invalid POM");
    } catch (RepositoryStorageMetadataInvalidException e) {
        assertEquals("invalid-pom", e.getId());
    }
}
Also used : ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) Test(org.junit.Test)

Example 3 with RepositoryStorageMetadataInvalidException

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

the class ArchivaMetadataCreationConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    RepositorySession repositorySession = repositorySessionFactory.createSession();
    try {
        // note that we do minimal processing including checksums and POM information for performance of
        // the initial scan. Any request for this information will be intercepted and populated on-demand
        // or picked up by subsequent scans
        ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath(repoId, path);
        ProjectMetadata project = new ProjectMetadata();
        project.setNamespace(artifact.getNamespace());
        project.setId(artifact.getProject());
        String projectVersion = VersionUtil.getBaseVersion(artifact.getVersion());
        MetadataRepository metadataRepository = repositorySession.getRepository();
        boolean createVersionMetadata = false;
        // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
        ProjectVersionMetadata versionMetadata = null;
        try {
            ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(artifact.getNamespace()).projectId(artifact.getProject()).projectVersion(projectVersion);
            versionMetadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
            createVersionMetadata = true;
        } catch (RepositoryStorageMetadataNotFoundException e) {
            log.warn("Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path, repoId);
            versionMetadata = new ProjectVersionMetadata();
            versionMetadata.setId(projectVersion);
            versionMetadata.setIncomplete(true);
            createVersionMetadata = true;
        } catch (RepositoryStorageMetadataInvalidException e) {
            log.warn("Error occurred resolving POM for artifact:{} (repository:{}); message: {}", new Object[] { path, repoId, e.getMessage() });
        }
        // read the metadata and update it if it is newer or doesn't exist
        artifact.setWhenGathered(whenGathered);
        metadataRepository.updateArtifact(repoId, project.getNamespace(), project.getId(), projectVersion, artifact);
        if (createVersionMetadata) {
            metadataRepository.updateProjectVersion(repoId, project.getNamespace(), project.getId(), versionMetadata);
        }
        metadataRepository.updateProject(repoId, project);
        repositorySession.save();
    } catch (MetadataRepositoryException e) {
        log.warn("Error occurred persisting metadata for artifact:{} (repository:{}); message: {}", path, repoId, e.getMessage(), e);
        repositorySession.revert();
    } catch (RepositoryStorageRuntimeException e) {
        log.warn("Error occurred persisting metadata for artifact:{} (repository:{}); message: {}", path, repoId, e.getMessage(), e);
        repositorySession.revert();
    } finally {
        repositorySession.close();
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 4 with RepositoryStorageMetadataInvalidException

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

the class DefaultMetadataResolver method resolveProjectVersion.

@Override
public ProjectVersionMetadata resolveProjectVersion(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion) throws MetadataResolutionException {
    MetadataRepository metadataRepository = session.getRepository();
    ProjectVersionMetadata metadata = metadataRepository.getProjectVersion(repoId, namespace, projectId, projectVersion);
    // may then work here and be more efficient than always trying again)
    if (metadata == null || metadata.isIncomplete()) {
        try {
            ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion).browsingRequest(true);
            metadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
            log.debug("Resolved project version metadata from storage: {}", metadata);
            // eg. maven projects should be able to process parent here
            if (!metadata.getDependencies().isEmpty()) {
                ProjectVersionReference ref = new ProjectVersionReference();
                ref.setNamespace(namespace);
                ref.setProjectId(projectId);
                ref.setProjectVersion(projectVersion);
                ref.setReferenceType(ProjectVersionReference.ReferenceType.DEPENDENCY);
            }
            try {
                for (RepositoryListener listener : listeners) {
                    listener.addArtifact(session, repoId, namespace, projectId, metadata);
                }
                metadataRepository.updateProjectVersion(repoId, namespace, projectId, metadata);
            } catch (MetadataRepositoryException e) {
                log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
            }
            session.markDirty();
        } catch (RepositoryStorageMetadataInvalidException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
            throw new MetadataResolutionException(e.getMessage(), e);
        } catch (RepositoryStorageMetadataNotFoundException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
        // no need to rethrow - return null
        } catch (RepositoryStorageRuntimeException e) {
            for (RepositoryListener listener : listeners) {
                listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
            }
            throw new MetadataResolutionException(e.getMessage(), e);
        }
    }
    return metadata;
}
Also used : RepositoryListener(org.apache.archiva.repository.events.RepositoryListener) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException) RepositoryStorageRuntimeException(org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException) ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) ProjectVersionReference(org.apache.archiva.metadata.model.ProjectVersionReference) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)

Example 5 with RepositoryStorageMetadataInvalidException

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

the class DefaultMetadataResolver method resolveProjectVersions.

@Override
public Collection<String> resolveProjectVersions(RepositorySession session, String repoId, String namespace, String projectId) throws MetadataResolutionException {
    try {
        MetadataRepository metadataRepository = session.getRepository();
        Collection<String> projectVersions = metadataRepository.getProjectVersions(repoId, namespace, projectId);
        Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions(repoId, namespace, projectId, new ExcludesFilter<String>(projectVersions));
        if (storageProjectVersions != null && !storageProjectVersions.isEmpty()) {
            log.debug("Resolved project versions from storage: {}", storageProjectVersions);
            for (String projectVersion : storageProjectVersions) {
                try {
                    ReadMetadataRequest readMetadataRequest = new ReadMetadataRequest().repositoryId(repoId).namespace(namespace).projectId(projectId).projectVersion(projectVersion);
                    ProjectVersionMetadata versionMetadata = repositoryStorage.readProjectVersionMetadata(readMetadataRequest);
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifact(session, repoId, namespace, projectId, versionMetadata);
                    }
                    metadataRepository.updateProjectVersion(repoId, namespace, projectId, versionMetadata);
                } catch (MetadataRepositoryException e) {
                    log.warn("Unable to persist resolved information: {}", e.getMessage(), e);
                } catch (RepositoryStorageMetadataInvalidException e) {
                    log.warn("Not update project in metadata repository due to an error resolving it from storage: {}", e.getMessage());
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
                    }
                } catch (RepositoryStorageMetadataNotFoundException e) {
                    for (RepositoryListener listener : listeners) {
                        listener.addArtifactProblem(session, repoId, namespace, projectId, projectVersion, e);
                    }
                }
            }
            session.markDirty();
            projectVersions = new ArrayList<>(projectVersions);
            projectVersions.addAll(storageProjectVersions);
        }
        return projectVersions;
    } catch (RepositoryStorageRuntimeException 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) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) RepositoryStorageMetadataInvalidException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException) RepositoryStorageMetadataNotFoundException(org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)

Aggregations

RepositoryStorageMetadataInvalidException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException)6 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)5 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)4 RepositoryStorageMetadataNotFoundException (org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException)4 RepositoryStorageRuntimeException (org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException)4 RepositoryListener (org.apache.archiva.repository.events.RepositoryListener)2 Test (org.junit.Test)2 FileNotFoundException (java.io.FileNotFoundException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)1 ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)1 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)1 ProjectMetadata (org.apache.archiva.metadata.model.ProjectMetadata)1 ProjectVersionReference (org.apache.archiva.metadata.model.ProjectVersionReference)1 RepositoryProblemFacet (org.apache.archiva.metadata.model.facets.RepositoryProblemFacet)1