X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fxt%2FServlet.java;h=01acda700e1bde93fb7a1bbb6883888967e41ca9;hb=bb13e9521c7120aaf0bbbcc15dab318c31d76d0c;hp=38182cae3c29058351e21da2cc4fa99d33703909;hpb=8f8109c54eee0492842c79ae96ef311bc4d2ebd0;p=org.ibex.xt.git diff --git a/src/org/ibex/xt/Servlet.java b/src/org/ibex/xt/Servlet.java index 38182ca..01acda7 100644 --- a/src/org/ibex/xt/Servlet.java +++ b/src/org/ibex/xt/Servlet.java @@ -11,150 +11,119 @@ import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; -import com.thoughtworks.xstream.*; -import org.prevayler.*; -import org.prevayler.implementation.snapshot.*; +//import com.thoughtworks.xstream.*; +//import org.prevayler.*; +//import org.prevayler.implementation.snapshot.*; -public class Servlet extends HttpServlet { +public class Servlet extends Prevalence.PrevalentServlet { public static final JS METHOD = new JS.Method(); private ServletScope servletscope = null; private String path; - private Prevayler prevayler; - private JS prevalent; - private ServletContext cx = null; - - public void destroy() { try { - synchronized(this.getClass()) { - Prevayler privatePrevayler = prevayler; - if (prevayler == null) return; - prevayler = null; - Prevalence.destroy(cx, prevayler); - } - } catch (Exception e) { e.printStackTrace(); } } - - 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); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { servletscope = new ServletScope(request, response, cx); path = cx.getRealPath(((HttpServletRequest)request).getServletPath()); Reader xmlreader = new InputStreamReader(new FileInputStream(path)); - new Template(servletscope, 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 JS.Obj { + // 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(); this.request = (HttpServletRequest)request; this.response = (HttpServletResponse)response; + this.session = this.request.getSession(); this.cx = cx; } + + // 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))); } - public Enumeration keys() throws JSExn { return new JS.Enumeration.JavaStringEnumeration(request.getParameterNames()); } - }; - private JS cookies = new JS.Obj() { - /* - public Object get(Object key) { return request.getCookie(JSU.toString(key)); } - public Enumeration keys() { return request.getCookieNames(); } - */ - }; - 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.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()); } + 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()); } }; - 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.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(JSU.S(key)); } + //#end - 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 = 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