Separated the bsvc console for clarity.
[jsvc.git] / samples / bsh / src / dolda / bsvc / ShellPage.java
CommitLineData
eb81f42e
FT
1package dolda.bsvc;
2
3import dolda.jsvc.*;
4import dolda.jsvc.util.*;
cb67d09c
FT
5import dolda.jsvc.next.*;
6import org.w3c.dom.*;
eb81f42e
FT
7import java.io.*;
8import bsh.Interpreter;
9
cb67d09c 10public class ShellPage implements Responder {
ba5757cb 11 private Console cons = new Console();
c04f28ae
FT
12 private Interpreter ip = new Interpreter(cons);
13
cb67d09c 14 public void respond(Request req) {
eb81f42e
FT
15 MultiMap<String, String> params = req.params();
16 String cmd = params.get("cmd");
17
cb67d09c
FT
18 Html buf = Html.xhtml11("Shell");
19 buf.addcss("css", null);
20 buf.insert("body", buf.el("h1", buf.text("Shell")));
21
eb81f42e 22 if((req.method() == "POST") && (cmd != null)) {
ba5757cb
FT
23 String eo, ee;
24 synchronized(cons) {
25 cons.reset();
26 Object resp;
c04f28ae 27 try {
ba5757cb
FT
28 ip.set("req", req);
29 resp = ip.eval(cmd);
cb67d09c 30 buf.insert("body", buf.el("pre", buf.text((resp == null)?"(null)":(resp.toString()))));
ba5757cb 31 } catch(bsh.EvalError exc) {
cb67d09c
FT
32 buf.insert("body", buf.el("h2", buf.text("Evaluation error")));
33 buf.insert("body", buf.el("pre", buf.text(exc.toString())));
ba5757cb
FT
34 if(exc instanceof bsh.TargetError) {
35 bsh.TargetError te = (bsh.TargetError)exc;
cb67d09c
FT
36 buf.insert("body", buf.el("h3", buf.text("Target error")));
37 StringWriter sbuf = new StringWriter();
38 te.getTarget().printStackTrace(new PrintWriter(sbuf));
39 buf.insert("body", buf.el("pre", buf.text(sbuf.toString())));
c04f28ae 40 }
eb81f42e 41 }
ba5757cb
FT
42 eo = new String(cons.obuf.toByteArray(), Misc.utf8);
43 ee = new String(cons.ebuf.toByteArray(), Misc.utf8);
eb81f42e 44 }
eb81f42e 45 if(eo.length() > 0) {
cb67d09c
FT
46 buf.insert("body", buf.el("h2", buf.text("Output")));
47 buf.insert("body", buf.el("pre", buf.text(eo)));
eb81f42e
FT
48 }
49 if(ee.length() > 0) {
cb67d09c
FT
50 buf.insert("body", buf.el("h2", buf.text("Errors")));
51 buf.insert("body", buf.el("pre", buf.text(ee)));
eb81f42e
FT
52 }
53 }
cb67d09c
FT
54
55 Element form;
56 buf.insert("body", buf.el("form", form = buf.el("p", null), "action=sh", "method=post"));
57 form.appendChild(buf.el("textarea", buf.text(cmd), "cols=80", "rows=5", "name=cmd"));
58 form.appendChild(buf.el("input", null, "type=submit", "value=Evaluate"));
59 form.appendChild(buf.el("input", null, "type=reset", "value=Reset"));
60 try {
61 buf.output(req);
62 } catch(IOException e) {
63 throw(new RuntimeException(e));
64 }
eb81f42e
FT
65 }
66}