org.apache.felix.webconsole
Class AbstractWebConsolePlugin

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.apache.felix.webconsole.AbstractWebConsolePlugin
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
SimpleWebConsolePlugin

public abstract class AbstractWebConsolePlugin
extends javax.servlet.http.HttpServlet

The Web Console can be extended by registering an OSGi service for the interface Servlet with the service property felix.webconsole.label set to the label (last segment in the URL) of the page. The respective service is called a Web Console Plugin or a plugin for short. To help rendering the response the Apache Felix Web Console bundle provides two options. One of the options is to extend the AbstractWebConsolePlugin overwriting the renderContent(HttpServletRequest, HttpServletResponse) method.

See Also:
Serialized Form

Field Summary
static java.lang.String ATTR_FILEUPLOAD
          The name of the request attribute containing the map of FileItems from the POST request
static java.lang.String GET_RESOURCE_METHOD_NAME
          Web Console Plugin typically consists of servlet and resources such as images, scripts or style sheets.
 
Constructor Summary
AbstractWebConsolePlugin()
           
 
Method Summary
 void activate(org.osgi.framework.BundleContext bundleContext)
          This method is called from the Felix Web Console to ensure the AbstractWebConsolePlugin is correctly setup.
 void deactivate()
          This method is called, by the Web Console to de-activate the plugin and release all used resources.
protected  void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Renders the web console page for the request.
protected  void endResponse(java.io.PrintWriter pw)
          This method is responsible for generating the footer of the page.
static BrandingPlugin getBrandingPlugin()
          Returns the BrandingPlugin currently used for web console branding.
 org.osgi.framework.Bundle getBundle()
          Returns the Bundle pertaining to the bundle context with which this plugin has been activated.
protected  org.osgi.framework.BundleContext getBundleContext()
          Returns the BundleContext with which this plugin has been activated.
 java.lang.String getCategory()
          This method should return category string which will be used to render the plugin in the navigation menu.
protected  java.lang.String[] getCssReferences()
          Returns a list of CSS reference paths or null if no additional CSS files are provided by the plugin.
abstract  java.lang.String getLabel()
          Retrieves the label.
