Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Sling Models and adaptables- SlingHttpServletRequest and Resource

Avatar

Level 10

Hi,

 

I had couple questions, i wanted to get the current page path as well as the properties under page/jcr:content/image/fileReference.

Option1- If i use a resource as adaptable, i can get file reference but unable to get current page path, the resource.getPath points to the jcr:content of the page.

If i use the resource resolver from the resource marked with @self, do i have to close it? Is it wise?

PageManager pageManager= resource.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(resource);

 

2. If i use SlingHttpServletRequest and Resource, i am not able to get the properties under page/jcr:content/image/fileReference while i could get the currentpage path.
Tried @Via but in vain

 

 

Is it wise to use both adaptables?

 

Thanks in advance

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@NitroHazeDev 
You're right about the part of not using both adaptables however in most of the use cases it doesn't even cause any issue. Explanation is given here:
https://stackoverflow.com/questions/68838818/aem-slingmodels-why-do-we-need-unused-adaptables-in-eac...

Option 2 - Above link should provide you explanation. 

Option 1 - The approach is okay. There's no need at all to close the resolver. Only service resolver should be closed and that should if not "try-with-resource"
https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-r...

Since your sling model is invoked at page level and injected resource points to sling resource type which is linked to jcr:content node of page and that's the reason you get the following when you try to get resource.getPath()

So, as you're already shared piece of code to get the page out of it - it's correct. 

View solution in original post

7 Replies

Avatar

Community Advisor

@NitroHazeDev Yes, it's absolutely fine to use both adaptables. 

Avatar

Level 10

@Himanshu_Singhal 

Thank you for your response, i felt the same. The reason for the ask is to get to the best practices cause i happened to see a blog mentioning it is not per the best practice

 

Option2-Is there a way to get the values with both adaptables? I am not able to get values with Slinghttpservlet request and resource adaptables for the properties under jcr:content/image/@fileReference


With option1 is the approach ok to get the page path and do i have to close the resource resolver explicitly? and is it the right way to get to the resource resolver? i know of another via annotations
@SlingObject
private ResourceResolver resolver;

 

Avatar

Correct answer by
Community Advisor

@NitroHazeDev 
You're right about the part of not using both adaptables however in most of the use cases it doesn't even cause any issue. Explanation is given here:
https://stackoverflow.com/questions/68838818/aem-slingmodels-why-do-we-need-unused-adaptables-in-eac...

Option 2 - Above link should provide you explanation. 

Option 1 - The approach is okay. There's no need at all to close the resolver. Only service resolver should be closed and that should if not "try-with-resource"
https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-r...

Since your sling model is invoked at page level and injected resource points to sling resource type which is linked to jcr:content node of page and that's the reason you get the following when you try to get resource.getPath()

So, as you're already shared piece of code to get the page out of it - it's correct. 

Avatar

Level 10

@Himanshu_Singhal  So the jist would be to not use both. I figured it was a mess while playing around a while back after i responded

Also, how do i get the properties via annotations for properties under jcr:content/image say?

 

 just to summarize, below is ok please let me know? with no closing resource resolver? @Himanshu_Singhal 

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

@Self
private Resource resource;

@SlingObject
private ResourceResolver resolver;

 

@Inject
@Optional
@Named("image/fileReference")
private String thumbUrl;

 

private String extPageUrl;

 

public String getExtPageUrl() {
PageManager pageManager= resolver.adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(resource);
logger.info("Page model  {}",currentPage.getPath());

//do some processing
return this.extPageUrl;
}

}

Avatar

Community Advisor

@NitroHazeDev Yes, your code seems alright. 
Also, instead of individual prop of child resource, if you wish to obtain entire resource that also you can do using annotation 

@ChildResource
private Resource image;

That will give you control over entire resource and whatever properties you wish to fetch. However, if there's only 1 prop value needed in that case, injecting is another alternative you've chosen. 

Avatar

Level 10

Thanks @Himanshu_Singhal, as you may have noticed, i haven't closed the resource resolver but wish there was doc on it for sling models specifically where we obtain resource resolver  from resource.


Apart from using below.. i wish there was a direct  way via annotations to get the properties, do you know?..   Page currentPage = pageManager.getContainingPage(resource);

if slinghttprequest is used, have you tried to obtain properties via annotations?

 

 

Avatar

Community Advisor
  • There's no need to close resolver. Only service created resource-resolver shall be closed and that too if used outside the context of "try-with-resource".
  • To get the properties - if you're using Resource adaptable then you can use @inject to get the property value. If you're working with Request adaptable then you can use @ValueMapValue to get the property value inside sling model. However, keep in mind that you directly get the property associated with resource via which sling model is invoked. Technically, we call sling model w.r.t. resource for which operation has to be performed. If you need to get any property that is different from resource from which sling model is called, then we usually try to get the resource first and then get the property out of it. 


    I hope it helps!