Oliver
Oliver

Reputation: 6250

Sling servlet bound to resource Type OSGi R6 annotations does not work

I want to register a servlet to my main base page resource type, such that when the same page is hit with a selector and extension my servlet should be chosen instead of normal page rendering.

I am able to achieve this behaviour easily using the old SCR annotations. See working code below.

@Component(metatype = false)
@Service({ javax.servlet.Servlet.class,
        org.apache.sling.api.servlets.SlingSafeMethodsServlet.class })
@Properties({
        @Property(name = "sling.servlet.resourceTypes", value = "project/project-foundation/components/structure/page"),
        @Property(name = "sling.servlet.selectors", value = {"info"}),
        @Property(name = "sling.servlet.extensions", value = "js"),
        @Property(name = "sling.servlet.methods", value = "GET"),
        @Property(name = "service.description", value = "Products Servlet") })

Now I want to do this using OSGi R6 annotations, below is the annotations I am using

@Component(service = Servlet.class, property = { 
        "sling.servlet.selectors=info",
        "sling.servlet.extensions=js",
        "sling.servlet.methods="+ HttpConstants.METHOD_GET,
        "sling.servlet.resourceTypes=project/project-foundation/components/structure/page"
})

When I hit the page /en-us/fun-games.info.js, my servlet should get called which for now is printing hello world string.(If called)

This servlet is not being picked by when I hit the above URL with selector = info and extension = js when I am using OSGi R6 annotations.

BUT this works with old SCR annotations. I've checked my servlet is being registered properly.

Not sure what is going on.

NOTE: I am using AEM 6.5

I referred this Link for OSGi R6

Upvotes: 0

Views: 1648

Answers (2)

gauti
gauti

Reputation: 11

Try hitting the servlet while giving JCR content in the path like:

en-us/fun-games/_jcr_content.info.js

I faced the same issue but when I gave the JCR content in the path, it worked.

Upvotes: 0

Oliver
Oliver

Reputation: 6250

My God, this was tougher than I thought.

I resolved this using OSGi R7 annotation. I had no idea they existed and AEM supports it.

@Component(immediate = true,service = { Servlet.class })
@SlingServletResourceTypes(
    resourceTypes="project/project-foundation/components/structure/page", 
    methods= "GET",
    selectors={ "info", "js"})

Also another thing to note is that I used "js" as a selector not extension.

I invoked the page this way /en-us/fun-games/mario.info.js

The same servlet can be called on full or shortened URL it does not matter.

And there is one dependency:

           <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.servlets.annotations</artifactId>
            <version>1.2.4</version>
          </dependency>

The official documentation I referred to was, OSGi R7 Anoted Servlets

Upvotes: 2

Related Questions