X-Git-Url: http://git.megacz.com/?p=org.ibex.xt.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fxt%2FServlet.java;h=0265e23d11b72d95b188321332a06a4ee1dc65ce;hp=be36ac86ce2c79b5b0e4157e3522bd8877e00e53;hb=e8f5044051de70a1cdad65df9c5ed6bff3cd1a08;hpb=5365f47787b1b4eeca31ad5da2373237371e264e diff --git a/src/org/ibex/xt/Servlet.java b/src/org/ibex/xt/Servlet.java index be36ac8..0265e23 100644 --- a/src/org/ibex/xt/Servlet.java +++ b/src/org/ibex/xt/Servlet.java @@ -1,3 +1,7 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.xt; import org.ibex.js.*; import org.ibex.util.*; @@ -13,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; @@ -31,6 +36,7 @@ public class Servlet extends HttpServlet { public void init(ServletConfig sc) throws ServletException { cx = sc.getServletContext(); prevayler = Prevalence.getPrevayler(cx); + prevalent = (JS)prevayler.prevalentSystem(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { doGet(request, response); } @@ -38,129 +44,101 @@ 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()); + Writer w = response.getWriter(); + new Template(servletscope, servletscope, xmlreader).wrap(null).toXML(w); + w.flush(); } - public class ServletScope extends JSScope { + // ServletScope ////////////////////////////////////////////////////////////////////////////// + + public class ServletScope extends JSSubProperties { HttpServletRequest request; HttpServletResponse response; + HttpSession session; 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.session = this.request.getSession(); 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 cookies = new JS() { - /* - public Object get(Object key) { return request.getCookie(JS.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 requestHeader = new JS() { - public Object get(Object key) { return request.getHeader(JS.toString(key)); } - public Enumeration keys() { return request.getHeaderNames(); } - }; - private JS responseHeader = new JS() { - public void put(Object key, Object val) { response.setHeader(JS.toString(key), JS.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 Sub getSub(String key) { return new Sub(key); } - public Object callMethod(Object method, final Object a, final Object b, Object c, Object[] rest, int nargs) throws JSExn { - //#switch(method) - case "prevalent.query": - try { - return prevayler.execute(new Prevalence.JSQuery(JS.cloneWithNewParentScope((JS)a, null))); - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); } + // FIXME: setattributes, cookies + //#repeat params/requestHeader/attributes \ + // getParameter/getHeader/getAttribute \ + // getParameterNames/getHeaderNames/getAttributeNames \ + // request/request/session/response \ + // setAttribute/setAttribute/setAttribute + private JS params = new JS.Obj() { + public JS get(JS key) throws JSExn { return JSU.S(request.getParameter(JSU.toString(key)).toString()); } + public void put(JS key, JS val) throws JSExn { request.setAttribute(JSU.toString(key), JSU.toString(val)); } + public Enumeration keys() throws JSExn { + return new JS.Enumeration.JavaStringEnumeration(request.getParameterNames()); } + }; + //#end - case "session.invalidate": request.getSession(true).invalidate(); return null; - case "context.list": - String path = JS.toString(a); - 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