Search in sources :

Example 11 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class NodeTest method testAddNodeItemExistsException.

/**
     * Tries to create a node using {@link javax.jcr.Node#addNode(String,
     * String)}  at a location where there is already a node with same name and
     * the parent does not allow same name siblings. 
     * <p>
     * This should throw an {@link javax.jcr.ItemExistsException}.
     * <p>
     * Prerequisites:
     * <ul> <li><code>javax.jcr.tck.NodeTest.testAddNodeItemExistsException.nodetype</code>
     * node type that does not allow same name siblings and allows to add child
     * nodes of the same type.</li> </ul>
     */
public void testAddNodeItemExistsException() throws RepositoryException {
    // get default workspace test root node using superuser session
    Node defaultRootNode = (Node) superuser.getItem(testRootNode.getPath());
    // add a node
    Node defaultTestNode = defaultRootNode.addNode(nodeName2, testNodeType);
    // add a child
    defaultTestNode.addNode(nodeName3, testNodeType);
    // save the new node
    defaultRootNode.save();
    try {
        // try to add a node with same name again
        defaultTestNode.addNode(nodeName3, testNodeType);
        defaultRootNode.save();
        fail("Adding a node to a location where same name siblings are not allowed, but a node with same name" + " already exists should throw ItemExistsException ");
    } catch (ItemExistsException e) {
    //ok, works as expected
    }
}
Also used : ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node)

Example 12 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class NodeImpl method addNode.

/**
     * Same as <code>{@link Node#addNode(String, String)}</code> except that
     * this method takes <code>Name</code> arguments instead of
     * <code>String</code>s and has an additional <code>uuid</code> argument.
     * <p>
     * <b>Important Notice:</b> This method is for internal use only! Passing
     * already assigned uuid's might lead to unexpected results and
     * data corruption in the worst case.
     *
     * @param nodeName     name of the new node
     * @param nodeTypeName name of the new node's node type or <code>null</code>
     *                     if it should be determined automatically
     * @param id           id of the new node or <code>null</code> if a new
     *                     id should be assigned
     * @return the newly added node
     * @throws RepositoryException if the node can not added
     */
