Added a way to query the current server context.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 21:10:52 +0000 (23:10 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 21:11:51 +0000 (23:11 +0200)
src/dolda/jsvc/Request.java
src/dolda/jsvc/RequestThread.java
src/dolda/jsvc/ServerContext.java [new file with mode: 0644]
src/dolda/jsvc/j2ee/J2eeContext.java [new file with mode: 0644]
src/dolda/jsvc/j2ee/J2eeRequest.java

index fbadd23..4dc9cf6 100644 (file)
@@ -21,4 +21,5 @@ public interface Request {
     
     /* Misc. */
     public Map<?, ?> props();
+    public ServerContext ctx();
 }
index 7940748..51f9d86 100644 (file)
@@ -19,6 +19,10 @@ public class RequestThread extends Thread {
        }
     }
     
+    public static ServerContext context() {
+       return(((RequestThread)Thread.currentThread()).req.ctx());
+    }
+    
     public static Request request() {
        return(((RequestThread)Thread.currentThread()).req);
     }
diff --git a/src/dolda/jsvc/ServerContext.java b/src/dolda/jsvc/ServerContext.java
new file mode 100644 (file)
index 0000000..b9809f9
--- /dev/null
@@ -0,0 +1,5 @@
+package dolda.jsvc;
+
+public interface ServerContext {
+    public String rootpath();
+}
diff --git a/src/dolda/jsvc/j2ee/J2eeContext.java b/src/dolda/jsvc/j2ee/J2eeContext.java
new file mode 100644 (file)
index 0000000..f253128
--- /dev/null
@@ -0,0 +1,21 @@
+package dolda.jsvc.j2ee;
+
+import dolda.jsvc.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class J2eeContext implements ServerContext {
+    private ServletConfig cfg;
+    private HttpServletRequest req;
+    private HttpServletResponse resp;
+    
+    J2eeContext(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
+       this.cfg = cfg;
+       this.req = req;
+       this.resp = resp;
+    }
+    
+    public String rootpath() {
+       return(req.getContextPath());
+    }
+}
index 9676c46..1e7d789 100644 (file)
@@ -48,6 +48,10 @@ public class J2eeRequest extends ResponseBuffer {
        return(props);
     }
     
+    public ServerContext ctx() {
+       return(new J2eeContext(cfg, req, resp));
+    }
+    
     public URL url() {
        return(url);
     }