X-Git-Url: http://git.megacz.com/?p=org.ibex.xt.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fxt%2FServlet.java;fp=src%2Forg%2Fibex%2Fxt%2FServlet.java;h=38182cae3c29058351e21da2cc4fa99d33703909;hp=69218b2fb092542b400eede184ca28d7f955d374;hb=8f8109c54eee0492842c79ae96ef311bc4d2ebd0;hpb=1db99552ca93981bac63573aabd9ccf3a528c254 diff --git a/src/org/ibex/xt/Servlet.java b/src/org/ibex/xt/Servlet.java index 69218b2..38182ca 100644 --- a/src/org/ibex/xt/Servlet.java +++ b/src/org/ibex/xt/Servlet.java @@ -17,6 +17,7 @@ import org.prevayler.implementation.snapshot.*; public class Servlet extends HttpServlet { + public static final JS METHOD = new JS.Method(); private ServletScope servletscope = null; private String path; private Prevayler prevayler; @@ -43,93 +44,89 @@ public class Servlet extends HttpServlet { servletscope = new ServletScope(request, response, cx); path = cx.getRealPath(((HttpServletRequest)request).getServletPath()); Reader xmlreader = new InputStreamReader(new FileInputStream(path)); - new Template(servletscope, new JSScope(servletscope), xmlreader).wrap(null).toXML(response.getWriter()); + new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(response.getWriter()); } - public class ServletScope extends JSScope { + public class ServletScope extends JS.Obj { HttpServletRequest request; HttpServletResponse response; ServletContext cx; public String getRealPath(String s) { return cx.getRealPath(s); } public ServletScope(ServletRequest request, ServletResponse response, ServletContext cx) { - super(null); + super(); this.request = (HttpServletRequest)request; this.response = (HttpServletResponse)response; this.cx = cx; } - private JS params = new JS() { - public Object get(Object key) { return request.getParameter(JS.toString(key)); } - public Enumeration keys() { return request.getParameterNames(); } + private JS params = new JS.Obj() { + public JS get(JS key) throws JSExn { return JSU.S(request.getParameter(JSU.toString(key))); } + public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getParameterNames()); } }; - private JS cookies = new JS() { + private JS cookies = new JS.Obj() { /* - public Object get(Object key) { return request.getCookie(JS.toString(key)); } + public Object get(Object key) { return request.getCookie(JSU.toString(key)); } public Enumeration keys() { return request.getCookieNames(); } */ }; - private JS sessionAttributes = new JS() { - public Object get(Object key) { return request.getSession(true).getAttribute(JS.toString(key)); } - public void put(Object key, Object val) { - if (val == null) request.getSession(true).removeAttribute(JS.toString(key)); - else request.setAttribute(JS.toString(key), val); } - public Enumeration keys() { return request.getSession(true).getAttributeNames(); } + private JS sessionAttributes = new JS.Obj() { + public JS get(JS key) throws JSExn { return JSU.S(request.getSession(true).getAttribute(JSU.toString(key)).toString()); } + public void put(JS key, JS val) throws JSExn { + if (val == null) request.getSession(true).removeAttribute(JSU.toString(key)); + else request.setAttribute(JSU.toString(key), val); } + public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getSession(true).getAttributeNames()); } }; - private JS requestHeader = new JS() { - public Object get(Object key) { return request.getHeader(JS.toString(key)); } - public Enumeration keys() { return request.getHeaderNames(); } + private JS requestHeader = new JS.Obj() { + public JS get(JS key) throws JSExn { return JSU.S(request.getHeader(JSU.toString(key))); } + public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getHeaderNames()); } }; - private JS responseHeader = new JS() { - public void put(Object key, Object val) { response.setHeader(JS.toString(key), JS.toString(val)); } + private JS responseHeader = new JS.Obj() { + public void put(JS key, JS val) throws JSExn { response.setHeader(JSU.toString(key), JSU.toString(val)); } }; /** lets us put multi-level get/put/call keys all in the same method */ - private class Sub extends JS { - Object key; - Sub(Object key) { this.key = key; } - public void put(Object key, Object val) throws JSExn { - ServletScope.this.put(JS.toString(this.key) + "." + JS.toString(key), val); } - public Object get(Object key) throws JSExn { - return ServletScope.this.get(JS.toString(this.key) + "." + JS.toString(key)); } - public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - return ServletScope.this.callMethod(this.key, a0, a1, a2, rest, nargs); - } - public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - return ServletScope.this.callMethod(JS.toString(this.key) + "." - + JS.toString(method), a0, a1, a2, rest, nargs); + private class Sub extends JS.Obj { + JS key; + Sub(JS key) { this.key = key; } + public void put(JS key, JS val) throws JSExn { + ServletScope.this.put(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key)), val); } + public JS get(JS key) throws JSExn { + return ServletScope.this.get(JSU.S(JSU.toString(this.key) + "." + JSU.toString(key))); } + public JS call(JS method, JS[] args) throws JSExn { + return ServletScope.this.call(JSU.S(JSU.toString(this.key) + "." + JSU.toString(method)), args); } } - private Sub getSub(String key) { return new Sub(key); } + private Sub getSub(String key) { return new Sub(JSU.S(key)); } - public Object callMethod(Object method, final Object a, final Object b, Object c, Object[] rest, int nargs) throws JSExn { - //#switch(method) + public JS call(JS method, JS[] args) throws JSExn { + //#switch(JSU.toString(method)) case "session.invalidate": request.getSession(true).invalidate(); return null; case "context.list": - String path = JS.toString(a); + String path = JSU.toString(args[0]); if (path.indexOf("..") != -1) throw new JSExn("cannot use .. in paths"); File f = new File(cx.getRealPath("/") + File.separatorChar + path); if (!f.isDirectory()) return null; String[] contents = f.list(); JSArray ret = new JSArray(contents.length); - for(int i=0; i