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