Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
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