Search in sources :

Example 76 with ItemNotFoundException

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

the class NodeImpl method getCorrespondingNodePath.

/**
     * {@inheritDoc}
     */
public String getCorrespondingNodePath(String workspaceName) throws ItemNotFoundException, NoSuchWorkspaceException, AccessDeniedException, RepositoryException {
    // check state of this instance
    sanityCheck();
    SessionImpl srcSession = null;
    try {
        // create session on other workspace for current subject
        // (may throw NoSuchWorkspaceException and AccessDeniedException)
        RepositoryImpl rep = (RepositoryImpl) getSession().getRepository();
        srcSession = rep.createSession(sessionContext.getSessionImpl().getSubject(), workspaceName);
        // search nearest ancestor that is referenceable
        NodeImpl m1 = this;
        while (m1.getDepth() != 0 && !m1.isNodeType(NameConstants.MIX_REFERENCEABLE)) {
            m1 = (NodeImpl) m1.getParent();
        }
        // if root is common ancestor, corresponding path is same as ours
        if (m1.getDepth() == 0) {
            // check existence
            if (!srcSession.getItemManager().nodeExists(getPrimaryPath())) {
                throw new ItemNotFoundException("Node not found: " + this);
            } else {
                return getPath();
            }
        }
        // get corresponding ancestor
        Node m2 = srcSession.getNodeByUUID(m1.getUUID());
        // return path of m2, if m1 == n1
        if (m1 == this) {
            return m2.getPath();
        }
        String relPath;
        try {
            Path p = m1.getPrimaryPath().computeRelativePath(getPrimaryPath());
            // use prefix mappings of srcSession
            relPath = sessionContext.getJCRPath(p);
        } catch (NameException be) {
            // should never get here...
            String msg = "internal error: failed to determine relative path";
            log.error(msg, be);
            throw new RepositoryException(msg, be);
        }
        if (!m2.hasNode(relPath)) {
            throw new ItemNotFoundException();
        } else {
            return m2.getNode(relPath).getPath();
        }
    } finally {
        if (srcSession != null) {
            // we don't need the other session anymore, logout
            srcSession.logout();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 77 with ItemNotFoundException

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

the class NodeImpl method orderBefore.

/**
     * Same as <code>{@link Node#orderBefore(String, String)}</code> except that
     * this method takes a <code>Path.Element</code> arguments instead of
     * <code>String</code>s.
     *
     * @param srcName
     * @param dstName
     * @throws UnsupportedRepositoryOperationException
     * @throws VersionException
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws LockException
     * @throws RepositoryException
     */
public synchronized void orderBefore(Path.Element srcName, Path.Element dstName) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
    // check state of this instance
    sanityCheck();
    if (!getPrimaryNodeType().hasOrderableChildNodes()) {
        throw new UnsupportedRepositoryOperationException("child node ordering not supported on " + this);
    }
    // check arguments
    if (srcName.equals(dstName)) {
        // there's nothing to do
        return;
    }
    // check existence
    if (!hasNode(srcName.getName(), srcName.getIndex())) {
        String name;
        try {
            Path.Element[] path = new Path.Element[] { srcName };
            name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
        } catch (NameException e) {
            name = srcName.toString();
        } catch (NamespaceException e) {
            name = srcName.toString();
        }
        throw new ItemNotFoundException(this + " has no child node with name " + name);
    }
    if (dstName != null && !hasNode(dstName.getName(), dstName.getIndex())) {
        String name;
        try {
            Path.Element[] path = new Path.Element[] { dstName };
            name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
        } catch (NameException e) {
            name = dstName.toString();
        } catch (NamespaceException e) {
            name = dstName.toString();
        }
        throw new ItemNotFoundException(this + " has no child node with name " + name);
    }
    // make sure this node is checked-out and neither protected nor locked
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    /*
        make sure the session is allowed to reorder child nodes.
        since there is no specific privilege for reordering child nodes,
        test if the the node to be reordered can be removed and added,
        i.e. treating reorder similar to a move.
        TODO: properly deal with sns in which case the index would change upon reorder.
        */
    AccessManager acMgr = sessionContext.getAccessManager();
    PathBuilder pb = new PathBuilder(getPrimaryPath());
    pb.addLast(srcName.getName(), srcName.getIndex());
    Path childPath = pb.getPath();
    if (!acMgr.isGranted(childPath, Permission.MODIFY_CHILD_NODE_COLLECTION)) {
        String msg = "Not allowed to reorder child node " + sessionContext.getJCRPath(childPath) + ".";
        log.debug(msg);
        throw new AccessDeniedException(msg);
    }
    ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(data.getNodeState().getChildNodeEntries());
    int srcInd = -1, destInd = -1;
    for (int i = 0; i < list.size(); i++) {
        ChildNodeEntry entry = list.get(i);
        if (srcInd == -1) {
            if (entry.getName().equals(srcName.getName()) && (entry.getIndex() == srcName.getIndex() || srcName.getIndex() == 0 && entry.getIndex() == 1)) {
                srcInd = i;
            }
        }
        if (destInd == -1 && dstName != null) {
            if (entry.getName().equals(dstName.getName()) && (entry.getIndex() == dstName.getIndex() || dstName.getIndex() == 0 && entry.getIndex() == 1)) {
                destInd = i;
                if (srcInd != -1) {
                    break;
                }
            }
        } else {
            if (srcInd != -1) {
                break;
            }
        }
    }
    // check if resulting order would be different to current order
    if (destInd == -1) {
        if (srcInd == list.size() - 1) {
            // no change, we're done
            return;
        }
    } else {
        if ((destInd - srcInd) == 1) {
            // no change, we're done
            return;
        }
    }
    // reorder list
    if (destInd == -1) {
        list.add(list.remove(srcInd));
    } else {
        if (srcInd < destInd) {
            list.add(destInd, list.get(srcInd));
            list.remove(srcInd);
        } else {
            list.add(destInd, list.remove(srcInd));
        }
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    thisState.setChildNodeEntries(list);
}
Also used : AccessManager(org.apache.jackrabbit.core.security.AccessManager) Path(org.apache.jackrabbit.spi.Path) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) AccessDeniedException(javax.jcr.AccessDeniedException) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 78 with ItemNotFoundException

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

the class NodeImpl method onRemove.

protected void onRemove(NodeId parentId) throws RepositoryException {
    // modify the state of 'this', i.e. the target node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // remove this node from its shared set
    if (thisState.isShareable()) {
        if (thisState.removeShare(parentId) > 0) {
            // this state is still connected to some parents, so
            // leave the child node entries and properties
            // set state of this instance to 'invalid'
            data.setStatus(STATUS_INVALIDATED);
            // notify the item manager that this instance has been
            // temporarily invalidated
            itemMgr.itemInvalidated(id, data);
            return;
        }
    }
    if (thisState.hasChildNodeEntries()) {
        // remove child nodes
        // use temp array to avoid ConcurrentModificationException
        ArrayList<ChildNodeEntry> tmp = new ArrayList<ChildNodeEntry>(thisState.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);
            // recursively remove child node
            NodeId childId = entry.getId();
            //NodeImpl childNode = (NodeImpl) itemMgr.getItem(childId);
            try {
                /* omit the read-permission check upon retrieving the
                       child item as this is an internal call to remove the
                       subtree which may contain (protected) child items which
                       are not visible to the caller of the removal. the actual
                       validation of the remove permission however is only
                       executed during Item.save(). 
                     */
                NodeImpl childNode = itemMgr.getNode(childId, getNodeId(), false);
                childNode.onRemove(thisState.getNodeId());
            // remove the child node entry
            } catch (ItemNotFoundException e) {
                boolean ignoreError = false;
                if (parentId != null && sessionContext.getSessionImpl().autoFixCorruptions()) {
                    // it might be an access right problem
                    // we need to check if the item doesn't exist in the ism
                    ItemStateManager ism = sessionContext.getItemStateManager();
                    if (!ism.hasItemState(childId)) {
                        log.warn("Child named " + entry.getName() + " (index " + entry.getIndex() + ", " + "node id " + childId + ") " + "not found when trying to remove " + getPath() + " " + "(node id " + getNodeId() + ") - ignored", e);
                        ignoreError = true;
                    }
                }
                if (!ignoreError) {
                    throw e;
                }
            }
            thisState.removeChildNodeEntry(childId);
        }
    }
    // remove properties
    // use temp set to avoid ConcurrentModificationException
    HashSet<Name> tmp = new HashSet<Name>(thisState.getPropertyNames());
    for (Name propName : tmp) {
        // remove the property entry
        thisState.removePropertyName(propName);
        // remove property
        PropertyId propId = new PropertyId(thisState.getNodeId(), propName);
        /* omit the read-permission check upon retrieving the
               child item as this is an internal call to remove the
               subtree which may contain (protected) child items which
               are not visible to the caller of the removal. the actual
               validation of the remove permission however is only
               executed during Item.save().
             */
        itemMgr.getItem(propId, false).setRemoved();
    }
    // finally remove this node
    thisState.setParentId(null);
    setRemoved();
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) ArrayList(java.util.ArrayList) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId) NodeId(org.apache.jackrabbit.core.id.NodeId) ItemStateManager(org.apache.jackrabbit.core.state.ItemStateManager) ItemNotFoundException(javax.jcr.ItemNotFoundException) HashSet(java.util.HashSet)

