Search in sources :

Example 71 with ItemNotFoundException

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

the class ReferencesTest method testVersionedReferences.

// OAK-1196 - Node.getReferences() should not show references in frozen nodes
public void testVersionedReferences() throws RepositoryException {
    Node ref = testRootNode.addNode(nodeName2, testNodeType);
    ref.addMixin(mixReferenceable);
    superuser.save();
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    n.addMixin(mixVersionable);
    n.setProperty("myref", ref);
    superuser.save();
    String p = n.getPath();
    VersionManager vMgr = superuser.getWorkspace().getVersionManager();
    Version v1 = vMgr.checkpoint(p);
    Node frozen = v1.getFrozenNode();
    assertEquals("ref", ref.getPath(), frozen.getProperty("myref").getNode().getPath());
    checkReferences("ref in version store", ref.getReferences(), n.getPath() + "/myref");
    // also test what happens if node is removed
    n.remove();
    ref.remove();
    superuser.save();
    try {
        frozen.getProperty("myref").getNode();
        fail("removed reference should not be accessible");
    } catch (ItemNotFoundException e) {
    // ok
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) VersionManager(javax.jcr.version.VersionManager) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 72 with ItemNotFoundException

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

the class PropertyImpl method getNode.

@Override
@Nonnull
public Node getNode() throws RepositoryException {
    return perform(new PropertyOperation<Node>(dlg, "getNode") {

        @Nonnull
        @Override
        public Node perform() throws RepositoryException {
            // TODO: avoid nested calls
            Value value = getValue();
            switch(value.getType()) {
                case PropertyType.REFERENCE:
                case PropertyType.WEAKREFERENCE:
                    return getSession().getNodeByIdentifier(value.getString());
                case PropertyType.PATH:
                case PropertyType.NAME:
                    String path = value.getString();
                    if (path.startsWith("[") && path.endsWith("]")) {
                        // identifier path
                        String identifier = path.substring(1, path.length() - 1);
                        return getSession().getNodeByIdentifier(identifier);
                    } else {
                        try {
                            return (path.charAt(0) == '/') ? getSession().getNode(path) : getParent().getNode(path);
                        } catch (PathNotFoundException e) {
                            throw new ItemNotFoundException(path);
                        }
                    }
                case PropertyType.STRING:
                    try {
                        Value refValue = ValueHelper.convert(value, PropertyType.REFERENCE, getValueFactory());
                        return getSession().getNodeByIdentifier(refValue.getString());
                    } catch (ItemNotFoundException e) {
                        throw e;
                    } catch (RepositoryException e) {
                        // try if STRING value can be interpreted as PATH value
                        Value pathValue = ValueHelper.convert(value, PropertyType.PATH, getValueFactory());
                        path = pathValue.getString();
                        try {
                            return (path.charAt(0) == '/') ? getSession().getNode(path) : getParent().getNode(path);
                        } catch (PathNotFoundException e1) {
                            throw new ItemNotFoundException(pathValue.getString());
                        }
                    }
                default:
                    throw new ValueFormatException("Property value cannot be converted to a PATH, REFERENCE or WEAKREFERENCE");
            }
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) Node(javax.jcr.Node) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException) Nonnull(javax.annotation.Nonnull)

Example 73 with ItemNotFoundException

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

the class FsItem method getAncestor.

@Override
public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
    String path;
    if (depth == 0) {
        path = "/";
    } else {
        String[] pathParts = StringUtils.splitPreserveAllTokens(getPath(), "/");
        path = StringUtils.join(pathParts, "/", 0, depth + 1);
    }
    Resource resource = resolver.getResource(path);
    if (resource != null) {
        Node refNode = resource.adaptTo(Node.class);
        if (refNode != null) {
            return refNode;
        }
    }
    throw new ItemNotFoundException();
}
Also used : Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 74 with ItemNotFoundException

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

the class Util method getProperty.

/**
     * Resolves the given <code>item</code> to a <code>Property</code> from
     * which contents can be read.
     * <p>
     * The following mechanism is used to derive the contents:
     * <ol>
     * <li>If the <code>item</code> is a property, this property is used</li>
     * <li>If the <code>item</code> is a node, three steps are tested:
     * <ol>
     * <li>If the node has a <code>jcr:content</code> child node, use that
     * child node in the next steps. Otherwise continue with the node.</li>
     * <li>Check for a <code>jcr:data</code> property and use that property
     * if existing.</li>
     * <li>Otherwise call <code>getPrimaryItem</code> method repeatedly until
     * a property is returned or until no more primary item is available.</li>
     * </ol>
     * </ol>
     * If no property can be resolved using the above algorithm or if the
     * resulting property is a multivalue property, <code>null</code> is
     * returned. Otherwise if the resulting property is a <code>REFERENCE</code>
     * property, the node referred to is retrieved and this method is called
     * recursively with the node. Otherwise, the resulting property is returned.
     *
     * @param item The <code>Item</code> to resolve to a <code>Property</code>.
     * @return The resolved <code>Property</code> or <code>null</code> if
     *         the resolved property is a multi-valued property or the
     *         <code>item</code> is a node which cannot be resolved to a data
     *         property.
     * @throws ValueFormatException If the <code>item</code> resolves to a
     *             single-valued <code>REFERENCE</code> type property which
     *             cannot be resolved to the node referred to.
     * @throws RepositoryException if another error occurrs accessing the
     *             repository.
     */
public static Property getProperty(Item item) throws ValueFormatException, RepositoryException {
    Property prop;
    if (item.isNode()) {
        // check whether the node has a jcr:content node (e.g. nt:file)
        Node node = (Node) item;
        if (node.hasNode("jcr:content")) {
            node = node.getNode("jcr:content");
        }
        // if the node has a jcr:data property, use that property
        if (node.hasProperty("jcr:data")) {
            prop = node.getProperty("jcr:data");
        } else {
            // otherwise try to follow default item trail
            try {
                item = node.getPrimaryItem();
                while (item.isNode()) {
                    item = ((Node) item).getPrimaryItem();
                }
                prop = (Property) item;
            } catch (ItemNotFoundException infe) {
                // we don't actually care, but log for completeness
                log.debug("getProperty: No primary items for " + node.getPath(), infe);
                return null;
            }
        }
    } else {
        prop = (Property) item;
    }
    // been thrown
    if (prop.getDefinition().isMultiple()) {
        log.error("{} is a multivalue property", prop.getPath());
        return null;
    } else if (prop.getType() == PropertyType.REFERENCE) {
        Node node = prop.getNode();
        log.info("Property {} refers to node {}; finding primary item", prop.getPath(), node.getPath());
        return getProperty(node);
    }
    return prop;
}
Also used : Node(javax.jcr.Node) Property(javax.jcr.Property) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 75 with ItemNotFoundException

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

the class JcrNodeResource method getInputStream.

/**
     * Returns a stream to the <em>jcr:data</em> property if the
     * {@link #getNode() node} is an <em>nt:file</em> or <em>nt:resource</em>
     * node. Otherwise returns <code>null</code>.
     */
private InputStream getInputStream() {
    // implement this for nt:file only
    final Node node = getNode();
    if (node != null) {
        try {
            // find the content node: for nt:file it is jcr:content
            // otherwise it is the node of this resource
            Node content = node.isNodeType(NT_FILE) ? node.getNode(JCR_CONTENT) : node.isNodeType(NT_LINKEDFILE) ? node.getProperty(JCR_CONTENT).getNode() : node;
            Property data;
            // if the node has a jcr:data property, use that property
            if (content.hasProperty(JCR_DATA)) {
                data = content.getProperty(JCR_DATA);
            } else {
                // otherwise try to follow default item trail
                try {
                    Item item = content.getPrimaryItem();
                    while (item.isNode()) {
                        item = ((Node) item).getPrimaryItem();
                    }
                    data = (Property) item;
                } catch (ItemNotFoundException infe) {
                    // we don't actually care, but log for completeness
                    LOGGER.debug("getInputStream: No primary items for {}", toString(), infe);
                    data = null;
                }
            }
            if (data != null) {
                return data.getBinary().getStream();
            }
        } catch (RepositoryException re) {
            LOGGER.error("getInputStream: Cannot get InputStream for " + this, re);
        }
    }
    // fallback to non-streamable resource
    return null;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property) 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