Commit | Line | Data |
---|---|---|
eb81f42e FT |
1 | package dolda.bsvc; |
2 | ||
3 | import dolda.jsvc.*; | |
4 | import dolda.jsvc.util.*; | |
cb67d09c FT |
5 | import dolda.jsvc.next.*; |
6 | import org.w3c.dom.*; | |
eb81f42e FT |
7 | import java.io.*; |
8 | import bsh.Interpreter; | |
9 | ||
cb67d09c | 10 | public class ShellPage implements Responder { |
ba5757cb | 11 | private Console cons = new Console(); |
c04f28ae FT |
12 | private Interpreter ip = new Interpreter(cons); |
13 | ||
eb81f42e FT |
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 | } | |
ba5757cb FT |
52 | |
53 | public void reset() { | |
54 | obuf.reset(); | |
55 | ebuf.reset(); | |
56 | } | |
eb81f42e FT |
57 | } |
58 | ||
cb67d09c | 59 | public void respond(Request req) { |
eb81f42e FT |
60 | MultiMap<String, String> params = req.params(); |
61 | String cmd = params.get("cmd"); | |
62 | ||
cb67d09c FT |
63 | Html buf = Html.xhtml11("Shell"); |
64 | buf.addcss("css", null); | |
65 | buf.insert("body", buf.el("h1", buf.text("Shell"))); | |
66 | ||
eb81f42e | 67 | if((req.method() == "POST") && (cmd != null)) { |
ba5757cb FT |
68 | String eo, ee; |
69 | synchronized(cons) { | |
70 | cons.reset(); | |
71 | Object resp; | |
c04f28ae | 72 | try { |
ba5757cb FT |
73 | ip.set("req", req); |
74 | resp = ip.eval(cmd); | |
cb67d09c | 75 | buf.insert("body", buf.el("pre", buf.text((resp == null)?"(null)":(resp.toString())))); |
ba5757cb | 76 | } catch(bsh.EvalError exc) { |
cb67d09c FT |
77 | buf.insert("body", buf.el("h2", buf.text("Evaluation error"))); |
78 | buf.insert("body", buf.el("pre", buf.text(exc.toString()))); | |
ba5757cb FT |
79 | if(exc instanceof bsh.TargetError) { |
80 | bsh.TargetError te = (bsh.TargetError)exc; | |
cb67d09c FT |
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()))); | |
c04f28ae | 85 | } |
eb81f42e | 86 | } |
ba5757cb FT |
87 | eo = new String(cons.obuf.toByteArray(), Misc.utf8); |
88 | ee = new String(cons.ebuf.toByteArray(), Misc.utf8); | |
eb81f42e | 89 | } |
eb81f42e | 90 | if(eo.length() > 0) { |
cb67d09c FT |
91 | buf.insert("body", buf.el("h2", buf.text("Output"))); |
92 | buf.insert("body", buf.el("pre", buf.text(eo))); | |
eb81f42e FT |
93 | } |
94 | if(ee.length() > 0) { | |
cb67d09c FT |
95 | buf.insert("body", buf.el("h2", buf.text("Errors"))); |
96 | buf.insert("body", buf.el("pre", buf.text(ee))); | |
eb81f42e FT |
97 | } |
98 | } | |
cb67d09c FT |
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 | } | |
eb81f42e FT |
110 | } |
111 | } |