Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to get session inside slingmodel

dipu2
Level 3
Level 3

Dear All,

 

I have one requirement like below. When Author will put the SKU Id in dialog , as shown in below,

dipu2_0-1595957871572.png

 

After author will put the SKU Id (for example 12345) , then this ID will search in content/dam/site/skuid folder and the document where this sku id is attached will come as link .

 

I am trying to do this logic inside

@PostConstruct
private void initModel() throws Exception {

 

But , I am not getting any session "or" resource inside  initModel().

 

Can anybody tell me how can I achieve the above scenario ?

1 Accepted Solution
Nupur_Jain
Correct answer by
Community Advisor
Community Advisor

Hi @dipu2 

 

In order to get session,

  • you can first get ResourceResolver object with 
    @SlingObject
    private ResourceResolver resourceResolver;

  • Then in initModel() method, you can adapt it to session like
    Session session =resourceResolver.adaptTo(Session.class);

In order to get resource, you can use
@SlingObject
private Resource resource;

 

Hope it helps!

Thanks!

Nupur

View solution in original post

4 Replies
Nupur_Jain
Correct answer by
Community Advisor
Community Advisor

Hi @dipu2 

 

In order to get session,

  • you can first get ResourceResolver object with 
    @SlingObject
    private ResourceResolver resourceResolver;

  • Then in initModel() method, you can adapt it to session like
    Session session =resourceResolver.adaptTo(Session.class);

In order to get resource, you can use
@SlingObject
private Resource resource;

 

Hope it helps!

Thanks!

Nupur

View solution in original post

BrianKasingli
Community Advisor
Community Advisor

@dipu2,

It might be possible to get the session from the current session of the current request, but the session obtained may not have the correct permissions to the path: content/damsite/skuid. If the session have no permission to the content path, then you can get a content not found an exception. Also, Sling Models should be lightweight and should have minimal logic. Your requirement seems like there might be mid-weight logic that can be re-usable. This would be a great time to create an OSGIService & Service Users.

Implementation Idea:

I hope this helps,

Brian.

dipu2
Level 3
Level 3
Thanks Brian , this is useful..I will try the above steps tomorrow and let you know if I am stuck anywhere
Ankur_Khare
Community Advisor
Community Advisor

Did u try to use-

@inject

SlingHttpServletRequest request;

 

init() {

request.getSession();

}