Initially working J2EE container.
[jsvc.git] / src / dolda / jsvc / j2ee / J2eeRequest.java
1 package dolda.jsvc.j2ee;
2
3 import dolda.jsvc.*;
4 import dolda.jsvc.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.net.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10
11 public class J2eeRequest extends ResponseBuffer {
12     private ServletConfig cfg;
13     private HttpServletRequest req;
14     private HttpServletResponse resp;
15     private String method, path;
16     private URL url;
17     private Map<?, ?> props = new HashMap();
18     
19     public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
20         this.cfg = cfg;
21         this.req = req;
22         this.resp = resp;
23         {
24             String host = req.getHeader("Host");
25             if((host == null) || (host.length() < 1))
26                 host = req.getLocalAddr();
27             String pi = req.getPathInfo();
28             if(pi == null)
29                 pi = "";
30             String q = req.getQueryString();
31             if(q != null)
32                 q = "?" + q;
33             else
34                 q = "";
35             try {
36                 url = new URL(req.getScheme(), host, req.getServerPort(), req.getContextPath() + req.getServletPath() + pi + q);
37             } catch(MalformedURLException e) {
38                 throw(new Error(e));
39             }
40         }
41         method = req.getMethod().toUpperCase().intern();
42         path = req.getPathInfo();
43         while((path.length() > 0) && (path.charAt(0) == '/'))
44             path = path.substring(1);
45     }
46     
47     public Map<?, ?> props() {
48         return(props);
49     }
50     
51     public URL url() {
52         return(url);
53     }
54     
55     public String method() {
56         return(method);
57     }
58     
59     public String path() {
60         return(path);
61     }
62
63     public InputStream input() {
64         try {
65             return(req.getInputStream());
66         } catch(IOException e) {
67             /* It is not obvious why this would happen, so I'll wait
68              * until I know whatever might happen to try and implement
69              * meaningful behavior. */
70             throw(new RuntimeException(e));
71         }
72     }
73
74     public MultiMap<String, String> inheaders() {
75         MultiMap<String, String> h = new HeaderTreeMap();
76         Enumeration ki = req.getHeaderNames();
77         if(ki != null) {
78             while(ki.hasMoreElements()) {
79                 String k = (String)ki.nextElement();
80                 Enumeration vi = req.getHeaders(k);
81                 if(vi != null) {
82                     while(vi.hasMoreElements()) {
83                         String v = (String)vi.nextElement();
84                         h.add(k, v);
85                     }
86                 }
87             }
88         }
89         return(h);
90     }
91     
92     public MultiMap<String, String> params() {
93         return(null);
94     }
95     
96     protected void backflush() {
97         for(String key : outheaders().keySet()) {
98             boolean first = true;
99             for(String val : outheaders().values(key)) {
100                 if(first) {
101                     resp.setHeader(key, val);
102                     first = false;
103                 } else {
104                     resp.addHeader(key, val);
105                 }
106             }
107         }
108     }
109     
110     protected OutputStream realoutput() {
111         try {
112             return(resp.getOutputStream());
113         } catch(IOException e) {
114             /* It is not obvious why this would happen, so I'll wait
115              * until I know whatever might happen to try and implement
116              * meaningful behavior. */
117             throw(new RuntimeException(e));
118         }
119     }
120 }