Added a request-restart-wrapper and some convenient redirections.
[jsvc.git] / src / dolda / jsvc / util / Restarts.java
diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java
new file mode 100644 (file)
index 0000000..1953667
--- /dev/null
@@ -0,0 +1,46 @@
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+import java.net.*;
+
+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;
+                   String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
+                   try {
+                       url = new URL(req.url(), rel);
+                   } catch(MalformedURLException e) {
+                       throw(new RuntimeException("Bad relative URL: + " + rel, e));
+                   }
+                   req.outheaders().put("Location", url.toString());
+               }
+           });
+    }
+}