Getting Sling Model for a page by JcrNodeResource | Community
Skip to main content
Level 3
January 17, 2025
Solved

Getting Sling Model for a page by JcrNodeResource

  • January 17, 2025
  • 3 replies
  • 1926 views

Hi,

I am building a REST endpoint which
1.) should execute some QueryBuilder queries based on the parameters passed to the endpoint,

2.) return the Sling models for the resources found by the QueryBuilder.

Point 1. is simple.
I have some concerns around point 2., where the hits returned by the QueryBuilder are JcrNodeResources and I would like to get Sling models for those resources (e.g. for a cq:Page with type "core/wcm/components/page/v3/page"). I have the idea to use the org.apache.sling.models.factory.ModelFactory -> getModelFromWrappedRequest method for this purpose like 
Page page = modelFactory.getModelFromWrappedRequest(request, createSyntheticResource(resource), Page.class);.
The method createSyntheticResource should create a ValueMapResource with the arguments for the Page to be created.

Does this make sense or is there an easier way to call Sling model exporter for a given JcrNodeResource?

Thanks,
Peter

Best answer by joerghoh

Hi @pnagy 

You can't adapt resource to Page. You need to adpat to PageManager.

PageManager pageManager = resourceResolver.adaptTo(PageManager.class); String currentPage = Optional.ofNullable(pageManager).map(pm -> pm.getContainingPage(currentResource));

 

https://developer.adobe.com/experience-manager/reference-materials/6-4/javadoc/com/day/cq/wcm/api/PageManager.html#getContainingPage-org.apache.sling.api.resource.Resource- 


It's possible to adapt a Resource directly to a com.day.cq.api.Page object.

3 replies

joerghoh
Adobe Employee
Adobe Employee
January 17, 2025

Hi Peter,

 

do you know the exact model type, or has to this to be based on the resource type of the resource you are currently looking at?

pnagyAuthor
Level 3
January 18, 2025

Hi Jorg,

I know that the model type is core/wcm/components/page/v3/page based on the JCR content and its Sling model exporter is Adobe's internal PageImpl (but that internal class can't be referenced by the app code) so using Page.class, which is the model interface.

joerghoh
Adobe Employee
Adobe Employee
January 19, 2025

Even if the Page model is adaptable only for SlingHttpServletRequest like

@Model(adaptables = SlingHttpServletRequest.class

 ?


 

You can adapt a resource to many things; and it's possible to adapt it both a Core Components SlingModel, but  also to a com.day.cq.api.Page object (you get a PageImpl object).

SreenivasBr
Level 4
January 17, 2025

If you want to get the page object from the resource object, you should simply use adaptTo, provided the resource is a cq:Page.

Page page = resource.adaptTo(Page.class)

 

Refer - https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/developing/platform/sling-adapters

pnagyAuthor
Level 3
January 18, 2025

I tried that, but it does not work, it returns null:

Anudeep_Garnepudi
Community Advisor
Community Advisor
January 22, 2025

@pnagy  If you have resource handy then just do resource.adaptTo(YourSlingModel.class)

pnagyAuthor
Level 3
January 22, 2025

That's still null. The only way I see is using modelFactory.getModelFromWrappedRequest.
That needs some tweaks though.