Skip to main content

Understanding ResourceResolver: Accessing Content Safely and Efficiently

Updated 3 min readMarkdown
Part of seriesApache Sling Series

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 Resource objects

  • Navigate 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 Mapping configuration in Apache Sling Service User Mapper).

  • Try-with-resources: Always close ResourceResolver to 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

  1. Prefer Service Users for System Access
    Always use ResourceResolverFactory.getServiceResourceResolver with a dedicated service user for non-request scoped operations (background jobs, workflows).

  2. Close Resolvers
    Request-based resolvers are closed automatically. System resolvers must be closed in a try-with-resources block.

  3. Avoid Reusing Resolvers Across Threads
    Each resolver is request/session-scoped; sharing across threads may lead to inconsistent behavior.

  4. Use Adaptation
    Adapting resources or requests is safer than creating new resolvers for every operation.

  5. Always Handle LoginException
    Service resource resolvers may throw exceptions if the service user is not mapped correctly.

No comments yet. Be the first to comment.