static java.lang.String getParameter(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Deprecated. use WebConsoleUtil.getParameter(HttpServletRequest, String)
protected  java.lang.Object getResourceProvider()
          Returns the object which might provide resources.
 java.lang.String getServletName()
          Returns the title for this plugin as returned by getTitle()
abstract  java.lang.String getTitle()
          Retrieves the title of the plug-in.
protected  boolean isHtmlRequest(javax.servlet.http.HttpServletRequest request)
          Detects whether this request is intended to have the headers and footers of this plugin be rendered or not.
 void log(int level, java.lang.String message)
          Calls the ServletContext.log(String) method if the configured log level is less than or equal to the given level.
 void log(int level, java.lang.String message, java.lang.Throwable t)
          Calls the ServletContext.log(String, Throwable) method if the configured log level is less than or equal to the given level.
protected  java.lang.String readTemplateFile(java.lang.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.
protected abstract  void renderContent(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          This method is used to render the content of the plug-in.
protected  void renderMenu(java.util.Map menuMap, java.lang.String appRoot, java.io.PrintWriter pw)
           
protected  void renderTopNavigation(javax.servlet.http.HttpServletRequest request, java.io.PrintWriter pw)
          This method is called to generate the top level links with the available plug-ins.
protected  void sendRedirect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String redirectUrl)
          Deprecated. use WebConsoleUtil.sendRedirect(HttpServletRequest, HttpServletResponse, String)
static void setBrandingPlugin(BrandingPlugin brandingPlugin)
          Sets the BrandingPlugin to use globally by all extensions of this class for branding.
static void setLogLevel(int logLevel)
          Sets the log level to be applied for calls to the log(int, String) and log(int, String, Throwable) methods.
protected  java.io.PrintWriter startResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          This method is responsible for generating the top heading of the page.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, init, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTR_FILEUPLOAD

public static final java.lang.String ATTR_FILEUPLOAD
The name of the request attribute containing the map of FileItems from the POST request

See Also:
Constant Field Values

GET_RESOURCE_METHOD_NAME

public static final java.lang.String GET_RESOURCE_METHOD_NAME
Web Console Plugin typically consists of servlet and resources such as images, scripts or style sheets. To load resources, a Resource Provider is used. The resource provider is an object, that provides a method which name is specified by this constants and it is 'getResource'.

See Also:
getResourceProvider(), Constant Field Values
Constructor Detail

AbstractWebConsolePlugin

public AbstractWebConsolePlugin()
Method Detail

getServletName

public java.lang.String getServletName()
Returns the title for this plugin as returned by getTitle()

Specified by:
getServletName in interface javax.servlet.ServletConfig
Overrides:
getServletName in class javax.servlet.GenericServlet
See Also:
GenericServlet.getServletName()

getCategory

public java.lang.String getCategory()
This method should return category string which will be used to render the plugin in the navigation menu. Default implementation returns null, which will result in the plugin link rendered as top level menu item. Concrete implementations wishing to be rendered as a sub-menu item under a category should override this method and return a string or define felix.webconsole.category OSGi property. Currently only single level categories are supported. So, this should be a simple String.

Returns:
category

doGet

protected void doGet(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
              throws javax.servlet.ServletException,
                     java.io.IOException
Renders the web console page for the request. This consist of the following five parts called in order:
  1. Send back a requested resource
  2. startResponse(HttpServletRequest, HttpServletResponse)
  3. renderTopNavigation(HttpServletRequest, PrintWriter)
  4. renderContent(HttpServletRequest, HttpServletResponse)
  5. endResponse(PrintWriter)

Note: If a resource is sent back for the request only the first step is executed. Otherwise the first step is a null-operation actually and the latter four steps are executed in order.

If the isHtmlRequest(HttpServletRequest) method returns false only the renderContent(HttpServletRequest, HttpServletResponse) method is called.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
java.io.IOException
See Also:
HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

isHtmlRequest

protected boolean isHtmlRequest(javax.servlet.http.HttpServletRequest request)
Detects whether this request is intended to have the headers and footers of this plugin be rendered or not. This method always returns true and may be overwritten by plugins to detect from the actual request, whether or not to render the header and footer.

Parameters:
request - the original request passed from the HTTP server
Returns:
true if the page should have headers and footers rendered

activate

public void activate(org.osgi.framework.BundleContext bundleContext)
This method is called from the Felix Web Console to ensure the AbstractWebConsolePlugin is correctly setup. It is called right after the Web Console receives notification for plugin registration.

Parameters:
bundleContext - the context of the plugin bundle

deactivate

public void deactivate()
This method is called, by the Web Console to de-activate the plugin and release all used resources.


renderContent

protected abstract void renderContent(javax.servlet.http.HttpServletRequest req,
                                      javax.servlet.http.HttpServletResponse res)
                               throws javax.servlet.ServletException,
                                      java.io.IOException
This method is used to render the content of the plug-in. It is called internally from the Web Console.

Parameters:
req - the HTTP request send from the user
res - the HTTP response object, where to render the plugin data.
Throws:
java.io.IOException - if an input or output error is detected when the servlet handles the request
javax.servlet.ServletException - if the request for the GET could not be handled

getLabel

public abstract java.lang.String getLabel()
Retrieves the label. This is the last component in the servlet path. This method MUST be overridden, if the AbstractWebConsolePlugin() constructor is used.

Returns:
the label.

getTitle

public abstract java.lang.String getTitle()
Retrieves the title of the plug-in. It is displayed in the page header and is also included in the title of the HTML document. This method MUST be overridden, if the AbstractWebConsolePlugin() constructor is used.

Returns:
the plugin title.

getCssReferences

protected java.lang.String[] getCssReferences()
Returns a list of CSS reference paths or null if no additional CSS files are provided by the plugin.

The result is an array of strings which are used as the value of the href attribute of the <link> elements placed in the head section of the HTML generated. If the reference is a relative path, it is turned into an absolute path by prepending the value of the WebConsoleConstants.ATTR_APP_ROOT request attribute.

Returns:
The list of additional CSS files to reference in the head section or null if no such CSS files are required.

getBundleContext

protected org.osgi.framework.BundleContext getBundleContext()
Returns the BundleContext with which this plugin has been activated. If the plugin has not be activated by calling the activate(BundleContext) method, this method returns null.

Returns:
the bundle context or null if the bundle is not activated.

getBundle

public final org.osgi.framework.Bundle getBundle()
Returns the Bundle pertaining to the bundle context with which this plugin has been activated. If the plugin has not be activated by calling the activate(BundleContext) method, this method returns null.

Returns:
the bundle or null if the plugin is not activated.

getResourceProvider

protected java.lang.Object getResourceProvider()
Returns the object which might provide resources. The class of this object is used to find the getResource method.

This method may be overwritten by extensions. This base class implementation returns this instance.

Returns:
The resource provider object or null if no resources will be provided by this plugin.

log

public void log(int level,
                java.lang.String message)
Calls the ServletContext.log(String) method if the configured log level is less than or equal to the given level.

Note, that the level paramter is only used to decide whether the GenericServlet.log(String) method is called or not. The actual implementation of the GenericServlet.log method is outside of the control of this method.

If the servlet has not been initialized yet or has already been destroyed the message is printed to stderr.

Parameters:
level - The log level at which to log the message
message - The message to log

log

public void log(int level,
                java.lang.String message,
                java.lang.Throwable t)
Calls the ServletContext.log(String, Throwable) method if the configured log level is less than or equal to the given level.

Note, that the level paramter is only used to decide whether the GenericServlet.log(String, Throwable) method is called or not. The actual implementation of the GenericServlet.log method is outside of the control of this method.

Parameters:
level - The log level at which to log the message
message - The message to log
t - The Throwable to log with the message

startResponse

protected java.io.PrintWriter startResponse(javax.servlet.http.HttpServletRequest request,
                                            javax.servlet.http.HttpServletResponse response)
                                     throws java.io.IOException
This method is responsible for generating the top heading of the page.

Parameters:
request - the HTTP request coming from the user
response - the HTTP response, where data is rendered
Returns:
the writer that was used for generating the response.
Throws:
java.io.IOException - on I/O error
See Also:
endResponse(PrintWriter)

renderTopNavigation

protected void renderTopNavigation(javax.servlet.http.HttpServletRequest request,
                                   java.io.PrintWriter pw)
This method is called to generate the top level links with the available plug-ins.

Parameters:
request - the HTTP request coming from the user
pw - the writer, where the HTML data is rendered

renderMenu

protected void renderMenu(java.util.Map menuMap,
                          java.lang.String appRoot,
                          java.io.PrintWriter pw)

endResponse

protected void endResponse(java.io.PrintWriter pw)
This method is responsible for generating the footer of the page.

Parameters:
pw - the writer, where the HTML data is rendered
See Also:
startResponse(HttpServletRequest, HttpServletResponse)

getParameter

public static final java.lang.String getParameter(javax.servlet.http.HttpServletRequest request,
                                                  java.lang.String name)
Deprecated. use WebConsoleUtil.getParameter(HttpServletRequest, String)

An utility method, that is used to filter out simple parameter from file parameter when multipart transfer encoding is used. This method processes the request and sets a request attribute ATTR_FILEUPLOAD. The attribute value is a Map where the key is a String specifying the field name and the value is a FileItem.

Parameters:
request - the HTTP request coming from the user
name - the name of the parameter
Returns:
if not multipart transfer encoding is used - the value is the parameter value or null if not set. If multipart is used, and the specified parameter is field - then the value of the parameter is returned.

sendRedirect

protected void sendRedirect(javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response,
                            java.lang.String redirectUrl)
                     throws java.io.IOException
Deprecated. use WebConsoleUtil.sendRedirect(HttpServletRequest, HttpServletResponse, String)

Utility method to handle relative redirects. Some application servers like Web Sphere handle relative redirects differently therefore we should make an absolute URL before invoking send redirect.

Parameters:
request - the HTTP request coming from the user
response - the HTTP response, where data is rendered
redirectUrl - the redirect URI.
Throws:
java.io.IOException - If an input or output exception occurs
java.lang.IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL

getBrandingPlugin

public static BrandingPlugin getBrandingPlugin()
Returns the BrandingPlugin currently used for web console branding.

Returns:
the brandingPlugin

setBrandingPlugin

public static final void setBrandingPlugin(BrandingPlugin brandingPlugin)
Sets the BrandingPlugin to use globally by all extensions of this class for branding.

Note: This method is intended to be used internally by the Web Console to update the branding plugin to use.

Parameters:
brandingPlugin - the brandingPlugin to set

setLogLevel

public static final void setLogLevel(int logLevel)
Sets the log level to be applied for calls to the log(int, String) and log(int, String, Throwable) methods.

Note: This method is intended to be used internally by the Web Console to update the log level according to the Web Console configuration.

Parameters:
logLevel - the maximum allowed log level. If message is logged with lower level it will not be forwarded to the logger.

readTemplateFile

protected final java.lang.String readTemplateFile(java.lang.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.

If the template file cannot read into a string and an exception is caused, the exception is logged and an empty string returned.

Parameters:
templateFile - The absolute path to the template file to read.
Returns:
The contents of the template file as a string or and empty string if the template file fails to be read.
Throws:
java.lang.NullPointerException - if templateFile is null
java.lang.RuntimeException - if an IOException is thrown reading the template file into a string. The exception provides the exception thrown as its cause.


Copyright © 2006-2014 The Apache Software Foundation. All Rights Reserved.