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);
}
}
}
});
}
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;
}
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);
}
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;
}
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();
}
};
}
Aggregations