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;
}
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);
}
}
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));
}
}
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());
}
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));
}
}
Aggregations