r9411 - in helma-ng/trunk: modules/helma/webapp src/org/helma/web

[email protected]
Newsgroups gmane.comp.java.helma.cvs
Message-ID <[email protected]>
Author: hannes
Date: 2008-12-11 14:26:52 +0100 (Thu, 11 Dec 2008)
New Revision: 9411

Modified:
   helma-ng/trunk/modules/helma/webapp/request.js
   helma-ng/trunk/modules/helma/webapp/response.js
   helma-ng/trunk/src/org/helma/web/Request.java
Log:
Find better way to implement cached getters for Request object. Move Request.cookies getter from Java to JavaScript.

Details at http://dev.helma.org/trac/helma/changeset/9411

Modified: helma-ng/trunk/modules/helma/webapp/request.js
===================================================================
--- helma-ng/trunk/modules/helma/webapp/request.js	2008-12-10 21:48:13 UTC (rev 9410)
+++ helma-ng/trunk/modules/helma/webapp/request.js	2008-12-11 13:26:52 UTC (rev 9411)
@@ -6,69 +6,77 @@
 
 var log = require('helma.logging').getLogger(__name__);
 
-/**
- * Return true if this is a HTTP POST request.
- */
-Request.prototype.isPost = function() {
-    return this.method == "POST";
-}
+(function() {
 
-/**
- * Return true if this is a HTTP GET request.
- */
-Request.prototype.isGet = function() {
-    return this.method == "GET";
-}
+    function cachedGetter(name, fn) {
+        return function() {
+            // make sure to never set anything on request prototype
+            if (this != Request.prototype) {
+                var cache = this.__property_cache__;
+                if (!cache) {
+                    cache = this.__property_cache__ = {};
+                }
+                if (!cache[name]) {
+                    cache[name] = fn.apply(this);
+                }
+                return cache[name];
+            }
+        };
+    }
 
-/* Object.defineProperty(Request.prototype, "cookies", {
-    getter: function() {
-        if (!this._cookies) {
-            this._cookies = {};
-            var cookies = this.getCookies();
-            for each (var cookie in cookies) {
+    /**
+     * Return true if this is a HTTP POST request.
+     */
+    this.isPost = function() {
+        return this.method == "POST";
+    }
 
-            }
-        }
-        return this._cookies;
+    /**
+     * Return true if this is a HTTP GET request.
+     */
+    this.isGet = function() {
+        return this.method == "GET";
     }
-}); */
 
-if (!Request.prototype.hasOwnProperty("params")) {
-    Object.defineProperty(Request.prototype, "params", {
-        getter: function() {
+    this.__defineGetter__("cookies", cachedGetter('cookies',
+        function() {
+            var cookies = {};
+            for each (var cookie in this.getCookies()) {
+                cookies[cookie.name] = cookie.value;
+            }
+            return cookies;
+        })
+    );
+
+    this.__defineGetter__("params", cachedGetter('params', function() {
             return new ParameterGroup("", this.getParameterMap());
-        }
-    });
-}
+        })
+    );
 
-if (!Request.prototype.hasOwnProperty("data")) {
-    Object.defineProperty(Request.prototype, "data", {
-        getter: function() {
+    this.__defineGetter__("data", cachedGetter('data', function() {
             return new ParameterGroup("", this.getParameterMap());
-        }
-    });
-}
+        })
+    );
+ 
+    function ParameterGroup(path, map) {
 
-function ParameterGroup(path, map) {
-
-    map = map || this.getParameterMap();
-
-    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];
+        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];
+                }
             }
-            if (!this.hasOwnProperty(key)) {
-                this[key] = value[0];
-            }
         }
+
+        return this;
     }
-
-    return this;
-}
\ No newline at end of file
+}).apply(Request.prototype);

Modified: helma-ng/trunk/modules/helma/webapp/response.js
===================================================================
--- helma-ng/trunk/modules/helma/webapp/response.js	2008-12-10 21:48:13 UTC (rev 9410)
+++ helma-ng/trunk/modules/helma/webapp/response.js	2008-12-11 13:26:52 UTC (rev 9411)
@@ -1,3 +1,4 @@
+include('helma.buffer');
 import('helma.system', 'system');
 
 system.addHostObject(org.helma.web.Response);
