Java Code Examples for javax.jcr.Node#addNode()
The following examples show how to use
javax.jcr.Node#addNode() .
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: TestEmptyPackage.java From jackrabbit-filevault with Apache License 2.0 | 6 votes |
/** * 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 2
Source File: ImportTests.java From jackrabbit-filevault with Apache License 2.0 | 6 votes |
/** * Creates an jcr archive at /archiveroot mapped to /testroot and imports it. */ @Test public void testJcrArchiveImport() throws IOException, RepositoryException, ConfigurationException { // create Jcr Archive Node archiveNode = admin.getRootNode().addNode(ARCHIVE_ROOT.substring(1, ARCHIVE_ROOT.length())); admin.save(); createNodes(archiveNode, 2, 4); admin.save(); assertNodeExists(ARCHIVE_ROOT + "/n3/n3/n3"); JcrArchive archive = new JcrArchive(archiveNode, TEST_ROOT); Node testRoot = admin.getRootNode().addNode(TEST_ROOT.substring(1, TEST_ROOT.length())); testRoot.addNode("dummy", "nt:folder"); admin.save(); archive.open(true); Node rootNode = admin.getNode(TEST_ROOT); ImportOptions opts = getDefaultOptions(); //opts.setListener(new DefaultProgressListener()); Importer importer = new Importer(opts); importer.run(archive, rootNode); admin.save(); assertNodeExists(TEST_ROOT + "/n3/n3/n3"); assertNodeMissing(TEST_ROOT + "dummy"); }
Example 3
Source File: TestFolderArtifactHandler.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
@Test public void testNotModifyingContainedNodeNtFolderPrimaryType() throws RepositoryException, IOException, PackageException { // create node "/testroot/foo" with node type "nt:unstructured" Node rootNode = admin.getRootNode(); Node testNode = rootNode.addNode("testroot", "nt:unstructured"); Node fooNode = testNode.addNode("foo", "nt:folder"); String oldId = fooNode.getIdentifier(); admin.save(); try (VaultPackage vltPackage = extractVaultPackageStrict("/test-packages/folder-without-docview-element.zip")) { assertNodeHasPrimaryType("/testroot/foo", "nt:folder"); assertPropertyMissing("/testroot/value"); assertEquals(oldId, admin.getNode("/testroot/foo").getIdentifier()); } }
Example 4
Source File: StorageUpdate14.java From nextreports-server with Apache License 2.0 | 5 votes |
private void createIFrameSettings() throws RepositoryException, IOException { LOG.info("Add IFrame Settings Node"); Node rootNode = getTemplate().getRootNode(); Node settingsNode = rootNode.getNode(StorageConstants.NEXT_SERVER_FOLDER_NAME + StorageConstants.PATH_SEPARATOR + StorageConstants.SETTINGS_FOLDER_NAME); Node iframeNode = settingsNode.addNode(StorageConstants.IFRAME); iframeNode.addMixin("mix:referenceable"); iframeNode.setProperty("className", IFrameSettings.class.getName()); iframeNode.setProperty(StorageConstants.IFRAME_ENABLE, true); iframeNode.setProperty(StorageConstants.IFRAME_AUTH, false); iframeNode.setProperty(StorageConstants.IFRAME_ENC, "next$"); getTemplate().save(); }
Example 5
Source File: TestSerialization.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
@Test public void exportJcrXmlTest() throws RepositoryException, IOException, PackageException { Node testRoot = admin.getRootNode().addNode("testroot", NodeType.NT_UNSTRUCTURED); Node nodeA = testRoot.addNode("a", NodeType.NT_UNSTRUCTURED); Node xmlText = nodeA.addNode("jcr:xmltext", NodeType.NT_UNSTRUCTURED); xmlText.setProperty("jcr:xmlcharacters", "Hello, World."); admin.save(); ExportOptions opts = new ExportOptions(); DefaultMetaInf inf = new DefaultMetaInf(); DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter(); filter.add(new PathFilterSet("/testroot/a")); inf.setFilter(filter); Properties props = new Properties(); props.setProperty(VaultPackage.NAME_GROUP, "jackrabbit/test"); props.setProperty(VaultPackage.NAME_NAME, "test-package"); inf.setProperties(props); opts.setMetaInf(inf); File tmpFile = File.createTempFile("vaulttest", "zip"); VaultPackage pkg = packMgr.assemble(admin, opts, tmpFile); // check if entries are present Archive.Entry e = pkg.getArchive().getEntry("/jcr_root/testroot/.content.xml"); assertNotNull("entry should exist", e); String src = IOUtils.toString(pkg.getArchive().getInputSource(e).getByteStream(), "utf-8"); String expected = "<?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" + " <a jcr:primaryType=\"nt:unstructured\">\n" + " <jcr:xmltext\n" + " jcr:primaryType=\"nt:unstructured\"\n" + " jcr:xmlcharacters=\"Hello, World.\"/>\n" + " </a>\n" + "</jcr:root>\n"; assertEquals("content.xml must be correct", expected, src); pkg.close(); tmpFile.delete(); }
Example 6
Source File: StorageUpdate1.java From nextreports-server with Apache License 2.0 | 5 votes |
private void createChartsNode() throws RepositoryException { LOG.info("Creating charts node"); Node rootNode = getTemplate().getRootNode(); Node nextServerNode = rootNode.getNode(StorageConstants.NEXT_SERVER_FOLDER_NAME); Node chartsNode = nextServerNode.addNode(StorageConstants.CHARTS_FOLDER_NAME); chartsNode.addMixin("mix:referenceable"); chartsNode.setProperty("className", Folder.class.getName()); getTemplate().save(); }
Example 7
Source File: TestFolderArtifactHandler.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
@Test public void testModifyingContainedNodeNonNtFolderPrimaryType() throws RepositoryException, IOException, PackageException { // create node "/testroot/foo" with node type "nt:unstructured" Node rootNode = admin.getRootNode(); Node testNode = rootNode.addNode("testroot", "nt:unstructured"); Node fooNode = testNode.addNode("foo", "nt:unstructured"); fooNode.setProperty("testProperty", "test"); admin.save(); try (VaultPackage vltPackage = extractVaultPackageStrict("/test-packages/folder-without-docview-element.zip")) { // make sure the primary type from "/test/foo" got overwritten! assertPropertyMissing("/testroot/foo/testProperty"); assertNodeHasPrimaryType("/testroot/foo", "nt:folder"); } }
Example 8
Source File: CNDImporter.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
private ImportInfoImpl parse(Node parent, String name) throws ParseException, RepositoryException { while (!currentTokenEquals(Lexer.EOF)) { if (!doNameSpace(parent.getSession())) { break; } } ImportInfoImpl info = new ImportInfoImpl(); while (!currentTokenEquals(Lexer.EOF)) { String ntName = doNodeTypeName(); if (name == null) { name = ntName; } if (parent.hasNode(name)) { parent.getNode(name).remove(); } Node node; if (parent.hasNode(name)) { parent.getNode(name).remove(); node = parent.addNode(name, JcrConstants.NT_NODETYPE); info.onReplaced(node.getPath()); } else { node = parent.addNode(name, JcrConstants.NT_NODETYPE); info.onCreated(node.getPath()); } node.setProperty(JcrConstants.JCR_NODETYPENAME, name); // init mandatory props node.setProperty(JcrConstants.JCR_HASORDERABLECHILDNODES, false); node.setProperty(JcrConstants.JCR_ISMIXIN, false); doSuperTypes(node); doOptions(node); doItemDefs(node); name = null; } return info; }
Example 9
Source File: StorageUpdate2.java From nextreports-server with Apache License 2.0 | 5 votes |
private void createDashboardsNode() throws RepositoryException { LOG.info("Creating dashboards node"); Node rootNode = getTemplate().getRootNode(); Node nextServerNode = rootNode.getNode(StorageConstants.NEXT_SERVER_FOLDER_NAME); Node dashboardsNode = nextServerNode.addNode(StorageConstants.DASHBOARDS_FOLDER_NAME); dashboardsNode.addMixin("mix:referenceable"); dashboardsNode.setProperty("className", Folder.class.getName()); getTemplate().save(); }
Example 10
Source File: ChildNodeStash.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
/** * create a transient node for temporarily stash the child nodes * @return the temporary node * @throws RepositoryException if an error occurrs */ private Node getOrCreateTemporaryNode() throws RepositoryException { if (tmpNode != null) { return tmpNode; } for (String rootPath: ROOTS) { try { Node root = session.getNode(rootPath); return tmpNode = root.addNode("tmp" + System.currentTimeMillis(), JcrConstants.NT_UNSTRUCTURED); } catch (RepositoryException e) { log.debug("unable to create temporary stash location below {}.", rootPath); } } throw new RepositoryException("Unable to create temporary root below."); }
Example 11
Source File: StorageUpdate7.java From nextreports-server with Apache License 2.0 | 5 votes |
private void addDestinations() throws RepositoryException { LOG.info("Add destinations to scheduler jobs"); String path = StorageConstants.SCHEDULER_ROOT ; String className = "ro.nextreports.server.domain.SchedulerJob"; String statement = "/jcr:root" + ISO9075.encodePath(path) + "//*[@className='" + className + "']"; QueryResult queryResult = getTemplate().query(statement); NodeIterator nodes = queryResult.getNodes(); LOG.info("Found " + nodes.getSize() + " scheduler job nodes"); while (nodes.hasNext()) { Node node = nodes.nextNode(); if (node.hasNode("mail")) { LOG.info("Found 'mail' node for '" + node.getName() + "'"); Node mailNode = node.getNode("mail"); LOG.info("Create '" + StorageConstants.DESTINATIONS + "' node for '" + node.getName() + "'"); Node destinationsNode = node.addNode(StorageConstants.DESTINATIONS); className = SmtpDestination.class.getName(); LOG.info("Change 'className' property for 'mail' node to '" + className + "'"); mailNode.setProperty("className", className); LOG.info("Move '" + mailNode.getName() + "' to '" + destinationsNode.getName() + "/mail"); getTemplate().move(mailNode.getPath(), destinationsNode.getPath() + "/mail"); } } getTemplate().save(); }
Example 12
Source File: StorageUpdate18.java From nextreports-server with Apache License 2.0 | 5 votes |
private void onUpdate() throws RepositoryException { String statement = "/jcr:root" + ISO9075.encodePath(StorageConstants.DATASOURCES_ROOT) + "//*[@className='ro.nextreports.server.domain.DataSource']"; QueryResult queryResult = getTemplate().query(statement); NodeIterator nodes = queryResult.getNodes(); LOG.info("Found " + nodes.getSize() + " data sources nodes"); while (nodes.hasNext()) { Node node = nodes.nextNode(); Node propertiesNode = node.addNode(StorageConstants.PROPERTIES); } getTemplate().save(); }
Example 13
Source File: StorageUpdate19.java From nextreports-server with Apache License 2.0 | 5 votes |
private void createIntegrationSettings() throws RepositoryException, IOException { LOG.info("Add Integration Settings Node"); Node rootNode = getTemplate().getRootNode(); Node settingsNode = rootNode.getNode(StorageConstants.NEXT_SERVER_FOLDER_NAME + StorageConstants.PATH_SEPARATOR + StorageConstants.SETTINGS_FOLDER_NAME); Node iframeNode = settingsNode.addNode(StorageConstants.INTEGRATION); iframeNode.addMixin("mix:referenceable"); iframeNode.setProperty("className", IntegrationSettings.class.getName()); iframeNode.setProperty(StorageConstants.INTEGRATION_DRILL_URL, ""); iframeNode.setProperty(StorageConstants.INTEGRATION_NOTIFY_URL, ""); getTemplate().save(); }
Example 14
Source File: JCRBuilder.java From jackalope with Apache License 2.0 | 5 votes |
/** * Builds the requested node. * * @param parent The parent node of the node to be built. If the node does not need a parent, null can be used. * @return the Node */ public NodeImpl build(Node parent) { try { NodeImpl node = (parent != null) ? (NodeImpl)parent.addNode(getName(), nodeTypeName) : new NodeImpl(new SessionImpl(), getName()); if (!Strings.isNullOrEmpty(nodeTypeName)) node.setPrimaryType(nodeTypeName); for (ItemBuilder builder : childBuilders) builder.build(node); node.getSession().save(); return node; } catch (RepositoryException re) { throw new SlingRepositoryException(re); } }
Example 15
Source File: JcrPackageRegistry.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
/** * yet another Convenience method to create intermediate nodes. * @param path path to create * @param autoSave if {@code true} all changes are automatically persisted * @return the node * @throws RepositoryException if an error occurrs */ public Node mkdir(String path, boolean autoSave) throws RepositoryException { if (session.nodeExists(path)) { return session.getNode(path); } String parentPath = Text.getRelativeParent(path, 1); if (path == null || ("/".equals(path) && parentPath.equals(path))) { throw new RepositoryException("could not create intermediate nodes"); } Node parent = mkdir(parentPath, autoSave); Node node = null; RepositoryException lastError = null; for (int i=0; node == null && i<FOLDER_TYPES.length; i++) { try { node = parent.addNode(Text.getName(path), FOLDER_TYPES[i]); } catch (RepositoryException e) { lastError = e; } } if (node == null) { if (lastError != null) { throw lastError; } else { throw new RepositoryException("Unable to create path: " + path); } } if (autoSave) { parent.getSession().save(); } return node; }
Example 16
Source File: IntegrationTestBase.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
public void createNodes(Node parent, int maxDepth, int nodesPerFolder) throws RepositoryException { for (int i=0; i<nodesPerFolder; i++) { Node n = parent.addNode("n" + i, "nt:folder"); if (maxDepth > 0) { createNodes(n, maxDepth - 1, nodesPerFolder); } } }
Example 17
Source File: JcrWorkspaceFilter.java From jackrabbit-filevault with Apache License 2.0 | 5 votes |
public static void saveFilter(WorkspaceFilter filter, Node defNode, boolean save) throws RepositoryException { if (defNode.hasNode(JcrPackageDefinitionImpl.NN_FILTER)) { defNode.getNode(JcrPackageDefinitionImpl.NN_FILTER).remove(); } Node filterNode = defNode.addNode(JcrPackageDefinitionImpl.NN_FILTER); int nr = 0; for (PathFilterSet set: filter.getFilterSets()) { Node setNode = filterNode.addNode("f" + nr); setNode.setProperty(JcrPackageDefinitionImpl.PN_ROOT, set.getRoot()); setNode.setProperty(JcrPackageDefinitionImpl.PN_MODE, set.getImportMode().name().toLowerCase()); List<String> rules = new LinkedList<String>(); for (ItemFilterSet.Entry e: set.getEntries()) { // expect path filter if (!(e.getFilter() instanceof DefaultPathFilter)) { throw new IllegalArgumentException("Can only handle default path filters."); } String type = e.isInclude() ? "include" : "exclude"; String patt = ((DefaultPathFilter) e.getFilter()).getPattern(); rules.add(type + ":" + patt); } setNode.setProperty(JcrPackageDefinitionImpl.PN_RULES, rules.toArray(new String[rules.size()])); nr++; } if (save) { defNode.getSession().save(); } }
Example 18
Source File: TestSpecialDoubleProperties.java From jackrabbit-filevault with Apache License 2.0 | 4 votes |
@Test public void exportDoubles() throws RepositoryException, IOException, PackageException { Node tmp = admin.getRootNode().addNode("tmp", "nt:unstructured"); Node content = tmp.addNode("jcr:content", "nt:unstructured"); content.setProperty("double_nan", Double.NaN); content.setProperty("double_pos_inf", Double.POSITIVE_INFINITY); content.setProperty("double_neg_inf", Double.NEGATIVE_INFINITY); admin.save(); ExportOptions opts = new ExportOptions(); DefaultMetaInf inf = new DefaultMetaInf(); DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter(); PathFilterSet set1 = new PathFilterSet("/tmp"); filter.add(set1); inf.setFilter(filter); Properties props = new Properties(); props.setProperty(VaultPackage.NAME_GROUP, "apache/test"); props.setProperty(VaultPackage.NAME_NAME, "test-package"); inf.setProperties(props); opts.setMetaInf(inf); File tmpFile = File.createTempFile("vaulttest", ".zip"); VaultPackage pkg = packMgr.assemble(admin, opts, tmpFile); Archive.Entry e = pkg.getArchive().getEntry("jcr_root/tmp/.content.xml"); InputSource is = pkg.getArchive().getInputSource(e); try (Reader r = new InputStreamReader(is.getByteStream(), "utf-8")) { String contentXml = IOUtils.toString(r); assertEquals("Serialized content", "<?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" + " <jcr:content\n" + " jcr:primaryType=\"nt:unstructured\"\n" + " double_nan=\"{Double}NaN\"\n" + " double_neg_inf=\"{Double}-Infinity\"\n" + " double_pos_inf=\"{Double}Infinity\"/>\n" + "</jcr:root>\n", contentXml); } pkg.close(); tmpFile.delete(); }
Example 19
Source File: JcrStorageDao.java From nextreports-server with Apache License 2.0 | 4 votes |
private Node addNodeWithUUID(Node parentNode, Entity entity, String UUID) { try { Node node; JcrNode jcrNode = ReflectionUtils.getJcrNodeAnnotation(entity.getClass()); // check if we should use a specific node type if (jcrNode == null || jcrNode.nodeType().equals("nt:unstructured")) { if (parentNode instanceof NodeImpl) { node = ((NodeImpl) parentNode).addNodeWithUuid(entity.getName(), UUID); } else { node = parentNode.addNode(entity.getName()); } } else { if (parentNode instanceof NodeImpl) { node = ((NodeImpl) parentNode).addNodeWithUuid(entity.getName(), jcrNode.nodeType(), UUID); } else { node = parentNode.addNode(entity.getName(), jcrNode.nodeType()); } } // add annotated mixin types if (jcrNode != null && jcrNode.mixinTypes() != null) { for (String mixinType : jcrNode.mixinTypes()) { if (node.canAddMixin(mixinType)) { node.addMixin(mixinType); } } } // update the object name and path setNodeName(entity, node.getName()); setNodePath(entity, node.getPath()); if (node.hasProperty("jcr:uuid")) { setUUID(entity, node.getIdentifier()); } // map the class name to a property if (jcrNode != null && !jcrNode.classNameProperty().equals("none")) { node.setProperty(jcrNode.classNameProperty(), entity.getClass().getCanonicalName()); } // do update to make internal jcrom business! getJcrom().updateNode(node, entity); return node; } catch (Exception e) { throw new JcrMappingException("Could not create node from object", e); } }
Example 20
Source File: StorageUpdate24.java From nextreports-server with Apache License 2.0 | 4 votes |
private void createOrUpdateCleanHistorySettings() throws RepositoryException, IOException { LOG.info("Update Clean History Settings Node"); Node rootNode = getTemplate().getRootNode(); Node settingsNode = rootNode.getNode(StorageConstants.NEXT_SERVER_FOLDER_NAME + StorageConstants.PATH_SEPARATOR + StorageConstants.SETTINGS_FOLDER_NAME); NodeIterator ni = settingsNode.getNodes(StorageConstants.CLEAN_HISTORY); Settings s = new Settings(); CleanHistorySettings ch = s.getCleanHistory(); boolean found = false; boolean foundSettings = false; while (ni.hasNext()) { Node nn = ni.nextNode(); // this is the node itself PropertyIterator ni2 = nn.getProperties(); foundSettings = true; while (ni2.hasNext()) { Property nn2 = ni2.nextProperty(); if (StorageConstants.DAYS_TO_DELETE.equals(nn2.getName())) { found = true; LOG.info("Found Clean History Settings Node DAYS_TO_DELETE skipping ..."); break; } } if (!found) { nn.setProperty(StorageConstants.DAYS_TO_DELETE, ch.getDaysToDelete()); getTemplate().save(); LOG.info("Did not Found Clean History Settings Node DAYS_TO_DELETE adding default ..."); break; } } if (!foundSettings) { LOG.info("Did not find Clean History Settings Node at all, adding default one"); Node cleanHistoryNode = settingsNode.addNode(StorageConstants.CLEAN_HISTORY); cleanHistoryNode.addMixin("mix:referenceable"); cleanHistoryNode.setProperty("className", CleanHistorySettings.class.getName()); cleanHistoryNode.setProperty(StorageConstants.DAYS_TO_KEEP, ch.getDaysToKeep()); cleanHistoryNode.setProperty(StorageConstants.CRON_EXPRESSION, ch.getCronExpression()); cleanHistoryNode.setProperty(StorageConstants.DAYS_TO_DELETE, ch.getDaysToDelete()); cleanHistoryNode.setProperty(StorageConstants.SHRINK_DATA_FOLDER, true); getTemplate().save(); } }