Class SlingAllMethodsServlet

  • All Implemented Interfaces:
    Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class SlingAllMethodsServlet
    extends SlingSafeMethodsServlet
    Helper base class for data modifying Servlets used in Sling. This class extends the SlingSafeMethodsServlet by support for the POST, PUT and DELETE methods.

    Implementors note: The methods in this class are all declared to throw the exceptions according to the intentions of the Servlet API rather than throwing their Sling RuntimeException counter parts. This is done to easy the integration with traditional servlets.

    See Also:
    for more information on supporting more HTTP methods, Serialized Form
    • Constructor Detail

      • SlingAllMethodsServlet

        public SlingAllMethodsServlet()
    • Method Detail

      • doPost

        protected void doPost​(@NotNull
                              @NotNull SlingHttpServletRequest request,
                              @NotNull
                              @NotNull SlingHttpServletResponse response)
                       throws javax.servlet.ServletException,
                              IOException
        Called by the mayService(SlingHttpServletRequest, SlingHttpServletResponse) method to handle an HTTP POST request.

        This default implementation reports back to the client that the method is not supported.

        Implementations of this class should overwrite this method with their implementation for the HTTP POST method support.

        Parameters:
        request - The HTTP request
        response - The HTTP response
        Throws:
        javax.servlet.ServletException - Not thrown by this implementation.
        IOException - If the error status cannot be reported back to the client.
      • doPut

        protected void doPut​(@NotNull
                             @NotNull SlingHttpServletRequest request,
                             @NotNull
                             @NotNull SlingHttpServletResponse response)
                      throws javax.servlet.ServletException,
                             IOException
        Called by the mayService(SlingHttpServletRequest, SlingHttpServletResponse) method to handle an HTTP PUT request.

        This default implementation reports back to the client that the method is not supported.

        Implementations of this class should overwrite this method with their implementation for the HTTP PUT method support.

        Parameters:
        request - The HTTP request
        response - The HTTP response
        Throws:
        javax.servlet.ServletException - Not thrown by this implementation.
        IOException - If the error status cannot be reported back to the client.
      • doDelete

        protected void doDelete​(@NotNull
                                @NotNull SlingHttpServletRequest request,
                                @NotNull
                                @NotNull SlingHttpServletResponse response)
                         throws javax.servlet.ServletException,
                                IOException
        Called by the mayService(SlingHttpServletRequest, SlingHttpServletResponse) method to handle an HTTP DELETE request.

        This default implementation reports back to the client that the method is not supported.

        Implementations of this class should overwrite this method with their implementation for the HTTP DELETE method support.

        Parameters:
        request - The HTTP request
        response - The HTTP response
        Throws:
        javax.servlet.ServletException - Not thrown by this implementation.
        IOException - If the error status cannot be reported back to the client.
      • mayService

        protected boolean mayService​(@NotNull
                                     @NotNull SlingHttpServletRequest request,
                                     @NotNull
                                     @NotNull SlingHttpServletResponse response)
                              throws javax.servlet.ServletException,
                                     IOException
        Tries to handle the request by calling a Java method implemented for the respective HTTP request method.

        This implementation first calls the base class implementation and only if the base class cannot dispatch will try to dispatch the supported methods POST, PUT and DELETE and returns true if any of these methods is requested. Otherwise false is just returned.

        Overrides:
        mayService in class SlingSafeMethodsServlet
        Parameters:
        request - The HTTP request
        response - The HTTP response
        Returns:
        true if the requested method (request.getMethod()) is known. Otherwise false is returned.
        Throws:
        javax.servlet.ServletException - Forwarded from any of the dispatched methods
        IOException - Forwarded from any of the dispatched methods
      • isMethodValid

        protected boolean isMethodValid​(Method method,
                                        String className)
        Returns true if method is not null and the method is not defined in the class named by className.

        This method may be used to make sure a method is actually overwritten and not just the default implementation.

        Parameters:
        method - The Method to check
        className - The name of class assumed to contained the initial declaration of the method.
        Returns:
        true if method is not null and the methods declaring class is not the given class.