Added a responder to serve static content from the classloader.
[jsvc.git] / src / dolda / jsvc / util / Restarts.java
index 1953667..7725f28 100644 (file)
@@ -2,6 +2,7 @@ package dolda.jsvc.util;
 
 import dolda.jsvc.*;
 import java.net.*;
+import java.io.*;
 
 public class Restarts {
     public static RequestRestart redirect(final URL to) {
@@ -43,4 +44,37 @@ public class Restarts {
                }
            });
     }
+    
+    public static RequestRestart stdresponse(final int code, final String title, final String message) {
+       return(new RequestRestart() {
+               public void respond(Request req) {
+                   req.status(code);
+                   req.outheaders().put("content-type", "text/html; charset=us-ascii");
+                   PrintWriter out;
+                   try {
+                       out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII"));
+                   } catch(UnsupportedEncodingException e) {
+                       throw(new Error(e));
+                   }
+                   out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+                   out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
+                   out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
+                   out.println("<head><title>" + title + "</title></head>");
+                   out.println("<body>");
+                   out.println("<h1>" + title + "</h1>");
+                   out.println(message);
+                   out.println("</body>");
+                   out.println("</html>");
+                   out.flush();
+               }
+           });
+    }
+    
+    public static RequestRestart stdresponse(int code, String message) {
+       return(stdresponse(code, "An error occurred", message));
+    }
+
+    public static RequestRestart stdresponse(int code) {
+       return(stdresponse(code, "An error occurred", Misc.statustext(code)));
+    }
 }