Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

resource.adaptTo(Session.class) returning null

Avatar

Level 5

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);

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@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