Monday, July 4, 2022

Sling Servlets in AEM

 

What are Servlets?

 

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

 

A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet technology defines HTTP-specific servlet classes.

 

All servlets must implement the Servlet interface, which defines life-cycle methods.
When implementing a generic service, we can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

 

Servlet Registration

 

For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either one or both of the sling.servlet.paths or the sling.servlet.resourceTypes service reference properties must be set. If neither is set, the Servlet service is ignored. 

A Sling servlet can be registered in two ways - 

1.    Using Resource Types - Using this way, we use the sling:resourceType property of the node. For this, we need to hit the path in the browser for which the sling:resourceType is the given one.

2.    Using Paths - Using this way, we can directly use the path specified in the request and our servlet will be executed.

 

Types of Servlets

There are two types of servlets in Sling which are nothing but the classes we need to extend while creating our servlet.

  • SlingSafeMethodsServlet - If we want to use only the read-only methods then we use this. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. 
    Supported Methods:
  •     GET
  •     HEAD
  •     Options
  •     etc

  • SlingAllMethodsServlet - If we want to use methods that write as well, then we use this. This class extends the SlingSafeMethodsServlet by support for the below methods.
Supported Methods:

  • POST
  • PUT
  • DELETE

 For Annotations refer here.

Sling Servlets

-Using Resource Path
import lombok.extern.slf4j.Slf4j;
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.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

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

@Slf4j
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Test Service",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/testget"})
public class SlingServletResourceType extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws ServletException, IOException {
String responseString = "response json";
response.getWriter().println(responseString);
}
}
Deploy code on AEM

How to call?

http://<host>:<port>/bin/testget

host : localhost

port: 4502 for author

-Using Resource Type-

import com.day.cq.commons.jcr.JcrConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

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

@Slf4j
@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Test Service",
"sling.servlet.resourceTypes="+ "myproject/components/structure/page",
"sling.servlet.extensions=" + "txt"})
class SlingServletResourceTypeExample extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws IOException {
final Resource resource = request.getResource();
response.setContentType("text/plain");
response.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE));
}
}


Happy Coding!

No comments:

Post a Comment

ElasticSearch vs Apache SolrCloud

  Elasticsearch and Apache SolrCloud are both powerful, distributed search engines built on top of Apache Lucene. Both Elasticsearch and S...