Search in sources :

Example 96 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit by apache.

the class NodeStateEx method removeProperty.

/**
     * removes the property with the given name
     *
     * @param name name of the property
     * @return <code>true</code> if the property was removed.
     * @throws RepositoryException if an error occurs
     */
public boolean removeProperty(Name name) throws RepositoryException {
    try {
        if (!nodeState.hasPropertyName(name)) {
            return false;
        } else {
            PropertyId propId = new PropertyId(nodeState.getNodeId(), name);
            ItemState state = stateMgr.getItemState(propId);
            stateMgr.destroy(state);
            nodeState.removePropertyName(name);
            if (nodeState.getStatus() != ItemState.STATUS_NEW) {
                nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED);
            }
            return true;
        }
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    }
}
Also used : ItemState(org.apache.jackrabbit.core.state.ItemState) RepositoryException(javax.jcr.RepositoryException) PropertyId(org.apache.jackrabbit.core.id.PropertyId) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 97 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit by apache.

the class NodeStateEx method getChildNodes.

/**
     * returns all child nodes
     *
     * @return the child nodes.
     * @throws RepositoryException if an error occurs
     */
public NodeStateEx[] getChildNodes() throws RepositoryException {
    try {
        List<ChildNodeEntry> entries = nodeState.getChildNodeEntries();
        NodeStateEx[] children = new NodeStateEx[entries.size()];
        int i = 0;
        for (ChildNodeEntry entry : entries) {
            NodeState state = (NodeState) stateMgr.getItemState(entry.getId());
            children[i++] = new NodeStateEx(stateMgr, ntReg, state, entry.getName());
        }
        return children;
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    }
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) RepositoryException(javax.jcr.RepositoryException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 98 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit by apache.

the class BatchedItemOperations method checkAddNode.

//--------------------------------------< misc. high-level helper methods >
/**
     * Checks if adding a child node called <code>nodeName</code> of node type
     * <code>nodeTypeName</code> to the given parent node is allowed in the
     * current context.
     *
     * @param parentState
     * @param nodeName
     * @param nodeTypeName
     * @param options      bit-wise OR'ed flags specifying the checks that should be
     *                     performed; any combination of the following constants:
     *                     <ul>
     *                     <li><code>{@link #CHECK_ACCESS}</code>: make sure
     *                     current session is granted read &amp; write access on
     *                     parent node</li>
     *                     <li><code>{@link #CHECK_LOCK}</code>: make sure
     *                     there's no foreign lock on parent node</li>
     *                     <li><code>{@link #CHECK_CHECKED_OUT}</code>: make sure
     *                     parent node is checked-out</li>
     *                     <li><code>{@link #CHECK_CONSTRAINTS}</code>:
     *                     make sure no node type constraints would be violated</li>
     *                     <li><code>{@link #CHECK_HOLD}</code>: check for effective holds preventing the add operation</li>
     *                     <li><code>{@link #CHECK_RETENTION}</code>: check for effective retention policy preventing the add operation</li>
     *                     </ul>
     * @throws ConstraintViolationException
     * @throws AccessDeniedException
     * @throws VersionException
     * @throws LockException
     * @throws ItemNotFoundException
     * @throws ItemExistsException
     * @throws RepositoryException
     */
public void checkAddNode(NodeState parentState, Name nodeName, Name nodeTypeName, int options) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ItemExistsException, RepositoryException {
    Path parentPath = hierMgr.getPath(parentState.getNodeId());
    if ((options & CHECK_LOCK) == CHECK_LOCK) {
        // make sure there's no foreign lock on parent node
        verifyUnlocked(parentPath);
    }
    if ((options & CHECK_CHECKED_OUT) == CHECK_CHECKED_OUT) {
        // make sure parent node is checked-out
        verifyCheckedOut(parentPath);
    }
    if ((options & CHECK_ACCESS) == CHECK_ACCESS) {
        AccessManager accessMgr = context.getAccessManager();
        // make sure current session is granted read access on parent node
        if (!accessMgr.isGranted(parentPath, Permission.READ)) {
            throw new ItemNotFoundException(safeGetJCRPath(parentState.getNodeId()));
        }
        // make sure current session is granted write access on parent node
        if (!accessMgr.isGranted(parentPath, nodeName, Permission.ADD_NODE)) {
            throw new AccessDeniedException(safeGetJCRPath(parentState.getNodeId()) + ": not allowed to add child node");
        }
        // specified node type (and ev. mixins)
        if (!accessMgr.isGranted(parentPath, nodeName, Permission.NODE_TYPE_MNGMT)) {
            throw new AccessDeniedException(safeGetJCRPath(parentState.getNodeId()) + ": not allowed to add child node");
        }
    }
    if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) {
        QItemDefinition parentDef = context.getItemManager().getDefinition(parentState).unwrap();
        // make sure parent node is not protected
        if (parentDef.isProtected()) {
            throw new ConstraintViolationException(safeGetJCRPath(parentState.getNodeId()) + ": cannot add child node to protected parent node");
        }
        // make sure there's an applicable definition for new child node
        EffectiveNodeType entParent = getEffectiveNodeType(parentState);
        entParent.checkAddNodeConstraints(nodeName, nodeTypeName, context.getNodeTypeRegistry());
        QNodeDefinition newNodeDef = findApplicableNodeDefinition(nodeName, nodeTypeName, parentState);
        // check for name collisions
        if (parentState.hasChildNodeEntry(nodeName)) {
            // there's already a node with that name...
            // get definition of existing conflicting node
            ChildNodeEntry entry = parentState.getChildNodeEntry(nodeName, 1);
            NodeState conflictingState;
            NodeId conflictingId = entry.getId();
            try {
                conflictingState = (NodeState) stateMgr.getItemState(conflictingId);
            } catch (ItemStateException ise) {
                String msg = "internal error: failed to retrieve state of " + safeGetJCRPath(conflictingId);
                log.debug(msg);
                throw new RepositoryException(msg, ise);
            }
            QNodeDefinition conflictingTargetDef = context.getItemManager().getDefinition(conflictingState).unwrap();
            // check same-name sibling setting of both target and existing node
            if (!conflictingTargetDef.allowsSameNameSiblings() || !newNodeDef.allowsSameNameSiblings()) {
                throw new ItemExistsException("cannot add child node '" + nodeName.getLocalName() + "' to " + safeGetJCRPath(parentState.getNodeId()) + ": colliding with same-named existing node");
            }
        }
    }
    RetentionRegistry retentionReg = context.getSessionImpl().getRetentionRegistry();
    if ((options & CHECK_HOLD) == CHECK_HOLD) {
        if (retentionReg.hasEffectiveHold(parentPath, false)) {
            throw new RepositoryException("Unable to add node. Parent is affected by a hold.");
        }
    }
    if ((options & CHECK_RETENTION) == CHECK_RETENTION) {
        if (retentionReg.hasEffectiveRetention(parentPath, false)) {
            throw new RepositoryException("Unable to add node. Parent is affected by a retention.");
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) AccessManager(org.apache.jackrabbit.core.security.AccessManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) RepositoryException(javax.jcr.RepositoryException) RetentionRegistry(org.apache.jackrabbit.core.retention.RetentionRegistry) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ItemExistsException(javax.jcr.ItemExistsException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 99 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit by apache.

the class BatchedItemOperations method checkRemoveNode.

/**
     * Checks if removing the given target node from the specifed parent
     * is allowed in the current context.
     *
     * @param targetState
     * @param parentId
     * @param options     bit-wise OR'ed flags specifying the checks that should be
     *                    performed; any combination of the following constants:
     *                    <ul>
     *                    <li><code>{@link #CHECK_ACCESS}</code>: make sure
     *                    current session is granted read access on parent
     *                    and remove privilege on target node</li>
     *                    <li><code>{@link #CHECK_LOCK}</code>: make sure
     *                    there's no foreign lock on parent node</li>
     *                    <li><code>{@link #CHECK_CHECKED_OUT}</code>: make sure
     *                    parent node is checked-out</li>
     *                    <li><code>{@link #CHECK_CONSTRAINTS}</code>:
     *                    make sure no node type constraints would be violated</li>
     *                    <li><code>{@link #CHECK_REFERENCES}</code>:
     *                    make sure no references exist on target node</li>
     *                    <li><code>{@link #CHECK_HOLD}</code>: check for effective holds preventing the add operation</li>
     *                    <li><code>{@link #CHECK_RETENTION}</code>: check for effective retention policy preventing the add operation</li>
     *                    </ul>
     * @throws ConstraintViolationException
     * @throws AccessDeniedException
     * @throws VersionException
     * @throws LockException
     * @throws ItemNotFoundException
     * @throws ReferentialIntegrityException
     * @throws RepositoryException
     */
public void checkRemoveNode(NodeState targetState, NodeId parentId, int options) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ReferentialIntegrityException, RepositoryException {
    if (targetState.getParentId() == null) {
        // root or orphaned node
        throw new ConstraintViolationException("cannot remove root node");
    }
    Path targetPath = hierMgr.getPath(targetState.getNodeId());
    NodeState parentState = getNodeState(parentId);
    Path parentPath = hierMgr.getPath(parentId);
    if ((options & CHECK_LOCK) == CHECK_LOCK) {
        // make sure there's no foreign lock on parent node
        verifyUnlocked(parentPath);
    }
    if ((options & CHECK_CHECKED_OUT) == CHECK_CHECKED_OUT) {
        // make sure parent node is checked-out
        verifyCheckedOut(parentPath);
    }
    if ((options & CHECK_ACCESS) == CHECK_ACCESS) {
        try {
            AccessManager accessMgr = context.getAccessManager();
            // make sure current session is granted read access on parent node
            if (!accessMgr.isGranted(targetPath, Permission.READ)) {
                throw new PathNotFoundException(safeGetJCRPath(targetPath));
            }
            // make sure current session is allowed to remove target node
            if (!accessMgr.isGranted(targetPath, Permission.REMOVE_NODE)) {
                throw new AccessDeniedException(safeGetJCRPath(targetPath) + ": not allowed to remove node");
            }
        } catch (ItemNotFoundException infe) {
            String msg = "internal error: failed to check access rights for " + safeGetJCRPath(targetPath);
            log.debug(msg);
            throw new RepositoryException(msg, infe);
        }
    }
    if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) {
        QItemDefinition parentDef = context.getItemManager().getDefinition(parentState).unwrap();
        if (parentDef.isProtected()) {
            throw new ConstraintViolationException(safeGetJCRPath(parentId) + ": cannot remove child node of protected parent node");
        }
        QItemDefinition targetDef = context.getItemManager().getDefinition(targetState).unwrap();
        if (targetDef.isMandatory()) {
            throw new ConstraintViolationException(safeGetJCRPath(targetPath) + ": cannot remove mandatory node");
        }
        if (targetDef.isProtected()) {
            throw new ConstraintViolationException(safeGetJCRPath(targetPath) + ": cannot remove protected node");
        }
    }
    if ((options & CHECK_REFERENCES) == CHECK_REFERENCES) {
        EffectiveNodeType ent = getEffectiveNodeType(targetState);
        if (ent.includesNodeType(NameConstants.MIX_REFERENCEABLE)) {
            NodeId targetId = targetState.getNodeId();
            if (stateMgr.hasNodeReferences(targetId)) {
                try {
                    NodeReferences refs = stateMgr.getNodeReferences(targetId);
                    if (refs.hasReferences()) {
                        throw new ReferentialIntegrityException(safeGetJCRPath(targetPath) + ": cannot remove node with references");
                    }
                } catch (ItemStateException ise) {
                    String msg = "internal error: failed to check references on " + safeGetJCRPath(targetPath);
                    log.error(msg, ise);
                    throw new RepositoryException(msg, ise);
                }
            }
        }
    }
    RetentionRegistry retentionReg = context.getSessionImpl().getRetentionRegistry();
    if ((options & CHECK_HOLD) == CHECK_HOLD) {
        if (retentionReg.hasEffectiveHold(targetPath, true)) {
            throw new RepositoryException("Unable to perform removal. Node is affected by a hold.");
        }
    }
    if ((options & CHECK_RETENTION) == CHECK_RETENTION) {
        if (retentionReg.hasEffectiveRetention(targetPath, true)) {
            throw new RepositoryException("Unable to perform removal. Node is affected by a retention.");
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) AccessManager(org.apache.jackrabbit.core.security.AccessManager) AccessDeniedException(javax.jcr.AccessDeniedException) NodeState(org.apache.jackrabbit.core.state.NodeState) RepositoryException(javax.jcr.RepositoryException) NodeReferences(org.apache.jackrabbit.core.state.NodeReferences) RetentionRegistry(org.apache.jackrabbit.core.retention.RetentionRegistry) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 100 with ItemStateException

use of org.apache.jackrabbit.core.state.ItemStateException in project jackrabbit by apache.

the class BatchedItemOperations method recursiveRemoveNodeState.

//------------------------------------------------------< private methods >
/**
     * Recursively removes the given node state including its properties and
     * child nodes.
     * <p>
     * The removal of child nodes is subject to the following checks:
     * access rights, locking & versioning status. Referential integrity
     * (references) is checked on commit.
     * <p>
     * Note that the child node entry refering to <code>targetState</code> is
     * <b><i>not</i></b> automatically removed from <code>targetState</code>'s
     * parent.
     *
     * @param targetState
     * @throws RepositoryException if an error occurs
     */
private void recursiveRemoveNodeState(NodeState targetState) throws RepositoryException {
    if (targetState.hasChildNodeEntries()) {
        // remove child nodes
        // use temp array to avoid ConcurrentModificationException
        ArrayList<ChildNodeEntry> tmp = new ArrayList<ChildNodeEntry>(targetState.getChildNodeEntries());
        // remove from tail to avoid problems with same-name siblings
        for (int i = tmp.size() - 1; i >= 0; i--) {
            ChildNodeEntry entry = tmp.get(i);
            NodeId nodeId = entry.getId();
            try {
                NodeState nodeState = (NodeState) stateMgr.getItemState(nodeId);
                // check if child node can be removed
                // (access rights, locking & versioning status as well
                //  as retention and hold);
                // referential integrity (references) is checked
                // on commit
                checkRemoveNode(nodeState, targetState.getNodeId(), CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_HOLD | CHECK_RETENTION);
                // remove child node
                recursiveRemoveNodeState(nodeState);
            } catch (ItemStateException ise) {
                String msg = "internal error: failed to retrieve state of " + nodeId;
                log.debug(msg);
                throw new RepositoryException(msg, ise);
            }
            // remove child node entry
            targetState.removeChildNodeEntry(entry.getName(), entry.getIndex());
        }
    }
    // remove properties
    // use temp set to avoid ConcurrentModificationException
    HashSet<Name> tmp = new HashSet<Name>(targetState.getPropertyNames());
    for (Name propName : tmp) {
        PropertyId propId = new PropertyId(targetState.getNodeId(), propName);
        try {
            PropertyState propState = (PropertyState) stateMgr.getItemState(propId);
            // remove property entry
            targetState.removePropertyName(propId.getName());
            // destroy property state
            stateMgr.destroy(propState);
        } catch (ItemStateException ise) {
            String msg = "internal error: failed to retrieve state of " + propId;
            log.debug(msg);
            throw new RepositoryException(msg, ise);
        }
    }
    // now actually do unlink target state
    targetState.setParentId(null);
    // destroy target state (pass overlayed state since target state
    // might have been modified during unlinking)
    stateMgr.destroy(targetState.getOverlayedState());
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) PropertyState(org.apache.jackrabbit.core.state.PropertyState) NodeId(org.apache.jackrabbit.core.id.NodeId) HashSet(java.util.HashSet)

Aggregations

ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)141 RepositoryException (javax.jcr.RepositoryException)87 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)86 NodeId (org.apache.jackrabbit.core.id.NodeId)44 NodeState (org.apache.jackrabbit.core.state.NodeState)39 FileSystemException (org.apache.jackrabbit.core.fs.FileSystemException)31 IOException (java.io.IOException)28 InvalidItemStateException (javax.jcr.InvalidItemStateException)27 PropertyState (org.apache.jackrabbit.core.state.PropertyState)24 SQLException (java.sql.SQLException)23 PropertyId (org.apache.jackrabbit.core.id.PropertyId)22 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)19 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)19 Name (org.apache.jackrabbit.spi.Name)17 ItemNotFoundException (javax.jcr.ItemNotFoundException)15 InternalValue (org.apache.jackrabbit.core.value.InternalValue)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 NodeReferences (org.apache.jackrabbit.core.state.NodeReferences)13 InputStream (java.io.InputStream)12