Call AEM Servlet based on asset path | Community
Skip to main content
New Participant
July 20, 2023
Solved

Call AEM Servlet based on asset path

  • July 20, 2023
  • 2 replies
  • 2200 views

Hello All, 

Any thoughts on how can we call a servlet based on the asset path, say /content/dam/cms/assetpath. 

 

If I want to load a servlet if we hit any asset under this path.

 

 

 

Thanks,

Sateesh K.

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 EstebanBustamante

Technically you can register to any resource, but you cannot register a servlet by resourceType and specify the filter on it, instead, you could use a Sling servlet Filter, I would recommend exploring the Sling Filter first. Answering your question, the resource type to register an asset is "dam/Asset".

 

Another option could be to use a Workflow  + WF Launcher to make sure a workflow is executed only under that asset. 

Just for reference, this is how you will register the servlet for all the resources which are assets:

 

(service = { Servlet.class }) @SlingServletResourceTypes( resourceTypes="dam/Asset", methods=HttpConstants.METHOD_GET, extensions="txt") @ServiceDescription("Simple Demo Servlet") public class MyServlet extends SlingSafeMethodsServlet { .. .. ..

Then, you will invoke the servlet like this: 

http://localhost:4502/content/dam/myasset.jpg.txt

 

Hope it helps

 

2 replies

EstebanBustamante
EstebanBustamanteAccepted solution
New Participant
July 20, 2023

Technically you can register to any resource, but you cannot register a servlet by resourceType and specify the filter on it, instead, you could use a Sling servlet Filter, I would recommend exploring the Sling Filter first. Answering your question, the resource type to register an asset is "dam/Asset".

 

Another option could be to use a Workflow  + WF Launcher to make sure a workflow is executed only under that asset. 

Just for reference, this is how you will register the servlet for all the resources which are assets:

 

(service = { Servlet.class }) @SlingServletResourceTypes( resourceTypes="dam/Asset", methods=HttpConstants.METHOD_GET, extensions="txt") @ServiceDescription("Simple Demo Servlet") public class MyServlet extends SlingSafeMethodsServlet { .. .. ..

Then, you will invoke the servlet like this: 

http://localhost:4502/content/dam/myasset.jpg.txt

 

Hope it helps

 

Esteban Bustamante
AsifChowdhury
New Participant
July 20, 2023

Hi @sateeshkreddy 

You can try with resource type servlet with selectors.

An example you will find here: https://aemsimplifiedbynikhil.wordpress.com/2020/10/16/calling-post-servlet-using-resourcetypesselectors-and-extensions-in-aem/ 

Sling servlet: Path vs Resource Type: https://medium.com/@toimrank/aem-sling-servlet-ca9e5926a384 

New Participant
July 20, 2023

Thanks for you reply but I am looking for more precise solution like if the asset path is /content/cms/asset/******/*****.pdf. 

AsifChowdhury
New Participant
July 20, 2023

you can apply resource type with the extension ".pdf"