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() {
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()));
- }
}
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."));
}
});
}
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);
--- /dev/null
+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));
+ }
+ }
+ }
+}
+++ /dev/null
-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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
- out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
- out.println("<head><title>" + title + "</title></head>");
- out.println("<body>");
- out.println("<h1>" + title + "</h1>");
- out.println(message);
- out.println("</body>");
- out.println("</html>");
- 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) {
- }
- });
- }
-}
--- /dev/null
+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));
+ }
+ }
+}
in = base.getResourceAsStream(nm);
}
if(in == null)
- throw(Restarts.stdresponse(404));
+ throw(new StdResponse(404));
Cache.checkmtime(req, req.ctx().starttime());
try {
try {
--- /dev/null
+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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
+ out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
+ out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\">");
+ out.println("<head><title>" + title + "</title></head>");
+ out.println("<body>");
+ out.println("<h1>" + title + "</h1>");
+ out.println(getMessage());
+ out.println("</body>");
+ out.println("</html>");
+ out.flush();
+ }
+}