Java Code Examples for org.apache.jackrabbit.commons.JcrUtils#getOrCreateByPath()

The following examples show how to use org.apache.jackrabbit.commons.JcrUtils#getOrCreateByPath() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: PageManagerImpl.java    From jackalope with Apache License 2.0 5 votes vote down vote up
@Override
public Page create(String parentPath, String pageName, String template, String title,
                   boolean autoSave) throws WCMException {
    if (parentPath == null) throw new IllegalArgumentException("Parent path can't be null.");
    if (pageName == null && title == null)
        throw new IllegalArgumentException("Page and title name can't be both null.");
    if (template != null && !template.isEmpty())
        throw new UnsupportedOperationException("Templates are not supported.");

    try {
        Node parent = JcrUtils.getOrCreateByPath(parentPath, JcrConstants.NT_UNSTRUCTURED, session);

        if (pageName == null || pageName.isEmpty())
            pageName = JcrUtil.createValidName(title, JcrUtil.HYPHEN_LABEL_CHAR_MAPPING);
        if (!JcrUtil.isValidName(pageName)) throw new IllegalArgumentException("Illegal page name: " + pageName);

        Node pageNode = parent.addNode(pageName, JcrConstants.CQ_PAGE);
        Node contentNode = pageNode.addNode("jcr:content", JcrConstants.CQ_PAGE_CONTENT);

        if (title != null && !title.isEmpty()) contentNode.setProperty("jcr:title", title);
        if (autoSave) {
            session.save();
        }

        return getPage(pageNode.getPath());
    }
    catch (RepositoryException e) {
        throw new WCMException("Unable to create page", e);
    }
}
 
Example 7
Source File: JcrPackageRegistry.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the primary package root. If the root does not exist yet and {@code autoCreate} is {@code true} it will
 * be created.
 *
 * @param autoCreate if {@code true} the roots are created if missing.
 * @return the the package root or {@code null}
 * @throws RepositoryException if an error occurs.
 */
@Nullable
public Node getPrimaryPackageRoot(boolean autoCreate) throws RepositoryException {
    if (packRoots[0] == null) {
        if (session.nodeExists(packRootPaths[0])) {
            packRoots[0] = session.getNode(packRootPaths[0]);
        } else if (autoCreate) {
            if (session.hasPendingChanges()) {
                throw new RepositoryException("Unwilling to create package root folder while session has transient changes.");
            }
            packRoots[0] = JcrUtils.getOrCreateByPath(packRootPaths[0], NodeType.NT_FOLDER, NodeType.NT_FOLDER, session, true);
        }
    }
    return packRoots[0];
}
 
Example 8
Source File: DocViewSaxFormatterTest.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if an 'empty' node serialization includes the jcr namespace. see JCRVLT-266
 */
@Test
public void testFormatterIncludesJcrNamespace() throws Exception {
    // rep:itemNames restrictions are only supported in oak.
    Assume.assumeTrue(isOak());

    JcrUtils.getOrCreateByPath("/testroot", NodeType.NT_UNSTRUCTURED, admin);
    admin.save();

    // setup access control
    AccessControlManager acMgr = admin.getAccessControlManager();
    JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acMgr, "/testroot");

    Privilege[] privs = new Privilege[]{acMgr.privilegeFromName(Privilege.JCR_READ)};
    Map<String, Value[]> rest = new HashMap<>();
    rest.put("rep:itemNames", new Value[]{
            admin.getValueFactory().createValue("jcr:mixinTypes", PropertyType.NAME),
            admin.getValueFactory().createValue("jcr:primaryType", PropertyType.NAME)
    });
    acl.addEntry(EveryonePrincipal.getInstance(), privs, false, null, rest);
    acMgr.setPolicy("/testroot", acl);
    admin.save();

    Session guest = repository.login(new GuestCredentials());

    DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
    filter.add(new PathFilterSet("/testroot"));
    RepositoryAddress addr = new RepositoryAddress("/" + admin.getWorkspace().getName() + "/");
    VaultFileSystem jcrfs = Mounter.mount(null, filter, addr, null, guest);
    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\"/>\n", out.toString("utf-8"));
}
 
Example 9
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
@Test
public void testMixin() throws IOException, RepositoryException, ConfigurationException {
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_FOLDER, NodeType.NT_FOLDER, admin, true);
    RepositoryCopier rcp = new RepositoryCopier();
    a.addMixin(NodeType.MIX_TITLE);
    a.setProperty("jcr:title", "Hello");
    admin.save();

    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 10
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveMixin() throws IOException, RepositoryException, ConfigurationException {
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_FOLDER, NodeType.NT_FOLDER, admin, true);
    RepositoryCopier rcp = new RepositoryCopier();
    a.addMixin(NodeType.MIX_TITLE);
    a.setProperty("jcr:title", "Hello");
    admin.save();

    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"});


    a.removeMixin(NodeType.MIX_TITLE);
    admin.save();
    // removing a mixing should remove the undeclared properties
    assertPropertyMissing(SRC_TEST_NODE_PATH + "/jcr:title");
    assertPropertyMissingOrEmpty(SRC_TEST_NODE_PATH + "/jcr:mixinTypes");

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

    assertNodeExists(DST_TEST_NODE_PATH);
    assertPropertyMissing(DST_TEST_NODE_PATH + "/jcr:title");
    assertPropertyMissingOrEmpty(DST_TEST_NODE_PATH + "/jcr:mixinTypes");
}
 
