Getting Resources and Properties in SlingPage edited by Jean-Christophe KAUTZMANNChanges (1)
Full ContentGetting Resources and Properties in SlingThe Resource is one of the central parts of Sling. Extending from JCR's Everything is Content, Sling assumes Everthing is a Resource. Thus Sling is maintaining a virtual tree of resources, which is a merger of the actual contents in the JCR Repository and resources provided by so called resource providers. By doing this Sling fits very well in the paradigm of the REST architecture. In this article we will explore a few ways to programmatically map a resource path (String) to a resource object (Resource) and its properties in Sling, from within an OSGI service, a servlet and a JSP. The whole game consists in first getting a ResourceResolver and then getting the Resource itself. Within an OSGI Service/CompomentYou can access a resource through the JcrResourceResolverFactory service:
/** @scr.reference */
private SlingRepository repository;
/** @scr.reference */
private JcrResourceResolverFactory resolverFactory;
public void myMethod() {
Session adminSession = null;
try {
String resourcePath = "path/to/resource";
adminSession = repository.loginAdministrative(null);
ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);
Resource res = resourceResolver.getResource(resourcePath);
} catch (RepositoryException e) {
log.error("RepositoryException: " + e);
} finally {
if (adminSession != null && adminSession.isLive()) {
adminSession.logout();
adminSession = null;
}
}
}
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Sling Website > Getting Resources and Properti... confluence
- [CONF] Apache Sling Website > Getting Resources and Pro... confluence
- [CONF] Apache Sling Website > Getting Resources and Pro... confluence
- [CONF] Apache Sling Website > Getting Resources and Pro... confluence
- [CONF] Apache Sling Website > Getting Resources and Pro... confluence
- [CONF] Apache Sling Website > Getting Resources and Pro... confluence
