Added a request-restart-wrapper and some convenient redirections.
[jsvc.git] / src / dolda / jsvc / util / Restarts.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.net.*;
5
6 public class Restarts {
7     public static RequestRestart redirect(final URL to) {
8         return(new RequestRestart() {
9                 public void respond(Request req) {
10                     req.status(303);
11                     req.outheaders().put("Location", to.toString());
12                 }
13             });
14     }
15
16     public static RequestRestart redirect(final String path) {
17         return(new RequestRestart() {
18                 public void respond(Request req) {
19                     req.status(303);
20                     URL url;
21                     try {
22                         url = new URL(req.url(), path);
23                     } catch(MalformedURLException e) {
24                         throw(new RuntimeException("Bad relative URL: + " + path, e));
25                     }
26                     req.outheaders().put("Location", url.toString());
27                 }
28             });
29     }
30
31     public static RequestRestart redirectctx(final String path) {
32         return(new RequestRestart() {
33                 public void respond(Request req) {
34                     req.status(303);
35                     URL url;
36                     String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
37                     try {
38                         url = new URL(req.url(), rel);
39                     } catch(MalformedURLException e) {
40                         throw(new RuntimeException("Bad relative URL: + " + rel, e));
41                     }
42                     req.outheaders().put("Location", url.toString());
43                 }
44             });
45     }
46 }