March 28, 2020
Estimated Post Reading Time ~

Sling Servlets

In Sling, Servlets can be registered as OSGi services like below:

1. The @SlingServlet annotation

@SlingServlet(
resourceTypes = "sling/servlet/default",
selectors = "hello",
extensions = "html",
methods = "GET")
public class MyServlet extends SlingSafeMethodsServlet {
    //TO DO
}


2. The @Properties and @Property annotations

@Component(metatype = true)
@Service(Servlet.class)
@Properties({
@Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
@Property(name = "sling.servlet.selectors", value = "hello"),
@Property(name = "sling.servlet.extensions", value = "html"),
@Property(name = "sling.servlet.methods", value = "GET")
})
public class MyServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
    //TO DO
}
}

3. Registering the servlet by path

@SlingServlet(
paths={"/bin/customservlet/hashim"})
@Properties({
@Property(name="service.pid", value="com.day.servlets.SampleServlet",propertyPrivate=false),
@Property(name="service.description",value="SampleDescription", propertyPrivate=false),
@Property(name="service.vendor",value="SampleVendor", propertyPrivate=false)
})
public class SampleServlet extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException
{
    //TO DO
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException
{
    //TO DO
}
}

4. Register servlet by Resource Type

@SlingServlet(
resourceTypes = {"rep:User"},
methods = {"GET", "POST"}
)

@Properties({
@Property(name="service.pid", value="com.day.servlets.SampleServlet",propertyPrivate=false),
@Property(name="service.description",value="SampleDescription", propertyPrivate=false),
@Property(name="service.vendor",value="SampleVendor", propertyPrivate=false)
})
public class SampleServlet extends SlingAllMethodsServlet

{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException
{
    //TO DO
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException
{
    //TODO
}
}

Note: If you want your @SlingServlet to fetch some properties from Felix Console Configurations using @Properties, add the parameter “metatype=true” in the form of declaration where @SlingServlet is used.
This parameter is responsible for a Service component to be available in Felix Console ConfigMgr.
why resourceType is much more preferred for writing SlingServlets. Below are the reasons:

While defining a path, you must be specific about what all paths are allowed to be used in the ServletResource OSGi service. If you define something randomly, your servlet might not be functional. Only limited paths are allowed and the rest are blocked unless you open them up. This is resolved using resourceType.
You may have also configured the dispatcher if you use some random path for your servlet. This might be a potential security threat and a needless configuration.

You might also have to specify the paths to your consumers for your servlet and any change in that path could have a serious effect. This might not be the case when you use resourceType


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.