Console: Move resource helper to war, doesn't work from jar

This commit is contained in:
zzz
2020-12-22 12:37:47 -05:00
parent e20a19c358
commit 010dbfa6f2
3 changed files with 90 additions and 60 deletions

View File

@@ -18,7 +18,6 @@ public class ContentHelper extends HelperBase {
/**
* Caution, use absolute paths only for getContent() and getTextContent(),
* do not assume files are in CWD.
* Use relative path for getResource().
*/
public void setPage(String page) { _page = page; }
@@ -114,61 +113,4 @@ public class ContentHelper extends HelperBase {
return newname;
return _page;
}
/**
* Convert file.ext to file_lang.ext if it exists.
* Get lang from the cgi lang param, then properties, then from the default locale.
* _context must be set to check the property.
* @return "" on error
* @since 0.9.49
*/
public String getResource() {
if (_page == null || _page.contains(".."))
return "";
String lang = _lang;
String page = null;
int lastdot = _page.lastIndexOf('.');
if (lastdot <= 0) {
page = _page;
} else {
if (lang == null || lang.length() <= 0) {
if (_context != null)
lang = _context.getProperty(Messages.PROP_LANG);
if (lang == null || lang.length() <= 0) {
lang = Locale.getDefault().getLanguage();
if (lang == null || lang.length() <= 0)
page = _page;
}
}
}
if (page == null) {
if (lang.equals("en"))
page = _page;
else
page = _page.substring(0, lastdot) + '_' + lang + _page.substring(lastdot);
}
InputStream is = ContentHelper.class.getResourceAsStream("/net/i2p/router/web/resources/" + page);
if (is == null) {
is = ContentHelper.class.getResourceAsStream("/net/i2p/router/web/resources/" + _page);
if (is == null)
return "";
}
BufferedReader in = null;
StringBuilder buf = new StringBuilder(20000);
try {
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
int i = 0;
while ( (line = in.readLine()) != null) {
buf.append(line);
if (_maxLines > 0 && ++i >= _maxLines)
break;
}
} catch (IOException ioe) {
} finally {
if (in != null) try { in.close(); } catch (IOException ioe) {}
try { is.close(); } catch (IOException ioe) {}
}
return buf.toString();
}
}

View File

@@ -0,0 +1,89 @@
package net.i2p.router.web.helpers;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Locale;
import net.i2p.router.web.HelperBase;
import net.i2p.router.web.Messages;
/**
* @since 0.9.49
*/
public class ResourceHelper extends HelperBase {
protected String _page;
private int _maxLines;
/**
* Use relative path for getResource().
*/
public void setPage(String page) { _page = page; }
public void setMaxLines(String lines) {
if (lines != null) {
try {
_maxLines = Integer.parseInt(lines);
} catch (NumberFormatException nfe) {
_maxLines = -1;
}
} else {
_maxLines = -1;
}
}
/**
* Convert file.ext to file_lang.ext if it exists.
* Get lang from the cgi lang param, then properties, then from the default locale.
* _context must be set to check the property.
* @return "" on error
*/
public String getResource() {
if (_page == null || _page.contains(".."))
return "";
String lang = null;
String page = null;
int lastdot = _page.lastIndexOf('.');
if (lastdot <= 0) {
page = _page;
} else {
if (_context != null)
lang = _context.getProperty(Messages.PROP_LANG);
if (lang == null || lang.length() <= 0) {
lang = Locale.getDefault().getLanguage();
if (lang == null || lang.length() <= 0)
page = _page;
}
}
if (page == null) {
if (lang.equals("en"))
page = _page;
else
page = _page.substring(0, lastdot) + '_' + lang + _page.substring(lastdot);
}
InputStream is = ResourceHelper.class.getResourceAsStream("/net/i2p/router/web/resources/" + page);
if (is == null) {
is = ResourceHelper.class.getResourceAsStream("/net/i2p/router/web/resources/" + _page);
if (is == null)
return "";
}
BufferedReader in = null;
StringBuilder buf = new StringBuilder(20000);
try {
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
int i = 0;
while ( (line = in.readLine()) != null) {
buf.append(line);
if (_maxLines > 0 && ++i >= _maxLines)
break;
}
} catch (IOException ioe) {
} finally {
if (in != null) try { in.close(); } catch (IOException ioe) {}
try { is.close(); } catch (IOException ioe) {}
}
return buf.toString();
}
}

View File

@@ -25,7 +25,7 @@
<jsp:setProperty name="updatehelper" property="contextId" value="<%=i2pcontextId%>" />
<jsp:getProperty name="updatehelper" property="newsStatus" /><br>
</div><div class="main" id="console">
<jsp:useBean class="net.i2p.router.web.ContentHelper" id="contenthelper" scope="request" />
<jsp:useBean class="net.i2p.router.web.helpers.ResourceHelper" id="contenthelper" scope="request" />
<div class="welcome">
<div class="langbox" title="<%=intl._t("Configure Language")%>">
<a href="/configui#langheading"><img src="/themes/console/images/info/control.png" alt="<%=intl._t("Configure Language")%>"></a>
@@ -33,7 +33,6 @@
<a name="top"></a>
<h2><%=intl._t("Welcome to I2P")%></h2>
</div>
<% java.io.File fpath = new java.io.File(net.i2p.I2PAppContext.getGlobalContext().getBaseDir(), "docs/readme.html"); %>
<jsp:setProperty name="contenthelper" property="page" value="docs/readme.html" />
<jsp:setProperty name="contenthelper" property="maxLines" value="300" />
<jsp:setProperty name="contenthelper" property="contextId" value="<%=i2pcontextId%>" />