org.apache.jackrabbit.commons.JcrUtils Java Examples

The following examples show how to use org.apache.jackrabbit.commons.JcrUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestBinarylessExport.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
@Before
@Ignore
public void setup() throws RepositoryException, PackageException, IOException {
    // test only works for Jackrabbit 2.0 or Oak with FileDataStore
    Assume.assumeTrue(!isOak() || useFileStore());

    Node binaryNode = JcrUtils.getOrCreateByPath(BINARY_NODE_PATH, "nt:unstructured", admin);

    Binary bigBin = admin.getValueFactory().createBinary(IOUtils.toInputStream(BIG_TEXT, "UTF-8"));
    Property bigProperty = binaryNode.setProperty(BIG_BINARY_PROPERTY, bigBin);
    String referenceBigBinary = ((ReferenceBinary) bigProperty.getBinary()).getReference();
    assertNotNull(referenceBigBinary);

    Binary smallBin = admin.getValueFactory().createBinary(IOUtils.toInputStream(SMALL_TEXT, "UTF-8"));
    Property smallProperty = binaryNode.setProperty(SMALL_BINARY_PROPERTY, smallBin);
    if (isOak()) {
        assertTrue(smallProperty.getBinary() instanceof ReferenceBinary);
    } else {
        assertFalse(smallProperty.getBinary() instanceof ReferenceBinary);
    }

    JcrUtils.putFile(binaryNode.getParent(), "file", "text/plain", IOUtils.toInputStream(BIG_TEXT, "UTF-8"));
    admin.save();
}
 
