New code to find out which action to call. From now on, every

function ending with "_action" may be invoked via HTTP. It
isn't necessary anymore to define the action in a .hac file.

As a consequence, it's not possible anymore to invoke
templates (.hsp files) via HTTP. Thus, the exposeTemplates
option has become obsolete.

Implemented onRequest() function which is called on the
object handling the HTTP request before the actual action
is invoked.
This commit is contained in:
hns 2002-01-30 15:37:35 +00:00
parent a132619c8d
commit 896e5e1854

View file

@ -184,7 +184,7 @@ public class RequestEvaluator implements Runnable {
localrtx.timer.beginEvent (requestPath+" init");
localrtx.begin (requestPath);
Action action = null;
String action = null;
root = app.getDataRoot ();
@ -220,7 +220,7 @@ public class RequestEvaluator implements Runnable {
reqPath.putHiddenProperty ("root", current);
Prototype p = app.getPrototype (root);
String errorAction = app.props.getProperty ("error", "error");
action = p.getActionOrTemplate (errorAction);
action = getAction (current, errorAction);
if (action == null)
throw new RuntimeException (error);
@ -230,7 +230,7 @@ public class RequestEvaluator implements Runnable {
reqPath.putProperty (0, current);
reqPath.putHiddenProperty ("root", current);
Prototype p = app.getPrototype (root);
action = p.getActionOrTemplate (null);
action = getAction (current, null);
if (action == null)
throw new FrameworkException ("Action not found");
@ -277,10 +277,13 @@ public class RequestEvaluator implements Runnable {
}
} else {
// if we're at the last element of the path,
// try to interpret it as action name.
if (i == ntokens-1) {
Prototype p = app.getPrototype (currentElement);
if (p != null)
action = p.getActionOrTemplate (pathItems[i]);
action = getAction (current, pathItems[i]);
}
if (action == null) {
@ -313,7 +316,7 @@ public class RequestEvaluator implements Runnable {
if (action == null) {
Prototype p = app.getPrototype (currentElement);
if (p != null)
action = p.getActionOrTemplate (null);
action = getAction (current, null);
}
if (action == null)
@ -330,10 +333,10 @@ public class RequestEvaluator implements Runnable {
res.status = 404;
String notFoundAction = app.props.getProperty ("notFound", "notfound");
Prototype p = app.getPrototype (root);
action = p.getActionOrTemplate (notFoundAction);
current = getElementWrapper (root);
action = getAction (current, notFoundAction);
if (action == null)
throw new FrameworkException (notfound.getMessage ());
current = getElementWrapper (root);
}
localrtx.timer.endEvent (requestPath+" init");
@ -342,9 +345,17 @@ public class RequestEvaluator implements Runnable {
localrtx.timer.beginEvent (requestPath+" execute");
// set the req.action property
req.action = action.getName ();
req.action = action;
// try calling onRequest() function on object before
// calling the actual action
try {
current.doIndirectCall (evaluator, current, "onRequest", new ESValue[0]);
} catch (Exception ignore) {
System.err.println ("error in onRequest(): "+ignore);
// onRequest not defined
}
// do the actual action invocation
current.doIndirectCall (evaluator, current, action.getFunctionName (), new ESValue[0]);
current.doIndirectCall (evaluator, current, action, new ESValue[0]);
// check if the script set the name of a skin to render in res.skin
if (res.skin != null) {
@ -607,6 +618,16 @@ public class RequestEvaluator implements Runnable {
Transactor localrtx = (Transactor) Thread.currentThread ();
if (reqtype != NONE)
return; // is there a new request already?
// clear/reset global vars
/* for (Enumeration en = global.getProperties(); en.hasMoreElements(); ) {
Object obj = en.nextElement ();
try {
global.deleteProperty(obj.toString(), obj.hashCode());
} catch (Exception x) {}
// System.err.println (">>> "+obj);
} */
notifyAll ();
try {
// wait for request, max 30 min
@ -881,6 +902,21 @@ public class RequestEvaluator implements Runnable {
v.copyInto (skinmanagers);
}
/**
* Check if an action with a given name is defined for a scripted object. If it is,
* return the action's function name. Otherwise, return null.
*/
public String getAction (ESObject obj, String action) {
if (obj == null)
return null;
String act = action == null ? "main_action" : action+"_action";
try {
ESValue esv = obj.getProperty (act, act.hashCode());
if (esv != null && esv instanceof FunctionPrototype)
return act;
} catch (EcmaScriptException notfound) {}
return null;
}
/**
* Returns a node wrapper only if it already exists in the cache table. This is used