use of javax.jcr.ItemNotFoundException in project jackrabbit by apache.
the class DescendantSelfAxisQuery method execute.
//------------------------< JackrabbitQuery >-------------------------------
/**
* {@inheritDoc}
*/
public QueryHits execute(final JackrabbitIndexSearcher searcher, final SessionImpl session, final Sort sort) throws IOException {
if (sort.getSort().length == 0 && subQueryMatchesAll()) {
// maps path String to ScoreNode
Map<String, ScoreNode> startingPoints = new TreeMap<String, ScoreNode>();
QueryHits result = searcher.evaluate(getContextQuery());
try {
// intermediate ChildNodesQueryHits are required.
for (int i = 2; i <= getMinLevels(); i++) {
result = new ChildNodesQueryHits(result, session);
}
ScoreNode sn;
while ((sn = result.nextScoreNode()) != null) {
NodeId id = sn.getNodeId();
try {
Node node = session.getNodeById(id);
startingPoints.put(node.getPath(), sn);
} catch (ItemNotFoundException e) {
// JCR-3001 access denied to score node, will just skip it
log.warn("Access denied to node id {}.", id);
} catch (RepositoryException e) {
throw Util.createIOException(e);
}
}
} finally {
result.close();
}
// prune overlapping starting points
String previousPath = null;
for (Iterator<String> it = startingPoints.keySet().iterator(); it.hasNext(); ) {
String path = it.next();
// current path is obsolete
if (previousPath != null && path.startsWith(previousPath)) {
it.remove();
} else {
previousPath = path;
}
}
final Iterator<ScoreNode> scoreNodes = startingPoints.values().iterator();
return new AbstractQueryHits() {
private NodeTraversingQueryHits currentTraversal;
{
fetchNextTraversal();
}
public void close() throws IOException {
if (currentTraversal != null) {
currentTraversal.close();
}
}
public ScoreNode nextScoreNode() throws IOException {
while (currentTraversal != null) {
ScoreNode sn = currentTraversal.nextScoreNode();
if (sn != null) {
return sn;
} else {
fetchNextTraversal();
}
}
// if we get here there are no more score nodes
return null;
}
private void fetchNextTraversal() throws IOException {
if (currentTraversal != null) {
currentTraversal.close();
}
currentTraversal = null;
// iterate until we find a good one
while (scoreNodes.hasNext()) {
ScoreNode sn = scoreNodes.next();
NodeId id = sn.getNodeId();
try {
Node node = session.getNodeById(id);
currentTraversal = new NodeTraversingQueryHits(node, getMinLevels() == 0);
break;
} catch (ItemNotFoundException e) {
// JCR-3001 node access denied, will just skip it
log.warn("Access denied to node id {}.", id);
} catch (RepositoryException e) {
throw Util.createIOException(e);
}
}
}
};
} else {
return null;
}
}
use of javax.jcr.ItemNotFoundException in project jackrabbit by apache.
the class ChildNodesQueryHits method fetchNextChildHits.
/**
* Fetches the next {@link #childHits}
*
* @throws IOException if an error occurs while reading from the index.
*/
private void fetchNextChildHits() throws IOException {
if (childHits != null) {
childHits.close();
}
ScoreNode nextParent = parents.nextScoreNode();
if (nextParent != null) {
try {
Node parent = session.getNodeById(nextParent.getNodeId());
childHits = new NodeTraversingQueryHits(parent, false, 1);
} catch (ItemNotFoundException e) {
// access denied to node, will just skip it
} catch (RepositoryException e) {
throw Util.createIOException(e);
}
} else {
childHits = null;
}
}
use of javax.jcr.ItemNotFoundException 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;
}
use of javax.jcr.ItemNotFoundException in project jackrabbit by apache.
the class RemoveOrphanVersionHistoryTest method testRemoveOrphanVersionHistory.
/**
* Test orphan version history cleaning in a single workspace.
* @throws RepositoryException if an error occurs.
*/
public void testRemoveOrphanVersionHistory() throws RepositoryException {
Node n = testRootNode.addNode(nodeName1);
n.addMixin(mixVersionable);
testRootNode.save();
Session session = n.getSession();
VersionHistory vh = n.getVersionHistory();
String vhUuid = vh.getUUID();
assertExists(session, vhUuid);
// First version
Version v10 = n.checkin();
n.checkout();
// Second version
Version v11 = n.checkin();
n.checkout();
// Remove node
n.remove();
testRootNode.save();
assertExists(session, vhUuid);
// Remove the first version
vh.removeVersion(v10.getName());
assertExists(session, vhUuid);
// Remove the second and last version
vh.removeVersion(v11.getName());
try {
session.getNodeByUUID(vhUuid);
fail("Orphan version history must have been removed");
} catch (ItemNotFoundException e) {
// Expected
}
}
use of javax.jcr.ItemNotFoundException in project jackrabbit by apache.
the class RemoveOrphanVersionHistoryTest method testEmptyNonOrphanVersionHistory.
/**
* Test that an emptied version history that is still being referenced
* from another workspace does not get removed.
*
* @throws RepositoryException if an error occurs.
*/
public void testEmptyNonOrphanVersionHistory() throws RepositoryException {
Session session = testRootNode.getSession();
// Create versionable test node
Node node = testRootNode.addNode(nodeName1);
node.addMixin(mixVersionable);
session.save();
VersionHistory history = node.getVersionHistory();
String uuid = history.getUUID();
// Create version 1.0
Version v10 = node.checkin();
// Remove the test node
node.checkout();
node.remove();
session.save();
Session otherSession = getHelper().getReadWriteSession(workspaceName);
try {
// create a reference to the version history in another workspace
Node otherRoot = otherSession.getRootNode();
Property reference = otherRoot.setProperty("RemoveOrphanVersionTest", uuid, PropertyType.REFERENCE);
otherSession.save();
// Now remove the contents of the version history
history.removeVersion(v10.getName());
// Check that the version history still exists!
try {
session.getNodeByUUID(uuid);
} catch (ItemNotFoundException e) {
fail("Referenced empty version history must note be removed");
}
// Cleanup
reference.remove();
otherSession.save();
} finally {
otherSession.logout();
}
}
Aggregations