How to get resources from path in Adobe AEM?

Member

by berta , in category: Java , a year ago

How to get resources from path in Adobe AEM?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jakob , a year ago

@berta 

In Adobe Experience Manager (AEM), you can use the ResourceResolver interface to get a Resource object that represents a resource in the AEM repository. You can then use the Resource object to access the properties and child resources of the resource.


To get a Resource object from a path in AEM, you can use the getResource method of the ResourceResolver interface. Here's an example of how you can do this in a Java servlet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Get the ResourceResolver from the request
ResourceResolver resolver = request.getResourceResolver();

// Use the ResourceResolver to get a Resource object for a path in the AEM repository
Resource resource = resolver.getResource("/content/my-project/en/my-page");

// Use the Resource object to access the properties and child resources of the resource
ValueMap properties = resource.getValueMap();
String title = properties.get("jcr:title", String.class);
Resource childResource = resource.getChild("my-resource");


You can also use the getResource method to get a Resource object from a path that includes a resource type, by specifying the resource type as the second argument. For example:

1
Resource resource = resolver.getResource("/content/my-project/en/my-page", "cq:Page");


This will return a Resource object only if the resource at the specified path has the specified resource type.


It's worth noting that the getResource method will return null if a resource does not exist at the specified path. It's a good idea to check for null before using the Resource object to avoid a NullPointerException.


I hope this helps! Let me know if you have any questions.

by cruz.howell , 4 months ago

@berta 

To get resources from a path in Adobe AEM, you can use the Sling API. Here's an example of how you can do this in Java:

  1. Inject the SlingHttpServletRequest object into your Java class:
1
2
@SlingObject
private SlingHttpServletRequest request;


  1. Use the ResourceResolverFactory to get the ResourceResolver object:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Reference
private ResourceResolverFactory resolverFactory;

private ResourceResolver getResourceResolver() throws LoginException {
    // Get the service user
    Map<String, Object> authInfo = new HashMap<>();
    authInfo.put(ResourceResolverFactory.SUBSERVICE, "your-service-user");

    // Get the ResourceResolver
    return resolverFactory.getServiceResourceResolver(authInfo);
}


  1. Use the ResourceResolver object to get the Resource object from the path:
1
2
3
4
ResourceResolver resolver = getResourceResolver();
String path = "/content/my-project/en/my-page";

Resource resource = resolver.getResource(path);


  1. Access the properties and child resources of the resource:
1
2
3
4
5
6
7
8
9
if (resource != null) {
    ValueMap properties = resource.getValueMap();
    String title = properties.get("jcr:title", String.class);

    Resource childResource = resource.getChild("child-resource");
    if (childResource != null) {
        // Do something with childResource
    }
}


It's important to note that you should always close the ResourceResolver object once you're done using it:

1
2
3
if (resolver != null && resolver.isLive()) {
    resolver.close();
}


This will release the resources and avoid potential memory leaks.


Hope this helps! Let me know if you have any further questions.