Author: hannes
Date: 2009-01-14 12:43:48 +0100 (Wed, 14 Jan 2009)
New Revision: 9479
Added:
helma-ng/trunk/src/org/helma/util/ParameterMap.java
Modified:
helma-ng/trunk/apps/demo/actions.js
helma-ng/trunk/modules/helma/webapp/request.js
helma-ng/trunk/src/org/helma/web/Request.java
Log:
Bring over ParameterMap class from Helma 1 to implement parameter handling in Java. Use ParameterMap for req.params and
req.cookies. Start throwing out superfluous methods and properties from Request.java, as these are covered by the
ParameterMap.
Details at http://dev.helma.org/trac/helma/changeset/9479
Modified: helma-ng/trunk/apps/demo/actions.js
===================================================================
--- helma-ng/trunk/apps/demo/actions.js 2009-01-14 11:39:17 UTC (rev 9478)
+++ helma-ng/trunk/apps/demo/actions.js 2009-01-14 11:43:48 UTC (rev 9479)
@@ -6,6 +6,7 @@
function index_action(req, res) {
res.render('skins/index.html', { title: 'Welcome to Helma NG' });
res.debug(req.cookies.toSource());
+ res.debug(req.params.toSource());
res.flushDebug();
}
Modified: helma-ng/trunk/modules/helma/webapp/request.js
===================================================================
--- helma-ng/trunk/modules/helma/webapp/request.js 2009-01-14 11:39:17 UTC (rev 9478)
+++ helma-ng/trunk/modules/helma/webapp/request.js 2009-01-14 11:43:48 UTC (rev 9479)
@@ -24,6 +24,28 @@
};
}
+ function ParameterGroup(path, map) {
+
+ for (var i in map) {
+ if (i.startsWith(path)) {
+ var dot = i.indexOf('.', path.length);
+ var key, value;
+ if (dot > -1) {
+ key = i.slice(path.length, dot);
+ value = new ParameterGroup(i.slice(0, dot + 1), map);
+ } else {
+ key = i.slice(path.length);
+ value = map[i];
+ }
+ if (!this.hasOwnProperty(key)) {
+ this[key] = value[0];
+ }
+ }
+ }
+
+ return this;
+ }
+
/**
* Return true if this is a HTTP POST request.
*/
@@ -38,7 +60,7 @@
return this.method == "GET";
}
- this.__defineGetter__("cookies", cachedGetter('cookies',
+ /* this.__defineGetter__("cookies", cachedGetter('cookies',
function() {
var cookies = {};
for each (var cookie in this.getCookies()) {
@@ -46,37 +68,16 @@
}
return cookies;
})
- );
+ ); */
- this.__defineGetter__("params", cachedGetter('params', function() {
+ /* this.__defineGetter__("params", cachedGetter('params', function() {
return new ParameterGroup("", this.getParameterMap());
})
- );
+ ); */
this.__defineGetter__("data", cachedGetter('data', function() {
return new ParameterGroup("", this.getParameterMap());
})
);
- function ParameterGroup(path, map) {
-
- for (var i in map) {
- if (i.startsWith(path)) {
- var dot = i.indexOf('.', path.length);
- var key, value;
- if (dot > -1) {
- key = i.slice(path.length, dot);
- value = new ParameterGroup(i.slice(0, dot + 1), map);
- } else {
- key = i.slice(path.length);
- value = map[i];
- }
- if (!this.hasOwnProperty(key)) {
- this[key] = value[0];
- }
- }
- }
-
- return this;
- }
}).apply(Request.prototype);
Added: helma-ng/trunk/src/org/helma/util/ParameterMap.java
Modified: helma-ng/trunk/src/org/helma/web/Request.java
===================================================================
--- helma-ng/trunk/src/org/helma/web/Request.java 2009-01-14 11:39:17 UTC (rev 9478)
+++ helma-ng/trunk/src/org/helma/web/Request.java 2009-01-14 11:43:48 UTC (rev 9479)
@@ -18,6 +18,7 @@
import org.helma.util.CaseInsensitiveMap;
import org.helma.util.ScriptableMap;
+import org.helma.util.ParameterMap;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
@@ -36,9 +37,9 @@
*/
public class Request extends ScriptableObject {
- HttpServletRequest request;
- Session session;
- Scriptable cookies, data, params, headers, attributes;
+ private HttpServletRequest request;
+ private Session session;
+ private Scriptable cookies, data, params, headers, attributes;
private static final long serialVersionUID = -2167096504665220425L;
public Request() {
@@ -234,37 +235,29 @@
}
/**
- * Returns the value of a request parameter as a String, or null if the parameter does not exist.
- * You should only use this method when you are sure the parameter has only one value. If the
- * parameter might have more than one value, use *getParameters(String)*.
- *
- * Request parameters are extra information sent with the request either in the
- * query string or posted form data.
- *
- * @param name the parameter name
- * @return the paremeter value
- */
- public String jsFunction_getParameter(String name) {
- return request.getParameter(name);
- }
-
- /**
* Return the parameter map wrapped as scriptable object.
* @return the parameter map, wrapped as scriptable object.
*/
- public Object jsFunction_getParameterMap() {
- return new ScriptableMap(getParentScope(), request.getParameterMap());
+ public Object jsGet_params() {
+ if (params == null) {
+ params = new ScriptableMap(getParentScope(),
+ new ParameterMap(request.getParameterMap()));
+ }
+ return params;
}
- /**
- * Returns an array of Strings containing all of the values the given request parameter has,
- * or null if the parameter does not exist.
- *
- * @param name the parameter name
- * @return the parameter values
- */
- public Object jsFunction_getParameters(String name) {
- return getJsArray(request.getParameterValues(name));
+ public Object jsGet_cookies() {
+ if (cookies == null) {
+ Cookie[] cookieArray = request.getCookies();
+ ParameterMap cookieMap = new ParameterMap();
+ if (cookieArray != null) {
+ for (Cookie cookie : cookieArray) {
+ cookieMap.put(cookie.getName(), cookie);
+ }
+ }
+ cookies = new ScriptableMap(getParentScope(), cookieMap);
+ }
+ return cookies;
}
/**
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.