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) {
37 String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
39 url = new URL(req.url(), rel);
40 } catch(MalformedURLException e) {
41 throw(new RuntimeException("Bad relative URL: + " + rel, e));
43 req.outheaders().put("Location", url.toString());
48 public static RequestRestart stdresponse(final int code, final String title, final String message) {
49 return(new RequestRestart() {
50 public void respond(Request req) {
52 req.outheaders().put("content-type", "text/html; charset=us-ascii");
55 out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII"));
56 } catch(UnsupportedEncodingException e) {
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>");
66 out.println("</body>");
67 out.println("</html>");
73 public static RequestRestart stdresponse(int code, String message) {
74 return(stdresponse(code, "An error occurred", message));
77 public static RequestRestart stdresponse(int code) {
78 return(stdresponse(code, "An error occurred", Misc.statustext(code)));
81 public static RequestRestart done() {
82 return(new RequestRestart() {
83 public void respond(Request req) {