2002-03-11 13:49:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduler function, runs global.appStat every minute
|
|
|
|
*/
|
2002-05-31 14:14:37 +00:00
|
|
|
function scheduler() {
|
2002-03-11 13:49:50 +00:00
|
|
|
appStat();
|
|
|
|
return 60000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-11-21 18:36:03 +00:00
|
|
|
* initializes app.data.stat storage on startup,
|
2002-05-31 14:14:37 +00:00
|
|
|
* creates app.data.addressFilter
|
2002-03-11 13:49:50 +00:00
|
|
|
*/
|
2002-05-31 14:14:37 +00:00
|
|
|
function onStart() {
|
|
|
|
app.data.addressFilter = createAddressFilter();
|
2002-11-22 11:58:37 +00:00
|
|
|
app.data.addressString = root.getProperty ("allowadmin");
|
2002-03-25 17:56:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initializes addressFilter from app.properties,
|
|
|
|
* hostnames are converted, wildcards are only allowed in ip-addresses
|
|
|
|
* (so, no network-names, sorry)
|
|
|
|
*/
|
|
|
|
function createAddressFilter() {
|
|
|
|
var filter = new Packages.helma.util.InetAddressFilter();
|
2002-03-11 13:49:50 +00:00
|
|
|
var str = root.getProperty("allowadmin");
|
|
|
|
if ( str!=null && str!="" ) {
|
|
|
|
var arr = str.split(",");
|
|
|
|
for ( var i in arr ) {
|
|
|
|
var str = new java.lang.String(arr[i]);
|
2003-06-23 15:03:34 +00:00
|
|
|
try {
|
|
|
|
filter.addAddress(str.trim());
|
|
|
|
} catch (a) {
|
|
|
|
try {
|
|
|
|
var str = java.net.InetAddress.getByName(str.trim()).getHostAddress();
|
|
|
|
filter.addAddress (str);
|
|
|
|
} catch (b) {
|
|
|
|
app.log ("error using address " + arr[i] + ": " + b);
|
|
|
|
}
|
|
|
|
}
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
2002-03-25 17:56:26 +00:00
|
|
|
} else {
|
2002-05-31 14:14:37 +00:00
|
|
|
app.log("no addresses allowed for app manage, all access will be denied");
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
2002-03-25 17:56:26 +00:00
|
|
|
return filter;
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-11-21 18:36:03 +00:00
|
|
|
* updates the stats in app.data.stat every 5 minutes
|
2002-03-11 13:49:50 +00:00
|
|
|
*/
|
2002-11-21 18:36:03 +00:00
|
|
|
function appStat () {
|
|
|
|
if (app.data.stat==null)
|
|
|
|
app.data.stat = new HopObject ();
|
|
|
|
if ((new Date()-300000) < app.data.stat.lastRun)
|
2002-03-11 13:49:50 +00:00
|
|
|
return;
|
2002-11-21 18:36:03 +00:00
|
|
|
var arr = root.getApplications ();
|
|
|
|
for (var i=0; i<arr.length; i++) {
|
|
|
|
var tmp = app.data.stat.get (arr[i].getName());
|
|
|
|
if (tmp==null) {
|
2002-03-11 13:49:50 +00:00
|
|
|
tmp = new HopObject();
|
2002-11-21 18:36:03 +00:00
|
|
|
tmp.lastTotalRequestCount = 0;
|
|
|
|
tmp.lastTotalErrorCount = 0;
|
|
|
|
}
|
|
|
|
tmp.requestCount = arr[i].getRequestCount () - tmp.lastTotalRequestCount;
|
|
|
|
tmp.lastTotalRequestCount = arr[i].getRequestCount ();
|
|
|
|
tmp.errorCount = arr[i].getErrorCount () - tmp.lastTotalErrorCount;
|
|
|
|
tmp.lastTotalErrorCount = arr[i].getErrorCount ();
|
|
|
|
app.data.stat.set (arr[i].getName(), tmp);
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
2002-11-21 18:36:03 +00:00
|
|
|
app.data.stat.lastRun = new Date();
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* utility function to sort object-arrays by name
|
|
|
|
*/
|
|
|
|
function sortByName(a,b) {
|
2002-11-22 11:58:37 +00:00
|
|
|
if (a.name > b.name)
|
2002-03-11 13:49:50 +00:00
|
|
|
return 1;
|
2002-11-22 11:58:37 +00:00
|
|
|
else if (a.name == b.name)
|
2002-03-11 13:49:50 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* utility function to sort property-arrays by key
|
|
|
|
*/
|
|
|
|
function sortProps(a,b) {
|
|
|
|
if ( a>b )
|
|
|
|
return 1;
|
|
|
|
else if ( a==b )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check access to an application or the whole server, authenticate against md5-encrypted
|
|
|
|
* properties of base-app or the particular application. if username or password aren't set
|
|
|
|
* go into stealth-mode and return a 404. if username|password are wrong, prepare response-
|
|
|
|
* object for http-auth and return false.
|
2002-03-26 16:10:28 +00:00
|
|
|
* @arg appObj application object to check against (if adminUsername etc are set in app.properties)
|
2002-03-11 13:49:50 +00:00
|
|
|
*/
|
|
|
|
function checkAuth(appObj) {
|
|
|
|
var ok = false;
|
|
|
|
|
|
|
|
// check against root
|
2002-11-22 14:31:28 +00:00
|
|
|
var adminAccess = root.getProperty("adminAccess");
|
2002-03-11 13:49:50 +00:00
|
|
|
|
2002-11-22 14:31:28 +00:00
|
|
|
if (adminAccess==null || adminAccess=="") {
|
2002-12-04 09:21:40 +00:00
|
|
|
res.redirect (root.href ("makekey"));
|
2002-03-25 17:56:26 +00:00
|
|
|
}
|
2002-03-11 13:49:50 +00:00
|
|
|
|
2002-05-31 14:14:37 +00:00
|
|
|
var uname = req.username;
|
|
|
|
var pwd = req.password;
|
2002-03-11 13:49:50 +00:00
|
|
|
|
|
|
|
if ( uname==null || uname=="" || pwd==null || pwd=="" )
|
|
|
|
return forceAuth();
|
|
|
|
|
2002-11-22 14:31:28 +00:00
|
|
|
var md5key = Packages.helma.util.MD5Encoder.encode(uname + "-" + pwd);
|
2002-03-11 13:49:50 +00:00
|
|
|
|
2002-11-22 14:31:28 +00:00
|
|
|
if (md5key==adminAccess)
|
2002-03-11 13:49:50 +00:00
|
|
|
return true;
|
|
|
|
|
2002-11-22 11:58:37 +00:00
|
|
|
if (appObj!=null && appObj.isActive()) {
|
2002-03-11 13:49:50 +00:00
|
|
|
// check against application
|
|
|
|
var appUsername = appObj.getProperty("adminusername");
|
|
|
|
var appPassword = appObj.getProperty("adminpassword");
|
|
|
|
if ( md5username==appUsername && md5password==appPassword )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return forceAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2002-07-16 14:45:34 +00:00
|
|
|
* check access to the manage-app by ip-addresses
|
2002-03-11 13:49:50 +00:00
|
|
|
*/
|
|
|
|
function checkAddress() {
|
2002-11-22 11:58:37 +00:00
|
|
|
// if allowadmin value in server.properties has changed,
|
|
|
|
// re-construct the addressFilter
|
|
|
|
if (app.data.addressString != root.getProperty ("allowadmin")){
|
|
|
|
app.data.addressFilter = createAddressFilter();
|
|
|
|
app.data.addressString = root.getProperty ("allowadmin");
|
|
|
|
}
|
2002-05-31 14:14:37 +00:00
|
|
|
if ( !app.data.addressFilter.matches(java.net.InetAddress.getByName(req.data.http_remotehost)) ) {
|
|
|
|
app.log("denied request from " + req.data.http_remotehost );
|
2002-07-16 14:45:34 +00:00
|
|
|
// forceStealth seems a bit like overkill here.
|
|
|
|
// display a message that the ip address must be added to server.properties
|
2002-11-21 18:36:03 +00:00
|
|
|
res.write ("Access from address "+req.data.http_remotehost+" denied.");
|
2002-07-16 14:45:34 +00:00
|
|
|
return false;
|
2002-03-25 17:56:26 +00:00
|
|
|
} else {
|
2002-03-11 13:49:50 +00:00
|
|
|
return true;
|
2002-03-25 17:56:26 +00:00
|
|
|
}
|
2002-03-11 13:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* response is reset to 401 / authorization required
|
2002-03-26 16:10:28 +00:00
|
|
|
* @arg realm realm for http-auth
|
2002-03-11 13:49:50 +00:00
|
|
|
*/
|
2002-03-26 16:10:28 +00:00
|
|
|
function forceAuth(realm) {
|
2002-03-11 13:49:50 +00:00
|
|
|
res.reset();
|
2002-03-26 16:10:28 +00:00
|
|
|
res.status = 401;
|
|
|
|
res.realm = (realm!=null) ? realm : "helma";
|
2002-03-11 13:49:50 +00:00
|
|
|
res.write ("Authorization Required. The server could not verify that you are authorized to access the requested page.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-11-21 18:36:03 +00:00
|
|
|
|
2002-03-11 13:49:50 +00:00
|
|
|
|
2002-06-25 14:04:29 +00:00
|
|
|
/**
|
|
|
|
* macro-utility: formatting property lists
|
|
|
|
*/
|
|
|
|
function formatProperties(props,par) {
|
|
|
|
if ( props.size()==0 )
|
|
|
|
return "";
|
|
|
|
var e = props.keys();
|
|
|
|
var arr = new Array();
|
|
|
|
while ( e.hasMoreElements() ) {
|
|
|
|
arr[arr.length] = e.nextElement();
|
|
|
|
}
|
|
|
|
arr.sort(sortProps);
|
|
|
|
for ( var i in arr ) {
|
|
|
|
// don't print the admin-password
|
|
|
|
if ( arr[i].toLowerCase()=="adminusername" || arr[i].toLowerCase()=="adminpassword" ) continue;
|
2002-10-26 11:07:50 +00:00
|
|
|
res.write ( par.itemprefix + arr[i] + par.separator + props.getProperty(arr[i]) + par.itemsuffix );
|
2002-06-25 14:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* macro-utility: formatting an integer value as human readable bytes
|
|
|
|
*/
|
|
|
|
function formatBytes(bytes) {
|
|
|
|
if ( bytes > Math.pow(2,30) ) {
|
|
|
|
res.write( Math.round( 100*bytes/Math.pow(2,30) ) / 100 + "gb" );
|
|
|
|
} else if ( bytes > Math.pow(2,20) ) {
|
|
|
|
res.write( Math.round( 100*bytes/Math.pow(2,20) ) / 100 + "mb" );
|
|
|
|
} else {
|
|
|
|
res.write( Math.round( 100*bytes/Math.pow(2,10) ) / 100 + "kb" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* macro-utility: formatting time in millis as human readable age
|
|
|
|
*/
|
|
|
|
function formatAge(age) {
|
|
|
|
var str = "";
|
|
|
|
var days = Math.floor(age/86400);
|
|
|
|
var age = age - days * 86400;
|
|
|
|
var hours = Math.floor(age / 3600);
|
|
|
|
var age = age - hours * 3600;
|
|
|
|
var minutes = Math.floor(age / 60);
|
|
|
|
var seconds = Math.floor(age - minutes * 60);
|
|
|
|
if (days > 0)
|
|
|
|
str += (days + " days, ");
|
|
|
|
if (hours>0)
|
|
|
|
str += (hours + "h, ");
|
|
|
|
str += (minutes + "min");
|
|
|
|
if (days == 0) str += (", " + seconds + "sec");
|
|
|
|
return(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* macro-utility: formatting a number-suffix by choosing between singular and plural
|
|
|
|
* @arg value number to be formatted
|
|
|
|
* @arg param-object object to get fields
|
|
|
|
* @param singular string used for value==1
|
|
|
|
* @param plural string used for value!=1
|
|
|
|
*/
|
|
|
|
function formatCount(ct, par) {
|
|
|
|
if ( !par || !par.singular || !par.plural ) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if ( ct==1 )
|
|
|
|
return par.singular;
|
|
|
|
else
|
|
|
|
return par.plural;
|
|
|
|
}
|
2002-11-21 18:36:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tries to make out if this server is running linux from java's system properties
|
|
|
|
*/
|
|
|
|
function isLinux () {
|
|
|
|
var str = java.lang.System.getProperty("os.name");
|
|
|
|
return (str!=null && str.toLowerCase().indexOf("linux")!=-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|