AbstractWebConsolePlugin

class cppmicroservices::AbstractWebConsolePlugin

The Web Console can be extended by registering an OSGi service for the interface HttpServlet with the service property org.cppmicroservices.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 Web Console bundle provides two options. One of the options is to extend the AbstractWebConsolePlugin overwriting the RenderContent(HttpServletRequest&, HttpServletResponse&) method.

Inherits from cppmicroservices::HttpServlet

Subclassed by cppmicroservices::SimpleWebConsolePlugin

Public Types

using TemplateData = Kainjow::Mustache::Data

Public Functions

virtual std::string GetLabel() const = 0

Retrieves the label.

This is the last component in the servlet path.

Return
the label.

virtual std::string GetTitle() const = 0

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.

Return
the plugin title.

virtual std::string GetCategory() const

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 org.cppmicroservices.webconsole.category service property. Currently only single level categories are supported. So, this should be a simple string.

Return
category

virtual std::shared_ptr<WebConsoleVariableResolver> GetVariableResolver(HttpServletRequest &request)
virtual void SetVariableResolver(HttpServletRequest &request, const std::shared_ptr<WebConsoleVariableResolver> &resolver)

Protected Functions

virtual bool IsHtmlRequest(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.

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

virtual void DoGet(HttpServletRequest &request, HttpServletResponse &response)

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&, std::ostream&)
  4. RenderContent(HttpServletRequest&, HttpServletResponse&)
  5. EndResponse(HttpServletRequest&, std::ostream&)

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.

See
HttpServlet::DoGet(HttpServletRequest&, HttpServletResponse&)

virtual void RenderContent(HttpServletRequest &request, HttpServletResponse &response) = 0

This method is used to render the content of the plug-in.

It is called internally from the Web Console.

Parameters
  • request: the HTTP request send from the user
  • response: the HTTP response object, where to render the plugin data.

std::ostream &StartResponse(HttpServletRequest &request, HttpServletResponse &response)

This method is responsible for generating the top heading of the page.

Return
the stream that was used for generating the response.
See
EndResponse(HttpServletRequest&, std::ostream&)
Parameters
  • request: the HTTP request coming from the user
  • response: the HTTP response, where data is rendered

void RenderTopNavigation(HttpServletRequest &request, std::ostream &writer)

This method is called to generate the top level links with the available plug-ins.

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

void EndResponse(HttpServletRequest &request, std::ostream &writer)

This method is responsible for generating the footer of the page.

See
StartResponse(HttpServletRequest&, HttpServletResponse&)
Parameters
  • request: the HTTP request coming from the user
  • writer: the writer, where the HTML data is rendered

std::vector<std::string> GetCssReferences() const

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.

Return
The list of additional CSS files to reference in the head section or an empty list if no such CSS files are required.

std::string ReadTemplateFile(const std::string &templateFile, cppmicroservices::BundleContext context = cppmicroservices::GetBundleContext ()) const