Search in sources :

Example 96 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.

the class IntermediatePathTest method testInvalidAbsolutePaths.

@Test
public void testInvalidAbsolutePaths() throws Exception {
    new NodeUtil(root.getTree("/")).addChild("testNode", NodeTypeConstants.NT_OAK_UNSTRUCTURED);
    List<String> invalidPaths = ImmutableList.of("/", PathUtils.getAncestorPath(UserConstants.DEFAULT_GROUP_PATH, 1), PathUtils.getAncestorPath(UserConstants.DEFAULT_GROUP_PATH, 2), "/testNode", "/nonExisting");
    for (String absPath : invalidPaths) {
        try {
            createAuthorizable(false, absPath);
            fail("Invalid path " + absPath + " outside of configured scope.");
        } catch (ConstraintViolationException e) {
        // success
        } finally {
            root.refresh();
        }
    }
}
Also used : ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeUtil(org.apache.jackrabbit.oak.util.NodeUtil) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Example 97 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.

the class NodeImpl method addNode.

@Override
@Nonnull
public Node addNode(final String relPath, String primaryNodeTypeName) throws RepositoryException {
    final String oakPath = getOakPathOrThrowNotFound(relPath);
    final String oakTypeName;
    if (primaryNodeTypeName != null) {
        oakTypeName = getOakName(primaryNodeTypeName);
    } else {
        oakTypeName = null;
    }
    checkIndexOnName(relPath);
    return perform(new ItemWriteOperation<Node>("addNode") {

        @Nonnull
        @Override
        public Node perform() throws RepositoryException {
            String oakName = PathUtils.getName(oakPath);
            String parentPath = PathUtils.getParentPath(oakPath);
            NodeDelegate parent = dlg.getChild(parentPath);
            if (parent == null) {
                // is it a property?
                String grandParentPath = PathUtils.getParentPath(parentPath);
                NodeDelegate grandParent = dlg.getChild(grandParentPath);
                if (grandParent != null) {
                    String propName = PathUtils.getName(parentPath);
                    if (grandParent.getPropertyOrNull(propName) != null) {
                        throw new ConstraintViolationException("Can't add new node to property.");
                    }
                }
                throw new PathNotFoundException(relPath);
            }
            if (parent.getChild(oakName) != null) {
                throw new ItemExistsException(relPath);
            }
            // modification of that property in the PermissionValidator
            if (oakTypeName != null) {
                PropertyState prop = PropertyStates.createProperty(JCR_PRIMARYTYPE, oakTypeName, NAME);
                sessionContext.getAccessManager().checkPermissions(parent.getTree(), prop, Permissions.NODE_TYPE_MANAGEMENT);
            }
            NodeDelegate added = parent.addChild(oakName, oakTypeName);
            if (added == null) {
                throw new ItemExistsException(format("Node [%s/%s] exists", getNodePath(), oakName));
            }
            return createNode(added, sessionContext);
        }

        @Override
        public String toString() {
            return format("Adding node [%s/%s]", dlg.getPath(), relPath);
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ItemExistsException(javax.jcr.ItemExistsException) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) Nonnull(javax.annotation.Nonnull)

Example 98 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.

the class CompatibilityIssuesTest method addNodeTest.

@Test
public void addNodeTest() throws RepositoryException {
    Session session = getAdminSession();
    // node type with default child-node type of to nt:base
    String ntName = "test";
    NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    ntt.setName(ntName);
    NodeDefinitionTemplate child = ntm.createNodeDefinitionTemplate();
    child.setName("*");
    child.setDefaultPrimaryTypeName("nt:base");
    child.setRequiredPrimaryTypeNames(new String[] { "nt:base" });
    List<NodeDefinition> children = ntt.getNodeDefinitionTemplates();
    children.add(child);
    ntm.registerNodeType(ntt, true);
    // try to create a node with the default nt:base
    Node node = session.getRootNode().addNode("defaultNtBase", ntName);
    // See OAK-1013
    node.addNode("nothrow");
    try {
        node.addNode("throw", "nt:hierarchyNode");
        fail("Abstract primary type should cause ConstraintViolationException");
    } catch (ConstraintViolationException expected) {
    }
    session.save();
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session) Test(org.junit.Test)

Example 99 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.

the class NodeTypeTest method removeNodeType.

@Test
public void removeNodeType() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    ValueFactory vf = session.getValueFactory();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    Node n = root.addNode("q1", "nt:query");
    n.setProperty("jcr:statement", vf.createValue("statement"));
    n.setProperty("jcr:language", vf.createValue("language"));
    session.save();
    try {
        manager.unregisterNodeType("nt:query");
        fail();
    } catch (ConstraintViolationException expected) {
    // this type is referenced in content, so it can't be removed
    }
    n.remove();
    session.save();
    try {
        manager.unregisterNodeType("nt:query");
    // no longer referenced in content, so removal should succeed
    } catch (ConstraintViolationException unexpected) {
        fail();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ValueFactory(javax.jcr.ValueFactory) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 100 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit-oak by apache.

the class NodeTypeTest method removeMandatoryPropertyFlag.

@Test
public void removeMandatoryPropertyFlag() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
    String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string) mandatory";
    CndImporter.registerNodeTypes(new StringReader(cnd), session);
    Node n = root.addNode("test", "test:MyType");
    n.setProperty("test:mandatory", "value");
    session.save();
    try {
        n.getProperty("test:mandatory").remove();
        session.save();
        fail("Must fail with ConstraintViolationException");
    } catch (ConstraintViolationException e) {
        // expected
        session.refresh(false);
    }
    // remove the mandatory property flag
    cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)";
    CndImporter.registerNodeTypes(new StringReader(cnd), session, true);
    // check node type
    NodeTypeDefinition ntd = manager.getNodeType("test:MyType");
    assertEquals(1, ntd.getDeclaredPropertyDefinitions().length);
    assertFalse(ntd.getDeclaredPropertyDefinitions()[0].isMandatory());
    // now we should be able to remove the property
    n.getProperty("test:mandatory").remove();
    session.save();
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) StringReader(java.io.StringReader) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)180 Node (javax.jcr.Node)74 RepositoryException (javax.jcr.RepositoryException)40 Name (org.apache.jackrabbit.spi.Name)33 Value (javax.jcr.Value)31 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)27 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)23 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)23 Session (javax.jcr.Session)18 ItemExistsException (javax.jcr.ItemExistsException)17 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 NodeId (org.apache.jackrabbit.core.id.NodeId)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 NodeType (javax.jcr.nodetype.NodeType)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)12