| 1 | package dolda.bsvc; |
| 2 | |
| 3 | import dolda.jsvc.*; |
| 4 | import dolda.jsvc.util.*; |
| 5 | import dolda.jsvc.next.*; |
| 6 | import org.w3c.dom.*; |
| 7 | import java.io.*; |
| 8 | import bsh.Interpreter; |
| 9 | |
| 10 | public class ShellPage implements Responder { |
| 11 | private Console cons = new Console(); |
| 12 | private Interpreter ip = new Interpreter(cons); |
| 13 | |
| 14 | private static class Console implements bsh.ConsoleInterface { |
| 15 | ByteArrayOutputStream obuf = new ByteArrayOutputStream(); |
| 16 | ByteArrayOutputStream ebuf = new ByteArrayOutputStream(); |
| 17 | Reader in = new StringReader(""); |
| 18 | PrintStream out; |
| 19 | PrintStream err; |
| 20 | { |
| 21 | try { |
| 22 | out = new PrintStream(obuf, false, "UTF-8"); |
| 23 | err = new PrintStream(ebuf, false, "UTF-8"); |
| 24 | } catch(UnsupportedEncodingException e) { |
| 25 | throw(new Error(e)); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public void error(Object msg) { |
| 30 | getErr().println(msg); |
| 31 | } |
| 32 | |
| 33 | public void print(Object o) { |
| 34 | getOut().print(o); |
| 35 | } |
| 36 | |
| 37 | public void println(Object o) { |
| 38 | getOut().println(o); |
| 39 | } |
| 40 | |
| 41 | public PrintStream getOut() { |
| 42 | return(out); |
| 43 | } |
| 44 | |
| 45 | public PrintStream getErr() { |
| 46 | return(err); |
| 47 | } |
| 48 | |
| 49 | public Reader getIn() { |
| 50 | return(in); |
| 51 | } |
| 52 | |
| 53 | public void reset() { |
| 54 | obuf.reset(); |
| 55 | ebuf.reset(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public void respond(Request req) { |
| 60 | MultiMap<String, String> params = req.params(); |
| 61 | String cmd = params.get("cmd"); |
| 62 | |
| 63 | Html buf = Html.xhtml11("Shell"); |
| 64 | buf.addcss("css", null); |
| 65 | buf.insert("body", buf.el("h1", buf.text("Shell"))); |
| 66 | |
| 67 | if((req.method() == "POST") && (cmd != null)) { |
| 68 | String eo, ee; |
| 69 | synchronized(cons) { |
| 70 | cons.reset(); |
| 71 | Object resp; |
| 72 | try { |
| 73 | ip.set("req", req); |
| 74 | resp = ip.eval(cmd); |
| 75 | buf.insert("body", buf.el("pre", buf.text((resp == null)?"(null)":(resp.toString())))); |
| 76 | } catch(bsh.EvalError exc) { |
| 77 | buf.insert("body", buf.el("h2", buf.text("Evaluation error"))); |
| 78 | buf.insert("body", buf.el("pre", buf.text(exc.toString()))); |
| 79 | if(exc instanceof bsh.TargetError) { |
| 80 | bsh.TargetError te = (bsh.TargetError)exc; |
| 81 | buf.insert("body", buf.el("h3", buf.text("Target error"))); |
| 82 | StringWriter sbuf = new StringWriter(); |
| 83 | te.getTarget().printStackTrace(new PrintWriter(sbuf)); |
| 84 | buf.insert("body", buf.el("pre", buf.text(sbuf.toString()))); |
| 85 | } |
| 86 | } |
| 87 | eo = new String(cons.obuf.toByteArray(), Misc.utf8); |
| 88 | ee = new String(cons.ebuf.toByteArray(), Misc.utf8); |
| 89 | } |
| 90 | if(eo.length() > 0) { |
| 91 | buf.insert("body", buf.el("h2", buf.text("Output"))); |
| 92 | buf.insert("body", buf.el("pre", buf.text(eo))); |
| 93 | } |
| 94 | if(ee.length() > 0) { |
| 95 | buf.insert("body", buf.el("h2", buf.text("Errors"))); |
| 96 | buf.insert("body", buf.el("pre", buf.text(ee))); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | Element form; |
| 101 | buf.insert("body", buf.el("form", form = buf.el("p", null), "action=sh", "method=post")); |
| 102 | form.appendChild(buf.el("textarea", buf.text(cmd), "cols=80", "rows=5", "name=cmd")); |
| 103 | form.appendChild(buf.el("input", null, "type=submit", "value=Evaluate")); |
| 104 | form.appendChild(buf.el("input", null, "type=reset", "value=Reset")); |
| 105 | try { |
| 106 | buf.output(req); |
| 107 | } catch(IOException e) { |
| 108 | throw(new RuntimeException(e)); |
| 109 | } |
| 110 | } |
| 111 | } |