// FIXME: This method should not be public
public synchronized NodeImpl addNode(Name nodeName, Name nodeTypeName, NodeId id) throws RepositoryException {
    // check state of this instance
    sanityCheck();
    Path nodePath = PathFactoryImpl.getInstance().create(getPrimaryPath(), nodeName, true);
    // Check the explicitly specified node type (if any)
    NodeTypeImpl nt = null;
    if (nodeTypeName != null) {
        nt = sessionContext.getNodeTypeManager().getNodeType(nodeTypeName);
        if (nt.isMixin()) {
            throw new ConstraintViolationException("Unable to add a node with a mixin node type: " + sessionContext.getJCRName(nodeTypeName));
        } else if (nt.isAbstract()) {
            throw new ConstraintViolationException("Unable to add a node with an abstract node type: " + sessionContext.getJCRName(nodeTypeName));
        } else {
            // adding a node with explicit specifying the node type name
            // requires the editing session to have nt_management privilege.
            sessionContext.getAccessManager().checkPermission(nodePath, Permission.NODE_TYPE_MNGMT);
        }
    }
    // Get the applicable child node definition for this node.
    NodeDefinitionImpl def;
    try {
        def = getApplicableChildNodeDefinition(nodeName, nodeTypeName);
    } catch (RepositoryException e) {
        throw new ConstraintViolationException("No child node definition for " + sessionContext.getJCRName(nodeName) + " found in " + this, e);
    }
    // Use default node type from child node definition if needed
    if (nt == null) {
        nt = (NodeTypeImpl) def.getDefaultPrimaryType();
    }
    // check the new name
    NodeNameNormalizer.check(nodeName);
    // check for name collisions
    NodeState thisState = data.getNodeState();
    ChildNodeEntry cne = thisState.getChildNodeEntry(nodeName, 1);
    if (cne != null) {
        // check same-name sibling setting of new node
        if (!def.allowsSameNameSiblings()) {
            throw new ItemExistsException("This node already exists: " + itemMgr.safeGetJCRPath(nodePath));
        }
        // check same-name sibling setting of existing node
        NodeImpl existing = itemMgr.getNode(cne.getId(), getNodeId());
        if (!existing.getDefinition().allowsSameNameSiblings()) {
            throw new ItemExistsException("Same-name siblings not allowed for " + existing);
        }
    }
    // check protected flag of parent (i.e. this) node and retention/hold
    // make sure this node is checked-out and not locked by another session.
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD | ItemValidator.CHECK_RETENTION;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    // now do create the child node
    return createChildNode(nodeName, nt, id);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Example 13 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class UserImporterTest method testImportUuidCollisionThrow.

public void testImportUuidCollisionThrow() throws RepositoryException, IOException, SAXException {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sv:node sv:name=\"t\" xmlns:mix=\"http://www.jcp.org/jcr/mix/1.0\" xmlns:nt=\"http://www.jcp.org/jcr/nt/1.0\" xmlns:fn_old=\"http://www.w3.org/2004/10/xpath-functions\" xmlns:fn=\"http://www.w3.org/2005/xpath-functions\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:sv=\"http://www.jcp.org/jcr/sv/1.0\" xmlns:rep=\"internal\" xmlns:jcr=\"http://www.jcp.org/jcr/1.0\">" + "   <sv:property sv:name=\"jcr:primaryType\" sv:type=\"Name\"><sv:value>rep:User</sv:value></sv:property>" + "   <sv:property sv:name=\"jcr:uuid\" sv:type=\"String\"><sv:value>e358efa4-89f5-3062-b10d-d7316b65649e</sv:value></sv:property>" + "   <sv:property sv:name=\"rep:password\" sv:type=\"String\"><sv:value>{sha1}8efd86fb78a56a5145ed7739dcb00c78581c5375</sv:value></sv:property>" + "   <sv:property sv:name=\"rep:principalName\" sv:type=\"String\"><sv:value>t</sv:value></sv:property>" + "</sv:node>";
    NodeImpl target = (NodeImpl) sImpl.getNode(umgr.getUsersPath());
    try {
        doImport(target, xml);
        doImport(target, xml, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW, UserImporter.ImportBehavior.BESTEFFORT);
        fail("UUID collision must be handled according to the uuid behavior.");
    } catch (SAXException e) {
        assertTrue(e.getException() instanceof ItemExistsException);
    // success.
    } finally {
        sImpl.refresh(false);
        if (target.hasNode("t")) {
            target.getNode("t").remove();
            sImpl.save();
        }
    }
}
Also used : NodeImpl(org.apache.jackrabbit.core.NodeImpl) ItemExistsException(javax.jcr.ItemExistsException) SAXException(org.xml.sax.SAXException)

Example 14 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class WorkspaceRestoreTest method testWorkspaceRestoreWithUUIDConflict.

/**
     * Tests if restoring the <code>Version</code> of an existing node throws an
     * <code>ItemExistsException</code> if removeExisting is set to FALSE.
     */
@SuppressWarnings("deprecation")
public void testWorkspaceRestoreWithUUIDConflict() throws RepositoryException, NotExecutableException {
    try {
        // Verify that nodes used for the test are indeed versionable
        NodeDefinition nd = wVersionableNode.getDefinition();
        if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
            throw new NotExecutableException("Nodes must be versionable in order to run this test.");
        }
        Version v = wVersionableNode.checkin();
        wVersionableNode.checkout();
        wSuperuser.move(wVersionableChildNode.getPath(), wVersionableNode2.getPath() + "/" + wVersionableChildNode.getName());
        wSuperuser.save();
        wSuperuser.getWorkspace().restore(new Version[] { v }, false);
        fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
    } catch (ItemExistsException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 15 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class RestoreTest method testRestoreWithUUIDConflictJcr2_3.

/**
     * Tests if restoring the <code>Version</code> of an existing node throws an
     * <code>ItemExistsException</code> if removeExisting is set to FALSE.
     */
public void testRestoreWithUUIDConflictJcr2_3() throws RepositoryException, NotExecutableException {
    try {
        Node naa = createVersionableNode(versionableNode, nodeName4, versionableNodeType);
        // Verify that nodes used for the test have proper opv behaviour
        NodeDefinition nd = naa.getDefinition();
        if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
            throw new NotExecutableException("Child nodes must have OPV COPY or VERSION in order to be able to test Node.restore with uuid conflict.");
        }
        Version v = versionManager.checkin(versionableNode.getPath());
        versionManager.checkout(versionableNode.getPath());
        superuser.move(naa.getPath(), versionableNode2.getPath() + "/" + naa.getName());
        superuser.save();
        versionManager.restore(versionableNode.getPath(), v.getName(), false);
        fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
    } catch (ItemExistsException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Aggregations

ItemExistsException (javax.jcr.ItemExistsException)63 Node (javax.jcr.Node)25 RepositoryException (javax.jcr.RepositoryException)25 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 Version (javax.jcr.version.Version)13 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Name (org.apache.jackrabbit.spi.Name)12 NodeState (org.apache.jackrabbit.core.state.NodeState)10 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)9 Path (org.apache.jackrabbit.spi.Path)8 PathNotFoundException (javax.jcr.PathNotFoundException)6 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)6 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)6 NodeImpl (org.apache.jackrabbit.core.NodeImpl)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4