Can we pass dynamic values to register a Sling Servlet? | Community
Skip to main content
Level 2
July 1, 2018
Question

Can we pass dynamic values to register a Sling Servlet?

  • July 1, 2018
  • 6 replies
  • 5166 views

Hi,

I would like to know if there is a possibility to somehow pass dynamic values to the properties sling.servlet.resourceTypes, sling.servlet.selectors, sling.servlet.extensions while registering a servlet.
Is there a way to use custom OSGi configuration nodes in this case?

A sample use case :

I have a servlet registered using resourceType= /something/xyz, selectors = [json, tab, img, pdf], extension = html. In case, i would like to add more selectors to the same servlet in future based on my extending requirement, how can i do it without a code change?

Thanks in advance!

Regards,

Bhavya S

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

6 replies

Kunal_Gaba_
July 2, 2018

You can register your servlet to a more generic resource type and then use OptingServlet interface to accept requests which meets your dynamic qualification criteria.

For more details see this documentation - OptingServlet (Apache Sling 7 API)  - Your logic can be defined in accepts method.

Example - acs-aem-samples/SampleAllMethodsServlet.java at master · Adobe-Consulting-Services/acs-aem-samples · GitHub

joerghoh
Adobe Employee
Adobe Employee
July 2, 2018

If you don't use the @SlingServlet annotation, but rather the standard way using SCR/OSGI properties, these special properties are available via OSGI webconsole (unless you configure them with privateProperties=true).  So you should be able to do that by default :-)

Regards,

Jörg

S_BhavyaAuthor
Level 2
July 2, 2018

Thank you Jörg!

Could you please post a code snippet or an example.
It would be really helpful to understand better

joerghoh
Adobe Employee
Adobe Employee
July 2, 2018

From the top of my head (haven't found a good example in the net that fast):

@Component
@Service
@Properties{(
   @Property(name="sling.servlet.selectors" value={"pdf","txt"})
   @Property(name="sling.servlet.resourceTypes" value="my/super/resource/type")
)}

Something along that line should work.

Adobe Employee
July 3, 2018

I think we need to set metatype=true also to change these properties from /system/console/configMgr UI.

@Component(metatype=true)

@Service

@Properties{(

   @Property(name="sling.servlet.selectors" value={"pdf","txt"})

   @Property(name="sling.servlet.resourceTypes" value="my/super/resource/type")

)}

joerghoh
Adobe Employee
Adobe Employee
July 3, 2018

Yepp, forgot the metatype property. Although technically it's not really necessary, it just makes the webconsole displaying the configuration as editable.

regards,
Jörg