Whats the differnece between getResource() and resolve() methods | Community
Skip to main content
Tyler_Maynard
May 2, 2017
Solved

Whats the differnece between getResource() and resolve() methods

  • May 2, 2017
  • 4 replies
  • 6003 views

When developing we've come across the permissions/dispatcher issue of the getResource() method breaking page.

These forums suggested that we needed to use resolve() method instead of the getResource() method and that does work, but I am more interested in why that works.

I understand that it has to do with not having the appropriate permissions, because you might be trying to get a resource that an anonymous user does not have access to.

But in the end, they both return a Resource.

Can anyone explain the differences between the two methods and why one works with anonymous permissions and one does not. I have read the documentation multiple times, located here, but it doesn't quite make complete sense to me. Just trying to understand the JCR resourceResolver a little better.

Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by awadheshv
ResourceResolver.getResource() return the resource at the exact path, resolve() methods would also look for sling mappings and take them into account before returning a resource.

4 replies

awadheshvAccepted solution
May 3, 2017
ResourceResolver.getResource() return the resource at the exact path, resolve() methods would also look for sling mappings and take them into account before returning a resource.
Nupur_Jain
Adobe Employee
Adobe Employee
May 4, 2017

Hi,

One more difference that I can find is that getResource() method returns null if the resource does not exist in the repository whereas the resolve() method returns the instance of the NonExistingResource if the resource does not exist where this NonExistingResource instance can further be used to create that resource.

Thanks!

Nupur

Tyler_Maynard
May 4, 2017

My question is more about what is happening behind these methods I suppose.

Why does one fail on publish and on does not.

The resolve() method will work on publish instances when not logged in, and the getResource() will not. I am more curious on why that is? How is the algorithm on how these two methods get the resource different? Does one use the user session and one does not? 

varunmitra
Adobe Employee
Adobe Employee
May 4, 2017

you can look up the code for ResourceResolver on grepcode[1]. by the looks of it, getResource() makes use of ResourceResolverContext[2]. ResourceResolverContext takes into account the logged in user info.

I couldn't find any such requirement in resolve() method. that being said, you can delve into the code and look.

happy hunting 

 

[1] http://grepcode.com/file/repo1.maven.org/maven2/org.apache.sling/org.apache.sling.resourceresolver/1.0.4/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java#ResourceResolverImpl.resolve%28javax.servlet.http.HttpServletRequest%2Cjava.lang.String%29

[2] http://grepcode.com/file/repo1.maven.org/maven2/org.apache.sling/org.apache.sling.resourceresolver/1.0.4/org/apache/sling/resourceresolver/impl/helper/ResourceResolverContext.java#ResourceResolverContext.%3Cinit%3E%28boolean%2Cjava.util.Map%29