Package org.apache.sling.api.servlets

# Class SlingJakartaSafeMethodsServlet

java.lang.Object
jakarta.servlet.GenericServlet
org.apache.sling.api.servlets.SlingJakartaSafeMethodsServlet

All Implemented Interfaces:
  `jakarta.servlet.Servlet`, `jakarta.servlet.ServletConfig`, `Serializable`

Direct Known Subclasses:
  `SlingJakartaAllMethodsServlet`

public class SlingJakartaSafeMethodsServlet extends jakarta.servlet.GenericServlet
Helper base class for read-only Servlets used in Sling. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. So extensions of this class have great control over what methods to overwrite.

If any of the default HTTP methods is to be implemented just overwrite the respective doXXX method. If additional methods should be supported implement appropriate doXXX methods and overwrite the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method to dispatch to the doXXX methods as appropriate and overwrite the `getAllowedRequestMethods(Map)` to add the new method names.

Please note, that this base class is intended for applications where data is only read. As such, this servlet by itself does not support the POST, PUT and DELETE methods. Extensions of this class should either overwrite any of the doXXX methods of this class or add support for other read-only methods only. Applications wishing to support data modification should rather use or extend the `SlingJakartaAllMethodsServlet` which also contains support for the POST, PUT and DELETE methods. This latter class should also be overwritten to add support for HTTP methods modifying data.

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 ease the integration with traditional servlets.

Since:
  2.5.0
See Also:
  - `SlingJakartaAllMethodsServlet`
  - Serialized Form

## Constructor Summary

