private void processChildNodeDefinition( Node nodeTypeNode, NodeDefinition childNodeDefinition ) throws RepositoryException { Node childNode = nodeTypeNode.addNode(CHILD_NODE_DEFINITION, CHILD_NODE_DEFINITION); if (!RESIDUAL_ITEM_NAME.equals(childNodeDefinition.getName())) { childNode.setProperty(NAME, childNodeDefinition.getName()); } childNode.setProperty(AUTO_CREATED, childNodeDefinition.isAutoCreated()); childNode.setProperty(MANDATORY, childNodeDefinition.isMandatory()); childNode.setProperty(ON_PARENT_VERSION, OnParentVersionAction.nameFromValue(childNodeDefinition.getOnParentVersion())); childNode.setProperty(PROTECTED, childNodeDefinition.isProtected()); String[] requiredPrimaryTypeNames = childNodeDefinition.getRequiredPrimaryTypeNames(); childNode.setProperty(REQUIRED_PRIMARY_TYPES, requiredPrimaryTypeNames != null ? requiredPrimaryTypeNames : new String[0]); childNode.setProperty(SAME_NAME_SIBLINGS, childNodeDefinition.allowsSameNameSiblings()); childNode.setProperty(DEFAULT_PRIMARY_TYPE, childNodeDefinition.getDefaultPrimaryTypeName()); }
private void delete(Node folder, Node offspring) throws RepositoryException, WorkflowException { if (offspring.isNodeType(HippoNodeType.NT_DOCUMENT) && offspring.getParent().isNodeType(NT_HANDLE)) { offspring = offspring.getParent(); } if (!offspring.isNodeType(NT_HANDLE)) { for (NodeIterator iter = offspring.getNodes(); iter.hasNext(); ) { Node child = iter.nextNode(); NodeDefinition nd = child.getDefinition(); if (!nd.getDeclaringNodeType().isMixin()) { throw new WorkflowException("Folder is not empty; cannot be deleted"); } } } offspring.remove(); folder.getSession().save(); }
/** * Creates a new node definition based on a JCR <code>NodeDefinition</code>. * * @param nodeDef the node definition. * @param resolver the name/path resolver of the session that provided the * node definition * @throws NameException if <code>nodeDef</code> contains an illegal * name. * @throws NamespaceException if <code>nodeDef</code> contains a name with * an namespace prefix that is unknown to * <code>resolver</code>. */ public QNodeDefinitionImpl(NodeDefinition nodeDef, NamePathResolver resolver) throws NameException, NamespaceException { this(nodeDef.getName().equals(NameConstants.ANY_NAME.getLocalName()) ? NameConstants.ANY_NAME : resolver.getQName(nodeDef.getName()), nodeDef.getDeclaringNodeType() != null ? resolver.getQName(nodeDef.getDeclaringNodeType().getName()) : null, nodeDef.isAutoCreated(), nodeDef.isMandatory(), nodeDef.getOnParentVersion(), nodeDef.isProtected(), nodeDef.getDefaultPrimaryType() != null ? resolver.getQName(nodeDef.getDefaultPrimaryType().getName()) : null, getNodeTypeNames(nodeDef.getRequiredPrimaryTypes(), resolver), nodeDef.allowsSameNameSiblings()); }
private NodeDefinitionImpl(NodeDefinition definition) { super(definition); requiredPrimaryTypes = definition.getRequiredPrimaryTypes(); defaultPrimaryType = definition.getDefaultPrimaryType(); allowsSameNameSiblings = definition.allowsSameNameSiblings(); }
NodeDefinitionId(NodeDefinition def) { declaringNodeType = def.getDeclaringNodeType().getName(); name = def.getName(); }
public void testIndexedChildDefinition() throws Exception { String ntPath = NodeTypeConstants.NODE_TYPES_PATH + '/' + NodeTypeConstants.NT_VERSIONHISTORY; assertTrue(superuser.nodeExists(ntPath + "/jcr:childNodeDefinition")); assertTrue(superuser.nodeExists(ntPath + "/jcr:childNodeDefinition[1]")); Node cdNode = superuser.getNode(ntPath + "/jcr:childNodeDefinition[1]"); assertEquals(ntPath + "/jcr:childNodeDefinition", cdNode.getPath()); List<String> defNames = new ArrayList(); NodeType nt = superuser.getWorkspace().getNodeTypeManager().getNodeType(NodeTypeConstants.NT_VERSIONHISTORY); for (NodeDefinition nd : nt.getDeclaredChildNodeDefinitions()) { defNames.add(nd.getName()); } Node ntNode = superuser.getNode(ntPath); NodeIterator it = ntNode.getNodes("jcr:childNodeDefinition*"); while (it.hasNext()) { Node def = it.nextNode(); int index = getIndex(def); String name = (def.hasProperty(NodeTypeConstants.JCR_NAME)) ? def.getProperty(NodeTypeConstants.JCR_NAME).getString() : NodeTypeConstants.RESIDUAL_NAME; assertEquals(name, defNames.get(index-1)); } }
/** * Make sure repo-level acl is not imported below any other node than the * root node. * * @throws Exception */ public void testImportRepoACLAtTestNode() throws Exception { try { Node target = testRootNode.addNode("test"); target.addMixin("rep:RepoAccessControllable"); doImport(target.getPath(), XML_REPO_POLICY_TREE); assertTrue(target.hasNode("rep:repoPolicy")); assertFalse(target.hasNode("rep:repoPolicy/allow0")); Node n = target.getNode("rep:repoPolicy"); assertEquals("rep:RepoAccessControllable", n.getDefinition().getDeclaringNodeType().getName()); try { superuser.save(); fail("Importing repo policy to non-root node must fail"); } catch (AccessControlException e) { // success } } finally { superuser.refresh(false); } } }
@Override protected void setUp() throws Exception { super.setUp(); Node n1 = testRootNode.addNode(nodeName1, NT_FILE); Node content = n1.addNode(JCR_CONTENT, NT_RESOURCE); content.setProperty(JCR_DATA, "val"); assertEquals(OnParentVersionAction.VERSION, n1.getDefinition().getOnParentVersion()); assertEquals(OnParentVersionAction.COPY, content.getDefinition().getOnParentVersion()); testRootNode.addMixin(MIX_VERSIONABLE); superuser.save(); versionManager = superuser.getWorkspace().getVersionManager(); frozen = versionManager.checkpoint(testRoot).getFrozenNode(); }
private void clone(Node node, Node parent) throws RepositoryException { if (node.getDefinition().isAutoCreated()) { Node destination = parent.getNode(node.getName()); this.removeProperties(destination); this.updateProperties(node, destination); } else { final String parentPath = parent.getPath(); final String srcWorkspaceLogicalName = node.getSession().getWorkspace().getName(); final String srcWorkspacePhysicalName = repositoryManager.getWorkspaceMapping(srcWorkspaceLogicalName).getPhysicalWorkspaceName(); final Workspace targetWorkspace = parent.getSession().getWorkspace(); final String srcPath = node.getPath(); final String targetPath = parentPath + (parentPath != null && parentPath.endsWith("/") ? "" : "/") + node.getName(); log.debug("workspace level clone from {}:{} to {}:{}", srcWorkspaceLogicalName, srcPath, targetWorkspace.getName(), parentPath); targetWorkspace.clone(srcWorkspacePhysicalName, srcPath, targetPath, true); } }
public void testDirectChild() throws Exception { testRootNode.addMixin("OpvAbortTest"); Node n = testRootNode.addNode("child", NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertEquals(OnParentVersionAction.ABORT, n.getDefinition().getOnParentVersion()); superuser.save(); try { vMgr.checkpoint(testRootNode.getPath()); fail(); } catch (VersionException e) { // success } finally { superuser.refresh(false); } }
private void clear(final Node node) throws RepositoryException { for (Property property : new PropertyIterable(node.getProperties())) { if (!property.getDefinition().isProtected()) { property.remove(); } } for (Node child : new NodeIterable(node.getNodes())) { if (!child.getDefinition().isProtected()) { child.remove(); } } for (NodeType nodeType : node.getMixinNodeTypes()) { if (!nodeType.isNodeType(JcrConstants.MIX_VERSIONABLE)) { node.removeMixin(nodeType.getName()); } } }
@Test public void shouldAllowAddingNewChildNodeAfterAddingMixin() throws Exception { session.getRootNode().addNode("a", PRIMARY_TYPE_A).addMixin("mix:referenceable"); session.save(); Node rootNode = session.getRootNode(); Node nodeA = rootNode.getNode("a"); assertThat(nodeA.canAddMixin(MIXIN_TYPE_B), is(true)); nodeA.addMixin(MIXIN_TYPE_B); nodeA.addNode(CHILD_NODE_B); session.save(); Node newRootNode = session.getRootNode(); Node newNodeA = newRootNode.getNode("a"); Node newNodeB = newNodeA.getNode(CHILD_NODE_B); assertThat(newNodeB, is(notNullValue())); assertThat(newNodeB.getDefinition().getDeclaringNodeType().getName(), is(MIXIN_TYPE_B)); }
private void addIgnoredChild(@NotNull Node node) throws Exception { AccessControlManager acMgr = superuser.getAccessControlManager(); JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acMgr, node.getPath()); acl.addAccessControlEntry(EveryonePrincipal.getInstance(), AccessControlUtils.privilegesFromNames(acMgr, Privilege.JCR_READ)); acMgr.setPolicy(acl.getPath(), acl); superuser.save(); Node c = node.getNode(AccessControlConstants.REP_POLICY); assertEquals(OnParentVersionAction.IGNORE, c.getDefinition().getOnParentVersion()); }
protected String createNodeName(final Node sourceNode) throws RepositoryException { final String name = sourceNode.getName(); if (sourceNode.getIndex() > 1) { return name + "[" + sourceNode.getIndex() + "]"; } else { if (sourceNode.getDefinition().allowsSameNameSiblings() && sourceNode.getParent().hasNode(name + "[2]")) { return name + "[1]"; } } return name; }
/** * Tests the element test with a name test argument and a type argument that * matches only certain child nodes. Additonally this test requires that * testroot allows same name sibling child nodes. */ public void testElementTestNameTestSomeNTWithSNS() throws RepositoryException, NotExecutableException { Node n1 = testRootNode.addNode(nodeName1, testNodeType); if (!n1.getDefinition().allowsSameNameSiblings()) { throw new NotExecutableException("Node at " + testRoot + " does not allow same name siblings with name " + nodeName1); } testRootNode.addNode(nodeName1, simpleNodeType); Node n2 = testRootNode.addNode(nodeName1, testNodeType); testRootNode.addNode(nodeName2, simpleNodeType); testRootNode.addNode(nodeName3, testNodeType); testRootNode.getSession().save(); String query = "/" + jcrRoot + testRoot + "/element(" + nodeName1 + ", " + testNodeType + ")"; executeXPathQuery(superuser, query, new Node[]{n1, n2}); } }
public void testCreation() throws NotExecutableException, RepositoryException { Node n; try { n = testRootNode.addNode(nodeName1, testNodeType); } catch (RepositoryException e) { throw new NotExecutableException(); } try { testRootNode.save(); fail("Saving without having added the mandatory child items must fail."); } catch (ConstraintViolationException e) { // success } if (childNodeDef != null) { n.addNode(childNodeDef.getName(), childNodeDef.getDefaultPrimaryType().getName()); } if (childPropDef != null) { // TODO: check if definition defines default values n.setProperty(childPropDef.getName(), "any value"); } // now save must succeed. testRootNode.save(); }
@Override public void checkMandatoryItems(Tree tree) throws ConstraintViolationException { for (NodeType nodeType : nodeTypes.values()) { for (PropertyDefinition pd : nodeType.getPropertyDefinitions()) { String name = pd.getName(); if (pd.isMandatory() && !pd.isProtected() && tree.getProperty(name) == null) { throw new ConstraintViolationException( "Property '" + name + "' in '" + nodeType.getName() + "' is mandatory"); } } for (NodeDefinition nd : nodeType.getChildNodeDefinitions()) { String name = nd.getName(); if (nd.isMandatory() && !nd.isProtected() && !tree.hasChild(name)) { throw new ConstraintViolationException( "Node '" + name + "' in '" + nodeType.getName() + "' is mandatory"); } } } }
private static void clear(Node node) throws RepositoryException { for (Node child : new NodeIterable(node.getNodes())) { if (child.getDefinition().isAutoCreated()) { clear(child); } else { child.remove(); } } for (Property property : new PropertyIterable(node.getProperties())) { if (property.getName().startsWith("jcr:") || property.getDefinition().isProtected() || immune.contains(property.getName())) { continue; } property.remove(); } }
@Override protected void setUp() throws Exception { super.setUp(); NodeType nt = superuser.getWorkspace().getNodeTypeManager().getNodeType(testNodeType); NodeDefinition[] ndefs = nt.getChildNodeDefinitions(); for (int i = 0; i < ndefs.length; i++) { if (ndefs[i].isMandatory() && !ndefs[i].isProtected() && !ndefs[i].isAutoCreated()) { childNodeDef = ndefs[i]; break; } } PropertyDefinition[] pdefs = nt.getPropertyDefinitions(); for (int i = 0; i < pdefs.length; i++) { if (pdefs[i].isMandatory() && !pdefs[i].isProtected() && !pdefs[i].isAutoCreated()) { childPropDef = pdefs[i]; break; } } if (childPropDef == null && childNodeDef == null) { cleanUp(); throw new NotExecutableException(); } }
/** * Make sure repo-level acl is not imported below any other node than the * root node. * * @throws Exception */ public void testImportRepoACLAtTestNode() throws Exception { NodeImpl target = (NodeImpl) testRootNode.addNode("test"); target.addMixin("rep:RepoAccessControllable"); AccessControlManager acMgr = sImpl.getAccessControlManager(); try { InputStream in = new ByteArrayInputStream(XML_REPO_POLICY_TREE.getBytes("UTF-8")); SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, new PseudoConfig()); ImportHandler ih = new ImportHandler(importer, sImpl); new ParsingContentHandler(ih).parse(in); AccessControlPolicy[] policies = acMgr.getPolicies(null); assertEquals(0, policies.length); assertTrue(target.hasNode("rep:repoPolicy")); assertFalse(target.hasNode("rep:repoPolicy/allow0")); Node n = target.getNode("rep:repoPolicy"); assertEquals("rep:RepoAccessControllable", n.getDefinition().getDeclaringNodeType().getName()); } finally { superuser.refresh(false); } }