Example #2
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repoId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);

    List<ArtifactMetadata> artifacts;
    try {
        QueryResult result = queryArtifactByDateRange(jcrSession, repoId, startTime, endTime, queryParameter);

        artifacts = new ArrayList<>();
        for (Node n : JcrUtils.getNodes(result)) {
            artifacts.add(getArtifactFromNode(repoId, n));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return artifacts;
}
 
Example #3
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private <T extends MetadataFacet> T createFacetFromNode(final MetadataFacetFactory<T> factory, final Node node,
                                                        final String repositoryId, final String name) throws RepositoryException {
    if (factory != null) {
        T metadataFacet;
        if (repositoryId != null) {
            metadataFacet = factory.createMetadataFacet(repositoryId, name);
        } else {
            metadataFacet = factory.createMetadataFacet();
        }
        Map<String, String> map = new HashMap<>();
        for (Property property : JcrUtils.getProperties(node)) {
            String p = property.getName();
            if (!p.startsWith("jcr:")) {
                map.put(p, property.getString());
            }
        }
        metadataFacet.fromProperties(map);
        return metadataFacet;
    }
    return null;
}
 
Example #4
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public List<ArtifactMetadata> getArtifactsByChecksum(RepositorySession session, String repositoryId, String checksum)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    List<ArtifactMetadata> artifacts;

    String q = getArtifactQuery(repositoryId).append(" AND ([artifact].[checksums/*/value] = $checksum)").toString();

    try {
        Query query = jcrSession.getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        ValueFactory valueFactory = jcrSession.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;
}
 
Example #5
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeProject(RepositorySession session, String repositoryId, String namespace, String projectId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String namespacePath = getNamespacePath(repositoryId, namespace);

        if (root.hasNode(namespacePath)) {
            Iterator<Node> nodeIterator = JcrUtils.getChildNodes(root.getNode(namespacePath)).iterator();
            while (nodeIterator.hasNext()) {
                Node node = nodeIterator.next();
                if (node.isNodeType(org.apache.archiva.metadata.repository.jcr.JcrConstants.PROJECT_MIXIN_TYPE) && projectId.equals(node.getName())) {
                    node.remove();
                }
            }

        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }

}
 
Example #6
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public List<ArtifactMetadata> getArtifacts(RepositorySession session, String repositoryId)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    List<ArtifactMetadata> artifacts;

    String q = getArtifactQuery(repositoryId).toString();

    try {
        Query query = jcrSession.getWorkspace().getQueryManager().createQuery(q, Query.JCR_SQL2);
        QueryResult result = query.execute();

        artifacts = new ArrayList<>();
        for (Node n : JcrUtils.getNodes(result)) {
            if (n.isNodeType(ARTIFACT_NODE_TYPE)) {
                artifacts.add(getArtifactFromNode(repositoryId, n));
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return artifacts;
}
 
Example #7
Source File: PageServiceImpl.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Page getPageByVersion(final String path, final String versionName) {
    return jcrTemplate.execute(session -> {
        Node rootNode = session.getRootNode();
        Node node = JcrUtils.getNodeIfExists(rootNode, path);
        if (node != null) {
            VersionManager vm = session.getWorkspace().getVersionManager();
            VersionHistory history = vm.getVersionHistory("/" + path);
            Version version = history.getVersion(versionName);
            if (version != null) {
                Node frozenNode = version.getFrozenNode();
                return convertNodeToPage(frozenNode);
            } else {
                return null;
            }
        } else {
            return null;
        }
    });
}
 
Example #8
Source File: PageServiceImpl.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Page restorePage(final String path, final String versionName) {
    return jcrTemplate.execute(session -> {
        Node rootNode = session.getRootNode();
        Node node = JcrUtils.getNodeIfExists(rootNode, path);
        if (node != null) {
            VersionManager vm = session.getWorkspace().getVersionManager();
            try {
                vm.restore("/" + path, versionName, true);
                node = JcrUtils.getNodeIfExists(rootNode, path);
                return convertNodeToPage(node);
            } catch (Exception e) {
                LOG.error("Error when restore document {} to version {}", path, versionName, e);
            }
        }
        return null;
    });

}
 
Example #9
Source File: PageServiceImpl.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<PageVersion> getPageVersions(final String path) {
    return jcrTemplate.execute(session -> {
        Node rootNode = session.getRootNode();
        Node node = JcrUtils.getNodeIfExists(rootNode, path);
        if (node != null) {
            VersionManager vm = session.getWorkspace().getVersionManager();
            VersionHistory history = vm.getVersionHistory("/" + path);
            List<PageVersion> versions = new ArrayList<>();
            for (VersionIterator it = history.getAllVersions(); it.hasNext(); ) {
                Version version = (Version) it.next();
                if (!"jcr:rootVersion".equals(version.getName())) {
                    versions.add(convertNodeToPageVersion(version));
                }
            }
            return versions;
        } else {
            return null;
        }
    });
}
 
Example #10
Source File: PageServiceImpl.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Page getPage(final String path, final String requestedUser) {
    return jcrTemplate.execute(session -> {
        Node rootNode = session.getRootNode();
        Node node = JcrUtils.getNodeIfExists(rootNode, path);
        if (node != null) {
            if (isNodePage(node)) {
                if (isAccessible(node, requestedUser)) {
                    return convertNodeToPage(node);
                } else {
                    return null;
                }
            }
        }

        return null;
    });
}
 
Example #11
Source File: TestAtomicCounter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if installing a package with a mix:atomicCounter works (update)
 */
@Test
public void updateAtomicCounter() throws RepositoryException, IOException, PackageException {
    Assume.assumeTrue(isOak());

    Node tmp = JcrUtils.getOrAddNode(admin.getRootNode(), "tmp", NodeType.NT_UNSTRUCTURED);
    Node testroot = JcrUtils.getOrAddNode(tmp, "testroot", NodeType.NT_UNSTRUCTURED);
    testroot.addMixin("mix:atomicCounter");
    testroot.setProperty("oak:increment", 5);
    admin.save();
    assertEquals(5L, testroot.getProperty("oak:counter").getLong());

    JcrPackage pack = packMgr.upload(getStream("/test-packages/atomic-counter-test.zip"), false);
    assertNotNull(pack);
    ImportOptions opts = getDefaultOptions();
    pack.install(opts);

    assertProperty("/tmp/testroot/oak:counter", "42");
}
 
Example #12
Source File: TestEmptyPackage.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * Installs a package that contains /tmp/test/content/foo/foo.jsp and then creates a new node
 * /tmp/test/content/bar/bar.jsp. Tests if after reinstall the new node was deleted.
 */
@Test
public void installEmptyFolder() throws RepositoryException, IOException, PackageException {
    JcrPackage pack = packMgr.upload(getStream("/test-packages/tmp_test_folders.zip"), false);
    assertNotNull(pack);
    pack.install(getDefaultOptions());
    assertNodeExists("/tmp/test/content/foo/foo.jsp");

    // create new node
    Node content = admin.getNode("/tmp/test/content");
    Node bar = content.addNode("bar", NodeType.NT_FOLDER);
    InputStream is = new ByteArrayInputStream("hello, world.".getBytes());
    JcrUtils.putFile(bar, "bar.jsp", "text/plain", is);
    admin.save();

    // now re-install package
    pack.install(getDefaultOptions());

    assertNodeMissing("/tmp/test/content/bar");
    assertNodeMissing("/tmp/test/content/bar/bar.jsp");
}
 
Example #13
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeProjectVersion(RepositorySession session, String repoId, String namespace, String projectId, String projectVersion)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {

        String path = getProjectPath(repoId, namespace, projectId);
        Node root = jcrSession.getRootNode();

        Node nodeAtPath = root.getNode(path);

        for (Node node : JcrUtils.getChildNodes(nodeAtPath)) {
            if (node.isNodeType(PROJECT_VERSION_NODE_TYPE) && StringUtils.equals(projectVersion,
                    node.getName())) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example #14
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node findArtifactNode(Session jcrSession, String namespace, String projectId,
                              String projectVersion, String id) throws RepositoryException {

    if (namespace==null || projectId==null||projectVersion==null||id==null) {
        return null;
    }
    Node root = jcrSession.getRootNode();
    Node node = JcrUtils.getOrAddNode(root, "repositories");
    for (Node n : JcrUtils.getChildNodes(node)) {
        String repositoryId = n.getName();
        Node repo = getOrAddRepositoryContentNode(jcrSession, repositoryId);
        Node nsNode = JcrUtils.getNodeIfExists(repo, StringUtils.replaceChars(namespace, '.', '/'));
        if (nsNode!=null) {
            Node projNode = JcrUtils.getNodeIfExists(nsNode, projectId);
            if (projNode !=null ) {
                Node projVersionNode = JcrUtils.getNodeIfExists(projNode, projectVersion);
                if (projVersionNode != null) {
                    return JcrUtils.getNodeIfExists(projVersionNode, id);
                }
            }
        }
    }

    return null;
}
 
Example #15
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node getOrAddNodeByPath(Node baseNode, String name, String nodeType, boolean primaryType)
        throws RepositoryException {
    log.debug("getOrAddNodeByPath " + baseNode + " " + name + " " + nodeType);
    Node node = baseNode;
    for (String n : name.split("/")) {
        if (nodeType != null && primaryType) {
            node = JcrUtils.getOrAddNode(node, n, nodeType);
        } else {
            node = JcrUtils.getOrAddNode(node, n);
            if (nodeType != null && !node.isNodeType(nodeType)) {
                node.addMixin(nodeType);
            }
        }
        if (!node.hasProperty("id")) {
            node.setProperty("id", n);
        }
    }
    return node;
}
 
Example #16
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
private Node getOrAddNodeByPath(Node baseNode, String name, String primaryType, String... mixinTypes)
        throws RepositoryException {
    log.debug("getOrAddNodeByPath baseNode={}, name={}, primary={}, mixin={}", baseNode, name, primaryType, mixinTypes);
    Node node = baseNode;
    for (String n : name.split("/")) {
        node = JcrUtils.getOrAddNode(node, n, primaryType);
        for (String mixin : mixinTypes) {
            if (mixin != null && !node.isNodeType(mixin)) {
                node.addMixin(mixin);
            }

        }
        if (!node.hasProperty("id")) {
            node.setProperty("id", n);
        }
    }
    return node;
}
 
Example #17
Source File: DocViewSaxFormatterTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * Tests minimal serialization
 */
@Test
public void testMinimalSerialization() throws Exception {
    JcrUtils.getOrCreateByPath("/testroot", NodeType.NT_UNSTRUCTURED, admin);
    admin.save();

    DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
    filter.add(new PathFilterSet("/testroot"));
    RepositoryAddress addr = new RepositoryAddress("/" + admin.getWorkspace().getName() + "/");
    VaultFileSystem jcrfs = Mounter.mount(null, filter, addr, null, admin);
    Aggregate a = jcrfs.getAggregateManager().getRoot().getAggregate("testroot");
    DocViewSerializer s = new DocViewSerializer(a);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    s.writeContent(out);

    assertEquals("valid xml",
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<jcr:root xmlns:jcr=\"http://www.jcp.org/jcr/1.0\" xmlns:nt=\"http://www.jcp.org/jcr/nt/1.0\"\n" +
            "    jcr:primaryType=\"nt:unstructured\"/>\n", out.toString("utf-8"));
}
 
Example #18
Source File: DocViewSaxFormatterTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * Tests export of mixed case named properties serialization
 */
@Test
public void testMixedCaseSerialization() throws Exception {
    Node node = JcrUtils.getOrCreateByPath("/testroot", NodeType.NT_UNSTRUCTURED, admin);
    node.setProperty("testproperty", "lowercase");
    node.setProperty("TestProperty", "MixedCase");
    admin.save();

    DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
    filter.add(new PathFilterSet("/testroot"));
    RepositoryAddress addr = new RepositoryAddress("/" + admin.getWorkspace().getName() + "/");
    VaultFileSystem jcrfs = Mounter.mount(null, filter, addr, null, admin);
    Aggregate a = jcrfs.getAggregateManager().getRoot().getAggregate("testroot");
    DocViewSerializer s = new DocViewSerializer(a);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    s.writeContent(out);

    assertEquals("valid xml",
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<jcr:root xmlns:jcr=\"http://www.jcp.org/jcr/1.0\" xmlns:nt=\"http://www.jcp.org/jcr/nt/1.0\"\n" +
            "    jcr:primaryType=\"nt:unstructured\"\n" +
            "    TestProperty=\"MixedCase\"\n" +
            "    testproperty=\"lowercase\"/>\n", out.toString("utf-8"));
}
 
Example #19
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddMixin() throws IOException, RepositoryException, ConfigurationException {
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_FOLDER, NodeType.NT_FOLDER, admin, true);
    RepositoryCopier rcp = new RepositoryCopier();
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);
    assertNodeExists(DST_TEST_NODE_PATH);
    assertPropertyMissing(DST_TEST_NODE_PATH + "/jcr:title");

    a.addMixin(NodeType.MIX_TITLE);
    a.setProperty("jcr:title", "Hello");
    admin.save();
    assertProperty(SRC_TEST_NODE_PATH + "/jcr:title", "Hello");

    rcp = new RepositoryCopier();
    rcp.setOnlyNewer(false);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);

    assertProperty(DST_TEST_NODE_PATH + "/jcr:title", "Hello");
    assertProperty(DST_TEST_NODE_PATH + "/jcr:mixinTypes", new String[]{"mix:title"});
}
 
Example #20
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() throws IOException, RepositoryException, ConfigurationException {
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_UNSTRUCTURED, NodeType.NT_UNSTRUCTURED, admin, true);
    a.setProperty("p0", "0");
    a.setProperty("p1", "1");
    a.setProperty("m0", new String[]{"0", "1", "2"}, PropertyType.STRING);
    admin.save();

    assertNodeExists(SRC_TEST_NODE_PATH);

    RepositoryCopier rcp = new RepositoryCopier();
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);

    assertProperty(DST_TEST_NODE_PATH + "/p0", "0");
    assertProperty(DST_TEST_NODE_PATH + "/p1", "1");
    assertProperty(DST_TEST_NODE_PATH + "/m0", new String[]{"0", "1", "2"});
}
 
