| 1 | package dolda.bsvc; |
| 2 | |
| 3 | import dolda.jsvc.*; |
| 4 | import dolda.jsvc.util.*; |
| 5 | import java.io.*; |
| 6 | import bsh.Interpreter; |
| 7 | |
| 8 | public class ShellPage extends SimpleWriter { |
| 9 | private RConsole cons = new RConsole(); |
| 10 | private Interpreter ip = new Interpreter(cons); |
| 11 | |
| 12 | private static class RConsole implements bsh.ConsoleInterface { |
| 13 | public Console back; |
| 14 | Reader in = new StringReader(""); |
| 15 | |
| 16 | public void error(Object msg) { |
| 17 | if(back != null) |
| 18 | back.error(msg); |
| 19 | } |
| 20 | |
| 21 | public void print(Object msg) { |
| 22 | if(back != null) |
| 23 | back.print(msg); |
| 24 | } |
| 25 | |
| 26 | public void println(Object msg) { |
| 27 | if(back != null) |
| 28 | back.println(msg); |
| 29 | } |
| 30 | |
| 31 | public PrintStream getOut() { |
| 32 | if(back == null) |
| 33 | return(null); |
| 34 | return(back.getOut()); |
| 35 | } |
| 36 | |
| 37 | public PrintStream getErr() { |
| 38 | if(back == null) |
| 39 | return(null); |
| 40 | return(back.getErr()); |
| 41 | } |
| 42 | |
| 43 | public Reader getIn() { |
| 44 | if(back == null) |
| 45 | return(null); |
| 46 | return(in); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | private static class Console implements bsh.ConsoleInterface { |
| 51 | ByteArrayOutputStream obuf = new ByteArrayOutputStream(); |
| 52 | ByteArrayOutputStream ebuf = new ByteArrayOutputStream(); |
| 53 | Reader in = new StringReader(""); |
| 54 | PrintStream out; |
| 55 | PrintStream err; |
| 56 | { |
| 57 | try { |
| 58 | out = new PrintStream(obuf, false, "UTF-8"); |
| 59 | err = new PrintStream(ebuf, false, "UTF-8"); |
| 60 | } catch(UnsupportedEncodingException e) { |
| 61 | throw(new Error(e)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public void error(Object msg) { |
| 66 | getErr().println(msg); |
| 67 | } |
| 68 | |
| 69 | public void print(Object o) { |
| 70 | getOut().print(o); |
| 71 | } |
| 72 | |
| 73 | public void println(Object o) { |
| 74 | getOut().println(o); |
| 75 | } |
| 76 | |
| 77 | public PrintStream getOut() { |
| 78 | return(out); |
| 79 | } |
| 80 | |
| 81 | public PrintStream getErr() { |
| 82 | return(err); |
| 83 | } |
| 84 | |
| 85 | public Reader getIn() { |
| 86 | return(in); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public void respond(Request req, PrintWriter out) { |
| 91 | MultiMap<String, String> params = req.params(); |
| 92 | String cmd = params.get("cmd"); |
| 93 | |
| 94 | out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 95 | out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"); |
| 96 | out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">"); |
| 97 | out.println("<head>"); |
| 98 | out.println("<title>Shell</title>"); |
| 99 | out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"css\" />"); |
| 100 | out.println("</head>"); |
| 101 | out.println("<body>"); |
| 102 | out.println("<h1>Shell</h1>"); |
| 103 | if((req.method() == "POST") && (cmd != null)) { |
| 104 | Console cons = new Console(); |
| 105 | synchronized(ip) { |
| 106 | this.cons.back = cons; |
| 107 | try { |
| 108 | Object resp; |
| 109 | try { |
| 110 | ip.set("req", req); |
| 111 | resp = ip.eval(cmd); |
| 112 | out.println("<pre>"); |
| 113 | out.println(Misc.htmlq((resp == null)?"(null)":(resp.toString()))); |
| 114 | out.println("</pre>"); |
| 115 | } catch(bsh.EvalError exc) { |
| 116 | out.println("<h2>Evaluation error</h2>"); |
| 117 | out.println("<pre>"); |
| 118 | out.print(exc.toString()); |
| 119 | out.println("</pre>"); |
| 120 | if(exc instanceof bsh.TargetError) { |
| 121 | bsh.TargetError te = (bsh.TargetError)exc; |
| 122 | out.println("<h3>Target error</h3>"); |
| 123 | out.println("<pre>"); |
| 124 | te.getTarget().printStackTrace(out); |
| 125 | out.println("</pre>"); |
| 126 | } |
| 127 | } |
| 128 | } finally { |
| 129 | this.cons.back = null; |
| 130 | } |
| 131 | } |
| 132 | String eo = new String(cons.obuf.toByteArray(), Misc.utf8); |
| 133 | String ee = new String(cons.ebuf.toByteArray(), Misc.utf8); |
| 134 | cons = null; |
| 135 | if(eo.length() > 0) { |
| 136 | out.println("<h2>Output</h2>"); |
| 137 | out.println("<pre>"); |
| 138 | out.println(Misc.htmlq(eo)); |
| 139 | out.println("</pre>"); |
| 140 | } |
| 141 | if(ee.length() > 0) { |
| 142 | out.println("<h2>Errors</h2>"); |
| 143 | out.println("<pre>"); |
| 144 | out.println(Misc.htmlq(ee)); |
| 145 | out.println("</pre>"); |
| 146 | } |
| 147 | } |
| 148 | out.println("<form action=\"sh\" method=\"post\">"); |
| 149 | out.println("<textarea cols=\"80\" rows=\"5\" name=\"cmd\">"); |
| 150 | if(cmd != null) |
| 151 | out.print(cmd); |
| 152 | out.println("</textarea>"); |
| 153 | out.println("<input type=\"submit\" value=\"Evaluate\" />"); |
| 154 | out.println("<input type=\"reset\" value=\"Reset\" />"); |
| 155 | out.println("</form>"); |
| 156 | out.println("</body>"); |
| 157 | out.println("</html>"); |
| 158 | } |
| 159 | } |