Class AbstractServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
org.apache.felix.webconsole.servlet.AbstractServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public abstract class AbstractServlet extends jakarta.servlet.http.HttpServlet
This class can be used as a base class for a web console plugin. The plugin (servlet) needs to be registered as a servlet service with at least the label property.

Extending this servlet is one way of providing a plugin. The other is to simply direclty implement the servlet interface. In both cases requests to resources with a path of "LABEL/res/*" are automatically handled.

For html (content) requests, the web console automatically renders the header, footer and navigation.

Support for Jakarta servlets requires that the Jakarta Servlet API and the Apache Felix Http Wrappers are available in the runtime.

If you are upgrading from AbstractWebConsolePlugin there are some changes to be aware of:

  • Resources are detected via the getResource(String) method.
  • Regardless of the http method, if the corresponding doXXX method does not create a response, the renderContent(HttpServletRequest, HttpServletResponse) method is invoked.
  • If a doXXX method other than doGet wants to create a response, it must not call doGet or renderContent directly. It rather just does not create any response and returns. In this case the web console will invoke the renderContent method afterwards.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    Handle get requests.
    protected URL
    Called to identify resources.
    getVariableResolver(jakarta.servlet.http.HttpServletRequest request)
     
    protected final String
    readTemplateFile(String templateFile)
    Reads the templateFile as a resource through the class loader of this class converting the binary data into a string using UTF-8 encoding.
    void
    renderContent(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    This method is used to render the main contents of the plugin
    static final void
    setNoCache(jakarta.servlet.http.HttpServletResponse response)
    Sets response headers to force the client to not cache the response sent back.
    protected final void
    spoolResource(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
    If the request addresses a resource , this method serves it and returns true.

    Methods inherited from class jakarta.servlet.http.HttpServlet

    doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service

    Methods inherited from class jakarta.servlet.GenericServlet

    destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, 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

    • AbstractServlet

      public AbstractServlet()
  • Method Details

    • getResource

      protected URL getResource(String path)
      Called to identify resources. By default, if the path starts with "/res/" this is treated as a resource and the URL to the resource is tried to be loaded via the class loader.
      Parameters:
      path - the path
      Returns:
      the URL of the resource or null if not found.
    • doGet

      protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Handle get requests. This method can be used to return resources, like JSON responses etc. If the plugin is serving a resource, this method call HttpServletResponse.setStatus(int). This method is also allowed to send a redirect or an error. If none of the three applies, the webconsole will call renderContent(HttpServletRequest, HttpServletResponse)
      Overrides:
      doGet in class jakarta.servlet.http.HttpServlet
      Throws:
      jakarta.servlet.ServletException
      IOException
    • renderContent

      public void renderContent(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      This method is used to render the main contents of the plugin
      Parameters:
      request - The request
      response - The response
      Throws:
      jakarta.servlet.ServletException - If an error occurs
      IOException - If writing the response fails
    • spoolResource

      protected final void spoolResource(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      If the request addresses a resource , this method serves it and returns true. Otherwise false is returned.

      If true is returned, the request is considered complete and request processing terminates. Otherwise request processing continues with normal plugin rendering.

      Parameters:
      request - The request object
      response - The response object
      Throws:
      IOException - If an error occurs accessing or spooling the resource.
    • readTemplateFile

      protected final String readTemplateFile(String templateFile) throws IOException
      Reads the templateFile as a resource through the class loader of this class converting the binary data into a string using UTF-8 encoding.
      Parameters:
      templateFile - The absolute path to the template file to read.
      Returns:
      The contents of the template file as a string
      Throws:
      NullPointerException - if templateFile is null
      FileNotFoundException - If template file cannot be found
      IOException - On any other error reading the template file
    • getVariableResolver

      protected RequestVariableResolver getVariableResolver(jakarta.servlet.http.HttpServletRequest request)
    • setNoCache

      public static final void setNoCache(jakarta.servlet.http.HttpServletResponse response)
      Sets response headers to force the client to not cache the response sent back. This method must be called before the response is committed otherwise it will have no effect.

      This method sets the Cache-Control, Expires, and Pragma headers.

      Parameters:
      response - The response for which to set the cache prevention