Search in sources :

Example 91 with ArtifactMetadata

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

the class JcrMetadataRepository method removeArtifact.

@Override
public void removeArtifact(String repositoryId, String namespace, String project, String projectVersion, MetadataFacet metadataFacet) throws MetadataRepositoryException {
    try {
        Node root = getJcrSession().getRootNode();
        String path = getProjectVersionPath(repositoryId, namespace, project, projectVersion);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            for (Node n : JcrUtils.getChildNodes(node)) {
                if (n.isNodeType(ARTIFACT_NODE_TYPE)) {
                    ArtifactMetadata artifactMetadata = getArtifactFromNode(repositoryId, n);
                    log.debug("artifactMetadata: {}", artifactMetadata);
                    MetadataFacet metadataFacetToRemove = artifactMetadata.getFacet(metadataFacet.getFacetId());
                    if (metadataFacetToRemove != null && metadataFacet.equals(metadataFacetToRemove)) {
                        n.remove();
                    }
                }
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Node(javax.jcr.Node) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 92 with ArtifactMetadata

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

the class JcrMetadataRepository method getArtifactFromNode.

private ArtifactMetadata getArtifactFromNode(String repositoryId, Node artifactNode) throws RepositoryException {
    String id = artifactNode.getName();
    ArtifactMetadata artifact = new ArtifactMetadata();
    artifact.setId(id);
    artifact.setRepositoryId(repositoryId == null ? artifactNode.getAncestor(2).getName() : repositoryId);
    Node projectVersionNode = artifactNode.getParent();
    Node projectNode = projectVersionNode.getParent();
    Node namespaceNode = projectNode.getParent();
    artifact.setNamespace(namespaceNode.getProperty("namespace").getString());
    artifact.setProject(projectNode.getName());
    artifact.setProjectVersion(projectVersionNode.getName());
    artifact.setVersion(artifactNode.hasProperty("version") ? artifactNode.getProperty("version").getString() : projectVersionNode.getName());
    if (artifactNode.hasProperty(JCR_LAST_MODIFIED)) {
        artifact.setFileLastModified(artifactNode.getProperty(JCR_LAST_MODIFIED).getDate().getTimeInMillis());
    }
    if (artifactNode.hasProperty("whenGathered")) {
        artifact.setWhenGathered(artifactNode.getProperty("whenGathered").getDate().getTime());
    }
    if (artifactNode.hasProperty("size")) {
        artifact.setSize(artifactNode.getProperty("size").getLong());
    }
    if (artifactNode.hasProperty("md5")) {
        artifact.setMd5(artifactNode.getProperty("md5").getString());
    }
    if (artifactNode.hasProperty("sha1")) {
        artifact.setSha1(artifactNode.getProperty("sha1").getString());
    }
    for (Node n : JcrUtils.getChildNodes(artifactNode)) {
        if (n.isNodeType(FACET_NODE_TYPE)) {
            String name = n.getName();
            MetadataFacetFactory factory = metadataFacetFactories.get(name);
            if (factory == null) {
                log.error("Attempted to load unknown project version metadata facet: {}", name);
            } else {
                MetadataFacet facet = factory.createMetadataFacet();
                Map<String, String> map = new HashMap<>();
                for (Property p : JcrUtils.getProperties(n)) {
                    String property = p.getName();
                    if (!property.startsWith("jcr:")) {
                        map.put(property, p.getString());
                    }
                }
                facet.fromProperties(map);
                artifact.addFacet(facet);
            }
        }
    }
    return artifact;
}
Also used : HashMap(java.util.HashMap) Node(javax.jcr.Node) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Property(javax.jcr.Property) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory)

Example 93 with ArtifactMetadata

use of org.apache.archiva.metadata.model.ArtifactMetadata 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 94 with ArtifactMetadata

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

the class ArtifactExtensionMappingTest method getTypeFromArtifactId.

private String getTypeFromArtifactId(String artifactId) {
    ArtifactMetadata artifact = pathTranslator.getArtifactFromId(null, "groupId", artifactId, "1.0", artifactId + "-1.0.jar");
    MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet(MavenArtifactFacet.FACET_ID);
    return facet.getType();
}
Also used : MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata)

Example 95 with ArtifactMetadata

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

the class Maven2RepositoryMetadataResolverTest method testGetArtifactMetadataSnapshots.

@Test
public void testGetArtifactMetadataSnapshots() throws Exception {
    Collection<ArtifactMetadata> testArtifacts = storage.readArtifactsMetadata(new ReadMetadataRequest(TEST_REPO_ID, "com.example.test", "test-artifact", "1.0-SNAPSHOT", ALL));
    List<ArtifactMetadata> artifacts = new ArrayList<>(testArtifacts);
    Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {

        @Override
        public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });
    assertEquals(6, artifacts.size());
    ArtifactMetadata artifactMetadata = artifacts.get(0);
    assertEquals("test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId());
    MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(1, facet.getBuildNumber());
    assertEquals("20100308.230825", facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("jar", facet.getType());
    artifactMetadata = artifacts.get(1);
    assertEquals("test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(1, facet.getBuildNumber());
    assertEquals("20100308.230825", facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("pom", facet.getType());
    artifactMetadata = artifacts.get(2);
    assertEquals("test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(2, facet.getBuildNumber());
    assertEquals("20100310.014828", facet.getTimestamp());
    assertEquals("javadoc", facet.getClassifier());
    assertEquals("javadoc", facet.getType());
    artifactMetadata = artifacts.get(3);
    assertEquals("test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(2, facet.getBuildNumber());
    assertEquals("20100310.014828", facet.getTimestamp());
    assertEquals("sources", facet.getClassifier());
    assertEquals("java-source", facet.getType());
    artifactMetadata = artifacts.get(4);
    assertEquals("test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(2, facet.getBuildNumber());
    assertEquals("20100310.014828", facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("jar", facet.getType());
    artifactMetadata = artifacts.get(5);
    assertEquals("test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId());
    facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
    assertEquals(2, facet.getBuildNumber());
    assertEquals("20100310.014828", facet.getTimestamp());
    assertNull(facet.getClassifier());
    assertEquals("pom", facet.getType());
}
Also used : ReadMetadataRequest(org.apache.archiva.metadata.repository.storage.ReadMetadataRequest) ArrayList(java.util.ArrayList) MavenArtifactFacet(org.apache.archiva.metadata.model.maven2.MavenArtifactFacet) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) Test(org.junit.Test)

Aggregations

ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)96 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)23 Path (java.nio.file.Path)20 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)17 Date (java.util.Date)15 HashSet (java.util.HashSet)11 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)10 MavenArtifactFacet (org.apache.archiva.metadata.model.maven2.MavenArtifactFacet)9 HashMap (java.util.HashMap)8 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)8 MetadataResolutionException (org.apache.archiva.metadata.repository.MetadataResolutionException)7 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)7 ReadMetadataRequest (org.apache.archiva.metadata.repository.storage.ReadMetadataRequest)7 ArtifactCleanupFeature (org.apache.archiva.repository.features.ArtifactCleanupFeature)7 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)7 Node (javax.jcr.Node)6 ProjectVersionMetadata (org.apache.archiva.metadata.model.ProjectVersionMetadata)6 IOException (java.io.IOException)5 RepositoryException (javax.jcr.RepositoryException)5