Understanding ResourceResolver: Accessing Content Safely and Efficiently
In Apache Sling, ResourceResolver is your gateway to content stored in the JCR. It is the primary API to access resources, navigate the content tree, and perform CRUD operations. Understanding how to obtain and manage ResourceResolver instances is crucial for building robust, scalable, and secure AEM applications.
What Is a ResourceResolver?
A ResourceResolver is an interface that allows you to:
Resolve paths to
ResourceobjectsNavigate the content tree (
Resource.getChild(),Resource.getParent())Access properties of nodes (
ValueMap)Adapt resources to Sling Models or other objects
Create, update, or delete content (if the resolver has write access)
Commit or rollback changes
Think of it as a session-like object that gives controlled access to the repository content.
Ways to Obtain a ResourceResolver
There are multiple ways to obtain a ResourceResolver, depending on your context:
1. Using SlingHttpServletRequest
When you are in a Sling Servlet or Sling Model:
@Resource
private SlingHttpServletRequest request;
public void doSomething() {
ResourceResolver resolver = request.getResourceResolver();
Resource resource = resolver.getResource("/content/my-site");
}
Scope: Limited to the request; automatically closed after the request ends.
Use case: Most common in servlets, filters, and Sling Models.
2. Using ResourceResolverFactory (System or Service ResourceResolver)
ResourceResolverFactory provides more flexibility, including service users for system-level access:
@Reference
private ResourceResolverFactory resolverFactory;
public void doSomething() throws LoginException {
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "data-reader");
try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(param)) {
Resource resource = resolver.getResource("/content/my-site");
// read or modify content
}
}
Key points:
Service ResourceResolver: Allows code to access JCR using a dedicated service user.
Requires: Service user mapping in OSGi (
User Mappingconfiguration in Apache Sling Service User Mapper).Try-with-resources: Always close
ResourceResolverto avoid memory leaks.
3. Adaptation
Sling allows adaptation from SlingHttpServletRequest, Resource, or SlingScriptHelper:
ResourceResolver resolver = request.adaptTo(ResourceResolver.class);
Returns the resolver associated with the current request.
Lightweight and safe; no explicit closing needed.
Best Practices
Prefer Service Users for System Access
Always useResourceResolverFactory.getServiceResourceResolverwith a dedicated service user for non-request scoped operations (background jobs, workflows).Close Resolvers
Request-based resolvers are closed automatically. System resolvers must be closed in a try-with-resources block.Avoid Reusing Resolvers Across Threads
Each resolver is request/session-scoped; sharing across threads may lead to inconsistent behavior.Use Adaptation
Adapting resources or requests is safer than creating new resolvers for every operation.Always Handle LoginException
Service resource resolvers may throw exceptions if the service user is not mapped correctly.
Comments
Join the discussionNo comments yet. Be the first to comment.

