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