Retrieving the 'SlingHttpServletRequest request' from a parent node. | Community
Skip to main content
kennyhank
Level 3
September 5, 2017
Solved

Retrieving the 'SlingHttpServletRequest request' from a parent node.

  • September 5, 2017
  • 21 replies
  • 10212 views

Hi Community!

For some reasons I need to retrieve the 'SlingHttpServletRequest request' from a parent node and I'm not be able to do it, I have already the request from the actual node but I'm passing on HTL an wcmmode='disabled' fixed value, this is why I'm going to need the parent request, sample code here:

          Resource resource = request.getResource();       

          Resource parent = resource.getParent();

          SlingHttpServletRequest requestParent = parent.adaptTo(SlingHttpServletRequest.class);

This is how I'm trying on the constructor on my class, but always I retrieve null as requestParent value.

I'll appreciate any suggestions on this, thanks in advance.

AEM6.2

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 VeenaVikraman

Hi Kenny

     I am afraid , I am still not clear about your use case. Could you please try to explain your use case or requirement around the component little more clearly

   To answer your this question, (even though i didn't understand what your are trying to do here ) , you can always get a SlingHttpServletRequest object in your model using the code snippet I have mentioned in the below answer Re: Sightly question: how to adapt a class with a path

@Model(adaptables = SlingHttpServletRequest.class)

    Once you have the request you can always adapt a resource and play around

BUT, What i see in your code is you already have a request object ,

Resource resource = request.getResource();   

     Could you tell me what exactly are you trying to implement by fetching another request object (which doesn't make sense since you already have one . ) , when you can do almost everything using the Resource API .

PS:- All my above answers are direct to your question. Still I am not sure about your requirement . And as smacdonald2008 said, I don't think this is a correct approach.

21 replies

edubey
Level 10
September 5, 2017

Request object will have the resource which has been requested and you will get that in

Resource resource = request.getResource();   

I don't think what you are trying to do will work.

What is the use-case, there could be better sol available

Thanks

Level 3
September 5, 2017

Sad to say that, but Resource class has no adapter to SlingHttpServletRequest class. This is a problem with adaptTo method is not reversable so aInstance.adaptTo(B.class).adaptTo(A.class) don't have to work and those transitions are described with adapters you can find in OSGi console.

So, for example, if you are using Sling Models you can simply set class to be adaptable from request, and after that inject main resource using @Via(Resource.class) @Self annotations.

This discussion about properties of adaptTo method might be quite longer but I think it deep architectural problem/issue.

Hope that helps, and absolutely out of curiosity why do you need a different request for parent resource?

kennyhank
kennyhankAuthor
Level 3
September 6, 2017

Hi all thanks for your thoughts, I agree with last comment maybe my problem is on the architecture. I have a component that I passed its html to another different component (B), but my B component is composed for text & image components, and I passed the param wcmmode=disabled because I don't want to make editable those components as separate.

Part of the htl code:

<sly data-sly-resource="${'image' @ resourceType='xxx/components/content/image', wcmmode='disabled'}" ></sly>

<sly data-sly-resource="${'image' @ resourceType='xxx/components/content/text', wcmmode='disabled'}" ></sly>

With this I make my entire component "text&image" editable as one, and not 2 separate components. But the problem is with another component that I included its html on the image component. On this case I need validate how I'm going to formed the html of that third component depends on wcmmode value, but always I receive 'disabled'.

For that reason I try to find different ways to solve this, thinking if I call for parent request would solve my problem. Sorry for my lack of knowledge.

smacdonald2008
Level 10
September 6, 2017

What use case are you trying to build - why are you passing HTML of component A to component B for example. I always found building an HTL component that uses values (from Sling Models or WCMUsePOJO) and its own HTML works much  better.

kennyhank
kennyhankAuthor
Level 3
September 6, 2017

I have a component for social share, this component doesn't work by itself, but I include it to other components like Image and Videoplayer for instance because is similar for both, I reuse its dialog by including on the last tab of the dialogs of the other components and forming its own html depending of the options that author used.

VeenaVikraman
Community Advisor
VeenaVikramanCommunity AdvisorAccepted solution
Community Advisor
September 6, 2017

Hi Kenny

     I am afraid , I am still not clear about your use case. Could you please try to explain your use case or requirement around the component little more clearly

   To answer your this question, (even though i didn't understand what your are trying to do here ) , you can always get a SlingHttpServletRequest object in your model using the code snippet I have mentioned in the below answer Re: Sightly question: how to adapt a class with a path

@Model(adaptables = SlingHttpServletRequest.class)

    Once you have the request you can always adapt a resource and play around

BUT, What i see in your code is you already have a request object ,

Resource resource = request.getResource();   

     Could you tell me what exactly are you trying to implement by fetching another request object (which doesn't make sense since you already have one . ) , when you can do almost everything using the Resource API .

PS:- All my above answers are direct to your question. Still I am not sure about your requirement . And as smacdonald2008 said, I don't think this is a correct approach.

smacdonald2008
Level 10
September 6, 2017

"this component doesn't work by itself," - always best to design and develop an AEM component to work on its own.

kennyhank
kennyhankAuthor
Level 3
September 6, 2017

You are right, but on this particular case I prefer create an small component with its html, dialog, and Java backend, and I can include my component as part of any other component because is a good way to reuse my code, for instance:

VeenaVikraman
Community Advisor
Community Advisor
September 6, 2017

I appreciate that. I always recommend everyone to code in a such a way to have maximum re-usability but less complexity. I am a big fan of re-usability, but when we design , we have to make sure that doesn't make the life of authors hard.

May be instead of reusing the entire component you can reuse the dialog alone too. But it all at the end depends on what exactly you want to achieve. We cannot impose re-usability just because two components have same fields. We have to make sure reusing a component won't make the component hard to author and complex to author or code.

I would like some expert advice here ..... smacdonald2008Ratna Kumarkautuksahni​ @jorg

kennyhank
kennyhankAuthor
Level 3
September 6, 2017

I used the annotations to my class as well:

@Model(adaptables = SlingHttpServletRequest.class)

public class AssetSharing {

    //ATTRIBUTES - GENERAL

    //ATTRIBUTES - DIALOG PROPERTIES

    

    public AssetSharing(SlingHttpServletRequest request) {

          .... //code here

          this.wcmmode = (WCMMode.fromRequest(request) != WCMMode.DISABLED) && (WCMMode.fromRequest(request) != WCMMode.PREVIEW);

          .... // more code here

     }

that is why I got already the request, but how I said before when I try to fetch the wcmmode, I always receive DISABLED as value even if I'm on author mode, because I'm passing this fixed value to my mixed component as I explain on one of my comments above.

Maybe the problem here was think that this would be solved using a different request.

Thanks for all the comments on this.