X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2FServlet.java;fp=src%2Fjava%2Forg%2Fibex%2Fxt%2FServlet.java;h=ff63ab9cbdc9a4912feecb9966e1a92077a5b263;hb=50cd2b0cdda6355214e16e8e9f8dd65a31fe6934;hp=0000000000000000000000000000000000000000;hpb=ad6ea5cd8473bd28086d657a2582c5f8c62ad4fb;p=org.ibex.xt-crawshaw.git diff --git a/src/java/org/ibex/xt/Servlet.java b/src/java/org/ibex/xt/Servlet.java new file mode 100644 index 0000000..ff63ab9 --- /dev/null +++ b/src/java/org/ibex/xt/Servlet.java @@ -0,0 +1,174 @@ +package ibex.xt; + +import java.io.*; +import java.net.*; +import javax.servlet.*; +import javax.servlet.http.*; + +import java.util.*; +import ibex.util.*; +import ibex.util.Collections; +import ibex.js.*; + +import org.prevayler.*; +import org.prevayler.implementation.snapshot.*; + +public class Servlet extends HttpServlet { + + 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 rq, HttpServletResponse rs) throws IOException { + String path = cx.getRealPath(rq.getServletPath()); + Servlet.Scope scope = new Servlet.Scope(cx, rq, rs, prevayler); + try { Template.wrap(Template.parse(path, scope), scope).toXML(rs.getWriter()); } + catch (Exception e) { e.printStackTrace(); System.out.println("e = "+e); } + } + + public static class Scope extends Template.Scope { + private final ServletContext cx; + private final HttpServletRequest request; + private final HttpServletResponse response; + private final Prevayler prevayler; + + public Scope(ServletContext cx, HttpServletRequest rq, HttpServletResponse rs, Prevayler p) { + super(null); this.cx = cx; request = rq; response = rs; prevayler = p; + } + + public String getLocalPath() { return cx.getRealPath("/") + "/WEB-INF/"; } + public void transaction(JS t) { + try { prevayler.execute(new Prevalence.JSTransaction(t)); } + catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } + } + + private JS params = new JS() { + private List keys = null; + public Object get(Object key) { return request.getParameter(JS.toString(key)); } + public Collection keys() { + return keys == null ? keys = Collections.list(request.getParameterNames()) : keys; } + }; + 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() { + private List keys = null; + 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 Collection keys() { + return keys == null ? keys = Collections.list(request.getSession(true).getAttributeNames()) : keys; } + }; + private JS requestHeader = new JS() { + private List keys = null; + public Object get(Object key) { return request.getHeader(JS.toString(key)); } + public Collection keys() { + return keys == null ? keys = Collections.list(request.getHeaderNames()) : keys; } + }; + 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 { + Scope.this.put(JS.toString(this.key) + "." + JS.toString(key), val); } + public Object get(Object key) throws JSExn { + return Scope.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 Scope.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 Scope.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 "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