Replaced the "Restarts" class with individual restart classes.
[jsvc.git] / src / dolda / jsvc / util / Redirect.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.net.*;
5
6 public class Redirect extends RequestRestart {
7     private URL abs;
8     private String rel;
9     
10     protected Redirect() {
11     }
12
13     public Redirect(URL to) {
14         this.abs = to;
15         this.rel = null;
16     }
17     
18     public Redirect(String to) {
19         this.abs = null;
20         this.rel = to;
21     }
22     
23     public void respond(Request req) {
24         req.status(303);
25         req.outheaders().put("Location", target(req).toString());
26     }
27     
28     protected URL target(Request req) {
29         if(this.abs != null) {
30             return(this.abs);
31         } else {
32             try {
33                 return(new URL(req.url(), this.rel));
34             } catch(MalformedURLException e) {
35                 throw(new RuntimeException("Bad relative URL: + " + this.rel, e));
36             }
37         }
38     }
39 }