The 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.
You can access a resource through the ResourceResolverFactory service:
You can access the resource defined by the request URL through the SlingHttpServletRequest:
You can access any resource by first accessing the ResourceResolver:
When you use the <sling:defineObjects> tag in a JSP file, you have access to a few handy objects, one of them is resource, the resource that is resolved from the URL. Another one is resourceResolver, the ResourceResolver defined through the SlingHttpServletRequest.
To access a resource:
If needed you can adapt a Sling Resource to a JCR Node:
Note: resource.adaptTo(Node.class) may return null if the resource is not backed by a JCR node. This is particularly the case for NonExistingResource resources or resource provided by a non-JCR resource provider.
The ValueMap is an easy way to access properties of a resource. With most resources you can use Adaptable.adaptTo(Class) to adapt the resource to a value map:
You can also access the properties through the ResourceUtil utility class:
Then, to access a specific String property called propName:
For more details about resources and how to access them in Sling, you can refer to the Sling documentation about Resources.