Work around Tomcat/AJP bug.
[jsvc.git] / src / dolda / jsvc / j2ee / J2eeRequest.java
index 9beddf5..6b1ba0b 100644 (file)
@@ -13,7 +13,8 @@ public class J2eeRequest extends ResponseBuffer {
     private HttpServletRequest req;
     private HttpServletResponse resp;
     private String method, path;
-    private URL url;
+    private URL url, context;
+    private MultiMap<String, String> params = null;
     private Map<Object, Object> props = new HashMap<Object, Object>();
     
     public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
@@ -59,6 +60,7 @@ public class J2eeRequest extends ResponseBuffer {
                q = "";
            try {
                url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q);
+               context = new URL(scheme, host, port, req.getContextPath());
            } catch(MalformedURLException e) {
                throw(new Error(e));
            }
@@ -73,13 +75,13 @@ public class J2eeRequest extends ResponseBuffer {
        return(props);
     }
     
-    public ServerContext ctx() {
-       return(new J2eeContext(cfg, req, resp));
-    }
-    
     public SocketAddress remoteaddr() {
        try {
-           return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort()));
+           /* Apparently getRemotePort returns -1 when running on Tomcat over AJP. */
+           int port = req.getRemotePort();
+           if(port < 0)
+               port = 0;
+           return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), port));
        } catch(UnknownHostException e) {
            /* req.getRemoteAddr should always be a valid IP address,
             * so this should never happen. */
@@ -101,6 +103,14 @@ public class J2eeRequest extends ResponseBuffer {
        return(url);
     }
     
+    public URL rooturl() {
+       return(context);
+    }
+    
+    public ServerContext ctx() {
+       return(ThreadContext.current().server());
+    }
+    
     public String method() {
        return(method);
     }
@@ -139,7 +149,15 @@ public class J2eeRequest extends ResponseBuffer {
     }
     
     public MultiMap<String, String> params() {
-       return(null);
+       if(params == null) {
+           params = Params.urlparams(this);
+           if(method == "POST") {
+               MultiMap<String, String> pp = Params.postparams(this);
+               if(pp != null)
+                   params.putAll(pp);
+           }
+       }
+       return(params);
     }
     
     protected void backflush() {