Search in sources :

Example 6 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class VersionManagerImpl method restore.

@Override
public void restore(final Version[] versions, final boolean removeExisting) throws ItemExistsException, UnsupportedRepositoryOperationException, VersionException, LockException, InvalidItemStateException, RepositoryException {
    if (versions.length > 1) {
        throw new UnsupportedRepositoryOperationException("OAK-168: Restore of multiple versions not implemented.");
    }
    final Version version = versions[0];
    VersionHistory history = (VersionHistory) version.getParent();
    final String versionableId = history.getVersionableIdentifier();
    if (history.getRootVersion().isSame(version)) {
        throw new VersionException("Restore of root version not possible");
    }
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("restore", true) {

        @Override
        public void performVoid() throws RepositoryException {
            // check for pending changes
            checkPendingChangesForRestore(sessionDelegate);
            NodeDelegate n = sessionDelegate.getNodeByIdentifier(versionableId);
            if (n == null) {
                throw new VersionException("Unable to restore version. " + "No versionable node with identifier: " + versionableId);
            }
            // check lock status
            checkNotLocked(n.getPath());
            // check for existing nodes
            List<NodeDelegate> existing = getExisting(version, Collections.singleton(n.getPath()));
            boolean success = false;
            try {
                if (!existing.isEmpty()) {
                    if (removeExisting) {
                        removeExistingNodes(existing);
                    } else {
                        List<String> paths = new ArrayList<String>();
                        for (NodeDelegate nd : existing) {
                            paths.add(nd.getPath());
                        }
                        throw new ItemExistsException("Unable to restore with " + "removeExisting=false. Existing nodes in " + "workspace: " + paths);
                    }
                }
                // ready for restore
                VersionDelegate vd = versionManagerDelegate.getVersionByIdentifier(version.getIdentifier());
                versionManagerDelegate.restore(n.getParent(), n.getName(), vd);
                sessionDelegate.commit();
                success = true;
            } catch (CommitFailedException e) {
                throw new RepositoryException(e);
            } finally {
                if (!success) {
                    // refresh if one of the modifying operations fail
                    sessionDelegate.refresh(false);
                }
            }
        }
    });
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) VersionDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) ArrayList(java.util.ArrayList) List(java.util.List) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) VersionException(javax.jcr.version.VersionException)

Example 7 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class SessionImpl method hasCapability.

@Override
public boolean hasCapability(String methodName, Object target, Object[] arguments) throws RepositoryException {
    checkNotNull(methodName);
    checkNotNull(target);
    checkAlive();
    if (target instanceof ItemImpl) {
        ItemDelegate dlg = ((ItemImpl<?>) target).dlg;
        if (dlg.isProtected()) {
            return false;
        }
        boolean isNode = ((ItemImpl<?>) target).isNode();
        Node parent = (isNode) ? (Node) target : ((ItemImpl<?>) target).getParent();
        if (!parent.isCheckedOut()) {
            return false;
        }
        if (parent.isLocked()) {
            return false;
        }
        AccessManager accessMgr = sessionContext.getAccessManager();
        long permission = Permissions.NO_PERMISSION;
        if (isNode) {
            Tree tree = ((NodeDelegate) dlg).getTree();
            if ("addNode".equals(methodName)) {
                if (arguments != null && arguments.length > 0) {
                    // add-node needs to be checked on the (path of) the
                    // new node that has/will be added
                    String path = PathUtils.concat(tree.getPath(), sessionContext.getOakName(arguments[0].toString()));
                    return accessMgr.hasPermissions(path, Session.ACTION_ADD_NODE) && !isMountedReadOnly(path);
                }
            } else if ("setPrimaryType".equals(methodName) || "addMixin".equals(methodName) || "removeMixin".equals(methodName)) {
                permission = Permissions.NODE_TYPE_MANAGEMENT;
            } else if ("orderBefore".equals(methodName)) {
                if (tree.isRoot()) {
                    return false;
                } else {
                    permission = Permissions.MODIFY_CHILD_NODE_COLLECTION;
                    tree = tree.getParent();
                }
            } else if ("setProperty".equals(methodName)) {
                permission = Permissions.ADD_PROPERTY;
            } else if ("remove".equals(methodName)) {
                permission = Permissions.REMOVE_NODE;
            }
            return accessMgr.hasPermissions(tree, null, permission) && !isMountedReadOnly(tree.getPath());
        } else {
            if ("setValue".equals(methodName)) {
                permission = Permissions.MODIFY_PROPERTY;
            } else if ("remove".equals(methodName)) {
                permission = Permissions.REMOVE_PROPERTY;
            }
            NodeDelegate parentDelegate = dlg.getParent();
            if (parentDelegate != null) {
                return accessMgr.hasPermissions(parentDelegate.getTree(), ((PropertyDelegate) dlg).getPropertyState(), permission) && !isMountedReadOnly(parentDelegate.getPath());
            } else {
                return accessMgr.hasPermissions(dlg.getPath(), (permission == Permissions.MODIFY_PROPERTY) ? Session.ACTION_SET_PROPERTY : Session.ACTION_REMOVE) && !isMountedReadOnly(dlg.getPath());
            }
        }
    }
    // TODO: add more best-effort checks
    return true;
}
Also used : AccessManager(org.apache.jackrabbit.oak.jcr.security.AccessManager) Node(javax.jcr.Node) Tree(org.apache.jackrabbit.oak.api.Tree) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) ItemDelegate(org.apache.jackrabbit.oak.jcr.delegate.ItemDelegate) PropertyDelegate(org.apache.jackrabbit.oak.jcr.delegate.PropertyDelegate)

Example 8 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class VersionManagerImpl method internalGetVersionHistory.