@@ -2,41 +3,45 @@
 
-/**
- * Render a skin to the response's buffer
- * @param skin
- * @param context
- * @param scope
- */
-Response.prototype.render = function render(skin, context, scope) {
-    var render = require('helma.skin').render;
-    this.write(render(skin, context, scope));
-}
+(function() {
 
-/**
- * Print a debug message to the rendered page.
- */
-Response.prototype.debug = function debug() {
-    var buffer = this.debugBuffer || new Buffer();
-    buffer.write("<div class=\"helma-debug-line\" style=\"background: yellow;");
-    buffer.write("color: black; border-top: 1px solid black;\">");
-    var length = arguments.length;
-    for (var i = 0; i < length; i++) {
-        buffer.write(arguments[i]);
-        if (i < length - 1) {
-            buffer.write(" ");
-        }
+    /**
+     * Render a skin to the response's buffer
+     * @param skin
+     * @param context
+     * @param scope
+     */
+    this.render = function render(skin, context, scope) {
+        var render = require('helma.skin').render;
+        this.write(render(skin, context, scope));
     }
-    buffer.writeln("</div>");
-    this.debugBuffer = buffer;
-    return null;
-};
 
-/**
- * Write the debug buffer to the response's main buffer.
- */
-Response.prototype.flushDebug = function() {
-    if (this.debugBuffer != null) {
-        this.write(this.debugBuffer);
-        this.debugBuffer.reset();
-    }
-    return null;
-};
+    /**
+     * Print a debug message to the rendered page.
+     */
+    this.debug = function debug() {
+        var buffer = this.debugBuffer || new Buffer();
+        buffer.write("<div class=\"helma-debug-line\" style=\"background: yellow;");
+        buffer.write("color: black; border-top: 1px solid black;\">");
+        var length = arguments.length;
+        for (var i = 0; i < length; i++) {
+            buffer.write(arguments[i]);
+            if (i < length - 1) {
+                buffer.write(" ");
+            }
+        }
+        buffer.writeln("</div>");
+        this.debugBuffer = buffer;
+        return null;
+    };
+
+    /**
+     * Write the debug buffer to the response's main buffer.
+     */
+    this.flushDebug = function() {
+        if (this.debugBuffer != null) {
+            this.write(this.debugBuffer);
+            this.debugBuffer.reset();
+        }
+        return null;
+    };
+
+}).apply(Response.prototype);

Modified: helma-ng/trunk/src/org/helma/web/Request.java
===================================================================
--- helma-ng/trunk/src/org/helma/web/Request.java	2008-12-10 21:48:13 UTC (rev 9410)
+++ helma-ng/trunk/src/org/helma/web/Request.java	2008-12-11 13:26:52 UTC (rev 9411)
@@ -279,27 +279,6 @@
         return getJsArray(request.getCookies());
     }
 
-    /**
-     * A JavaScript object reflecting the request's HTTP cookies. The object
-     * contains the cookie names as property names and the cookie values as property
-     * values. To access other cookie properties such as max-age, domain, or cookie path,
-     * and to access multiple cookies with the same name, use *req.getCookies()|getCookies()*,
-     * which returns a JavaScript arrays containing the raw
-     * *cookie objects|http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/Cookie.html*.
-     * @deprecated
-     * @return a JavaScript object containing the request's HTTP cookies
-     */
-    public Object jsGet_cookies() {
-        if (cookies == null) {
-            Map<String, String> cookieMap = new CaseInsensitiveMap<String, String>();
-            for (Cookie cookie: request.getCookies()) {
-                cookieMap.put(cookie.getName(), cookie.getValue());
-            }
-            cookies = new ScriptableMap(getParentScope(), cookieMap);
-        }
-        return cookies;
-    }
-
     public String getClassName() {
         return "Request";
     }
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.