cq5 accessing osgi servlet through Url
Asked Answered
H

1

1

I am trying to access following sling servlet using http://localhost:4502/sling/test-services/planet.html

But, it is giving 404 error, not sure what I am doing wrong here.

@Component
@Service(value=javax.servlet.Servlet.class)
@Properties({
    @Property(name="service.description", value="HTML renderer for Planet resources"),
    @Property(name="service.vendor", value="The Apache Software Foundation"),
    @Property(name="sling.servlet.resourceTypes", value="sling/test-services/planet"),
    @Property(name="sling.servlet.extensions", value="html"),
    @Property(name="sling.servlet.methods", value="GET")
})
@SuppressWarnings("serial")
public class PlanetResourceRenderingServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        final ValueMap properties = request.getResource().adaptTo(ValueMap.class);

        // TODO should escape output - good enough for our tests 
        final PrintWriter pw = response.getWriter();
        pw.println(String.format("<html><head><title>Planet at %s</title></head><body>", request.getResource().getPath()));
        pw.println(String.format("<p>Name: %s</p>", properties.get("name")));
        pw.println(String.format("<p>Distance: %s</p>", properties.get("distance")));
        pw.println("</body></html>");
        pw.flush();
    }

}

Is it possible, I could access the servlet service without ".html" extension, if I remove extension property?

I appreciate any help.

Thank you!

Hectogram answered 29/1, 2016 at 4:5 Comment(0)
Q
5

When you want to access a servlet through an URL you need to set the sling.servlet.paths instead of the sling.servlet.resourceTypes. A similar issue has been answered here.

If you are setting the sling.servlet.resourceTypes property, then you need to access a resource whose sling:resourceType is sling/test-services/planet.

Your annotations should be

@Component
@Service(value=javax.servlet.Servlet.class)
@Properties({
    @Property(name="service.description", value="HTML renderer for Planet resources"),
    @Property(name="service.vendor", value="The Apache Software Foundation"),
    @Property(name="sling.servlet.paths", value="/sling/test-services/planet"),
    @Property(name="sling.servlet.extensions", value="html"),
    @Property(name="sling.servlet.methods", value="GET")
})

Or this can be further simplified using the @SlingServlet annotation as shown below

@SlingServlet(paths="/sling/test-services/planet", methods="GET", extensions="html")

Make sure that you allow the following path is allowed in Apache Sling Servlet/Script Resolver and Error Handler configuration available in OSGi console.

Quittance answered 29/1, 2016 at 5:21 Comment(3)
I will try with paths. I see lot of servlets wit path "/bin" , should I need to have the as "/bin/sling/test-services/planet" or "sling/test-services/planet" would work ?Hectogram
/bin is allowed in the Script Resolver configuration by default. Hence using /bin/sling/test-services/planet wouldn't require any change in the configuration. But if you use /sling/test-services/planet, you may need to allow /sling/ in the Script Resolver explicitly.Quittance
As usual, let me remind people that registering Sling servlets by path is not recommended if you can avoid it, see the "sling.servlets.path" section at sling.apache.org/documentation/the-sling-engine/servlets.htmlAluminize

© 2022 - 2024 — McMap. All rights reserved.