Search in sources :

Example 61 with MetadataRepositoryException

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

the class JcrMetadataRepository method removeMetadataFacet.

@Override
public void removeMetadataFacet(String repositoryId, String facetId, String name) throws MetadataRepositoryException {
    try {
        Node root = getJcrSession().getRootNode();
        String path = getFacetPath(repositoryId, facetId, name);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            do {
                // also remove empty container nodes
                Node parent = node.getParent();
                node.remove();
                node = parent;
            } while (!node.hasNodes());
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 62 with MetadataRepositoryException

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

the class JcrMetadataRepository method removeNamespace.

@Override
public void removeNamespace(String repositoryId, String projectId) throws MetadataRepositoryException {
    try {
        Node root = getJcrSession().getRootNode();
        String path = getNamespacePath(repositoryId, projectId);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            if (node.isNodeType(NAMESPACE_NODE_TYPE)) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 63 with MetadataRepositoryException

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

the class JcrMetadataRepository method getArtifactsByChecksum.

@Override
public List<ArtifactMetadata> getArtifactsByChecksum(String repositoryId, String checksum) throws MetadataRepositoryException {
    List<ArtifactMetadata> artifacts;
    String q = getArtifactQuery(repositoryId) + " AND ([sha1] = $checksum OR [md5] = $checksum)";
    try {
        Query query = getJcrSession().getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        ValueFactory valueFactory = getJcrSession().getValueFactory();
        query.bindValue("checksum", valueFactory.createValue(checksum));
        QueryResult result = query.execute();
        artifacts = new ArrayList<>();
        for (Node n : JcrUtils.getNodes(result)) {
            artifacts.add(getArtifactFromNode(repositoryId, n));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return artifacts;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 64 with MetadataRepositoryException

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

the class RepositoryProblemEventListener method addArtifact.

@Override
public void addArtifact(RepositorySession session, String repoId, String namespace, String projectId, ProjectVersionMetadata metadata) {
    // Remove problems associated with this version on successful addition
    // TODO: this removes all problems - do we need something that just remove the problems we know are corrected?
    String name = RepositoryProblemFacet.createName(namespace, projectId, metadata.getId(), null);
    try {
        MetadataRepository metadataRepository = session.getRepository();
        metadataRepository.removeMetadataFacet(repoId, RepositoryProblemFacet.FACET_ID, name);
        session.markDirty();
    } catch (MetadataRepositoryException e) {
        log.warn("Unable to remove repository problem facets for the version being corrected in the repository: {}", e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository)

Example 65 with MetadataRepositoryException

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

the class DuplicateArtifactsConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    Path artifactFile = this.repositoryDir.resolve(path);
    // TODO: would be quicker to somehow make sure it ran after the update database consumer, or as a part of that
    // perhaps could use an artifact context that is retained for all consumers? First in can set the SHA-1
    // alternatively this could come straight from the storage resolver, which could populate the artifact metadata
    // in the later parse call with the desired checksum and use that
    String checksumSha1;
    ChecksummedFile checksummedFile = new ChecksummedFile(artifactFile);
    try {
        checksumSha1 = checksummedFile.calculateChecksum(ChecksumAlgorithm.SHA1);
    } catch (IOException e) {
        throw new ConsumerException(e.getMessage(), e);
    }
    MetadataRepository metadataRepository = repositorySession.getRepository();
    Collection<ArtifactMetadata> results;
    try {
        results = metadataRepository.getArtifactsByChecksum(repoId, checksumSha1);
    } catch (MetadataRepositoryException e) {
        repositorySession.close();
        throw new ConsumerException(e.getMessage(), e);
    }
    if (CollectionUtils.isNotEmpty(results)) {
        ArtifactMetadata originalArtifact;
        try {
            originalArtifact = pathTranslator.getArtifactForPath(repoId, path);
        } catch (Exception e) {
            log.warn("Not reporting problem for invalid artifact in checksum check: {}", e.getMessage());
            return;
        }
        for (ArtifactMetadata dupArtifact : results) {
            String id = path.substring(path.lastIndexOf('/') + 1);
            if (dupArtifact.getId().equals(id) && dupArtifact.getNamespace().equals(originalArtifact.getNamespace()) && dupArtifact.getProject().equals(originalArtifact.getProject()) && dupArtifact.getVersion().equals(originalArtifact.getVersion())) {
                // Skip reference to itself.
                log.debug("Not counting duplicate for artifact {} for path {}", dupArtifact, path);
                continue;
            }
            RepositoryProblemFacet problem = new RepositoryProblemFacet();
            problem.setRepositoryId(repoId);
            problem.setNamespace(originalArtifact.getNamespace());
            problem.setProject(originalArtifact.getProject());
            problem.setVersion(originalArtifact.getVersion());
            problem.setId(id);
            // FIXME: need to get the right storage resolver for the repository the dupe artifact is in, it might be
            // a different type
            // FIXME: we need the project version here, not the artifact version
            problem.setMessage("Duplicate Artifact Detected: " + path + " <--> " + pathTranslator.toPath(dupArtifact.getNamespace(), dupArtifact.getProject(), dupArtifact.getVersion(), dupArtifact.getId()));
            problem.setProblem("duplicate-artifact");
            try {
                metadataRepository.addMetadataFacet(repoId, problem);
            } catch (MetadataRepositoryException e) {
                throw new ConsumerException(e.getMessage(), e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) RepositoryProblemFacet(org.apache.archiva.metadata.model.facets.RepositoryProblemFacet) ConsumerException(org.apache.archiva.consumers.ConsumerException) IOException(java.io.IOException) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ConsumerException(org.apache.archiva.consumers.ConsumerException) IOException(java.io.IOException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException)

Aggregations

MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)65 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)24 RepositoryException (javax.jcr.RepositoryException)22 Node (javax.jcr.Node)21 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)19 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)18 Path (java.nio.file.Path)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)10 HashMap (java.util.HashMap)8 RepositoryException (org.apache.archiva.repository.RepositoryException)8 Map (java.util.Map)6 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)6 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)6 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)6 Date (java.util.Date)5 Query (javax.jcr.query.Query)5 QueryResult (javax.jcr.query.QueryResult)5 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)5