Search in sources :

Example 11 with Item

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

the class JcrNodeResourceMetadata method get.

@Override
public Object get(final Object key) {
    final Object result = super.get(key);
    if (result != null) {
        return result;
    }
    if (CREATION_TIME.equals(key)) {
        promoteNode();
        internalPut(CREATION_TIME, creationTime);
        return creationTime;
    } else if (CONTENT_TYPE.equals(key)) {
        String contentType = null;
        final Node targetNode = promoteNode();
        try {
            if (targetNode.hasProperty(JCR_MIMETYPE)) {
                contentType = targetNode.getProperty(JCR_MIMETYPE).getString();
            }
        } catch (final RepositoryException re) {
            report(re);
        }
        internalPut(CONTENT_TYPE, contentType);
        return contentType;
    } else if (CHARACTER_ENCODING.equals(key)) {
        String characterEncoding = null;
        final Node targetNode = promoteNode();
        try {
            if (targetNode.hasProperty(JCR_ENCODING)) {
                characterEncoding = targetNode.getProperty(JCR_ENCODING).getString();
            }
        } catch (final RepositoryException re) {
            report(re);
        }
        internalPut(CHARACTER_ENCODING, characterEncoding);
        return characterEncoding;
    } else if (MODIFICATION_TIME.equals(key)) {
        long modificationTime = -1;
        final Node targetNode = promoteNode();
        try {
            if (targetNode.hasProperty(JCR_LASTMODIFIED)) {
                // We don't check node type, so JCR_LASTMODIFIED might not be a long
                final Property prop = targetNode.getProperty(JCR_LASTMODIFIED);
                try {
                    modificationTime = prop.getLong();
                } catch (final ValueFormatException vfe) {
                    LOGGER.debug("Property {} cannot be converted to a long, ignored ({})", prop.getPath(), vfe);
                }
            }
        } catch (final RepositoryException re) {
            report(re);
        }
        internalPut(MODIFICATION_TIME, modificationTime);
        return modificationTime;
    } else if (CONTENT_LENGTH.equals(key)) {
        long contentLength = -1;
        final Node targetNode = promoteNode();
        try {
            // if the node has a jcr:data property, use that property
            if (targetNode.hasProperty(JCR_DATA)) {
                final Property prop = targetNode.getProperty(JCR_DATA);
                contentLength = JcrItemResource.getContentLength(prop);
            } else {
                // otherwise try to follow default item trail
                Item item = getPrimaryItem(targetNode);
                while (item != null && item.isNode()) {
                    item = getPrimaryItem((Node) item);
                }
                if (item != null) {
                    final Property data = (Property) item;
                    // set the content length property as a side effect
                    // for resources which are not nt:file based and whose
                    // data is not in jcr:content/jcr:data this will lazily
                    // set the correct content length
                    contentLength = JcrItemResource.getContentLength(data);
                }
            }
        } catch (final RepositoryException re) {
            report(re);
        }
        internalPut(CONTENT_LENGTH, contentLength);
        return contentLength;
    }
    return null;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) ValueFormatException(javax.jcr.ValueFormatException) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 12 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 13 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)

Example 14 with Item

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

the class MockSession method listChildren.

RangeIterator listChildren(final String parentPath, final ItemFilter filter) throws RepositoryException {
    List<Item> children = new ArrayList<Item>();
    //remove trailing slash or make root path / empty string
    final String path = parentPath.replaceFirst("/$", "");
    // build regex pattern for all child paths of parent
    Pattern pattern = Pattern.compile("^" + Pattern.quote(path) + "/[^/]+$");
    // collect child resources
    for (ItemData item : this.items.values()) {
        if (pattern.matcher(item.getPath()).matches() && (filter == null || filter.accept(item))) {
            children.add(item.getItem(this));
        }
    }
    return new RangeIteratorAdapter(children.iterator(), children.size());
}
Also used : Item(javax.jcr.Item) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) RangeIteratorAdapter(org.apache.jackrabbit.commons.iterator.RangeIteratorAdapter)

Example 15 with Item

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

the class MockSession method getProperty.

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

Aggregations

Item (javax.jcr.Item)138 Node (javax.jcr.Node)61 RepositoryException (javax.jcr.RepositoryException)34 Session (javax.jcr.Session)26 Property (javax.jcr.Property)24 PathNotFoundException (javax.jcr.PathNotFoundException)20 ArrayList (java.util.ArrayList)7 JcrCallback (org.springframework.extensions.jcr.JcrCallback)7 PrintWriter (java.io.PrintWriter)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 PropertyIterator (javax.jcr.PropertyIterator)5 ValueFormatException (javax.jcr.ValueFormatException)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)5 MetadataAccessException (com.thinkbiganalytics.metadata.api.MetadataAccessException)4 MetadataExecutionException (com.thinkbiganalytics.metadata.api.MetadataExecutionException)4 JcrPath (com.thinkbiganalytics.metadata.modeshape.support.JcrPath)4