Expand my Community achievements bar.

SOLVED

AEM Servlet by SuperSlingResourceType

Avatar

Level 7

is it possible to register a servlet with SuperSlingResourceType? How?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @AEMWizard,

Yes this is possible to register servlet for sling resource super type. There is dedicated property that have to be used sling.servlet.resourceSuperType. Please be aware that it requires sling.servlet.resourceTypes property as well.

More details you can find under Apache Sling documentation:

Here are also few code examples:

  1. Using SlingServletResourceTypes annotation:
    package com.mysite.core.servlets;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
    import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import java.io.IOException;
    
    @Component(service = { Servlet.class })
    @SlingServletResourceTypes(
            resourceTypes = "base/resource/type",
            resourceSuperType = "super/resource/type",
            methods = HttpConstants.METHOD_GET,
            extensions = "html",
            selectors = "customSelector"
    )
    public class ResourceBaseServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            // place for your code
        }
    }​
  2. Using OSGi DS 1.2 annotations:
    package com.mysite.core.servlets;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.api.servlets.ServletResolverConstants;
    import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import java.io.IOException;
    
    @Component(service = Servlet.class, property = {
            ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
            ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=base/resource/type",
            ServletResolverConstants.SLING_SERVLET_RESOURCE_SUPER_TYPE + "=super/resource/type",
            ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html",
            ServletResolverConstants.SLING_SERVLET_SELECTORS + "=customSelector"})
    public class ResourceBaseServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            // place for your code
        }
    }​

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @AEMWizard,

Yes this is possible to register servlet for sling resource super type. There is dedicated property that have to be used sling.servlet.resourceSuperType. Please be aware that it requires sling.servlet.resourceTypes property as well.

More details you can find under Apache Sling documentation:

Here are also few code examples:

  1. Using SlingServletResourceTypes annotation:
    package com.mysite.core.servlets;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
    import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import java.io.IOException;
    
    @Component(service = { Servlet.class })
    @SlingServletResourceTypes(
            resourceTypes = "base/resource/type",
            resourceSuperType = "super/resource/type",
            methods = HttpConstants.METHOD_GET,
            extensions = "html",
            selectors = "customSelector"
    )
    public class ResourceBaseServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            // place for your code
        }
    }​
  2. Using OSGi DS 1.2 annotations:
    package com.mysite.core.servlets;
    
    import org.apache.sling.api.SlingHttpServletRequest;
    import org.apache.sling.api.SlingHttpServletResponse;
    import org.apache.sling.api.servlets.HttpConstants;
    import org.apache.sling.api.servlets.ServletResolverConstants;
    import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
    import org.osgi.service.component.annotations.Component;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletException;
    import java.io.IOException;
    
    @Component(service = Servlet.class, property = {
            ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
            ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=base/resource/type",
            ServletResolverConstants.SLING_SERVLET_RESOURCE_SUPER_TYPE + "=super/resource/type",
            ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html",
            ServletResolverConstants.SLING_SERVLET_SELECTORS + "=customSelector"})
    public class ResourceBaseServlet extends SlingSafeMethodsServlet {
    
        @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
                throws ServletException, IOException {
            // place for your code
        }
    }​