Interface NonExistingResourceServlet

  • All Superinterfaces:
    OptingServlet, Servlet

    public interface NonExistingResourceServlet
    extends OptingServlet
    NonExistingResourceServlet is a specific servlet interface for servlets that need to handle non-existing resources. Via the OptingServlet.accepts(org.apache.sling.api.SlingHttpServletRequest) method the implementing servlet must check if it will handle the given non-existing path.

    Please note: This is a temporary solution until Sling provides a built-in mechanism for this use case. Not to be used by client implementations!

    This servlet will not be registered directly with Sling but rather with a distinct dispatcher servlet that is registered for the GET, HEAD, POST and PUT HTTP methods on the sling:nonexisting resource ( NonExistingDispatcherServlet).

    To give a bit control over the order, set the standard OSGi property service.ranking (Integer) on the servlet to define an integer value for the order - the implementations with the highest number are called first. The first servlet that returns true in its OptingServlet.accepts(org.apache.sling.api.SlingHttpServletRequest) method will handle the request.

    Example code:

     @Component
     @Service(NonExistingResourceServlet.class)
     @Property(name="service.ranking",intValue=10)
                   
     @SuppressWarnings("serial")
      public class TesterServlet extends SlingAllMethodsServlet implements NonExistingResourceServlet {
     
          public boolean accepts(SlingHttpServletRequest request) {
              // get non-existing path (incl. selectors and extension!)
              String path = request.getResource().getPath();
              // return true if this servlet can handle this path
              return true;
          }
         
         @Override
          protected void doGet(SlingHttpServletRequest request,
                      SlingHttpServletResponse response) throws ServletException,
                      IOException {
              // handle actual GET request here
              // ...
          }