4 import dolda.jsvc.util.*;
5 import dolda.jsvc.next.*;
8 import bsh.Interpreter;
10 public class ShellPage implements Responder {
11 private Console cons = new Console();
12 private Interpreter ip = new Interpreter(cons);
14 private static class Console implements bsh.ConsoleInterface {
15 ByteArrayOutputStream obuf = new ByteArrayOutputStream();
16 ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
17 Reader in = new StringReader("");
22 out = new PrintStream(obuf, false, "UTF-8");
23 err = new PrintStream(ebuf, false, "UTF-8");
24 } catch(UnsupportedEncodingException e) {
29 public void error(Object msg) {
30 getErr().println(msg);
33 public void print(Object o) {
37 public void println(Object o) {
41 public PrintStream getOut() {
45 public PrintStream getErr() {
49 public Reader getIn() {
59 public void respond(Request req) {
60 MultiMap<String, String> params = req.params();
61 String cmd = params.get("cmd");
63 Html buf = Html.xhtml11("Shell");
64 buf.addcss("css", null);
65 buf.insert("body", buf.el("h1", buf.text("Shell")));
67 if((req.method() == "POST") && (cmd != null)) {
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())));
87 eo = new String(cons.obuf.toByteArray(), Misc.utf8);
88 ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
91 buf.insert("body", buf.el("h2", buf.text("Output")));
92 buf.insert("body", buf.el("pre", buf.text(eo)));
95 buf.insert("body", buf.el("h2", buf.text("Errors")));
96 buf.insert("body", buf.el("pre", buf.text(ee)));
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"));
107 } catch(IOException e) {
108 throw(new RuntimeException(e));