Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Can I get 2 levels of extensions in servlet path resolved in Apache Sling Resolver ?

Avatar

Level 1

Hi everyone, 

 

I have 2 levels of extensions on my servlet url path.
My servlet path is similar to " /product.extension1.extension2"


In the apache sling servlet resolver configuration, using my servlet path to get it resolved via execution paths field, I am getting forbidden error message when I try to access the servlet. I want  "localhost:4502/product.extension1.extension2" to work. 


Can someone suggest how to get the servlet path working for 2 level of extensions ?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@bimalpa3 

I see you are using extension in place of selector. Try as below, registering with two selectors.

@Component(service = Servlet.class, property = {"service.description=Product Servlet",
        "sling.servlet.methods=GET", "sling.servlet.paths=/product","sling.servlet.selectors=ext1", "sling.servlet.selectors=ext2"})

View solution in original post

4 Replies

Avatar

Community Advisor

The selectors must be configured as they would be specified in the URL that is as a list of dot-separated strings such as product.extension1. In case this is not empty, the first selector(s) (i.e. the one(s) on the left) in the request URL must match, otherwise, the servlet is not executed. 

You need to make sure your servlet is registered with path /product and extension extension1.

After that, in your Java code, you can look for extension2 in the request object to run logic in your servlet.

 

Avatar

Level 1

Hi Varun, 
I made my servlet registered with /product and selector extension1 in OSGi config Apache sling servlet resolver configuration. , defined
Also added "extension1.extension2" in index resources in config of org.apache.sling.servlets.get.DefaultGetServlet. 

My Java servlet class has the following servlet class definition with proper doGet method.  

@Component(service = Servlet.class, property = {"service.description=Product Servlet",
"sling.servlet.methods=GET", "sling.servlet.paths=/product","sling.servlet.selectors=ext1", "sling.servlet.extensions=ext2"})

If I change the config to ` "sling.servlet.paths=/product/ext1.ext2"`
, this will work, while my expected servlet path doesn't get created with above option. 

 

Avatar

Correct answer by
Community Advisor

@bimalpa3 

I see you are using extension in place of selector. Try as below, registering with two selectors.

@Component(service = Servlet.class, property = {"service.description=Product Servlet",
        "sling.servlet.methods=GET", "sling.servlet.paths=/product","sling.servlet.selectors=ext1", "sling.servlet.selectors=ext2"})

Avatar

Community Advisor

Hi @bimalpa3,

There are few things regarding registering servlet via path using sling.servlet.paths. According to Apache Sling Servlet documentation sling.servlet.selectors, sling.servlet.extensions, sling.servlet.methods are mainly supported when you are registering your servlet via resource type - sling.servlet.resourceTypes.

There is one exception for above rule, by adding sling.servlet.paths.strict property you can enable support of above props also for servlets registered via path.

So the configuration could look like this:

@Component(service = Servlet.class,
    property = {
        "sling.servlet.methods=GET",
        "sling.servlet.paths.strict=true",
        "sling.servlet.paths=/product",
        "sling.servlet.selectors=ext1",
        "sling.servlet.extensions=ext2"})

Second aspect, is that, if you would like to register servlet under path other than /bin, you will have to add new path under Execution Paths under Apache Sling Servlet/Script Resolver and Error Handler OSGi configuration.

Last but not least, you should consider to register your servlet using resource types instead of path. Using path is not recommended way.

For more details (including best practices), please check below documentation: