Resource Resolver in AEM

Contents

Objective

After reading this Article, You should have an Understanding of –

  • Resource Resolver in AEM.
  • Getting Resource Resolver with ResourceResolverFactory.
  • Getting Resource Resolver in Sling Model
  • Getting Resource Resolver in Sling Servlet. 

Introduction

In AEM Development we mostly come across to get Resource Resolver in Java Code and there are many ways to get Resource Resolver using Resource Resolver API. It is used to resolve the Resource Objects. we will cover all approach to get Resource Resolver step by step.

Hence, without any further delay,  let’s get started :-

Getting Resource Resolver with ResourceResolverFactory

To get Resource Resolver with ResourceResolverFactory we need to perform following steps

Step #1 : Create System User

  •  Navigate to crx/explorer – http://localhost:4502/crx/explorer/index.jsp
  • Click on Log In & login with AEM user and password.
  • Now, Click on User Administration. On the popup, click on Create System User, enter the UserID ( e.g. learning_system_user) and click on create icon.
Unlock System User
System User

Step #2 : Provide appropriate permission to new system user “unlock_system_user”.

  • Navigate to useradmin  – http://localhost:4502/useradmin.
  • Search for newly created system user (i.e. unlock_system_user).
  • Provide Last Name * Required field.
  •  Provide the permissions for the nodes where you need to use Resource Resolver to get the resource (i.e. read, write and replication permission on content).
User Permission
System User Permission

Step #3 : Configure Apache Sling Service User Mapper Service  / Amendment

  • Navigate to configMgr  – http://localhost:4502/system/console/configMgr.
  • Search for Apache Sling Service User Mapper Service Amendment.
  • Enter Service Mappings – learning.core:unlock_service=unlock_system_user
  •  General Service mapping be like – <Bundle Symbolic Name>:<Custom Sub Service Name>=<System User Id>.
  • Save the changes.  
User Mapper
User Mapper

Step #4 : Get Resource Resolver in Java Service

  • We can write Service to get Resource Resolver and reuse the service in different places in codebase.

ResourceResolverService.java

				
					package com.adobe.learning.core.service;

import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;

public interface ResourceResolverService {
    public ResourceResolver getResourceResolver() throws LoginException;
}
				
			

ResourceResolverServiceImpl.java

				
					package com.adobe.learning.core.service.impl;

import com.adobe.learning.core.service.ResourceResolverService;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.HashMap;
import java.util.Map;

@Component(service = ResourceResolverService.class, immediate = true)
public class ResourceResolverServiceImpl implements ResourceResolverService {

    private static final String SYSTEM_USER = "unlock_service";

    @Reference
    ResourceResolverFactory resourceResolverFactory;

    @Override
    public ResourceResolver getResourceResolver() throws LoginException {

        Map<String, Object> param = new HashMap<>();
        param.put(ResourceResolverFactory.SUBSERVICE, SYSTEM_USER);
        return resourceResolverFactory.getServiceResourceResolver(param);
    }
}

				
			

We can use above service to get Resource Resolver Using @Reference Annotations.

				
					    @Reference
    ResourceResolverService resourceResolverService;

    try (ResourceResolver resourceResolver = resourceResolverService.getResourceResolver()) {

        // Code Implementation

    } catch (Exception e) {
        LOGGER.error("Exception occurred: {}", e.getMessage());
    }
				
			

Getting Resource Resolver in Sling Model

 There are two ways to get Resource Resolver in Sling Model. Commonly we use adaptables SlingHttpServletRequest.class but we can also get Resource Resolver using Resource.class as adaptables.

Resource Resolver with @Model(adaptables = Resource.class)

				
					@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LearningPanelModel {

    @Inject
    private ResourceResolver resourceResolver;

    //OR
    @Inject @Source("sling-object")
    private ResourceResolver resourceResolver;

    //OR
    @SlingObject
    private ResourceResolver resourceResolver;
}
				
			

Resource Resolver with @Model(adaptables = SlingHttpServletRequest.class)

				
					@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LearningPanelModel {

    // @Inject @Source("script-bindings") @Named("resolver")
    // @Inject @Source("script-bindings")
    @ScriptVariable
    private ResourceResolver resolver;

    //OR
    @Self
    private SlingHttpServletRequest slingHttpServletRequest;

    ResourceResolver resourceResolver;

    @PostConstruct
    public void init() {
        resourceResolver = slingHttpServletRequest.getResourceResolver();
    }
}
				
			

Resource Resolver with @Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

				
					@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class LearningPanelModel {

    @Inject
    private ResourceResolver resourceResolver;

    //OR
    @Inject @Source("sling-object")
    private ResourceResolver resourceResolver;

    //OR
    @SlingObject
    private ResourceResolver resourceResolver;

    //OR
    @ScriptVariable
    private ResourceResolver resolver;
}
				
			

Getting Resource Resolver in Sling Servlet

We can get Resource Resolver through SlingHttpServletRequest.

				
					    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        ResourceResolver resourceResolver = request.getResourceResolver();
        //Custom Code
    }
				
			

Conclusion

  • So in this post, we tried to cover most of the approach to get Resource Resolver in AEM. I hope you enjoyed this post. If you find it useful, leave us a comment. I would love to hear your thoughts and suggestions to make it better.

15 Comments

  1. That is the proper blog for anybody who needs to search about this topic. You positively put a new spin on a topic that’s been written about for years. Nice stuff, simply great!

  2. […] We can use a service or servlet to create a user programmatically. In this scenario, we are going to use servlets. So for the creation of a user, we need user details like the user ID, user first name, user last name, user email, and password. These details we will fetch from the servlet request parameter. The user ID, user last name, and password are the only parameters required to create a user. We also need a resource resolver, which we will get with the help of the system user, as explained in the previous post – ResourceResolver  […]

Leave a Reply

Your email address will not be published. Required fields are marked *