Search in sources :

Example 6 with ItemExistsException

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

the class RestoreTest method testRestoreWithUUIDConflictJcr2_2.

/**
     * 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_2() 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(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) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 7 with ItemExistsException

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

the class WorkspaceMoveTest method testMovePropertyExists.

/**
     * Tries to move a node using to a location where a property already exists
     * with same name.
     * <br/> <br/>
     * With JCR 1.0 this should throw an <code>{@link javax.jcr.ItemExistsException}</code>.
     * With JCR 2.0 the support for same-named property and node is optional and
     * the expected behaviour depends on the
     * {@link Repository#OPTION_NODE_AND_PROPERTY_WITH_SAME_NAME_SUPPORTED} descriptor.
     */
@Override
public void testMovePropertyExists() throws RepositoryException, NotExecutableException {
    // try to create a property with the name of the node to be moved
    // to the destination parent
    Property destProperty;
    try {
        destProperty = destParentNode.setProperty(nodeName2, "anyString");
        destParentNode.save();
    } catch (RepositoryException e) {
        throw new NotExecutableException("Cannot create property with name '" + nodeName2 + "' and value 'anyString' at move destination.");
    }
    // TODO: fix 2.0 behaviour according to the OPTION_NODE_AND_PROPERTY_WITH_SAME_NAME_SUPPORTED descriptor
    if ("1.0".equals(getHelper().getRepository().getDescriptor(Repository.SPEC_VERSION_DESC))) {
        try {
            // move the node
            doMove(moveNode.getPath(), destProperty.getPath());
            fail("Moving a node to a location where a property exists must throw ItemExistsException");
        } catch (ItemExistsException e) {
        // ok, works as expected
        }
    } else {
        // JCR 2.0 move the node: same name property and node must be supported
        doMove(moveNode.getPath(), destProperty.getPath());
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) ItemExistsException(javax.jcr.ItemExistsException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 8 with ItemExistsException

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

the class WorkspaceImporter method resolveUUIDConflict.

/**
     * @param parent parent node state
     * @param conflicting conflicting node state
     * @param nodeInfo the node info
     * @return the resolved node state
     * @throws RepositoryException if an error occurs
     */
protected NodeState resolveUUIDConflict(NodeState parent, NodeState conflicting, NodeInfo nodeInfo) throws RepositoryException {
    NodeState node;
    if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW) {
        // create new with new uuid:
        // check if new node can be added (check access rights &
        // node type constraints only, assume locking & versioning status
        // and retention/hold has already been checked on ancestor)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_CONSTRAINTS);
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), null);
        // remember uuid mapping
        EffectiveNodeType ent = itemOps.getEffectiveNodeType(node);
        if (ent.includesNodeType(NameConstants.MIX_REFERENCEABLE)) {
            refTracker.mappedId(nodeInfo.getId(), node.getNodeId());
        }
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW) {
        // new node and share with existing
        if (conflicting.isShareable()) {
            itemOps.clone(conflicting, parent, nodeInfo.getName());
            return null;
        }
        String msg = "a node with uuid " + nodeInfo.getId() + " already exists!";
        log.debug(msg);
        throw new ItemExistsException(msg);
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING) {
        // make sure conflicting node is not importTarget or an ancestor thereof
        Path p0 = hierMgr.getPath(importTarget.getNodeId());
        Path p1 = hierMgr.getPath(conflicting.getNodeId());
        try {
            if (p1.equals(p0) || p1.isAncestorOf(p0)) {
                String msg = "cannot remove ancestor node";
                log.debug(msg);
                throw new ConstraintViolationException(msg);
            }
        } catch (MalformedPathException mpe) {
            // should never get here...
            String msg = "internal error: failed to determine degree of relationship";
            log.error(msg, mpe);
            throw new RepositoryException(msg, mpe);
        }
        // remove conflicting:
        // check if conflicting can be removed
        // (access rights, node type constraints, locking & versioning status)
        itemOps.checkRemoveNode(conflicting, BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // do remove conflicting (recursive)
        itemOps.removeNodeState(conflicting);
        // create new with given uuid:
        // check if new node can be added (check access rights &
        // node type constraints only, assume locking & versioning status
        // and retention/hold has already been checked on ancestor)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_CONSTRAINTS);
        // do create new node
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), nodeInfo.getId());
    } else if (uuidBehavior == ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING) {
        NodeId parentId = conflicting.getParentId();
        if (parentId == null) {
            String msg = "root node cannot be replaced";
            log.debug(msg);
            throw new RepositoryException(msg);
        }
        // 'replace' current parent with parent of conflicting
        try {
            parent = itemOps.getNodeState(parentId);
        } catch (ItemNotFoundException infe) {
            // should never get here...
            String msg = "internal error: failed to retrieve parent state";
            log.error(msg, infe);
            throw new RepositoryException(msg, infe);
        }
        // remove conflicting:
        // check if conflicting can be removed
        // (access rights, node type constraints, locking & versioning status)
        itemOps.checkRemoveNode(conflicting, BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // 'replace' is actually a 'remove existing/add new' operation;
        // this unfortunately changes the order of the parent's
        // child node entries (JCR-1055);
        // => backup list of child node entries beforehand in order
        // to restore it afterwards
        ChildNodeEntry cneConflicting = parent.getChildNodeEntry(nodeInfo.getId());
        List<ChildNodeEntry> cneList = new ArrayList<ChildNodeEntry>(parent.getChildNodeEntries());
        // do remove conflicting (recursive)
        itemOps.removeNodeState(conflicting);
        // create new with given uuid at same location as conflicting:
        // check if new node can be added at other location
        // (access rights, node type constraints, locking & versioning
        // status and retention/hold)
        itemOps.checkAddNode(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), BatchedItemOperations.CHECK_ACCESS | BatchedItemOperations.CHECK_LOCK | BatchedItemOperations.CHECK_CHECKED_OUT | BatchedItemOperations.CHECK_CONSTRAINTS | BatchedItemOperations.CHECK_HOLD | BatchedItemOperations.CHECK_RETENTION);
        // do create new node
        node = itemOps.createNodeState(parent, nodeInfo.getName(), nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(), nodeInfo.getId());
        // restore list of child node entries (JCR-1055)
        if (cneConflicting.getName().equals(nodeInfo.getName())) {
            // restore original child node list
            parent.setChildNodeEntries(cneList);
        } else {
            // replace child node entry with different name
            // but preserving original position
            parent.removeAllChildNodeEntries();
            for (ChildNodeEntry cne : cneList) {
                if (cne.getId().equals(nodeInfo.getId())) {
                    // replace entry with different name
                    parent.addChildNodeEntry(nodeInfo.getName(), nodeInfo.getId());
                } else {
                    parent.addChildNodeEntry(cne.getName(), cne.getId());
                }
            }
        }
    } else {
        String msg = "unknown uuidBehavior: " + uuidBehavior;
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    return node;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) RepositoryException(javax.jcr.RepositoryException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemExistsException(javax.jcr.ItemExistsException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 9 with ItemExistsException

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

the class Move method create.

//------------------------------------------------------------< Factory >---
public static Operation create(Path srcPath, Path destPath, HierarchyManager hierMgr, PathResolver resolver, boolean sessionMove) throws ItemExistsException, NoSuchNodeTypeException, RepositoryException {
    // src must not be ancestor of destination
    if (srcPath.isAncestorOf(destPath)) {
        String msg = "Invalid destination path: cannot be descendant of source path (" + LogUtil.safeGetJCRPath(destPath, resolver) + "," + LogUtil.safeGetJCRPath(srcPath, resolver) + ")";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // destination must not contain an index
    int index = destPath.getIndex();
    if (index != Path.INDEX_UNDEFINED) {
        // subscript in name element
        String msg = "Invalid destination path: subscript in name element is not allowed (" + LogUtil.safeGetJCRPath(destPath, resolver) + ")";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    // root node cannot be moved:
    if (srcPath.denotesRoot() || destPath.denotesRoot()) {
        String msg = "Cannot move the root node.";
        log.debug(msg);
        throw new RepositoryException(msg);
    }
    NodeState srcState = getNodeState(srcPath, hierMgr);
    NodeState srcParentState = getNodeState(srcPath.getAncestor(1), hierMgr);
    NodeState destParentState = getNodeState(destPath.getAncestor(1), hierMgr);
    Name destName = destPath.getName();
    if (sessionMove) {
        NodeEntry destEntry = (NodeEntry) destParentState.getHierarchyEntry();
        // force child node entries list to be present before the move is executed
        // on the hierarchy entry.
        assertChildNodeEntries(srcParentState);
        assertChildNodeEntries(destParentState);
        if (destEntry.hasNodeEntry(destName)) {
            NodeEntry existing = destEntry.getNodeEntry(destName, Path.INDEX_DEFAULT);
            if (existing != null && sessionMove) {
                try {
                    if (!existing.getNodeState().getDefinition().allowsSameNameSiblings()) {
                        throw new ItemExistsException("Node existing at move destination does not allow same name siblings.");
                    }
                } catch (ItemNotFoundException e) {
                // existing apparent not valid any more -> probably no conflict
                }
            }
        }
    }
    Move move = new Move(srcState, srcParentState, destParentState, destName, sessionMove);
    return move;
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) ItemExistsException(javax.jcr.ItemExistsException) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 10 with ItemExistsException

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

the class UserManagerImpl method createAdmin.

/**
     * Create the administrator user. If the node to be created collides
     * with an existing node (ItemExistsException) the existing node gets removed
     * and the admin user node is (re)created.
     * <p>
     * Collision with an existing node may occur under the following circumstances:
     *
     * <ul>
     * <li>The <code>usersPath</code> has been modified in the user manager
     * configuration after a successful repository start that already created
     * the administrator user.</li>
     * <li>The NodeId created by {@link #buildNodeId(String)} by coincidence
     * collides with another NodeId created during the regular node creation
     * process.</li>
     * </ul>
     *
     * @return The admin user.
     * @throws RepositoryException If an error occurs.
     */
private User createAdmin() throws RepositoryException {
    User admin;
    try {
        admin = createUser(adminId, adminId);
        if (!isAutoSave()) {
            session.save();
        }
        log.info("... created admin user with id \'" + adminId + "\' and default pw.");
    } catch (ItemExistsException e) {
        NodeImpl conflictingNode = session.getNodeById(buildNodeId(adminId));
        String conflictPath = conflictingNode.getPath();
        log.error("Detected conflicting node " + conflictPath + " of node type " + conflictingNode.getPrimaryNodeType().getName() + ".");
        // TODO move conflicting node of type rep:User instead of removing and recreating.
        conflictingNode.remove();
        log.info("Removed conflicting node at " + conflictPath);
        admin = createUser(adminId, adminId);
        if (!isAutoSave()) {
            session.save();
        }
        log.info("Resolved conflict and (re)created admin user with id \'" + adminId + "\' and default pw.");
    }
    return admin;
}
Also used : User(org.apache.jackrabbit.api.security.user.User) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ItemExistsException(javax.jcr.ItemExistsException)

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