1 package dolda.jsvc.util;
7 public class Restarts {
8 public static RequestRestart redirect(final URL to) {
9 return(new RequestRestart() {
10 public void respond(Request req) {
12 req.outheaders().put("Location", to.toString());
17 public static RequestRestart redirect(final String path) {
18 return(new RequestRestart() {
19 public void respond(Request req) {
23 url = new URL(req.url(), path);
24 } catch(MalformedURLException e) {
25 throw(new RuntimeException("Bad relative URL: + " + path, e));
27 req.outheaders().put("Location", url.toString());
32 public static RequestRestart redirectctx(final String path) {
33 return(new RequestRestart() {
34 public void respond(Request req) {
38 url = new URL(req.rooturl(), Misc.stripslashes(path, true, false));
39 } catch(MalformedURLException e) {
40 throw(new RuntimeException("Bad relative URL: + " + path, e));
42 req.outheaders().put("Location", url.toString());
47 public static RequestRestart stdresponse(final int code, final String title, final String message) {
48 return(new RequestRestart() {
49 public void respond(Request req) {
51 req.outheaders().put("content-type", "text/html; charset=us-ascii");
54 out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII"));
55 } catch(UnsupportedEncodingException e) {
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>");
65 out.println("</body>");
66 out.println("</html>");
72 public static RequestRestart stdresponse(int code, String message) {
73 return(stdresponse(code, "An error occurred", message));
76 public static RequestRestart stdresponse(int code) {
77 return(stdresponse(code, "An error occurred", Misc.statustext(code)));
80 public static RequestRestart done() {
81 return(new RequestRestart() {
82 public void respond(Request req) {