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.
-
-
Constructor Summary
Constructors Constructor Description AbstractServlet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
Handle get requests.protected URL
getResource(String path)
Called to identify resources.protected RequestVariableResolver
getVariableResolver(jakarta.servlet.http.HttpServletRequest request)
protected String
readTemplateFile(String templateFile)
Reads thetemplateFile
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 pluginstatic void
setNoCache(jakarta.servlet.http.HttpServletResponse response)
Sets response headers to force the client to not cache the response sent back.protected void
spoolResource(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response)
If the request addresses a resource , this method serves it and returnstrue
.-
Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
-
-
-
-
Method Detail
-
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 callHttpServletResponse.setStatus(int)
. This method is also allowed to send a redirect or an error. If none of the three applies, the webconsole will callrenderContent(HttpServletRequest, HttpServletResponse)
- Overrides:
doGet
in classjakarta.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 requestresponse
- The response- Throws:
jakarta.servlet.ServletException
- If an error occursIOException
- 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 returnstrue
. Otherwisefalse
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 objectresponse
- The response object- Throws:
IOException
- If an error occurs accessing or spooling the resource.
-
readTemplateFile
protected final String readTemplateFile(String templateFile) throws IOException
Reads thetemplateFile
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
- iftemplateFile
isnull
FileNotFoundException
- If template file cannot be foundIOException
- 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
, andPragma
headers.- Parameters:
response
- The response for which to set the cache prevention
-
-