r9605 - helma-ng/trunk/src/org/helma/jack

[email protected]
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090411150319.14E9D3D0D6@mia>
Author: hannes
Date: 2009-04-11 17:03:18 +0200 (Sat, 11 Apr 2009)
New Revision: 9605

Modified:
   helma-ng/trunk/src/org/helma/jack/JackEnv.java
Log:
Change property definition scheme in Jack env object to make all properties enumerable.

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

Modified: helma-ng/trunk/src/org/helma/jack/JackEnv.java
===================================================================
--- helma-ng/trunk/src/org/helma/jack/JackEnv.java	2009-04-11 14:18:06 UTC (rev 9604)
+++ helma-ng/trunk/src/org/helma/jack/JackEnv.java	2009-04-11 15:03:18 UTC (rev 9605)
@@ -21,6 +21,7 @@
 import javax.servlet.http.HttpServletRequest;
 import java.util.Enumeration;
 import java.io.IOException;
+import java.lang.reflect.Method;
 
 public class JackEnv extends ScriptableObject {
 
@@ -44,48 +45,82 @@
         }
     }
 
-    public String jsGet_SCRIPT_NAME() {
+
+    public String getScriptName() {
         return req.getServletPath();
     }
 
-    public String jsGet_PATH_INFO() {
+    public String getPathInfo() {
         return req.getPathInfo();
     }
 
-    public String jsGet_REQUEST_METHOD() {
+    public String getRequestMethod() {
         return req.getMethod();
     }
 
-    public String jsGet_SERVER_NAME() {
+    public String getServerName() {
         return req.getServerName();
     }
     
-    public String jsGet_SERVER_PORT() {
+    public String getServerPort() {
         return Integer.toString(req.getServerPort());
     }
 
-    public String jsGet_QUERY_STRING() {
+    public String getQueryString() {
         return req.getQueryString();
     }
 
-    public String jsGet_HTTP_VERSION() {
+    public String getHttpVersion() {
         return req.getProtocol();
     }
 
-    public String jsGet_REMOTE_HOST() {
+    public String getRemoteHost() {
         return req.getRemoteHost();
     }
 
-    public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) {
-        int flags = DONTENUM | READONLY | PERMANENT;
+    public String getUrlScheme() {
+        return req.isSecure() ? "https" : "http";
+    }
+
+    public Object getInputStream() {
+        try {
+            return req.getInputStream();
+        } catch (IOException iox) {
+            return Undefined.instance;
+        }
+    }
+
+    public Object getErrorStream() {
+        return System.err;
+    }
+
+    public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype)
+            throws NoSuchMethodException {
+        int flags = READONLY | PERMANENT;
         Context cx = Context.getCurrentContext();
+        ScriptableObject proto = (ScriptableObject) prototype;
+        proto.defineProperty("SCRIPT_NAME", null, getMethod("getScriptName"), null, flags);
+        proto.defineProperty("PATH_INFO", null, getMethod("getPathInfo"), null, flags);
+        proto.defineProperty("REQUEST_METHOD", null, getMethod("getRequestMethod"), null, flags);
+        proto.defineProperty("SERVER_NAME", null, getMethod("getServerName"), null, flags);
+        proto.defineProperty("SERVER_PORT", null, getMethod("getServerPort"), null, flags);
+        proto.defineProperty("QUERY_STRING", null, getMethod("getQueryString"), null, flags);
+        proto.defineProperty("HTTP_VERSION", null, getMethod("getHttpVersion"), null, flags);
+        proto.defineProperty("REMOTE_HOST", null, getMethod("getRemoteHost"), null, flags);
+        proto.defineProperty("jack.input", null, getMethod("getInputStream"), null, flags);
+        proto.defineProperty("jack.error", null, getMethod("getErrorStream"), null, flags);
         Scriptable version = cx.newArray(scope, new Object[] {new Integer(0), new Integer(1)});
-        ScriptableObject.defineProperty(prototype, "jack.version", version, flags);
-        ScriptableObject.defineProperty(prototype, "jack.multithread", Boolean.TRUE, flags);
-        ScriptableObject.defineProperty(prototype, "jack.multiprocess", Boolean.TRUE, flags);
-        ScriptableObject.defineProperty(prototype, "jack.run_once", Boolean.FALSE, flags);
+        ScriptableObject.defineProperty(proto, "jack.version", version, flags);
+        ScriptableObject.defineProperty(proto, "jack.multithread", Boolean.TRUE, flags);
+        ScriptableObject.defineProperty(proto, "jack.multiprocess", Boolean.TRUE, flags);
+        ScriptableObject.defineProperty(proto, "jack.run_once", Boolean.FALSE, flags);
+        proto.defineProperty("jack.url_scheme", null, getMethod("getUrlScheme"), null, flags);
     }
 
+    private static Method getMethod(String name) throws NoSuchMethodException {
+        return JackEnv.class.getDeclaredMethod(name);
+    }
+
     @Override
     public Object get(String name, Scriptable start) {
         // FIXME: implement IO wrappers
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.