Separated the bsvc console for clarity.
[jsvc.git] / src / dolda / jsvc / util / Restarts.java
CommitLineData
c25c3aad
FT
1package dolda.jsvc.util;
2
3import dolda.jsvc.*;
4import java.net.*;
7779099a 5import java.io.*;
c25c3aad
FT
6
7public class Restarts {
8 public static RequestRestart redirect(final URL to) {
9 return(new RequestRestart() {
10 public void respond(Request req) {
11 req.status(303);
12 req.outheaders().put("Location", to.toString());
13 }
14 });
15 }
16
17 public static RequestRestart redirect(final String path) {
18 return(new RequestRestart() {
19 public void respond(Request req) {
20 req.status(303);
21 URL url;
22 try {
23 url = new URL(req.url(), path);
24 } catch(MalformedURLException e) {
25 throw(new RuntimeException("Bad relative URL: + " + path, e));
26 }
27 req.outheaders().put("Location", url.toString());
28 }
29 });
30 }
31
32 public static RequestRestart redirectctx(final String path) {
33 return(new RequestRestart() {
34 public void respond(Request req) {
35 req.status(303);
36 URL url;
c25c3aad 37 try {
4b8346e1 38 url = new URL(req.rooturl(), Misc.stripslashes(path, true, false));
c25c3aad 39 } catch(MalformedURLException e) {
4b8346e1 40 throw(new RuntimeException("Bad relative URL: + " + path, e));
c25c3aad
FT
41 }
42 req.outheaders().put("Location", url.toString());
43 }
44 });
45 }
7779099a
FT
46
47 public static RequestRestart stdresponse(final int code, final String title, final String message) {
48 return(new RequestRestart() {
49 public void respond(Request req) {
50 req.status(code);
51 req.outheaders().put("content-type", "text/html; charset=us-ascii");
52 PrintWriter out;
53 try {
54 out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII"));
55 } catch(UnsupportedEncodingException e) {
56 throw(new Error(e));
57 }
58 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
59 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
60 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
61 out.println("<head><title>" + title + "</title></head>");
62 out.println("<body>");
63 out.println("<h1>" + title + "</h1>");
64 out.println(message);
65 out.println("</body>");
66 out.println("</html>");
67 out.flush();
68 }
69 });
70 }
71
72 public static RequestRestart stdresponse(int code, String message) {
73 return(stdresponse(code, "An error occurred", message));
74 }
75
76 public static RequestRestart stdresponse(int code) {
77 return(stdresponse(code, "An error occurred", Misc.statustext(code)));
78 }
60ef2885
FT
79
80 public static RequestRestart done() {
81 return(new RequestRestart() {
82 public void respond(Request req) {
83 }
84 });
85 }
c25c3aad 86}