Friday, 29 April 2016

AEM Sling Servlet

To create an AEM Sling Servlet you need to make sure that you have either used the mvn archetype to have the correct pom.xml file or include a number of dependencies.

    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr</artifactId>
        <version>1.6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.annotations</artifactId>
        <version>1.9.6</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.jcr</groupId>
        <artifactId>jcr</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.sling</groupId>
        <artifactId>org.apache.sling.models.api</artifactId>
        <version>1.0.0</version>
        <scope>provided</scope>
    </dependency>

Create a simple servlet with the following code,


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

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.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

/**
 * Simple first servlet
 */
@SlingServlet(paths = "/bin/firstserlvet/test", methods = "GET")
public class MyFirstServlet extends SlingSafeMethodsServlet
{

    /**
     * The method which receives the GET request.
     */
    @Override
    protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException
    {
        final Resource resource = request.getResource();
        
        // Get the path that was requested
        response.getOutputStream().println(resource.toString());
        
        // Output something
        response.getOutputStream().println("Output from simple servlet");
    }
}

The path here is defined in the @SlingServlet annotation.  It starts with /bin/ which is one of the defined permitted paths configured in the SlingServletResolver.  Any of the defaults can be used or new permitted values can be added.  The current configuration can be seen by browsing to

    http://localhost:4502/system/console/configMgr/org.apache.sling.servlets.resolver.SlingServletResolver

Or go 

    http://localhost:4502/system/console
    OSGi - Configuration
    Apache Sling Servlet/Script Resolver and Error Handler (click on it)

You can see here a list of the default permitted paths.  This can be added to by just clicking the + on the right hand side.

No comments:

Post a comment