FELIX-1988 Apply 5.main_template.patch by Valentin Valchev (thanks) with the following changes: (1) also externalize the footer as a template (for consistency), (2) create a readTemplateFile method to read the file, (3) return an empty string (instead of using null) of the file fails to be read
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@910864 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
index 0626667..20b27ea 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/AbstractWebConsolePlugin.java
@@ -65,6 +65,16 @@
public static final String GET_RESOURCE_METHOD_NAME = "getResource";
/**
+ * The header fragment read from the templates/main_header.html file
+ */
+ private static String HEADER;
+
+ /**
+ * The footer fragment read from the templates/main_footer.html file
+ */
+ private static String FOOTER;
+
+ /**
* The reference to the getResource method provided by the
* {@link #getResourceProvider()}. This is <code>null</code> if there is
* none or before the first check if there is one.
@@ -646,53 +656,49 @@
// 8 branding product image (BrandingPlugin.getProductImage())
// 9 additional HTML code to be inserted into the <head> section
// (for example plugin provided CSS links)
-
- final String header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
-
- + "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
- + " <head>"
- + " <meta http-equiv=\"Content-Type\" content=\"text/html; utf-8\">"
- + " <link rel=\"icon\" href=\"{4}\">"
- + " <title>{0} - {1}</title>"
-
- + " <link href=\"{2}/res/ui/admin.css\" rel=\"stylesheet\" type=\"text/css\">"
- + " <link href=\"{5}\" rel=\"stylesheet\" type=\"text/css\">"
- + " {9}"
-
- + " <script language=\"JavaScript\">"
- + " appRoot = \"{2}\";"
- + " pluginRoot = \"{2}/{3}\";"
- + " </script>"
-
- + " <script src=\"{2}/res/ui/jquery-1.3.2.min.js\" language=\"JavaScript\"></script>"
- + " <script src=\"{2}/res/ui/jquery.cookies-2.1.0.min.js\" language=\"JavaScript\"></script>"
- + " <script src=\"{2}/res/ui/jquery.tablesorter-2.0.3.min.js\" language=\"JavaScript\"></script>"
-
- + " <script src=\"{2}/res/ui/admin.js\" language=\"JavaScript\"></script>"
- + " <script src=\"{2}/res/ui/ui.js\" language=\"JavaScript\"></script>"
-
- + " </head>"
- + " <body>"
- + " <div id=\"main\">"
- + " <div id=\"lead\">"
- + " <h1>"
- + " {0}<br>{1}"
- + " </h1>"
- + " <p>"
- + " <a target=\"_blank\" href=\"{6}\" title=\"{7}\"><img src=\"{8}\" border=\"0\"></a>"
- + " </p>"
- + " </div>";
- return header;
+ if ( HEADER == null )
+ {
+ HEADER = readTemplateFile( "/templates/main_header.html" );
+ }
+ return HEADER;
}
private final String getFooter()
{
- // close <div id="main">, body and html
- final String footer = " </div>"
- + " </body>"
- + "</html>";
- return footer;
+ if ( FOOTER == null )
+ {
+ FOOTER = readTemplateFile( "/templates/main_footer.html" );
+ }
+ return FOOTER;
+ }
+
+
+ /**
+ * Reads the <code>templateFile</code> as a resource through the class
+ * loader of this class converting the binary data into a string using
+ * UTF-8 encoding.
+ * <p>
+ * If the template file cannot read into a string and an exception is
+ * caused, the exception is logged and an empty string returned.
+ *
+ * @param templateFile The absolute path to the template file to read.
+ * @return The contents of the template file as a string or and empty
+ * string if the template file fails to be read.
+ */
+ private final String readTemplateFile( final String templateFile )
+ {
+ try
+ {
+ return IOUtils.toString( getClass().getResourceAsStream( templateFile ), "UTF-8" );
+ }
+ catch ( IOException e )
+ {
+ log( "readTemplateFile: Error loading " + templateFile, e );
+ }
+
+ // fall back to empty contents to prevent NPE
+ return "";
}
diff --git a/webconsole/src/main/resources/templates/main_footer.html b/webconsole/src/main/resources/templates/main_footer.html
new file mode 100644
index 0000000..afb7967
--- /dev/null
+++ b/webconsole/src/main/resources/templates/main_footer.html
@@ -0,0 +1,3 @@
+ </div>
+ </body>
+</html>
\ No newline at end of file
diff --git a/webconsole/src/main/resources/templates/main_header.html b/webconsole/src/main/resources/templates/main_header.html
new file mode 100644
index 0000000..aee2d16
--- /dev/null
+++ b/webconsole/src/main/resources/templates/main_header.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; utf-8" />
+ <link rel="icon" href="{4}" />
+ <title>{0} - {1}</title>
+
+ <link href="{2}/res/lib/reset-min.css" rel="stylesheet" type="text/css" />
+ <link href="{2}/res/lib/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
+ <link href="{5}" rel="stylesheet" type="text/css" />
+ {9}
+
+ <script type="text/javascript">
+ // <![CDATA[
+ appRoot = "{2}";
+ pluginRoot = "{2}/{3}";
+ // ]]>
+ </script>
+
+ <script src="{2}/res/lib/jquery-1.3.2.js" type="text/javascript"></script>
+ <script src="{2}/res/lib/jquery-ui-1.7.2.js" type="text/javascript"></script>
+ <script src="{2}/res/lib/jquery-ui-i18n-1.7.2.js" type="text/javascript"></script>
+ <script src="{2}/res/lib/jquery.cookies-2.2.0.js" type="text/javascript"></script>
+ <script src="{2}/res/lib/jquery.tablesorter-2.0.3.js" type="text/javascript"></script>
+ <script src="{2}/res/lib/support.js" type="text/javascript"></script>
+
+ <!-- temporary here - for compatibility untill all plugins are refactored -->
+ <script src="{2}/res/ui/admin.js" type="text/javascript"></script>
+</head>
+
+<body class="ui-widget">
+ <div id="main">
+ <div id="lead"><!-- header: title and logo -->
+ <h1>
+ {0}<br/>{1}
+ </h1>
+ <p>
+ <a rel="external" href="{6}" title="{7}"><img src="{8}" alt="Logo" /></a>
+ </p>
+ </div>
+ <div class="ui-helper-clearfix" style="height:0px"> </div>
+
\ No newline at end of file