April 13, 2020
Estimated Post Reading Time ~

CQ Tips and Tricks #1 – How to define a SlingServlet

There are many ways to define an OSGI service.
This is the preferred coding style for CQ version 5.5-5.6 that we follow on our team. Registering the servlet by path

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
paths={"/services/unicom/v1/"}
)
@Properties({
@Property(name="service.pid", value="com.adobe.unicom.v1.servlets.OmnnitureLoggingServlet",propertyPrivate=false),
@Property(name="service.description",value="Omniture service call logging servlet", propertyPrivate=false),
@Property(name="service.vendor",value="Adobe Systems Incorporated - Adobe@Adobe Team", propertyPrivate=false)
})
public class OmnnitureLoggingServlet extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}
}

OR registering the servlet by resource type and extension

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
resourceTypes = {"rep:User"},
methods = {"GET", "POST"}
)
@Properties({
@Property(name="service.pid", value="com.adobe.unicom.v1.servlets.OmnnitureLoggingServlet",propertyPrivate=false),
@Property(name="service.description",value="Omniture service call logging servlet", propertyPrivate=false),
@Property(name="service.vendor",value="Adobe Systems Incorporated - Adobe@Adobe Team", propertyPrivate=false)
})
public class OmnnitureLoggingServlet extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
//Do something fun here
}
}

To handle all requests and filter on selector you can change the example above to read something like this.

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
resourceTypes = {"sling/servlet/default"},
methods = {"GET"},
selectors = {"report"},
extensions = {"json"}
)
@Properties({
@Property(name="service.pid", value="com.adobe.unicom.v1.servlets.OmnnitureLoggingServlet",propertyPrivate=false),
@Property(name="service.description",value="Omniture service call logging servlet", propertyPrivate=false),
@Property(name="service.vendor",value="Adobe Systems Incorporated - Adobe@Adobe Team", propertyPrivate=false)
})
public class OmnnitureLoggingServlet extends SlingAllMethodsServlet
{
........
To handle all requests for a Page with a special selector.
IE http://www.fake.com/content/page.mycustomselector.html

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
resourceTypes = {"cq:Page"},
methods = {"GET"},
selectors = {"mycustomselector"}
)
@Properties({
@Property(name="service.pid", value="com.adobe.unicom.v1.servlets.OmnnitureLoggingServlet",propertyPrivate=false),
@Property(name="service.description",value="Omniture service call logging servlet", propertyPrivate=false),
@Property(name="service.vendor",value="Adobe Systems Incorporated - Adobe@Adobe Team", propertyPrivate=false)
})
public class OmnnitureLoggingServlet extends SlingAllMethodsServlet
{
........

To handle all requests for a Page with a special extension.
IE http://www.fake.com/content/page.myfakeext

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
resourceTypes = {"cq:Page"},
methods = {"GET"},
extensions = {"myfakeext"}
)
@Properties({
@Property(name="service.pid", value="com.adobe.unicom.v1.servlets.OmnnitureLoggingServlet",propertyPrivate=false),
@Property(name="service.description",value="Omniture service call logging servlet", propertyPrivate=false),
@Property(name="service.vendor",value="Adobe Systems Incorporated - Adobe@Adobe Team", propertyPrivate=false)
})
public class OmnnitureLoggingServlet extends SlingAllMethodsServlet
{
........

For more details on the Felix, Annotations see


By aem4beginner

No comments:

Post a Comment

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