Example #21
Source File: JcrRepositoryStatisticsGatheringTest.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void loadContentIntoRepo( RepositorySession repoSession, String repoId )
    throws RepositoryException, IOException, MetadataRepositoryException
{
        jcrSession = ((JcrRepositorySession) repoSession).getJcrSession();
    Node n = JcrUtils.getOrAddNode( jcrSession.getRootNode( ), "repositories" );
    n = JcrUtils.getOrAddNode( n, repoId );
    n = JcrUtils.getOrAddNode( n, "content" );
    n = JcrUtils.getOrAddNode( n, "org" );
    n = JcrUtils.getOrAddNode( n, "apache" );

    InputStream inputStream = getClass( ).getResourceAsStream( "/artifacts.xml" );
    jcrSession.importXML( n.getPath( ), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
    jcrSession.save( );
}
 
Example #22
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeFacetFromArtifact(RepositorySession session, String repositoryId, String namespace, String project, String projectVersion,
                                    MetadataFacet metadataFacet)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.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);
    }
}
 
Example #23
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddArtifactNode(Session jcrSession, String repositoryId, String namespace, String projectId, String projectVersion,
                                  String id)
        throws RepositoryException {
    Node versionNode = getOrAddProjectVersionNode(jcrSession, repositoryId, namespace, projectId, projectVersion);
    Node node = JcrUtils.getOrAddNode(versionNode, id, ARTIFACT_NODE_TYPE);
    if (!node.hasProperty("id")) {
        node.setProperty("id", id);
    }
    return node;
}
 
