Added a request-restart-wrapper and some convenient redirections.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 22:46:54 +0000 (00:46 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 22:46:54 +0000 (00:46 +0200)
src/dolda/jsvc/RequestRestart.java [new file with mode: 0644]
src/dolda/jsvc/j2ee/J2eeContext.java
src/dolda/jsvc/util/Misc.java
src/dolda/jsvc/util/Rehandler.java [new file with mode: 0644]
src/dolda/jsvc/util/Restarts.java [new file with mode: 0644]

diff --git a/src/dolda/jsvc/RequestRestart.java b/src/dolda/jsvc/RequestRestart.java
new file mode 100644 (file)
index 0000000..4061905
--- /dev/null
@@ -0,0 +1,21 @@
+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);
+}
index e8f65a6..696e32b 100644 (file)
@@ -1,6 +1,7 @@
 package dolda.jsvc.j2ee;
 
 import dolda.jsvc.*;
+import dolda.jsvc.util.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
@@ -16,7 +17,7 @@ public class J2eeContext implements ServerContext {
     }
     
     public String rootpath() {
-       return(req.getContextPath());
+       return("/" + Misc.stripslashes(req.getContextPath(), true, true));
     }
     
     public long starttime() {
index c313183..cc01f1f 100644 (file)
@@ -7,12 +7,29 @@ public class Misc {
     
     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);
     }
 }
diff --git a/src/dolda/jsvc/util/Rehandler.java b/src/dolda/jsvc/util/Rehandler.java
new file mode 100644 (file)
index 0000000..7915f53
--- /dev/null
@@ -0,0 +1,29 @@
+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;
+       }
+    }
+}
diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java
new file mode 100644 (file)
index 0000000..1953667
--- /dev/null
@@ -0,0 +1,46 @@
+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());
+               }
+           });
+    }
+}