Replaced the "Restarts" class with individual restart classes.
[jsvc.git] / src / dolda / jsvc / util / Redirect.java
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));
+           }
+       }
+    }
+}