/**
 * Returns the version history for the versionable node at the given path.
 *
 * @param absPathVersionable path to a versionable node.
 * @return the version history.
 * @throws PathNotFoundException if the given path does not reference an
 *                               existing node.
 * @throws UnsupportedRepositoryOperationException
 *                               if the node at the given path is not
 *                               mix:versionable.
 * @throws RepositoryException if some other error occurs.
 */
@Nonnull
private VersionHistoryDelegate internalGetVersionHistory(@Nonnull String absPathVersionable) throws RepositoryException, UnsupportedRepositoryOperationException {
    SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    String oakPath = getOakPathOrThrowNotFound(checkNotNull(absPathVersionable));
    NodeDelegate nodeDelegate = sessionDelegate.getNode(oakPath);
    if (nodeDelegate == null) {
        throw new PathNotFoundException(absPathVersionable);
    }
    return versionManagerDelegate.getVersionHistory(nodeDelegate);
}
Also used : SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) Nonnull(javax.annotation.Nonnull)

Example 9 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class QueryImpl method storeAsNode.

@Override
public Node storeAsNode(String absPath) throws RepositoryException {
    manager.ensureIsAlive();
    String oakPath = sessionContext.getOakPathOrThrow(absPath);
    String parent = PathUtils.getParentPath(oakPath);
    NodeDelegate parentDelegate = sessionContext.getSessionDelegate().getNode(parent);
    if (parentDelegate == null) {
        throw new PathNotFoundException("The specified path does not exist: " + parent);
    }
    Node parentNode = NodeImpl.createNode(parentDelegate, sessionContext);
    if (!parentNode.isCheckedOut()) {
        throw new VersionException("Cannot store query. Node at " + absPath + " is checked in.");
    }
    String nodeName = PathUtils.getName(oakPath);
    ValueFactory vf = sessionContext.getValueFactory();
    Node n = parentNode.addNode(nodeName, JcrConstants.NT_QUERY);
    n.setProperty(JcrConstants.JCR_STATEMENT, vf.createValue(statement));
    n.setProperty(JcrConstants.JCR_LANGUAGE, vf.createValue(language));
    setStoredQueryPath(oakPath);
    return n;
}
Also used : Node(javax.jcr.Node) PathNotFoundException(javax.jcr.PathNotFoundException) ValueFactory(javax.jcr.ValueFactory) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) VersionException(javax.jcr.version.VersionException)

Example 10 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class QueryResultImpl method getNodes.

@Override
public NodeIterator getNodes() throws RepositoryException {
    String[] columnSelectorNames = result.getColumnSelectorNames();
    if (columnSelectorNames.length != 1) {
        throw new RepositoryException("Query contains more than one selector: " + Arrays.toString(columnSelectorNames));
    }
    final String selectorName = columnSelectorNames[0];
    if (selectorName == null) {
        throw new RepositoryException("Query does not contain a selector: " + Arrays.toString(columnSelectorNames));
    }
    Iterator<NodeImpl<? extends NodeDelegate>> nodeIterator = new Iterator<NodeImpl<? extends NodeDelegate>>() {

        private final Iterator<? extends ResultRow> it = result.getRows().iterator();

        private NodeImpl<? extends NodeDelegate> current;

        {
            fetch();
        }

        private void fetch() {
            current = null;
            while (it.hasNext()) {
                ResultRow r = it.next();
                Tree tree = r.getTree(selectorName);
                if (tree != null && tree.exists()) {
                    try {
                        current = getNode(tree);
                        break;
                    } catch (RepositoryException e) {
                        LOG.warn("Unable to fetch result node for path " + tree.getPath(), e);
                    }
                }
            }
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public NodeImpl<? extends NodeDelegate> next() {
            if (current == null) {
                throw new NoSuchElementException();
            }
            NodeImpl<? extends NodeDelegate> n = current;
            fetch();
            return n;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    final PrefetchIterator<NodeImpl<? extends NodeDelegate>> prefIt = new PrefetchIterator<NodeImpl<? extends NodeDelegate>>(sessionDelegate.sync(nodeIterator), new PrefetchOptions() {

        {
            size = result.getSize();
            fastSize = sessionContext.getFastQueryResultSize();
            fastSizeCallback = result;
        }
    });
    return new NodeIteratorAdapter(prefIt) {

        @Override
        public long getSize() {
            return prefIt.size();
        }
    };
}
Also used : ResultRow(org.apache.jackrabbit.oak.api.ResultRow) NodeImpl(org.apache.jackrabbit.oak.jcr.session.NodeImpl) PrefetchOptions(org.apache.jackrabbit.oak.jcr.query.PrefetchIterator.PrefetchOptions) NodeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter) RepositoryException(javax.jcr.RepositoryException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) NodeIterator(javax.jcr.NodeIterator) Tree(org.apache.jackrabbit.oak.api.Tree) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NodeDelegate (org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate)14 PathNotFoundException (javax.jcr.PathNotFoundException)7 RepositoryException (javax.jcr.RepositoryException)6 Nonnull (javax.annotation.Nonnull)5 SessionDelegate (org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate)5 Node (javax.jcr.Node)4 ArrayList (java.util.ArrayList)3 ItemExistsException (javax.jcr.ItemExistsException)3 VersionException (javax.jcr.version.VersionException)3 PropertyDelegate (org.apache.jackrabbit.oak.jcr.delegate.PropertyDelegate)3 List (java.util.List)2 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)2 Tree (org.apache.jackrabbit.oak.api.Tree)2 ItemDelegate (org.apache.jackrabbit.oak.jcr.delegate.ItemDelegate)2 VersionDelegate (org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate)2 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 CheckForNull (javax.annotation.CheckForNull)1 NodeIterator (javax.jcr.NodeIterator)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1