Example 11
Source File: TestCompressionExport.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
private String storeFile(boolean compressible, String mimeType, int size)
        throws RepositoryException {
    String path = String.format("%s/%s", TEST_PARENT_PATH, fileName(compressible, mimeType, size));
    Node node = JcrUtils.getOrCreateByPath(path, "nt:unstructured", admin);
    byte[] data = compressible ? compressibleData(size) : incompressibleData(size);
    JcrUtils.putFile(node, "file", mimeType, new ByteArrayInputStream(data));
    admin.save();
    return node.getPath();
}
 
Example 12
Source File: DumpCoverageTests.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws RepositoryException {
    clean(TEST_ROOT);

    JcrUtils.getOrCreateByPath(TEST_ROOT, "nt:folder", admin);
    for (String path: ALL_PAGES) {
        JcrUtils.getOrCreateByPath(path, "nt:folder", admin);
        JcrUtils.getOrCreateByPath(path + "/jcr:content", "nt:folder", admin);
    }
}
 
Example 13
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnlyNewer() throws IOException, RepositoryException, ConfigurationException {
    Calendar now = Calendar.getInstance();
    Calendar then = Calendar.getInstance();
    then.setTimeInMillis(now.getTimeInMillis() + 1);

    // create /testroot/src/a/jcr:content with 'now' as last modified
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_FOLDER, NodeType.NT_FILE, admin, false);
    Node content = a.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
    content.setProperty(Property.JCR_LAST_MODIFIED, now);
    content.setProperty("p0", "0");
    admin.save();
    assertProperty(SRC_TEST_NODE_PATH + "/jcr:content/p0", "0");

    RepositoryCopier rcp = new RepositoryCopier();
    rcp.setOnlyNewer(false);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "0");

    // modify property but don't update last modified
    content.setProperty("p0", "1");
    admin.save();
    rcp = new RepositoryCopier();
    rcp.setOnlyNewer(true);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);
    // property should still be the old value, since src is not "newer"
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "0");

    // now update last modified
    content.setProperty(Property.JCR_LAST_MODIFIED, then);
    admin.save();
    rcp = new RepositoryCopier();
    rcp.setOnlyNewer(true);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);
    // property should now be the new value, since src is now "newer"
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "1");

}
 
Example 14
Source File: RCPTest.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
/**
 * Special test where the source node is nt:file with no mixins, and the destination node is nt:file with a mixin
 * and properties, and content is updated since it is newer (JCRVLT-87)
 */
@Test
public void testMissingMixinWithNewer() throws IOException, RepositoryException, ConfigurationException {
    Calendar now = Calendar.getInstance();
    Calendar then = Calendar.getInstance();
    then.setTimeInMillis(now.getTimeInMillis() + 1);

    // create /testroot/src/a/jcr:content with 'now' as last modified
    Node a = JcrUtils.getOrCreateByPath(SRC_TEST_NODE_PATH, NodeType.NT_FOLDER, NodeType.NT_FILE, admin, false);
    Node content = a.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
    content.setProperty(Property.JCR_LAST_MODIFIED, now);
    content.setProperty("p0", "0");
    admin.save();
    assertProperty(SRC_TEST_NODE_PATH + "/jcr:content/p0", "0");

    RepositoryCopier rcp = new RepositoryCopier();
    rcp.setOnlyNewer(false);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "0");

    // modify source property and add mixin to destination
    content.setProperty("p0", "1");
    Node dst = admin.getNode(DST_TEST_NODE_PATH);
    dst.addMixin(NodeType.MIX_TITLE);
    dst.setProperty(Property.JCR_TITLE, "Hello");
    admin.save();
    assertProperty(DST_TEST_NODE_PATH + "/jcr:title", "Hello");
    assertProperty(DST_TEST_NODE_PATH + "/jcr:mixinTypes", new String[]{"mix:title"});

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

    // property should still be the old value, since src is not "newer"
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "0");
    // mixin should already be gone, since file does not have a lastModified.
    assertPropertyMissingOrEmpty(DST_TEST_NODE_PATH + "/jcr:mixinTypes");
    assertPropertyMissing(DST_TEST_NODE_PATH + "/jcr:title");

    // now update last modified
    content.setProperty(Property.JCR_LAST_MODIFIED, then);
    admin.save();
    rcp = new RepositoryCopier();
    rcp.setOnlyNewer(true);
    rcp.setUpdate(true);
    rcp.copy(admin, SRC_PATH, admin, DST_PATH, true);

    // property should now be the new value, since src is now "newer"
    assertProperty(DST_TEST_NODE_PATH + "/jcr:content/p0", "1");
}
 
Example 15
Source File: TestCompressionExport.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws RepositoryException, PackageException, IOException {
    JcrUtils.getOrCreateByPath(TEST_PARENT_PATH, "nt:unstructured", admin);
    admin.save();
}