Example 79 with ItemNotFoundException

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

the class AutoFixCorruptNode method testMissingVHR.

public void testMissingVHR() throws Exception {
    // new repository
    TransientRepository rep = new TransientRepository(new File(TEST_DIR));
    Session s = openSession(rep, false);
    String oldVersionRecoveryProp = System.getProperty("org.apache.jackrabbit.version.recovery");
    try {
        Node root = s.getRootNode();
        Node test = root.addNode("test");
        test.addMixin("mix:versionable");
        s.save();
        Node vhr = s.getWorkspace().getVersionManager().getVersionHistory(test.getPath());
        assertNotNull(vhr);
        Node brokenNode = vhr;
        String vhrRootVersionId = vhr.getNode("jcr:rootVersion").getIdentifier();
        UUID destroy = UUID.fromString(brokenNode.getIdentifier());
        s.logout();
        destroyBundle(destroy, "version");
        s = openSession(rep, false);
        ConsistencyReport report = TestHelper.checkVersionStoreConsistency(s, false, null);
        assertTrue("Report should have reported broken nodes", !report.getItems().isEmpty());
        try {
            test = s.getRootNode().getNode("test");
            vhr = s.getWorkspace().getVersionManager().getVersionHistory(test.getPath());
            fail("should not get here");
        } catch (Exception ex) {
        // expected
        }
        s.logout();
        System.setProperty("org.apache.jackrabbit.version.recovery", "true");
        s = openSession(rep, false);
        test = s.getRootNode().getNode("test");
        // versioning should be disabled now
        assertFalse(test.isNodeType("mix:versionable"));
        try {
            // try to enable versioning again
            test.addMixin("mix:versionable");
            s.save();
            fail("enabling versioning succeeded unexpectedly");
        } catch (Exception e) {
        // we expect this to fail
        }
        s.logout();
        // now redo after running fixup on versioning storage
        s = openSession(rep, false);
        report = TestHelper.checkVersionStoreConsistency(s, true, null);
        assertTrue("Report should have reported broken nodes", !report.getItems().isEmpty());
        int reportitems = report.getItems().size();
        // problems should now be fixed
        report = TestHelper.checkVersionStoreConsistency(s, false, null);
        assertTrue("Some problems should have been fixed but are not: " + report, report.getItems().size() < reportitems);
        // get a fresh session
        s.logout();
        s = openSession(rep, false);
        test = s.getRootNode().getNode("test");
        // versioning should be disabled now
        assertFalse(test.isNodeType("mix:versionable"));
        // try to enable versioning again
        test.addMixin("mix:versionable");
        s.save();
        Node oldRootVersion = s.getNodeByIdentifier(vhrRootVersionId);
        try {
            String path = oldRootVersion.getPath();
            fail("got path " + path + " for a node believed to be orphaned");
        } catch (ItemNotFoundException ex) {
        // orphaned
        }
        Node newRootVersion = s.getWorkspace().getVersionManager().getVersionHistory(test.getPath()).getRootVersion();
        assertFalse("new root version should be a different node than the one destroyed by the test case", newRootVersion.getIdentifier().equals(vhrRootVersionId));
        assertNotNull("new root version should have a intact path", newRootVersion.getPath());
    } finally {
        s.logout();
        System.setProperty("org.apache.jackrabbit.version.recovery", oldVersionRecoveryProp == null ? "" : oldVersionRecoveryProp);
    }
}
Also used : TransientRepository(org.apache.jackrabbit.core.TransientRepository) Node(javax.jcr.Node) UUID(java.util.UUID) File(java.io.File) ConsistencyReport(org.apache.jackrabbit.core.persistence.check.ConsistencyReport) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ItemNotFoundException(javax.jcr.ItemNotFoundException) SQLException(java.sql.SQLException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 80 with ItemNotFoundException

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

the class PropertyImpl method getProperty.

public Property getProperty() throws RepositoryException {
    Value value = getValue();
    Value pathValue = ValueHelper.convert(value, PATH, getSession().getValueFactory());
    String path = pathValue.getString();
    boolean absolute;
    try {
        Path p = sessionContext.getQPath(path);
        absolute = p.isAbsolute();
    } catch (RepositoryException e) {
        throw new ValueFormatException("Property value cannot be converted to a PATH");
    }
    try {
        return (absolute) ? getSession().getProperty(path) : getParent().getProperty(path);
    } catch (PathNotFoundException e) {
        throw new ItemNotFoundException(path);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

ItemNotFoundException (javax.jcr.ItemNotFoundException)139 RepositoryException (javax.jcr.RepositoryException)61 Node (javax.jcr.Node)44 Path (org.apache.jackrabbit.spi.Path)25 PathNotFoundException (javax.jcr.PathNotFoundException)23 NodeId (org.apache.jackrabbit.core.id.NodeId)22 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)16 IOException (java.io.IOException)14 InvalidItemStateException (javax.jcr.InvalidItemStateException)14 NodeState (org.apache.jackrabbit.core.state.NodeState)14 Name (org.apache.jackrabbit.spi.Name)14 AccessDeniedException (javax.jcr.AccessDeniedException)13 HttpResponse (org.apache.http.HttpResponse)13 DavException (org.apache.jackrabbit.webdav.DavException)13 ItemExistsException (javax.jcr.ItemExistsException)12 Session (javax.jcr.Session)12 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)11 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)10 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)10