Example #24
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void retrieveFacetProperties(FacetedMetadata metadata, Node node) throws RepositoryException {
    for (Node n : JcrUtils.getChildNodes(node)) {
        if (n.isNodeType(FACET_NODE_TYPE)) {
            String name = n.getName();
            MetadataFacetFactory factory = metadataService.getFactory(name);
            if (factory == null) {
                log.error("Attempted to load unknown project version metadata facet: {}", name);
            } else {
                MetadataFacet facet = createFacetFromNode(factory, n);
                metadata.addFacet(facet);
            }
        }
    }
}
 
Example #25
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddProjectVersionNode(Session jcrSession, String repositoryId, String namespace, String projectId,
                                        String projectVersion)
        throws RepositoryException {
    Node projectNode = getOrAddProjectNode(jcrSession, repositoryId, namespace, projectId);
    log.debug("Project node {}", projectNode);
    Node projectVersionNode = JcrUtils.getOrAddNode(projectNode, projectVersion, PROJECT_VERSION_NODE_TYPE);
    if (!projectVersionNode.hasProperty("id")) {
        projectVersionNode.setProperty("id", projectVersion);
    }

    log.debug("Project version node {}", projectVersionNode);
    return projectVersionNode;
}
 
Example #26
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeTimestampedArtifact(RepositorySession session, ArtifactMetadata artifactMetadata, String baseVersion)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    String repositoryId = artifactMetadata.getRepositoryId();

    try {
        Node root = jcrSession.getRootNode();
        String path =
                getProjectVersionPath(repositoryId, artifactMetadata.getNamespace(), artifactMetadata.getProject(),
                        baseVersion);

        if (root.hasNode(path)) {
            Node node = root.getNode(path);

            for (Node n : JcrUtils.getChildNodes(node)) {
                if (n.isNodeType(ARTIFACT_NODE_TYPE)) {
                    if (n.hasProperty("version")) {
                        String version = n.getProperty("version").getString();
                        if (StringUtils.equals(version, artifactMetadata.getVersion())) {
                            n.remove();
                        }
                    }

                }
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }


}
 
Example #27
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddProjectNode(Session jcrSession, String repositoryId, String namespace, String projectId)
        throws RepositoryException {
    Node namespaceNode = getOrAddNamespaceNode(jcrSession, repositoryId, namespace);
    Node node = JcrUtils.getOrAddNode(namespaceNode, projectId, FOLDER_TYPE);
    if (!node.isNodeType(PROJECT_MIXIN_TYPE)) {
        node.addMixin(PROJECT_MIXIN_TYPE);
    }
    if (!node.hasProperty("id")) {
        node.setProperty("id", projectId);
    }
    return node;
}
 
Example #28
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private Node getOrAddRepositoryNode(Session jcrSession, String repositoryId)
        throws RepositoryException {
    log.debug("getOrAddRepositoryNode " + repositoryId);
    Node root = jcrSession.getRootNode();
    Node node = JcrUtils.getOrAddNode(root, "repositories");
    log.debug("Repositories " + node);
    node = JcrUtils.getOrAddNode(node, repositoryId, REPOSITORY_NODE_TYPE);
    if (!node.hasProperty("id")) {
        node.setProperty("id", repositoryId);
    }
    return node;
}
 
Example #29
Source File: PageServiceImpl.java    From mycollab with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<Page> getPages(final String path, final String requestedUser) {
    return jcrTemplate.execute(session -> {
        Node rootNode = session.getRootNode();
        Node node = JcrUtils.getNodeIfExists(rootNode, path);
        if (node != null) {
            if (isNodeFolder(node)) {
                List<Page> pages = new ArrayList<>();
                NodeIterator childNodes = node.getNodes();
                while (childNodes.hasNext()) {
                    Node childNode = childNodes.nextNode();
                    if (isNodePage(childNode)) {
                        if (isAccessible(childNode, requestedUser)) {
                            Page page = convertNodeToPage(childNode);
                            pages.add(page);
                        }
                    }
                }

                return pages;
            } else {
                throw new ContentException(String.format("Do not support any node type except mycollab:folder. The current node has type: %s and its path is %s",
                        node.getPrimaryNodeType().getName(), path));
            }
        }
        return new ArrayList<Page>();
    });
}
 
Example #30
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
@Override
public void removeArtifact(RepositorySession session, String repositoryId, String namespace, String projectId, String projectVersion,
                           String id)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getArtifactPath(repositoryId, namespace, projectId, projectVersion, id);
        if (root.hasNode(path)) {
            root.getNode(path).remove();
        }

        // remove version

        path = getProjectPath(repositoryId, namespace, projectId);

        Node nodeAtPath = root.getNode(path);

        for (Node node : JcrUtils.getChildNodes(nodeAtPath)) {
            if (node.isNodeType(PROJECT_VERSION_NODE_TYPE) //
                    && StringUtils.equals(node.getName(), projectVersion)) {
                node.remove();
            }
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}