resource.adaptTo(Session.class) returning null | Community
Skip to main content
Level 4
July 3, 2023
Solved

resource.adaptTo(Session.class) returning null

  • July 3, 2023
  • 1 reply
  • 879 views

I'm trying to handle multiple user login sessions by creating user specific sessions. Below is the code snippet of the same.

ResourceResolver resourceResolver = request.getResourceResolver(); Resource userResource = resourceResolver.getResource(config.getUserDataFolderPath() + '/' + userProfile.getUid()); LOGGER.info("User specific resource in resource format is {}", userResource); assert userResource != null; Session userSpecificSession = userResource.adaptTo(Session.class); LOGGER.info("User specific session is {}", userSpecificSession);

The below line is returning null. Can someone please help me to understand why is this happening?

Session userSpecificSession = userResource.adaptTo(Session.class);

 

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 aanchal-sikka

@goyalkritika 

 

For an object to be adapted as an object of another class, the class types should be compatible.

 

Resource can't be adapted to Session, but a ResourceResolver can be

 

Each object is an entity in itself. When we adapt, we are just trying to access that object in a different way. Thats why classes need to be compatible

1 reply

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
July 3, 2023

@goyalkritika 

 

For an object to be adapted as an object of another class, the class types should be compatible.

 

Resource can't be adapted to Session, but a ResourceResolver can be

 

Each object is an entity in itself. When we adapt, we are just trying to access that object in a different way. Thats why classes need to be compatible

Aanchal Sikka
Level 4
July 3, 2023

Thank you.