Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

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

Avatar

Level 4

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!

1 Accepted Solution

Avatar

Correct answer by
Level 3
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.

View solution in original post

4 Replies

Avatar

Correct answer by
Level 3
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.

Avatar

Community Advisor

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

Avatar

Level 4

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? 

Avatar

Level 3

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...

[2] http://grepcode.com/file/repo1.maven.org/maven2/org.apache.sling/org.apache.sling.resourceresolver/1...