Expand my Community achievements bar.

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

Call servlet post request inside ResourceChangeListener in aem

Avatar

Level 2

I wrote Resource change Listener and it will call if i save my dialog with any modification, Now i want to call a servlet (POST request ) inside the listener and the servlet call should be success ,And any example would be helpful.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@YuganSan 

 

Requesting you to please go through the video that I had shared earlier. There would be a lot of blogs also available.

 

This code isn't right

resourceResolverFactory.getServiceResourceResolver(null)) {

 

As service use mapping needs to exist against the bundle/Service with proper privileges.


Aanchal Sikka

View solution in original post

9 Replies

Avatar

Community Advisor

@YuganSan 

 

  • Ideally one should create a Service and use it in the Listener.
    • Extract the required code in a common abstract class, which can be used by both Servlet and Service.
  • Do not execute heavy operations in listeners. Use Sling jobs instead 

Aanchal Sikka

Avatar

Level 3

Hi @YuganSan 
It's indeed like @aanchal-sikka is stating, the idea should be that your Servlet is using an underlying service which includes the logic that needs to be executed when calling your Servlet. If that is the case you could reuse the same service from your servlet into your ResourceChangeListener.

Greetings

Rik

Avatar

Level 2

How we can call our service inside onchange method

myservlet.doSomething() (dosomething method i wrote inside the servelt as we cant able to call dopost method)

Avatar

Community Advisor

@YuganSan 

 

Please create a separate service and declare the doSomething() in the Service. 

 

The following video should help you with better understanding of Services.

https://www.youtube.com/watch?v=cPylQu6I7Q0&ab_channel=SankhamMarTechChannel

 

To consume the service use @Reference annotation. That is also described in video around 9:17. They have used service in servlet. We need to use the same concept and use Service inside ResourceChangeListener  ( no intermediate servlet involved)

 

 


Aanchal Sikka

Avatar

Level 2

Hi 

Thank u for the video, its very useful, but now i want to get resource inside Service o Resoucechange listener, I tried to use @reference annotation for resourcereoslver and resourceresolverfactory, both return null for the resource , eventhough i am giving the correct path 

 

Avatar

Community Advisor

@YuganSan 

 

You would need to user service-user to access resources. The Service itself doesn't have a session. 

https://www.youtube.com/watch?v=ZaV-JvgbFTc


Aanchal Sikka

Avatar

Level 2

I am trying to access resource like below with resource reoslver
try
(ResourceResolver serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {

Resource resource = serviceResourceResolver.resolve(path);

}

i try to access resource.getPath() , getting the correct path, with the tag as

"NonExistingResource"

What will be the reason

Avatar

Correct answer by
Community Advisor

@YuganSan 

 

Requesting you to please go through the video that I had shared earlier. There would be a lot of blogs also available.

 

This code isn't right

resourceResolverFactory.getServiceResourceResolver(null)) {

 

As service use mapping needs to exist against the bundle/Service with proper privileges.


Aanchal Sikka

Avatar

Community Advisor

@YuganSan Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Esteban Bustamante