AEM Resource API

AEM 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. 

1. Create PracticeModel.java Sling Model class will help us to map resource jcr:title with PracticeModel.java class title field.

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. 

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">PracticeModel practiceModel = resource.getChild("jcr:content").adaptTo(PracticeModel.class);</pre>
<!-- /wp:preformatted -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">logger.error(">>>>>>>>>>>   practiceModel.getTitle() = '{}'", practiceModel.getTitle());</pre>
<!-- /wp:preformatted -->

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

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan