Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Retrieving the 'SlingHttpServletRequest request' from a parent node.

Avatar

Level 3

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

21 Replies

Avatar

Level 10

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

Avatar

Level 3

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?

Avatar

Level 3

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.

Avatar

Level 10

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.

Avatar

Level 3

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.

Avatar

Correct answer by
Community Advisor

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.

Avatar

Level 10

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

Avatar

Level 3

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:

assetsharing.JPG

Avatar

Community Advisor

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

Avatar

Level 3

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.

Avatar

Level 10

100% agree - lets see what other community experts say!

Avatar

Community Advisor

So here you are trying not to use the dialog capabilities of image and text (since you have disabled both the components , I assume you have the image and text authored in your 3rd component itself ). You just need the HTML of the image and text components respectively to be reused .

I will not recommend your approach as you just need the HTML part of these components and not the component features ; since you have disabled both the components which you have included. In HTL we have a feature called templates , which will perfectly suit your such requirement

Some pointers.

https://docs.adobe.com/docs/en/htl/docs/block-statements.html#template & call

Guide for working with Templates and Call in Sightly AEM 6.1 | Adobe AEM Club

May be a pretty googling will help you with more help guides on this topic. Let me know if you need more info around this

Avatar

Community Advisor

Hi Kenny,

I am agree with Veena_07​ .. Please reuse the dialog rather than entire component.

Regards,
Prince

Avatar

Level 3

I have a Text and Image components, and I create a new one wich is a mixed of Text&Image component, everyone has its own behaviour. On the other hand I have another component called assetsharing wich I include as part of any component with assets capable to share, for instance I include this component on the last tab of Image and Text&Image components's dialog.

I'll never used my assetsharing component as a separate component, I always use this component as part of other depending of the use of the authors, author can choose if they want this behaviour on theirs components or not.

Avatar

Community Advisor

          - I believe , you are reusing the assetshare component dialog to your Image or text component ? IS that correct ?

          If you are reusing the assetshare dialog then better to use the template feature of HTL . You can keep the assetshare HTML as a template in its .html file and try to use the same on each of your components

Your thoughts smacdonald2008Prince Shivharekautuksahni​ @Jorg Ratna Kumar

Avatar

Level 3

I have Text, Image and Text&Image components, I'm including assetshare on Image and Text&Image, not on Text, text component is just text, nothing to share there.

I always use templates on my HTL code when I have to call more than one .html file on the same component, here I'm including  components for that reason I'm using:

text&image.html

.........HTL Text&Image component code here

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

........more HTL Text&Image component code here

--------->

image.html

.........HTL Image component code here

<sly data-sly-resource="${'assetsharing' @ resourceType='xxx/components/content/assetsharing'}"></sly>

........more HTL Image component code here

---------->

assetsharing.html

..........HTL AssetSharing component code here

//wcmmode is ALWAYS DISABLED

Don't know how template features of HTL can solve this.

Avatar

Community Advisor

Question -

          --> What is the purpose of including an image component here if you are not going to author this? Where do you do the authoring ? How is this different from using a <template> ?

(I know I am rounding back to where we have started . I understand you might have some purpose for doing this way and I really wanna understand the same to make sure any solution which we provide is in the right direction . It should solve your problem rather than confuse you more.)

Avatar

Level 3

The reason is because on this mixed component (Text&Image) I have 3 scenarios:

1- Only text wich behaviour is the same as Text component.

2- Only image wich behaviour is the same as Image component.

3- Having both text and image on the same component.

So basically is more easily for author having these both components in only editable one component, and very useful, and the most important is a client requirement

Avatar

Level 10

As community has stated - it makes more sense to write a standalone HTL component that is independent of other components.

No need to read other nodes or parent nodes when developing a component like this. It complicates matters. 

See this recently added article from Prince Shivhare​ that shows how to develop a standalone text/image component. There is no need to read other nodes, parent nodes, etc. All logic and CSS is for that component only --

Creating a custom Image Text component for Experience Manager

Hope this helps...

Avatar

Level 10

THis is a really good community discussion however - i hope other community members provide their view!