--- /dev/null
+package dolda.jsvc;
+
+public abstract class RequestRestart extends RuntimeException implements Responder {
+ public RequestRestart() {
+ super("Unhandled restart for code that should be running inside a Rehandler");
+ }
+
+ public RequestRestart(String msg) {
+ super(msg);
+ }
+
+ public RequestRestart(Throwable t) {
+ super("Unhandled restart for code that should be running inside a Rehandler", t);
+ }
+
+ public RequestRestart(String msg, Throwable t) {
+ super(msg, t);
+ }
+
+ public abstract void respond(Request req);
+}
package dolda.jsvc.j2ee;
import dolda.jsvc.*;
+import dolda.jsvc.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
}
public String rootpath() {
- return(req.getContextPath());
+ return("/" + Misc.stripslashes(req.getContextPath(), true, true));
}
public long starttime() {
static {
stext.put(200, "OK");
+ stext.put(300, "Multiple Choices");
+ stext.put(301, "Permanently Moved");
+ stext.put(302, "Temporarily Moved");
+ stext.put(303, "See Other");
+ stext.put(400, "Bad Request");
+ stext.put(401, "Authentication Required");
+ stext.put(403, "Access Forbidden");
+ stext.put(404, "Resource Not Found");
+ stext.put(500, "Server Error");
}
public static String statustext(int status) {
String text;
if((text = stext.get(status)) != null)
return(text);
- return("Unknown Response");
+ return("Server Flimsiness");
+ }
+
+ public static String stripslashes(String p, boolean beg, boolean end) {
+ while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/'))
+ p = p.substring(0, p.length() - 1);
+ while(beg && (p.length() > 0) && (p.charAt(0) == '/'))
+ p = p.substring(1);
+ return(p);
}
}
--- /dev/null
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+
+public class Rehandler implements Responder {
+ private Responder next;
+
+ public Rehandler(Responder next) {
+ this.next = next;
+ }
+
+ public void respond(Request req) {
+ Responder cur = next;
+ while(true) {
+ try {
+ cur.respond(req);
+ } catch(RequestRestart t) {
+ if(req instanceof ResettableRequest) {
+ ResettableRequest rr = (ResettableRequest)req;
+ if(rr.canreset())
+ rr.reset();
+ }
+ cur = t;
+ continue;
+ }
+ return;
+ }
+ }
+}
--- /dev/null
+package dolda.jsvc.util;
+
+import dolda.jsvc.*;
+import java.net.*;
+
+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;
+ String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
+ try {
+ url = new URL(req.url(), rel);
+ } catch(MalformedURLException e) {
+ throw(new RuntimeException("Bad relative URL: + " + rel, e));
+ }
+ req.outheaders().put("Location", url.toString());
+ }
+ });
+ }
+}