Search in sources :

Example 51 with Item

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

the class JcrResourceProvider method delete.

@Override
public void delete(@Nonnull final ResolveContext<JcrProviderState> ctx, @Nonnull final Resource resource) throws PersistenceException {
    // try to adapt to Item
    Item item = resource.adaptTo(Item.class);
    try {
        if (item == null) {
            final String jcrPath = resource.getPath();
            if (jcrPath == null) {
                logger.debug("delete: {} maps to an empty JCR path", resource.getPath());
                throw new PersistenceException("Unable to delete resource", null, resource.getPath(), null);
            }
            item = ctx.getProviderState().getSession().getItem(jcrPath);
        }
        item.remove();
    } catch (final RepositoryException e) {
        throw new PersistenceException("Unable to delete resource", e, resource.getPath(), null);
    }
}
Also used : Item(javax.jcr.Item) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Example 52 with Item

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

the class JcrItemResourceFactory method createResource.

/**
     * Creates a <code>Resource</code> instance for the item found at the
     * given path. If no item exists at that path or the item does not have
     * read-access for the session of this resolver, <code>null</code> is
     * returned.
     *
     * @param resourcePath The absolute path
     * @return The <code>Resource</code> for the item at the given path.
     * @throws RepositoryException If an error occurrs accessing checking the
     *             item in the repository.
     */
public JcrItemResource<?> createResource(final ResourceResolver resourceResolver, final String resourcePath, final Resource parent, final Map<String, String> parameters) throws RepositoryException {
    final String jcrPath = resourcePath;
    if (jcrPath == null) {
        log.debug("createResource: {} maps to an empty JCR path", resourcePath);
        return null;
    }
    final String version;
    if (parameters != null && parameters.containsKey("v")) {
        version = parameters.get("v");
    } else {
        version = null;
    }
    Node parentNode = null;
    String parentResourcePath = null;
    if (parent != null) {
        parentNode = parent.adaptTo(Node.class);
        parentResourcePath = parent.getPath();
    }
    Item item;
    if (parentNode != null && resourcePath.startsWith(parentResourcePath)) {
        String subPath = resourcePath.substring(parentResourcePath.length());
        if (!subPath.isEmpty() && subPath.charAt(0) == '/') {
            subPath = subPath.substring(1);
        }
        item = getSubitem(parentNode, subPath);
    } else {
        item = getItemOrNull(jcrPath);
    }
    if (item != null && version != null) {
        item = getHistoricItem(item, version);
    }
    if (item == null) {
        log.debug("createResource: No JCR Item exists at path '{}'", jcrPath);
        return null;
    } else {
        final JcrItemResource<?> resource;
        if (item.isNode()) {
            log.debug("createResource: Found JCR Node Resource at path '{}'", resourcePath);
            resource = new JcrNodeResource(resourceResolver, resourcePath, version, (Node) item, helper);
        } else {
            log.debug("createResource: Found JCR Property Resource at path '{}'", resourcePath);
            resource = new JcrPropertyResource(resourceResolver, resourcePath, version, (Property) item);
        }
        resource.getResourceMetadata().setParameterMap(parameters);
        return resource;
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) Property(javax.jcr.Property)

Example 53 with Item

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

the class JcrItemResourceFactoryTest method compareGetItemOrNull.

private void compareGetItemOrNull(String path, String expectedPath) throws RepositoryException {
    HelperData helper = new HelperData(new AtomicReference<DynamicClassLoaderManager>());
    Item item1 = new JcrItemResourceFactory(session, helper).getItemOrNull(path);
    Item item2 = new JcrItemResourceFactory(nonJackrabbitSession, helper).getItemOrNull(path);
    if (expectedPath == null) {
        assertNull(item1);
        assertNull(item2);
    } else {
        assertNotNull(item1);
        assertEquals(expectedPath, item1.getPath());
        assertNotNull(item2);
        assertEquals(expectedPath, item2.getPath());
    }
}
Also used : Item(javax.jcr.Item) HelperData(org.apache.sling.jcr.resource.internal.HelperData) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager)

Example 54 with Item

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

the class ScriptableItemMap method get.

@Override
public Object get(String name, Scriptable start) {
    // special provision for the "length" property to simulate an array
    if ("length".equals(name)) {
        return ScriptRuntime.toNumber(this.items.keySet().size() + "");
    }
    Item item = items.get(name);
    Object result = Undefined.instance;
    if (item != null) {
        result = ScriptRuntime.toObject(this, item);
    }
    return result;
}
Also used : Item(javax.jcr.Item) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 55 with Item

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

the class MockSession method getNode.

@Override
public Node getNode(final String absPath) throws RepositoryException {
    checkLive();
    Item item = getItem(absPath);
    if (item instanceof Node) {
        return (Node) item;
    } else {
        throw new PathNotFoundException(String.format("No node found at: %s.", absPath));
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) PathNotFoundException(javax.jcr.PathNotFoundException)

Aggregations

Item (javax.jcr.Item)117 Node (javax.jcr.Node)48 RepositoryException (javax.jcr.RepositoryException)28 Property (javax.jcr.Property)19 Session (javax.jcr.Session)15 PathNotFoundException (javax.jcr.PathNotFoundException)11 ArrayList (java.util.ArrayList)7 ItemNotFoundException (javax.jcr.ItemNotFoundException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 IOException (java.io.IOException)5 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)5 HashSet (java.util.HashSet)4 NodeIterator (javax.jcr.NodeIterator)4 Principal (java.security.Principal)3 NoSuchElementException (java.util.NoSuchElementException)3 AccessDeniedException (javax.jcr.AccessDeniedException)3 PropertyIterator (javax.jcr.PropertyIterator)3 Version (javax.jcr.version.Version)3 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)2