Made cache checking easier.
[jsvc.git] / src / dolda / jsvc / util / Restarts.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.net.*;
5 import java.io.*;
6
7 public 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;
37                     String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
38                     try {
39                         url = new URL(req.url(), rel);
40                     } catch(MalformedURLException e) {
41                         throw(new RuntimeException("Bad relative URL: + " + rel, e));
42                     }
43                     req.outheaders().put("Location", url.toString());
44                 }
45             });
46     }
47     
48     public static RequestRestart stdresponse(final int code, final String title, final String message) {
49         return(new RequestRestart() {
50                 public void respond(Request req) {
51                     req.status(code);
52                     req.outheaders().put("content-type", "text/html; charset=us-ascii");
53                     PrintWriter out;
54                     try {
55                         out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII"));
56                     } catch(UnsupportedEncodingException e) {
57                         throw(new Error(e));
58                     }
59                     out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
60                     out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
61                     out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
62                     out.println("<head><title>" + title + "</title></head>");
63                     out.println("<body>");
64                     out.println("<h1>" + title + "</h1>");
65                     out.println(message);
66                     out.println("</body>");
67                     out.println("</html>");
68                     out.flush();
69                 }
70             });
71     }
72     
73     public static RequestRestart stdresponse(int code, String message) {
74         return(stdresponse(code, "An error occurred", message));
75     }
76
77     public static RequestRestart stdresponse(int code) {
78         return(stdresponse(code, "An error occurred", Misc.statustext(code)));
79     }
80     
81     public static RequestRestart done() {
82         return(new RequestRestart() {
83                 public void respond(Request req) {
84                 }
85             });
86     }
87 }