X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FRedirect.java;fp=src%2Fdolda%2Fjsvc%2Futil%2FRedirect.java;h=7d733598bf89bd017396715148b1c93c9330b394;hb=4126b9f4cd5367a198678f08508880b4a38aad1f;hp=0000000000000000000000000000000000000000;hpb=f997a53abaaaaa01ea1a7ae45ac576e41b5d886b;p=jsvc.git diff --git a/src/dolda/jsvc/util/Redirect.java b/src/dolda/jsvc/util/Redirect.java new file mode 100644 index 0000000..7d73359 --- /dev/null +++ b/src/dolda/jsvc/util/Redirect.java @@ -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)); + } + } + } +}