AEM Resource API
Resource API is most important interface which belongs to org.apache.sling.api.resource package and helps us to work with resources.
Resource is basic building block of AEM and in AEM everything (node, page asset, etc.) is a resource.
Resources are pieces of content on which Sling acts and also an Adaptable to get adapters to other types. A JCR based resource might support adapting to the JCR Node on which the resource is based.
ResourceResolver instance helps us to resolve resource object and helps us to read, write and delete operations on resource.
All implementations must support returning a value map from getValueMap(), even if the map is empty.
It is Not recommended to directly implement Resource interface. In place of that extend AbstractResource or ResourceWrapper. This extend approach will not force us to override missing methods.
We are going to use below screenshot hierarchy to access resources using ResourceResolver instance.
Follow link to create ResourceResolver instance to access Resources using service user.
test page as a resource can be access using below source code snippet:
Access test resource name, path and resourceType using below source code snippet
OUTPUT
>>>>>>>>>>> resource.getName() = test
>>>>>>>>>>> resource.getPath() = /content/practice/us/en/test
>>>>>>>>>>> resource.getResourceType() = practice/components/page
>>>>>>>>>>>
resource.getResourceSuperType() =
resource.getResourceSuperType() will not return anything in current use case as practice component is not inheriting other component.
resource.getValueMap() will return all properties present on /content/practice/us/en/test node.
OUTPUT
Now, its time to access parent resource of test which is en as mentioned below
resource.getParent() will return parent resource for test which is en.
OUTPUT
>>>>>>>>>>> Parent resource name = ‘en’
parentResource.listChildren() will return all test and jcr:content as child resources and print its name. In current scenario and as showcase in above screenshot the child resource for en is test and jcr:content.
OUTPUT
>>>>>>>>>>> Children Resource = ‘jcr:content’
>>>>>>>>>>> Children Resource = ‘test’
parentResource.getChild(“test”) will return test as a child resource for en.
OUTPUT
>>>>>>>>>>> Child resource name = ‘test’
Adapt Node
Sling provide a capability to adapting a resource with the help of adaptTo() method from Adaptable interface.
Using below statement we can adapt resource to Node class.
Node node = resource.adaptTo(Node.class);
We can get Node object belongs to javax.jcr.Node package with the help if resource.
OUTPUT
>>>>>>>>>>> node.getName() = ‘test’
>>>>>>>>>>> node.getPath() = ‘/content/practice/us/en/test’
Adapt Custom class
adaptTo() method provide a flexibility to adapt custom class to map resource properties with the help of Sling Model.
- Create PracticeModel.java Sling Model class will help us to map resource jcr:title with PracticeModel.java class title field.
package com.javadoubts.core.models;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.*;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class PracticeModel {
@Inject
@Optional
@Named("jcr:title")
protected String title;
@PostConstruct
protected void init() { }
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2. Use below statement to adapt a resource with PracticeModel.java class with the help of adaptTo() method.
PracticeModel practiceModel = resource.getChild("jcr:content").adaptTo(PracticeModel.class);logger.error(">>>>>>>>>>> practiceModel.getTitle() = '{}'", practiceModel.getTitle());
3. Below is the screenshot from CRX DE for jcr:title having value “Test Page Title” present at /content/practice/us/en/test/jcr:content resource:
OUTPUT
>>>>>>>>>>> practiceModel.getTitle() = ‘Test Page Title’
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.