From 4126b9f4cd5367a198678f08508880b4a38aad1f Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 14 Dec 2009 03:28:56 +0100 Subject: [PATCH] Replaced the "Restarts" class with individual restart classes. --- src/dolda/jsvc/util/Cache.java | 2 +- src/dolda/jsvc/util/ClientError.java | 11 +---- src/dolda/jsvc/util/Multiplexer.java | 4 +- src/dolda/jsvc/util/Redirect.java | 39 +++++++++++++++ src/dolda/jsvc/util/Restarts.java | 86 ---------------------------------- src/dolda/jsvc/util/RootRedirect.java | 20 ++++++++ src/dolda/jsvc/util/StaticContent.java | 2 +- src/dolda/jsvc/util/StdResponse.java | 40 ++++++++++++++++ 8 files changed, 105 insertions(+), 99 deletions(-) create mode 100644 src/dolda/jsvc/util/Redirect.java delete mode 100644 src/dolda/jsvc/util/Restarts.java create mode 100644 src/dolda/jsvc/util/RootRedirect.java create mode 100644 src/dolda/jsvc/util/StdResponse.java diff --git a/src/dolda/jsvc/util/Cache.java b/src/dolda/jsvc/util/Cache.java index 0021336..371699f 100644 --- a/src/dolda/jsvc/util/Cache.java +++ b/src/dolda/jsvc/util/Cache.java @@ -15,7 +15,7 @@ public class Cache { try { cldate = Http.parsedate(ims); } catch(java.text.ParseException e) { - throw(Restarts.stdresponse(400, "The If-Modified-Since header is not parseable.")); + throw(new ClientError("The If-Modified-Since header is not parseable.")); } if(mdate.compareTo(cldate) <= 0) { throw(new RequestRestart() { diff --git a/src/dolda/jsvc/util/ClientError.java b/src/dolda/jsvc/util/ClientError.java index e4713af..a84cf2e 100644 --- a/src/dolda/jsvc/util/ClientError.java +++ b/src/dolda/jsvc/util/ClientError.java @@ -2,19 +2,12 @@ package dolda.jsvc.util; import dolda.jsvc.*; -public class ClientError extends RequestRestart { - private final String title; - +public class ClientError extends StdResponse { public ClientError(String title, String msg) { - super(msg); - this.title = title; + super(400, title, msg); } public ClientError(String msg) { this("Invalid request", msg); } - - public void respond(Request req) { - throw(Restarts.stdresponse(400, title, getMessage())); - } } diff --git a/src/dolda/jsvc/util/Multiplexer.java b/src/dolda/jsvc/util/Multiplexer.java index 8b003b7..8e5aec4 100644 --- a/src/dolda/jsvc/util/Multiplexer.java +++ b/src/dolda/jsvc/util/Multiplexer.java @@ -18,7 +18,7 @@ public class Multiplexer implements Responder { public Multiplexer() { this(new Responder() { public void respond(Request req) { - throw(Restarts.stdresponse(404, "Resource not found", "The resource you requested could not be found on this server.")); + throw(new StdResponse(404, "Resource not found", "The resource you requested could not be found on this server.")); } }); } @@ -40,7 +40,7 @@ public class Multiplexer implements Responder { add(new Matcher() { public boolean match(Request req) { if(req.path().equals(fp)) { - throw(Restarts.redirect(fp + "/")); + throw(new Redirect(fp + "/")); } else if(req.path().startsWith(fp + "/")) { responder.respond(RequestWrap.chpath(req, req.path().substring(fp.length() + 1))); return(true); 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)); + } + } + } +} diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java deleted file mode 100644 index 06ab682..0000000 --- a/src/dolda/jsvc/util/Restarts.java +++ /dev/null @@ -1,86 +0,0 @@ -package dolda.jsvc.util; - -import dolda.jsvc.*; -import java.net.*; -import java.io.*; - -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; - try { - url = new URL(req.rooturl(), Misc.stripslashes(path, true, false)); - } catch(MalformedURLException e) { - throw(new RuntimeException("Bad relative URL: + " + path, e)); - } - req.outheaders().put("Location", url.toString()); - } - }); - } - - public static RequestRestart stdresponse(final int code, final String title, final String message) { - return(new RequestRestart() { - public void respond(Request req) { - req.status(code); - req.outheaders().put("content-type", "text/html; charset=us-ascii"); - PrintWriter out; - try { - out = new PrintWriter(new OutputStreamWriter(req.output(), "US-ASCII")); - } catch(UnsupportedEncodingException e) { - throw(new Error(e)); - } - out.println(""); - out.println(""); - out.println(""); - out.println("" + title + ""); - out.println(""); - out.println("

" + title + "

"); - out.println(message); - out.println(""); - out.println(""); - out.flush(); - } - }); - } - - public static RequestRestart stdresponse(int code, String message) { - return(stdresponse(code, "An error occurred", message)); - } - - public static RequestRestart stdresponse(int code) { - return(stdresponse(code, "An error occurred", Misc.statustext(code))); - } - - public static RequestRestart done() { - return(new RequestRestart() { - public void respond(Request req) { - } - }); - } -} diff --git a/src/dolda/jsvc/util/RootRedirect.java b/src/dolda/jsvc/util/RootRedirect.java new file mode 100644 index 0000000..e4dcf7c --- /dev/null +++ b/src/dolda/jsvc/util/RootRedirect.java @@ -0,0 +1,20 @@ +package dolda.jsvc.util; + +import dolda.jsvc.*; +import java.net.*; + +public class RootRedirect extends Redirect { + private final String to; + + public RootRedirect(String to) { + this.to = to; + } + + protected URL target(Request req) { + try { + return(new URL(req.rooturl(), Misc.stripslashes(to, true, false))); + } catch(MalformedURLException e) { + throw(new RuntimeException("Bad relative URL: + " + to, e)); + } + } +} diff --git a/src/dolda/jsvc/util/StaticContent.java b/src/dolda/jsvc/util/StaticContent.java index 6d74e37..0ef227d 100644 --- a/src/dolda/jsvc/util/StaticContent.java +++ b/src/dolda/jsvc/util/StaticContent.java @@ -33,7 +33,7 @@ public class StaticContent implements Responder { in = base.getResourceAsStream(nm); } if(in == null) - throw(Restarts.stdresponse(404)); + throw(new StdResponse(404)); Cache.checkmtime(req, req.ctx().starttime()); try { try { diff --git a/src/dolda/jsvc/util/StdResponse.java b/src/dolda/jsvc/util/StdResponse.java new file mode 100644 index 0000000..d84de76 --- /dev/null +++ b/src/dolda/jsvc/util/StdResponse.java @@ -0,0 +1,40 @@ +package dolda.jsvc.util; + +import dolda.jsvc.*; +import java.io.*; + +public class StdResponse extends RequestRestart { + private final int code; + private final String title; + + public StdResponse(int code, String title, String message) { + super(message); + this.code = code; + this.title = title; + } + + public StdResponse(int code, String message) { + this(code, "An error occurred", message); + } + + public StdResponse(int code) { + this(code, Misc.statustext(code)); + } + + public void respond(Request req) { + req.status(code); + req.outheaders().put("Content-Type", "text/html; charset=utf-8"); + PrintWriter out; + out = new PrintWriter(new OutputStreamWriter(req.output(), Misc.utf8)); + out.println(""); + out.println(""); + out.println(""); + out.println("" + title + ""); + out.println(""); + out.println("

" + title + "

"); + out.println(getMessage()); + out.println(""); + out.println(""); + out.flush(); + } +} -- 2.11.0