public abstract class Widget extends Object
Widgets are associated with resources through the following
Widgets allow you to add arbitrary HTML, CSS, and Javascript to the page.
The HTML content returned by the getHtml(RestRequest)
method is added where the
The following examples shows how to associate a widget with a REST method and then have it rendered in the links and aside section of the page:
The following shows an example of a widget that renders an image located in the htdocs
static files
directory in your classpath (see @RestResource.staticFiles()
):
Note the getClasspathResourceAsString(String)
and getClasspathResourceAsString(String, Locale)
convenience methods
provided for quickly loading javascript and css files from the classpath or file system.
These are useful if your script or styles are complex and you want them loaded from files.
Widgets must provide one of the following public constructors:
public Widget();
public Widget(PropertyStore);
Widgets can be defined as inner classes of REST resource classes.
Constructor and Description |
---|
Widget() |
Modifier and Type | Method and Description |
---|---|
protected String |
getClasspathResourceAsString(String name)
Retrieves the specified classpath resource and returns the contents as a string.
|
protected String |
getClasspathResourceAsString(String name,
Locale locale)
Same as
getClasspathResourceAsString(String) except also looks for localized-versions of the file. |
String |
getHtml(RestRequest req)
Resolves the HTML content for this widget.
|
String |
getName()
The widget key.
|
String |
getScript(RestRequest req)
Resolves any Javascript that should be added to the
|
String |
getStyle(RestRequest req)
Resolves any CSS styles that should be added to the
|
protected String |
loadHtml(String name)
Convenience method for calling
getClasspathResourceAsString(String) except also strips HTML comments from the
file. |
protected String |
loadScript(String name)
Convenience method for calling
getClasspathResourceAsString(String) except also strips Javascript comments from
the file. |
protected String |
loadStyle(String name)
Convenience method for calling
getClasspathResourceAsString(String) except also strips CSS comments from
the file. |
public Widget()
public String getName()
(i.e. The variable name inside the
The returned value must not be
If not overridden, the default value is the class simple name.
public String getHtml(RestRequest req) throws Exception
A returned value of
req
- The HTTP request object.Exception
public String getScript(RestRequest req) throws Exception
A returned value of
req
- The HTTP request object.Exception
public String getStyle(RestRequest req) throws Exception
A returned value of
req
- The HTTP request object.Exception
protected String getClasspathResourceAsString(String name) throws IOException
Same as Class.getResourceAsStream(String)
except if it doesn't find the resource on this class, searches
up the parent hierarchy chain.
If the resource cannot be found in the classpath, then an attempt is made to look relative to the JVM working directory.
Path traversals outside the working directory are not allowed for security reasons.
name
- Name of the desired resource.IOException
protected String getClasspathResourceAsString(String name, Locale locale) throws IOException
getClasspathResourceAsString(String)
except also looks for localized-versions of the file.
If the locale
is specified, then we look for resources whose name matches that locale.
For example, if looking for the resource
name
- Name of the desired resource.locale
- The locale. Can be IOException
protected String loadScript(String name) throws IOException
getClasspathResourceAsString(String)
except also strips Javascript comments from
the file.
Comments are assumed to be Java-style block comments:
name
- Name of the desired resource.IOException
protected String loadStyle(String name) throws IOException
getClasspathResourceAsString(String)
except also strips CSS comments from
the file.
Comments are assumed to be Java-style block comments:
name
- Name of the desired resource.IOException
protected String loadHtml(String name) throws IOException
getClasspathResourceAsString(String)
except also strips HTML comments from the
file.
Comment are assumed to be
name
- Name of the desired resource.IOException
Copyright © 2018 Apache. All rights reserved.