scribe1010
scribe1010

Reputation: 213

CQ5.3 Servlet Unavailable ( and Servlet Resolver Paths Configuration (5.3 v 5.6))

Historically I've only ever used CQ 5.6, but am now working on a project using 5.3 and am failing to get a servlet registered and accessible. When I try to hit the servlet (with and without and extension) I get the following INFO line in the error log:

GET /bin/TestServlet.json HTTP/1.1] org.apache.sling.engine.impl.SlingMainServlet service: Resource /bin/TestServlet.json not found

I can build the bundle containing the servlet, deploy it using the maven-sling-plugin, and the bundle ends up in the console as 'active', but the servlet isn't available. I'm using the following in my class file to define the servlet:

@SlingServlet(paths = {"/bin/TestServlet"}, methods = {"GET"}, extensions = {"json"}, metatype = true)
@Properties({
  @Property(name = "service.name", value = "com.something.servlets.TestServlet", propertyPrivate = true),
  @Property(name = "service.description", value= "Test servlet I am trying to geet working", propertyPrivate = true),
  @Property(name = "service.vendor", value = "something", propertyPrivate = true)
})

Please note that I have also tried that alternative using the @Component(.......) and @Service(value=Servlet.class) annotations

Looking to see if the problem is configuration based (thinking /bin was blocked) I went back to 5.6.1 to compare configuration and noticed that the config for Apache Sling Servlet/Script Resolver and Error Handler differs between 5.3 and 5.6.

In 5.6 you can configure the servletresolver.paths which (if I understand things correctly) opens up paths for execution, but this is not present in 5.3

So:

  1. Does the configuration for servletresolver.paths exist in 5.3?
  2. Can I even register a servlet like this in 5.3?
  3. If the answer to 2 is 'no', can you point me at an example/tutorial on how to do this in 5.3 (Docs and tutorials are thin on the ground for this version)?

Thanks in advance

EDIT (resolution): It seems to be down to versions in the POM (and possibly plugin config). May only have been one culprit, but I simply don't have time to try all permutations. If I get time to try I'll add an edit.

Successful versions: maven-bundle-plugin - v2.4.0 maven-scr-plugin - v1.13.0 maven-sling-plugin - v2.1.0

Upvotes: 3

Views: 1764

Answers (1)

Victar Kadol
Victar Kadol

Reputation: 141

in CQ5.3 you can use 'JCR resolver' to check servlet mapping. Go To

http://localhost:4502/system/console/jcrresolver

And enter path to your servlet in 'Configuration Test'. e.g.

http://localhost:4502/bin/TestServlet

resolve result should mapping to your servlet for example

ServletResource, servlet=com.test.impl.HelloJSONServlet, path=/bin/TestServlet

Also there is code snippet which works in CQ5.3

@Component(immediate = true, metatype = false, label = "Hello JSON Servlet")
@Service(serviceFactory = false, value = javax.servlet.Servlet.class)
@Properties(value = {
        @org.apache.felix.scr.annotations.Property(name = "sling.servlet.methods", value = { "GET" }),
        @org.apache.felix.scr.annotations.Property(name = "sling.servlet.extensions", value = { "json" }),
        @org.apache.felix.scr.annotations.Property(name = "sling.servlet.paths", value = {"/bin/TestServlet"})

})
public class HelloJSONServlet extends SlingAllMethodsServlet {

    protected void doGet(SlingHttpServletRequest request,
            SlingHttpServletResponse response) throws ServletException,
            IOException {
        String username = request.getParameter("username");
        response.setContentType("application/json");
        response.getWriter().write("{ \"hello\" : \""+username+"\" }");
    }
}

Upvotes: 2

Related Questions