April 16, 2020
Estimated Post Reading Time ~

How to get ResourceResolver From ResourceResolverFactory

In AEM, sometimes there is a need for having resourceResolver in our java code. But if we are not using a 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, but mainly we get ResourceResolver either by using request or Resource :
The syntax for getting ResourceResolver from Request object:
ResourceResolver resourceResolver=request.getResourceResolver();
The 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.


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


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 the privilege to them according to your needs.

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")
@Service
@Properties(value = {
@Property(name = "sling.servlet.paths", value = "/bin/resource")
})
public class MyResourceResolver extends SlingSafeMethodsServlet {
@Reference ResourceResolverFactory resourceResolverFactory;

public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
try {

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver");
ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);
String path = resourceResolver.getResource("/apps/sling").getPath();
response.getWriter().write(path);
} catch (Exception 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.



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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.