Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to read Sling model values into Servlet class

Avatar

Level 4

Hello All,

 

I am trying to read Sling model values into servlet class. My sling model is having:

 

@ValueMapValue

@getter 

 public String recaptchaSecretKey;

 

and I am reading it in servlet as below:

ReadForm model = request.adaptTo(ReadForm.class);
String keys = model.recaptchaSecretKey;

 

But this is getting null. Is there any other way to achieve this.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @supriya-hande 

 

option-1: You should take the reCaptcha out of this component into a separate OSGi service.

Anything reusable should not be part of a component.

 

option-2: You can read configs of different service via Configuration Admin

https://techrevel.blog/2020/09/20/configurationadmin-access-osgi-configuration-of-other-services/ 

So, servlet can read the config from Model ( if its configurable from OSGI)

 

Also, I might be wrong, but I guess a Sling HTTP request cannot be adapted to a Model.

Lets wait if someone can guide or confirm


Aanchal Sikka

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @supriya-hande 
Could you please share what is the use case? and what are the Model and Servlet classes.

 

How does the flow works?



Arun Patidar

Avatar

Level 4

hi @arunpatidar 

There is a task to integrate google reCaptcha module in AEM component. I have created a custom HTML form component and authoring reCaptcha secret and site key in this component. On submit action it invokes reCaptcha execute method and gives token. 

Then I have written a servlet to verify the token and passing the secret key to get response from google. Here I need both token and secret key in the servlet. I am able to get token in this servlet as this is query parameter but am unable to get secret key value from model class. I have implemented this feature by referring: https://aeminmyway.blogspot.com/p/google-recaptcha-v3-in-aem.html

Avatar

Correct answer by
Community Advisor

Hello @supriya-hande 

 

option-1: You should take the reCaptcha out of this component into a separate OSGi service.

Anything reusable should not be part of a component.

 

option-2: You can read configs of different service via Configuration Admin

https://techrevel.blog/2020/09/20/configurationadmin-access-osgi-configuration-of-other-services/ 

So, servlet can read the config from Model ( if its configurable from OSGI)

 

Also, I might be wrong, but I guess a Sling HTTP request cannot be adapted to a Model.

Lets wait if someone can guide or confirm


Aanchal Sikka

Avatar

Level 4

@aanchal-sikka I think 1st option we should use in such cases. I created separate OSGI service to read reCaptcha secret keys and am able to get success response from google server.
Thanks for your help  

@arunpatidar I am able to resolve this. Thanks