Constructors
Constructor
Description
`SlingJakartaSafeMethodsServlet()`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`doGeneric(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Called by the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method to handle a request for an HTTP method, which is not known and handled by this class or its extension.
`protected void`
`doGet(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Called by the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method to handle an HTTP GET request.
`protected void`
`doHead(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Handles the HEAD method.
`protected void`
`doOptions(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Handles the OPTIONS method by setting the HTTP `Allow` header on the response depending on the methods declared in this class.
`protected void`
`doTrace(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Handles the TRACE method by just returning the list of all header values in the response body.
`protected StringBuffer`
`getAllowedRequestMethods(Map<String,Method> declaredMethods)`
Helper method called by `doOptions(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` to calculate the value of the Allow header sent as the response to the HTTP OPTIONS request.
`@NotNull String`
`getServletInfo()`
Returns the simple class name of this servlet class.
`protected void`
`handleMethodNotImplemented(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Helper method which causes an appropriate HTTP response to be sent for an unhandled HTTP request method.
`protected boolean`
`mayService(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Tries to handle the request by calling a Java method implemented for the respective HTTP request method.
`void`
`service(@NotNull jakarta.servlet.ServletRequest req, @NotNull jakarta.servlet.ServletResponse res)`
Forwards the request to the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method if the request is a HTTP request.
`protected void`
`service(@NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response)`
Called by the `service(ServletRequest, ServletResponse)` method to handle the HTTP request.

### Methods inherited from class jakarta.servlet.GenericServlet

`destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletName, init, init, log, log`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### SlingJakartaSafeMethodsServlet

public SlingJakartaSafeMethodsServlet()

## Method Details

### doHead

protected void doHead(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Handles the HEAD method.

This base implementation just calls the `doGet(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method dropping the output. Implementations of this class may overwrite this method if they have a more performing implementation. Otherwise, they may just keep this base implementation.

Parameters:
  `request` - The HTTP request
  `response` - The HTTP response which only gets the headers set
Throws:
  `jakarta.servlet.ServletException` - Forwarded from the `doGet(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method called by this implementation.
  `IOException` - Forwarded from the `doGet(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method called by this implementation.

### doGet

protected void doGet(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Called by the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method to handle an HTTP GET 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 GET method support.

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

### doOptions

protected void doOptions(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Handles the OPTIONS method by setting the HTTP `Allow` header on the response depending on the methods declared in this class.

Extensions of this class should generally not overwrite this method but rather the `getAllowedRequestMethods(Map)` method. This method gathers all declared public and protected methods for the concrete class (upto but not including this class) and calls the `getAllowedRequestMethods(Map)` method with the methods gathered. The returned value is then used as the value of the `Allow` header set.

Parameters:
  `request` - The HTTP request object. Not used.
  `response` - The HTTP response object on which the header is set.
Throws:
  `jakarta.servlet.ServletException` - Not thrown by this implementation.
  `IOException` - Not thrown by this implementation.

### doTrace

protected void doTrace(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Handles the TRACE method by just returning the list of all header values in the response body.

Extensions of this class do not generally need to overwrite this method as it contains all there is to be done to the TRACE method.

Parameters:
  `request` - The HTTP request whose headers are returned.
  `response` - The HTTP response into which the request headers are written.
Throws:
  `jakarta.servlet.ServletException` - Not thrown by this implementation.
  `IOException` - May be thrown if there is an problem sending back the request headers in the response stream.

### doGeneric

protected void doGeneric(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Called by the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method to handle a request for an HTTP method, which is not known and handled by this class or its extension.

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

This method should be overwritten with great care. It is better to overwrite the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method and add support for any extension HTTP methods through an additional doXXX method.

Parameters:
  `request` - The HTTP request
  `response` - The HTTP response
Throws:
  `jakarta.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 SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Tries to handle the request by calling a Java method implemented for the respective HTTP request method.

This base class implentation dispatches the HEAD, GET, OPTIONS and TRACE to the respective doXXX methods and returns `true` if any of these methods is requested. Otherwise `false` is just returned.

Implementations of this class may overwrite this method but should first call this base implementation and in case `false` is returned add handling for any other method and of course return whether the requested method was known or not.

Parameters:
  `request` - The HTTP request
  `response` - The HTTP response
Returns:
  `true` if the requested method (`request.getMethod()`) is known. Otherwise `false` is returned.
Throws:
  `jakarta.servlet.ServletException` - Forwarded from any of the dispatched methods
  `IOException` - Forwarded from any of the dispatched methods

### handleMethodNotImplemented

protected void handleMethodNotImplemented(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws IOException
Helper method which causes an appropriate HTTP response to be sent for an unhandled HTTP request method. In case of HTTP/1.1 a 405 status code (Method Not Allowed) is returned, otherwise a 400 status (Bad Request) is returned.

Parameters:
  `request` - The HTTP request from which the method and protocol values are extracted to build the appropriate message.
  `response` - The HTTP response to which the error status is sent.
Throws:
  `IOException` - Thrown if the status cannot be sent to the client.

### service

protected void service(@NotNull @NotNull SlingJakartaHttpServletRequest request, @NotNull @NotNull SlingJakartaHttpServletResponse response) throws jakarta.servlet.ServletException, IOException
Called by the `service(ServletRequest, ServletResponse)` method to handle the HTTP request. This implementation calls the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method and depedending on its return value call the `doGeneric(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method. If the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method can handle the request, the `doGeneric(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method is not called otherwise it is called.

Implementations of this class should not generally overwrite this method. Rather the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method should be overwritten to add support for more HTTP methods.

Parameters:
  `request` - The HTTP request
  `response` - The HTTP response
Throws:
  `jakarta.servlet.ServletException` - Forwarded from the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` or `doGeneric(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` methods.
  `IOException` - Forwarded from the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` or `doGeneric(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` methods.

### service

public void service(@NotNull @NotNull jakarta.servlet.ServletRequest req, @NotNull @NotNull jakarta.servlet.ServletResponse res) throws jakarta.servlet.ServletException, IOException
Forwards the request to the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method if the request is a HTTP request.

Implementations of this class will not generally overwrite this method.

Specified by:
  `service` in interface `jakarta.servlet.Servlet`
Specified by:
  `service` in class `jakarta.servlet.GenericServlet`
Parameters:
  `req` - The Servlet request
  `res` - The Servlet response
Throws:
  `jakarta.servlet.ServletException` - If the request is not a HTTP request or forwarded from the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` called.
  `IOException` - Forwarded from the `service(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` called.

### getServletInfo

@NotNull public @NotNull String getServletInfo()
Returns the simple class name of this servlet class. Extensions of this class may overwrite to return more specific information.

Specified by:
  `getServletInfo` in interface `jakarta.servlet.Servlet`
Overrides:
  `getServletInfo` in class `jakarta.servlet.GenericServlet`

### getAllowedRequestMethods

protected StringBuffer getAllowedRequestMethods(Map<String,Method> declaredMethods)
Helper method called by `doOptions(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` to calculate the value of the Allow header sent as the response to the HTTP OPTIONS request.

This base class implementation checks whether any doXXX methods exist for GET and HEAD and returns the list of methods supported found. The list returned always includes the HTTP OPTIONS and TRACE methods.

Implementations of this class may overwrite this method check for more methods supported by the extension (generally the same list as used in the `mayService(SlingJakartaHttpServletRequest, SlingJakartaHttpServletResponse)` method). This base class implementation should always be called to make sure the default HTTP methods are included in the list.

Parameters:
  `declaredMethods` - The public and protected methods declared in the extension of this class.
Returns:
  A `StringBuffer` containing the list of HTTP methods supported.