Apache recommends not use the @SlingServletPaths annotation, Sling Servlet register by a path. Rather use the @SlingServletResourceTypes component type. Given the drawbacks in the caveats below, it is strongly recommended to bind servlets to resource types rather than paths.
Caveats when using @SlingServletPaths:
- Path-bound servlets cannot be access-controlled using the default JCR repository ACLs.
- Path-bound servlets can only be registered to a path and not a resource type (i.e. no suffix handling).
- If a path-bound servlet is not active, e.g. if the bundle is missing or not started, a POST might result in unexpected results. usually creating a node at /bin/xyz which subsequently overlays the servlets path binding.
- The mapping is not transparent to a developer looking just at the repository.
Also, have a look at the Apache Sling Servlet Documentation for the SlingServletPaths component property type, which what is mentioned above is clearly stated.
Example of @SlingServletResourceTypes:
The proper way to bind Sling Servlets is by binding the servlet to resource types. This is an example of a servlet binding to a resource type.
package com.sourcedcode.core.servlets.impl;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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 java.io.IOException;
import static org.apache.sling.api.servlets.HttpConstants.METHOD_GET;
/**
* Enables all resources to return formatted response data from the doGet() method.
* Appending ".example.json" on any resource will activate the doGet() method below.
*/
@Component(service = Servlet.class)
@SlingServletResourceTypes(
resourceTypes = "sling/servlet/default",
methods = "get",
extensions = "json",
selectors = "example")
public class SlingServletResourceTypesExampleServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse res) throws IOException {
res.setContentType("text/plain");
res.setCharacterEncoding("UTF-8");
res.setStatus(200);
res.getWriter().write("Done");
}
}
The proper way to bind Sling Servlets is by binding the servlet to resource types. This is an example of a servlet binding to a resource type.
package com.sourcedcode.core.servlets.impl;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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 java.io.IOException;
import static org.apache.sling.api.servlets.HttpConstants.METHOD_GET;
/**
* Enables all resources to return formatted response data from the doGet() method.
* Appending ".example.json" on any resource will activate the doGet() method below.
*/
@Component(service = Servlet.class)
@SlingServletResourceTypes(
resourceTypes = "sling/servlet/default",
methods = "get",
extensions = "json",
selectors = "example")
public class SlingServletResourceTypesExampleServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse res) throws IOException {
res.setContentType("text/plain");
res.setCharacterEncoding("UTF-8");
res.setStatus(200);
res.getWriter().write("Done");
}
}
No comments:
Post a Comment
If you have any doubts or questions, please let us know.