AEM 6.1 : Get ResourceResolver From ResourceResolverFactory

In AEM, sometimes there is a need of having resourceResolver in our java code. But if we are not using servlet, means don’t have any request or resource initially then how can we get it ?

The focus of this tutorial is to learn how we can get ResourceResolver from ResourceResolverFactory. ResourceResolverFactory provides us ResourceResolver object with the help of apache sling service user Mapper Amendment.

There are a lot of ways to get resourceResolver in aem, but mainly we get ResourceResolver either by using request or Resource  :

  • Syntax for getting ResourceResolver from Request object(means current user access):
    ResourceResolver resourceResolver=request.getResourceResolver();
  • Syntax for getting ResourceResolver from Resource object:
    ResourceResolver resourceResolver= resource.getResourceResolver();

Steps to get ResourceResolver From ResourceResolverFactory:

First of all, open your Felix Console console and search for your project and get its symbolic name.

get-symbolic-name-osgi-service-aem

  • Go to configuration of felix console.
  • Search for apache sling service user Mapper Amendment.
  • Click on plus sign to add new user mapper service.
  • Enter below details.

get-service-user-mapper-amedment-subservice-aem

The format of Service Mapping will be like this:

Symbolic Name of the project:SUBSERVICE =userName

  • Symbolic Name of the project : group-id-mobile.project-AEM-bundle
  •  SUBSERVICE : getResourceResolver.
  • userName : testSystemUser 

Note:- Remember that the user you define here must be a system user. If it is a normal user aem 6.1 will not work for this. So you can create your own system user and provide priviledge to them according to your need.

Learn more about How to create system user in aem.

Now let’s try this in code:

Below servlet is trying to get resource from the ResourceResolver using ResourceResolverFactory

@Component(immediate = true, metatype = false, label = "Getting Resource Resolver")
@Properties(value = { @Property(name = "sling.servlet.paths", value = "/bin/resource") }) 
@Service 
public class MyResourceResolver extends SlingSafeMethodsServlet { 

@Reference private transient ResourceResolverFactory resourceResolverFactory; 

public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { 
try { 
  final Map <String, Object > param = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "getResourceResolver"); 
  ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(param); 
  String path = resourceResolver.getResource("/apps/sling").getPath(); 
  response.getWriter().write(path); 
} catch (LoginException e) {
  e.printStackTrace(); 
  response.getWriter().write(e.getMessage()); 
} 
}
}

Hit the servlet i.e. localhost:4502/bin/resource and check the output.

In Addition to that,

  • Sometimes the servlet is not resolved, you can check it from here.
  • It will open Adobe Experience Manager Web Console Sling Servlet Resolver for checking your servlet path, whether it is resolving your servlet or not.
aem-servlet-resolver-test

Please drop a comment, if you face any issue in implementing it.

Spread the love

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.