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