2008-06-16 12:31:14 +00:00
package net.i2p.router.web ;
2010-02-08 16:15:23 +00:00
import java.text.SimpleDateFormat ;
import java.util.Date ;
2008-06-16 12:31:14 +00:00
import java.util.Iterator ;
import java.util.List ;
import java.util.Properties ;
import java.util.Set ;
import java.util.TreeSet ;
2008-07-16 13:42:54 +00:00
2008-06-16 12:31:14 +00:00
import net.i2p.router.startup.ClientAppConfig ;
2009-01-29 02:16:18 +00:00
public class ConfigClientsHelper extends HelperBase {
2010-01-06 21:24:08 +00:00
private String _edit ;
2008-06-16 12:31:14 +00:00
public ConfigClientsHelper ( ) { }
2010-01-06 21:24:08 +00:00
public void setEdit ( String edit ) {
if ( edit = = null )
return ;
String xStart = _ ( " Edit " ) ;
if ( edit . startsWith ( xStart + " <span class=hide> " ) & &
edit . endsWith ( " </span> " ) ) {
// 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 " ;
}
}
2008-06-16 12:31:14 +00:00
public String getForm1 ( ) {
2009-07-01 16:00:43 +00:00
StringBuilder buf = new StringBuilder ( 1024 ) ;
2009-07-30 23:10:48 +00:00
buf . append ( " <table> \ n " ) ;
2010-02-08 20:57:30 +00:00
buf . append ( " <tr><th align= \" right \" > " + _ ( " Client " ) + " </th><th> " + _ ( " Run at Startup? " ) + " </th><th> " + _ ( " Control " ) + " </th><th align= \" left \" > " + _ ( " Class and arguments " ) + " </th></tr> \ n " ) ;
2008-06-16 12:31:14 +00:00
2010-01-06 21:24:08 +00:00
List < ClientAppConfig > clients = ClientAppConfig . getClientApps ( _context ) ;
2008-06-16 12:31:14 +00:00
for ( int cur = 0 ; cur < clients . size ( ) ; cur + + ) {
2010-01-06 21:24:08 +00:00
ClientAppConfig ca = clients . get ( cur ) ;
2009-10-28 18:26:50 +00:00
renderForm ( buf , " " + cur , ca . clientName , false , ! ca . disabled ,
" webConsole " . equals ( ca . clientName ) | | " Web console " . equals ( ca . clientName ) ,
2010-02-22 18:17:11 +00:00
ca . className + ( ( ca . args ! = null ) ? " " + ca . args : " " ) , ( " " + cur ) . equals ( _edit ) ,
2010-04-18 23:06:04 +00:00
true , false , false , true , ca . disabled ) ;
2008-06-16 12:31:14 +00:00
}
2010-01-06 21:24:08 +00:00
if ( " new " . equals ( _edit ) )
2010-03-29 21:20:48 +00:00
renderForm ( buf , " " + clients . size ( ) , " " , false , false , false , " " , true , false , false , false , false , false ) ;
2008-06-16 12:31:14 +00:00
buf . append ( " </table> \ n " ) ;
return buf . toString ( ) ;
}
public String getForm2 ( ) {
2009-07-01 16:00:43 +00:00
StringBuilder buf = new StringBuilder ( 1024 ) ;
2009-07-30 23:10:48 +00:00
buf . append ( " <table> \ n " ) ;
2010-02-08 20:57:30 +00:00
buf . append ( " <tr><th align= \" right \" > " + _ ( " WebApp " ) + " </th><th> " + _ ( " Run at Startup? " ) + " </th><th> " + _ ( " Control " ) + " </th><th align= \" left \" > " + _ ( " Description " ) + " </th></tr> \ n " ) ;
2008-06-16 12:31:14 +00:00
Properties props = RouterConsoleRunner . webAppProperties ( ) ;
2010-01-06 21:24:08 +00:00
Set < String > keys = new TreeSet ( props . keySet ( ) ) ;
for ( Iterator < String > iter = keys . iterator ( ) ; iter . hasNext ( ) ; ) {
String name = iter . next ( ) ;
2008-06-16 12:31:14 +00:00
if ( name . startsWith ( RouterConsoleRunner . PREFIX ) & & name . endsWith ( RouterConsoleRunner . ENABLED ) ) {
2008-06-17 13:48:41 +00:00
String app = name . substring ( RouterConsoleRunner . PREFIX . length ( ) , name . lastIndexOf ( RouterConsoleRunner . ENABLED ) ) ;
2008-06-16 12:31:14 +00:00
String val = props . getProperty ( name ) ;
2010-01-06 21:24:08 +00:00
renderForm ( buf , app , app , ! " addressbook " . equals ( app ) ,
2010-02-22 18:17:11 +00:00
" true " . equals ( val ) , RouterConsoleRunner . ROUTERCONSOLE . equals ( app ) , app + " .war " ,
2010-03-29 21:20:48 +00:00
false , false , false , false , false , true ) ;
2008-06-16 12:31:14 +00:00
}
}
buf . append ( " </table> \ n " ) ;
return buf . toString ( ) ;
}
2010-02-10 19:09:35 +00:00
public boolean showPlugins ( ) {
return PluginStarter . pluginsEnabled ( _context ) ;
}
2010-02-07 13:32:49 +00:00
public String getForm3 ( ) {
StringBuilder buf = new StringBuilder ( 1024 ) ;
buf . append ( " <table> \ n " ) ;
2010-02-08 20:57:30 +00:00
buf . append ( " <tr><th align= \" right \" > " + _ ( " Plugin " ) + " </th><th> " + _ ( " Run at Startup? " ) + " </th><th> " + _ ( " Control " ) + " </th><th align= \" left \" > " + _ ( " Description " ) + " </th></tr> \ n " ) ;
2010-02-07 13:32:49 +00:00
Properties props = PluginStarter . pluginProperties ( ) ;
Set < String > keys = new TreeSet ( props . keySet ( ) ) ;
for ( Iterator < String > 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 ) ;
2010-02-08 16:15:23 +00:00
Properties appProps = PluginStarter . pluginProperties ( _context , app ) ;
2010-02-17 18:12:46 +00:00
if ( appProps . size ( ) < = 0 )
continue ;
2010-02-08 16:15:23 +00:00
StringBuilder desc = new StringBuilder ( 256 ) ;
desc . append ( " <table border= \" 0 \" > " )
2010-02-08 23:28:09 +00:00
. append ( " <tr><td><b> " ) . append ( _ ( " Version " ) ) . append ( " <td> " ) . append ( stripHTML ( appProps , " version " ) )
2010-02-08 16:15:23 +00:00
. append ( " <tr><td><b> " )
2010-02-08 23:28:09 +00:00
. append ( _ ( " Signed by " ) ) . append ( " <td> " ) ;
2010-02-17 18:12:46 +00:00
String s = stripHTML ( appProps , " signer " ) ;
if ( s ! = null ) {
if ( s . indexOf ( " @ " ) > 0 )
desc . append ( " <a href= \" mailto: " ) . append ( s ) . append ( " \" > " ) . append ( s ) . append ( " </a> " ) ;
else
desc . append ( s ) ;
}
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " date " ) ;
2010-02-08 16:15:23 +00:00
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 ( " <tr><td><b> " )
. append ( _ ( " Date " ) ) . append ( " <td> " ) . append ( date ) ;
}
}
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " author " ) ;
2010-02-08 16:15:23 +00:00
if ( s ! = null ) {
desc . append ( " <tr><td><b> " )
2010-02-08 23:28:09 +00:00
. append ( _ ( " Author " ) ) . append ( " <td> " ) ;
if ( s . indexOf ( " @ " ) > 0 )
desc . append ( " <a href= \" mailto: " ) . append ( s ) . append ( " \" > " ) . append ( s ) . append ( " </a> " ) ;
else
desc . append ( s ) ;
2010-02-08 16:15:23 +00:00
}
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " description_ " + Messages . getLanguage ( _context ) ) ;
2010-02-08 16:15:23 +00:00
if ( s = = null )
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " description " ) ;
2010-02-08 16:15:23 +00:00
if ( s ! = null ) {
desc . append ( " <tr><td><b> " )
. append ( _ ( " Description " ) ) . append ( " <td> " ) . append ( s ) ;
}
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " license " ) ;
2010-02-08 16:15:23 +00:00
if ( s ! = null ) {
desc . append ( " <tr><td><b> " )
. append ( _ ( " License " ) ) . append ( " <td> " ) . append ( s ) ;
}
2010-02-08 23:28:09 +00:00
s = stripHTML ( appProps , " websiteURL " ) ;
2010-02-08 16:15:23 +00:00
if ( s ! = null ) {
desc . append ( " <tr><td> " )
. append ( " <a href= \" " ) . append ( s ) . append ( " \" > " ) . append ( _ ( " Website " ) ) . append ( " </a><td> " ) ;
}
2010-02-08 23:28:09 +00:00
String updateURL = stripHTML ( appProps , " updateURL " ) ;
2010-02-08 19:04:46 +00:00
if ( updateURL ! = null ) {
2010-02-08 16:15:23 +00:00
desc . append ( " <tr><td> " )
2010-02-08 19:04:46 +00:00
. append ( " <a href= \" " ) . append ( updateURL ) . append ( " \" > " ) . append ( _ ( " Update link " ) ) . append ( " </a><td> " ) ;
2010-02-08 16:15:23 +00:00
}
desc . append ( " </table> " ) ;
2010-03-02 22:54:32 +00:00
boolean enableStop = ! Boolean . valueOf ( appProps . getProperty ( " disableStop " ) ) . booleanValue ( ) ;
2010-04-16 03:58:48 +00:00
enableStop & = PluginStarter . isPluginRunning ( app , _context ) ;
boolean enableStart = ! PluginStarter . isPluginRunning ( app , _context ) ;
2010-02-08 16:15:23 +00:00
renderForm ( buf , app , app , false ,
2010-02-08 20:57:30 +00:00
" true " . equals ( val ) , false , desc . toString ( ) , false , false ,
2010-04-16 03:58:48 +00:00
updateURL ! = null , enableStop , true , enableStart ) ;
2010-02-07 13:32:49 +00:00
}
}
buf . append ( " </table> \ n " ) ;
return buf . toString ( ) ;
}
2010-01-06 21:24:08 +00:00
/** ro trumps edit and showEditButton */
private void renderForm ( StringBuilder buf , String index , String name , boolean urlify ,
2010-02-08 19:04:46 +00:00
boolean enabled , boolean ro , String desc , boolean edit ,
2010-02-22 18:17:11 +00:00
boolean showEditButton , boolean showUpdateButton , boolean showStopButton ,
2010-03-29 21:20:48 +00:00
boolean showDeleteButton , boolean showStartButton ) {
2009-08-01 03:28:42 +00:00
buf . append ( " <tr><td class= \" mediumtags \" align= \" right \" width= \" 25% \" > " ) ;
2008-06-16 12:31:14 +00:00
if ( urlify & & enabled ) {
String link = " / " ;
if ( ! RouterConsoleRunner . ROUTERCONSOLE . equals ( name ) )
link + = name + " / " ;
2009-10-26 21:48:46 +00:00
buf . append ( " <a href= \" " ) . append ( link ) . append ( " \" > " ) . append ( _ ( name ) ) . append ( " </a> " ) ;
2010-01-06 21:24:08 +00:00
} else if ( edit & & ! ro ) {
buf . append ( " <input type= \" text \" name= \" name " ) . append ( index ) . append ( " \" value= \" " ) ;
2010-01-18 14:51:39 +00:00
if ( name . length ( ) > 0 )
buf . append ( _ ( name ) ) ;
2010-01-06 21:24:08 +00:00
buf . append ( " \" > " ) ;
2008-06-16 12:31:14 +00:00
} else {
2010-01-18 14:51:39 +00:00
if ( name . length ( ) > 0 )
buf . append ( _ ( name ) ) ;
2008-06-16 12:31:14 +00:00
}
2009-08-01 03:28:42 +00:00
buf . append ( " </td><td align= \" center \" width= \" 10% \" ><input type= \" checkbox \" class= \" optbox \" name= \" " ) . append ( index ) . append ( " .enabled \" value= \" true \" " ) ;
2008-06-16 12:31:14 +00:00
if ( enabled ) {
buf . append ( " checked= \" true \" " ) ;
if ( ro )
buf . append ( " disabled= \" true \" " ) ;
}
2010-02-10 19:09:35 +00:00
buf . append ( " ></td><td align= \" center \" width= \" 15% \" > " ) ;
2010-03-29 21:20:48 +00:00
if ( showStartButton & & ( ! ro ) & & ! edit ) {
2009-10-26 02:21:15 +00:00
buf . append ( " <button type= \" submit \" name= \" action \" value= \" Start " ) . append ( index ) . append ( " \" > " + _ ( " Start " ) + " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
2008-06-20 20:20:50 +00:00
}
2010-02-08 19:04:46 +00:00
if ( showEditButton & & ( ! edit ) & & ! ro )
2010-01-06 21:24:08 +00:00
buf . append ( " <button type= \" submit \" name= \" edit \" value= \" Edit " ) . append ( index ) . append ( " \" > " + _ ( " Edit " ) + " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
2010-02-08 20:57:30 +00:00
if ( showStopButton & & ( ! edit ) )
2010-02-11 21:41:54 +00:00
buf . append ( " <button type= \" submit \" name= \" action \" value= \" Stop " ) . append ( index ) . append ( " \" > " + _ ( " Stop " ) + " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
2010-02-08 19:04:46 +00:00
if ( showUpdateButton & & ( ! edit ) & & ! ro ) {
buf . append ( " <button type= \" submit \" name= \" action \" value= \" Check " ) . append ( index ) . append ( " \" > " + _ ( " Check for updates " ) + " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
buf . append ( " <button type= \" submit \" name= \" action \" value= \" Update " ) . append ( index ) . append ( " \" > " + _ ( " Update " ) + " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
2010-01-06 21:24:08 +00:00
}
2010-02-22 18:17:11 +00:00
if ( showDeleteButton & & ( ! edit ) & & ! ro ) {
buf . append ( " <button type= \" submit \" name= \" action \" value= \" Delete " ) . append ( index )
. append ( " \" onclick= \" if (!confirm(' " )
. append ( _ ( " Are you sure you want to delete {0}? " , _ ( name ) ) )
. append ( " ')) { return false; } \" > " )
. append ( _ ( " Delete " ) ) . append ( " <span class=hide> " ) . append ( index ) . append ( " </span></button> " ) ;
}
2010-01-06 21:24:08 +00:00
buf . append ( " </td><td align= \" left \" width= \" 50% \" > " ) ;
if ( edit & & ! ro ) {
buf . append ( " <input type= \" text \" size= \" 80 \" name= \" desc " ) . append ( index ) . append ( " \" value= \" " ) ;
buf . append ( desc ) ;
buf . append ( " \" > " ) ;
} else {
buf . append ( desc ) ;
}
buf . append ( " </td></tr> \ n " ) ;
2008-06-16 12:31:14 +00:00
}
2010-02-08 23:28:09 +00:00
/ * *
* 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 ;
}
2008-06-16 12:31:14 +00:00
}