package net.i2p.router.web; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.Set; import java.util.TreeSet; import net.i2p.router.startup.ClientAppConfig; public class ConfigClientsHelper extends HelperBase { private String _edit; public ConfigClientsHelper() {} public void setEdit(String edit) { if (edit == null) return; String xStart = _("Edit"); if (edit.startsWith(xStart + " ") && edit.endsWith("")) { // IE sucks _edit = edit.substring(xStart.length() + 18, edit.length() - 7); } else if (edit.startsWith("Edit ")) { _edit = edit.substring(5); } else if (edit.startsWith(xStart + ' ')) { _edit = edit.substring(xStart.length() + 1); } else if ((_("Add Client")).equals(edit)) { _edit = "new"; } } public String getForm1() { StringBuilder buf = new StringBuilder(1024); buf.append("\n"); buf.append("\n"); List clients = ClientAppConfig.getClientApps(_context); for (int cur = 0; cur < clients.size(); cur++) { ClientAppConfig ca = clients.get(cur); renderForm(buf, ""+cur, ca.clientName, false, !ca.disabled, "webConsole".equals(ca.clientName) || "Web console".equals(ca.clientName), ca.className + ((ca.args != null) ? " " + ca.args : ""), (""+cur).equals(_edit), true, false, false, true, ca.disabled); } if ("new".equals(_edit)) renderForm(buf, "" + clients.size(), "", false, false, false, "", true, false, false, false, false, false); buf.append("
" + _("Client") + "" + _("Run at Startup?") + "" + _("Control") + "" + _("Class and arguments") + "
\n"); return buf.toString(); } public String getForm2() { StringBuilder buf = new StringBuilder(1024); buf.append("\n"); buf.append("\n"); Properties props = RouterConsoleRunner.webAppProperties(); Set keys = new TreeSet(props.keySet()); for (Iterator iter = keys.iterator(); iter.hasNext(); ) { String name = iter.next(); if (name.startsWith(RouterConsoleRunner.PREFIX) && name.endsWith(RouterConsoleRunner.ENABLED)) { String app = name.substring(RouterConsoleRunner.PREFIX.length(), name.lastIndexOf(RouterConsoleRunner.ENABLED)); String val = props.getProperty(name); renderForm(buf, app, app, !"addressbook".equals(app), "true".equals(val), RouterConsoleRunner.ROUTERCONSOLE.equals(app), app + ".war", false, false, false, false, false, true); } } buf.append("
" + _("WebApp") + "" + _("Run at Startup?") + "" + _("Control") + "" + _("Description") + "
\n"); return buf.toString(); } public boolean showPlugins() { return PluginStarter.pluginsEnabled(_context); } public String getForm3() { StringBuilder buf = new StringBuilder(1024); buf.append("\n"); buf.append("\n"); Properties props = PluginStarter.pluginProperties(); Set keys = new TreeSet(props.keySet()); for (Iterator iter = keys.iterator(); iter.hasNext(); ) { String name = iter.next(); if (name.startsWith(PluginStarter.PREFIX) && name.endsWith(PluginStarter.ENABLED)) { String app = name.substring(PluginStarter.PREFIX.length(), name.lastIndexOf(PluginStarter.ENABLED)); String val = props.getProperty(name); Properties appProps = PluginStarter.pluginProperties(_context, app); if (appProps.isEmpty()) continue; StringBuilder desc = new StringBuilder(256); desc.append("
" + _("Plugin") + "" + _("Run at Startup?") + "" + _("Control") + "" + _("Description") + "
") .append("
").append(_("Version")).append("").append(stripHTML(appProps, "version")) .append("
") .append(_("Signed by")).append(""); String s = stripHTML(appProps, "signer"); if (s != null) { if (s.indexOf("@") > 0) desc.append("").append(s).append(""); else desc.append(s); } s = stripHTML(appProps, "date"); if (s != null) { long ms = 0; try { ms = Long.parseLong(s); } catch (NumberFormatException nfe) {} if (ms > 0) { String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(ms)); desc.append("
") .append(_("Date")).append("").append(date); } } s = stripHTML(appProps, "author"); if (s != null) { desc.append("
") .append(_("Author")).append(""); if (s.indexOf("@") > 0) desc.append("").append(s).append(""); else desc.append(s); } s = stripHTML(appProps, "description_" + Messages.getLanguage(_context)); if (s == null) s = stripHTML(appProps, "description"); if (s != null) { desc.append("
") .append(_("Description")).append("").append(s); } s = stripHTML(appProps, "license"); if (s != null) { desc.append("
") .append(_("License")).append("").append(s); } s = stripHTML(appProps, "websiteURL"); if (s != null) { desc.append("
") .append("").append(_("Website")).append(" "); } String updateURL = stripHTML(appProps, "updateURL"); if (updateURL != null) { desc.append("
") .append("").append(_("Update link")).append(" "); } desc.append("
"); boolean enableStop = !Boolean.valueOf(appProps.getProperty("disableStop")).booleanValue(); enableStop &= PluginStarter.isPluginRunning(app, _context); boolean enableStart = !PluginStarter.isPluginRunning(app, _context); renderForm(buf, app, app, false, "true".equals(val), false, desc.toString(), false, false, updateURL != null, enableStop, true, enableStart); } } buf.append("\n"); return buf.toString(); } /** ro trumps edit and showEditButton */ private void renderForm(StringBuilder buf, String index, String name, boolean urlify, boolean enabled, boolean ro, String desc, boolean edit, boolean showEditButton, boolean showUpdateButton, boolean showStopButton, boolean showDeleteButton, boolean showStartButton) { buf.append(""); if (urlify && enabled) { String link = "/"; if (! RouterConsoleRunner.ROUTERCONSOLE.equals(name)) link += name + "/"; buf.append("").append(_(name)).append(""); } else if (edit && !ro) { buf.append(" 0) buf.append(_(name)); buf.append("\" >"); } else { if (name.length() > 0) buf.append(_(name)); } buf.append(""); if (showStartButton && (!ro) && !edit) { buf.append(""); } if (showEditButton && (!edit) && !ro) buf.append(""); if (showStopButton && (!edit)) buf.append(""); if (showUpdateButton && (!edit) && !ro) { buf.append(""); buf.append(""); } if (showDeleteButton && (!edit) && !ro) { buf.append(""); } buf.append(""); if (edit && !ro) { buf.append(""); } else { buf.append(desc); } buf.append("\n"); } /** * Like in DataHelper but doesn't convert null to "" * There's a lot worse things a plugin could do but... */ static String stripHTML(Properties props, String key) { String orig = props.getProperty(key); if (orig == null) return null; String t1 = orig.replace('<', ' '); String rv = t1.replace('>', ' '); return rv; } }