2004-08-26 08:00:16 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2008-10-14 16:23:23 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2004-09-29 19:34:02 +00:00
|
|
|
import net.i2p.util.FileUtil;
|
2004-08-26 08:00:16 +00:00
|
|
|
|
2009-01-29 02:16:18 +00:00
|
|
|
public class ContentHelper extends HelperBase {
|
2010-11-26 00:32:00 +00:00
|
|
|
protected String _page;
|
2004-08-26 08:00:16 +00:00
|
|
|
private int _maxLines;
|
2004-11-25 21:57:19 +00:00
|
|
|
private boolean _startAtBeginning;
|
2008-10-14 16:23:23 +00:00
|
|
|
private String _lang;
|
2004-08-26 08:00:16 +00:00
|
|
|
|
Big directory rework.
Eliminate all uses of the current working directory, and
set up multiple directories specified by absolute paths for various uses.
Add a WorkingDir class to create a user config directory and
migrate files to it for new installs.
The directory will be $HOME/.i2p on linux and %APPDIR%\I2P on Windows,
or as specified in the system property -Di2p.dir.config=/path/to/i2pdir
All files except for the base install and temp files will be
in the config directory by default.
Temp files will be in a i2p-xxxxx subdirectory of the system temp directory
specified by the system property java.io.tmpdir.
Convert all file opens in the code to be relative to a specific directory,
as specified in the context. Code and applications should never open
files relative to the current working directory (e.g. new File("foo")).
All files should be accessed in the appropriate context directory,
e.g. new File(_context.getAppDir(), "foo").
The router.config file location may be specified as a system property on the
java command line with -Drouter.configLocation=/path/to/router.config
All directories may be specified as properties in the router.config file.
The migration will copy all files from an existing installation,
except i2psnark/, with the system property -Di2p.dir.migrate=true.
Otherwise it will just set up a new directory with a minimal configuration.
The migration will also create a modified wrapper.config and (on linux only)
a modified i2prouter script, and place them in the config directory.
There are no changes to the installer or the default i2prouter, i2prouter.bat,
i2prouter, wrapper.config, runplain.sh, windows service installer/uninstaller,
etc. in this checkin.
* Directories. These are all set at instantiation and will not be changed by
* subsequent property changes.
* All properties, if set, should be absolute paths.
*
* Name Property Method Files
* ----- -------- ----- -----
* Base i2p.dir.base getBaseDir() lib/, webapps/, docs/, geoip/, licenses/, ...
* Temp i2p.dir.temp getTempDir() Temporary files
* Config i2p.dir.config getConfigDir() *.config, hosts.txt, addressbook/, ...
*
* (the following all default to the same as Config)
*
* Router i2p.dir.router getRouterDir() netDb/, peerProfiles/, router.*, keyBackup/, ...
* Log i2p.dir.log getLogDir() wrapper.log*, logs/
* PID i2p.dir.pid getPIDDir() wrapper *.pid files, router.ping
* App i2p.dir.app getAppDir() eepsite/, ...
*
* Note that we can't control where the wrapper actually puts its files.
All these will be set appropriately in a Router Context.
In an I2P App Context, all except Temp will be the current working directory.
Lightly tested so far, needs much more testing.
2009-06-04 19:14:40 +00:00
|
|
|
/**
|
|
|
|
* Caution, use absolute paths only, do not assume files are in CWD
|
|
|
|
*/
|
2004-08-26 08:00:16 +00:00
|
|
|
public void setPage(String page) { _page = page; }
|
2004-11-25 21:57:19 +00:00
|
|
|
public void setStartAtBeginning(String moo) {
|
2012-09-28 17:50:41 +00:00
|
|
|
_startAtBeginning = Boolean.parseBoolean(moo);
|
2004-11-25 21:57:19 +00:00
|
|
|
}
|
2011-01-03 18:40:18 +00:00
|
|
|
public void setLang(String l) {
|
2011-03-08 03:07:02 +00:00
|
|
|
/*****
|
2011-01-07 17:09:27 +00:00
|
|
|
if((_lang == null || !_lang.equals(l)) && (l != null)) {
|
2011-01-03 18:40:18 +00:00
|
|
|
//Set language for router console
|
|
|
|
_lang = l;
|
2011-02-27 13:53:39 +00:00
|
|
|
TODO - Temporary for 0.8.4
|
|
|
|
Needed for desktopgui. But there's no nonce protection.
|
|
|
|
Move the following to CSSHelper setLang(), or disable completely,
|
|
|
|
See comments in CSSHelper
|
2011-01-03 18:40:18 +00:00
|
|
|
if(_context == null) {
|
|
|
|
setContextId(null);
|
|
|
|
}
|
|
|
|
|
2011-02-27 13:53:39 +00:00
|
|
|
if (_context.getBooleanProperty("desktopgui.enabled")) {
|
|
|
|
//Set language persistently throughout I2P
|
|
|
|
_context.router().setConfigSetting(Messages.PROP_LANG, _lang);
|
|
|
|
_context.router().saveConfig();
|
|
|
|
_context.setProperty(Messages.PROP_LANG, _lang);
|
|
|
|
}
|
2011-01-03 18:40:18 +00:00
|
|
|
}
|
2011-03-08 03:07:02 +00:00
|
|
|
*****/
|
2011-01-03 18:40:18 +00:00
|
|
|
}
|
2004-11-25 21:57:19 +00:00
|
|
|
|
2004-08-26 08:00:16 +00:00
|
|
|
public void setMaxLines(String lines) {
|
|
|
|
if (lines != null) {
|
|
|
|
try {
|
|
|
|
_maxLines = Integer.parseInt(lines);
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
_maxLines = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_maxLines = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public String getContent() {
|
2008-10-14 16:23:23 +00:00
|
|
|
String str = FileUtil.readTextFile(filename(), _maxLines, _startAtBeginning);
|
2004-08-26 08:00:16 +00:00
|
|
|
if (str == null)
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
public String getTextContent() {
|
2008-10-14 16:23:23 +00:00
|
|
|
String str = FileUtil.readTextFile(filename(), _maxLines, _startAtBeginning);
|
2004-08-26 08:00:16 +00:00
|
|
|
if (str == null)
|
|
|
|
return "";
|
2005-12-23 04:36:31 +00:00
|
|
|
else {
|
2009-07-01 16:00:43 +00:00
|
|
|
StringBuilder sb = new StringBuilder(str.length()+11);
|
2005-12-23 04:36:31 +00:00
|
|
|
sb.append("<pre>");
|
|
|
|
for (int i=0; i < str.length(); i++) {
|
|
|
|
char c = str.charAt(i);
|
|
|
|
switch (str.charAt(i)) {
|
|
|
|
case '<': sb.append("<"); break;
|
|
|
|
case '>': sb.append(">"); break;
|
|
|
|
case '&': sb.append("&"); break;
|
|
|
|
default: sb.append(c); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.append("</pre>").toString();
|
|
|
|
}
|
2004-08-26 08:00:16 +00:00
|
|
|
}
|
2008-10-14 16:23:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert file.ext to file_lang.ext if it exists.
|
2009-10-18 14:06:07 +00:00
|
|
|
* Get lang from the cgi lang param, then properties, then from the default locale.
|
|
|
|
* _context must be set to check the property.
|
2008-10-14 16:23:23 +00:00
|
|
|
*/
|
|
|
|
private String filename() {
|
|
|
|
int lastdot = _page.lastIndexOf('.');
|
|
|
|
if (lastdot <= 0)
|
|
|
|
return _page;
|
|
|
|
String lang = _lang;
|
|
|
|
if (lang == null || lang.length() <= 0) {
|
2009-10-18 14:06:07 +00:00
|
|
|
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)
|
|
|
|
return _page;
|
|
|
|
}
|
2008-10-14 16:23:23 +00:00
|
|
|
}
|
|
|
|
if (lang.equals("en"))
|
|
|
|
return _page;
|
|
|
|
String newname = _page.substring(0, lastdot) + '_' + lang + _page.substring(lastdot);
|
|
|
|
File newfile = new File(newname);
|
|
|
|
if (newfile.exists())
|
|
|
|
return newname;
|
|
|
|
return _page;
|
|
|
|
}
|
2004-08-26 08:00:16 +00:00
|
|
|
}
|