Skip to main content
Level 3
Jan 17, 2025 5:11 PM UTC
Solved

Getting Sling Model for a page by JcrNodeResource

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

joerghoh
joerghohAccepted solution
Adobe Employee
Jan 20, 2025 9:29 AM UTC

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.

15 replies

joerghoh
Adobe Employee
Adobe Employee
Jan 17, 2025 5:35 PM UTC

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?

Translate
pnagyAuthor
Level 3
Jan 18, 2025 12:56 PM UTC

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.

Translate
joerghoh
Adobe Employee
Adobe Employee
Jan 18, 2025 1:16 PM UTC

 

resource.adaptTo(com.day.cq.api.Page.class);

 

should return a page object if that resource actually describes a page (and according to the screenshots, it does).

Translate
SreenivasBr
Level 4
Jan 17, 2025 8:34 PM UTC

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

Translate
pnagyAuthor
Level 3
Jan 18, 2025 1:02 PM UTC

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

Translate
Anudeep_Garnepudi
Community Advisor
Community Advisor
Jan 22, 2025 3:02 AM UTC

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

Translate
AG
pnagyAuthor
Level 3
Jan 22, 2025 9:00 AM UTC

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

Translate