Replaced the "Restarts" class with individual restart classes.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 14 Dec 2009 02:28:56 +0000 (03:28 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 14 Dec 2009 02:28:56 +0000 (03:28 +0100)
src/dolda/jsvc/util/Cache.java
src/dolda/jsvc/util/ClientError.java
src/dolda/jsvc/util/Multiplexer.java
src/dolda/jsvc/util/Redirect.java [new file with mode: 0644]
src/dolda/jsvc/util/Restarts.java [deleted file]
src/dolda/jsvc/util/RootRedirect.java [new file with mode: 0644]
src/dolda/jsvc/util/StaticContent.java
src/dolda/jsvc/util/StdResponse.java [new file with mode: 0644]

index 0021336..371699f 100644 (file)
@@ -15,7 +15,7 @@ public class Cache {
            try {
                cldate = Http.parsedate(ims);
            } catch(java.text.ParseException e) {
-               throw(Restarts.stdresponse(400, "The If-Modified-Since header is not parseable."));
+               throw(new ClientError("The If-Modified-Since header is not parseable."));
            }
            if(mdate.compareTo(cldate) <= 0) {
                throw(new RequestRestart() {
index e4713af..a84cf2e 100644 (file)
@@ -2,19 +2,12 @@ package dolda.jsvc.util;
 
 import dolda.jsvc.*;
 
-public class ClientError extends RequestRestart {
-    private final String title;
-    
+public class ClientError extends StdResponse {
     public ClientError(String title, String msg) {
-       super(msg);
-       this.title = title;
+       super(400, title, msg);
     }
     
     public ClientError(String msg) {
        this("Invalid request", msg);
     }
-    
-    public void respond(Request req) {
-       throw(Restarts.stdresponse(400, title, getMessage()));
-    }
 }
index 8b003b7..8e5aec4 100644 (file)
@@ -18,7 +18,7 @@ public class Multiplexer implements Responder {
     public Multiplexer() {
        this(new Responder() {
                public void respond(Request req) {
-                   throw(Restarts.stdresponse(404, "Resource not found", "The resource you requested could not be found on this server."));
+                   throw(new StdResponse(404, "Resource not found", "The resource you requested could not be found on this server."));
                }
            });
     }
@@ -40,7 +40,7 @@ public class Multiplexer implements Responder {
        add(new Matcher() {
                public boolean match(Request req) {
                    if(req.path().equals(fp)) {
-                       throw(Restarts.redirect(fp + "/"));
+                       throw(new Redirect(fp + "/"));
                    } else if(req.path().startsWith(fp + "/")) {
                        responder.respond(RequestWrap.chpath(req, req.path().substring(fp.length() + 1)));
                        return(true);
diff --git a/src/dolda/jsvc/util/Redirect.java b/src/dolda/jsvc/util/Redirect.java
new file mode 100644 (file)
index 0000000..7d73359
--- /dev/null
@@ -0,0 +1,39 @@
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+import java.net.*;
+
+public class Redirect extends RequestRestart {
+    private URL abs;
+    private String rel;
+    
+    protected Redirect() {
+    }
+
+    public Redirect(URL to) {
+       this.abs = to;
+       this.rel = null;
+    }
+    
+    public Redirect(String to) {
+       this.abs = null;
+       this.rel = to;
+    }
+    
+    public void respond(Request req) {
+       req.status(303);
+       req.outheaders().put("Location", target(req).toString());
+    }
+    
+    protected URL target(Request req) {
+       if(this.abs != null) {
+           return(this.abs);
+       } else {
+           try {
+               return(new URL(req.url(), this.rel));
+           } catch(MalformedURLException e) {
+               throw(new RuntimeException("Bad relative URL: + " + this.rel, e));
+           }
+       }
+    }
+}
diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java
deleted file mode 100644 (file)
index 06ab682..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-package dolda.jsvc.util;
-
-import dolda.jsvc.*;
-import java.net.*;
-import java.io.*;
-
-public class Restarts {
-    public static RequestRestart redirect(final URL to) {
-       return(new RequestRestart() {
-               public void respond(Request req) {
-                   req.status(303);
-                   req.outheaders().put("Location", to.toString());
-               }
-           });
-    }
-
-    public static RequestRestart redirect(final String path) {
-       return(new RequestRestart() {
-               public void respond(Request req) {
-                   req.status(303);
-                   URL url;
-                   try {
-                       url = new URL(req.url(), path);
-                   } catch(MalformedURLException e) {
-                       throw(new RuntimeException("Bad relative URL: + " + path, e));
-                   }
-                   req.outheaders().put("Location", url.toString());
-               }
-           });
-    }
-
-    public static RequestRestart redirectctx(final String path) {
-       return(new RequestRestart() {
-               public void respond(Request req) {
-                   req.status(303);
-                   URL url;
-                   try {
-                       url = new URL(req.rooturl(), Misc.stripslashes(path, true, false));
-                   } catch(MalformedURLException e) {
-                       throw(new RuntimeException("Bad relative URL: + " + path, e));
-                   }
-                   req.outheaders().put("Location", url.toString());
-               }
-           });
-    }
-    
-    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)));
-    }
-    
-    public static RequestRestart done() {
-       return(new RequestRestart() {
-               public void respond(Request req) {
-               }
-           });
-    }
-}
diff --git a/src/dolda/jsvc/util/RootRedirect.java b/src/dolda/jsvc/util/RootRedirect.java
new file mode 100644 (file)
index 0000000..e4dcf7c
--- /dev/null
@@ -0,0 +1,20 @@
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+import java.net.*;
+
+public class RootRedirect extends Redirect {
+    private final String to;
+    
+    public RootRedirect(String to) {
+       this.to = to;
+    }
+    
+    protected URL target(Request req) {
+       try {
+           return(new URL(req.rooturl(), Misc.stripslashes(to, true, false)));
+       } catch(MalformedURLException e) {
+           throw(new RuntimeException("Bad relative URL: + " + to, e));
+       }
+    }
+}
index 6d74e37..0ef227d 100644 (file)
@@ -33,7 +33,7 @@ public class StaticContent implements Responder {
            in = base.getResourceAsStream(nm);
        }
        if(in == null)
-           throw(Restarts.stdresponse(404));
+           throw(new StdResponse(404));
        Cache.checkmtime(req, req.ctx().starttime());
        try {
            try {
diff --git a/src/dolda/jsvc/util/StdResponse.java b/src/dolda/jsvc/util/StdResponse.java
new file mode 100644 (file)
index 0000000..d84de76
--- /dev/null
@@ -0,0 +1,40 @@
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+import java.io.*;
+
+public class StdResponse extends RequestRestart {
+    private final int code;
+    private final String title;
+    
+    public StdResponse(int code, String title, String message) {
+       super(message);
+       this.code = code;
+       this.title = title;
+    }
+    
+    public StdResponse(int code, String message) {
+       this(code, "An error occurred", message);
+    }
+    
+    public StdResponse(int code) {
+       this(code, Misc.statustext(code));
+    }
+    
+    public void respond(Request req) {
+       req.status(code);
+       req.outheaders().put("Content-Type", "text/html; charset=utf-8");
+       PrintWriter out;
+       out = new PrintWriter(new OutputStreamWriter(req.output(), Misc.utf8));
+       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(getMessage());
+       out.println("</body>");
+       out.println("</html>");
+       out.flush();
+    }
+}