Initial commit with hopefully working J2EE request handler.
[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         try {
24             req.setCharacterEncoding("UTF-8");
25             resp.setCharacterEncoding("UTF-8");
26         } catch(UnsupportedEncodingException e) {
27             throw(new AssertionError(e));
28         }
29         {
30             String host = req.getHeader("Host");
31             if((host == null) || (host.length() < 1))
32                 host = req.getLocalAddr();
33             String pi = req.getPathInfo();
34             if(pi == null)
35                 pi = "";
36             String q = req.getQueryString();
37             if(q != null)
38                 q = "?" + q;
39             else
40                 q = "";
41             try {
42                 url = new URL(req.getScheme(), host, req.getServerPort(), req.getContextPath() + req.getServletPath() + pi + q);
43             } catch(MalformedURLException e) {
44                 throw(new Error(e));
45             }
46         }
47         method = req.getMethod().toUpperCase().intern();
48         path = req.getPathInfo();
49         while((path.length() > 0) && (path.charAt(0) == '/'))
50             path = path.substring(1);
51     }
52     
53     public Map<?, ?> props() {
54         return(props);
55     }
56     
57     public URL url() {
58         return(url);
59     }
60     
61     public String method() {
62         return(method);
63     }
64     
65     public String path() {
66         return(path);
67     }
68
69     public InputStream input() {
70         try {
71             return(req.getInputStream());
72         } catch(IOException e) {
73             /* It is not obvious why this would happen, so I'll wait
74              * until I know whatever might happen to try and implement
75              * meaningful behavior. */
76             throw(new RuntimeException(e));
77         }
78     }
79
80     public MultiMap<String, String> inheaders() {
81         MultiMap<String, String> h = new HeaderTreeMap();
82         Enumeration ki = req.getHeaderNames();
83         if(ki != null) {
84             while(ki.hasMoreElements()) {
85                 String k = (String)ki.nextElement();
86                 Enumeration vi = req.getHeaders(k);
87                 if(vi != null) {
88                     while(vi.hasMoreElements()) {
89                         String v = (String)vi.nextElement();
90                         h.add(k, v);
91                     }
92                 }
93             }
94         }
95         return(h);
96     }
97     
98     public MultiMap<String, String> params() {
99         return(null);
100     }
101     
102     protected void backflush() {
103         for(String key : outheaders().keySet()) {
104             boolean first = true;
105             for(String val : outheaders().values(key)) {
106                 if(first) {
107                     resp.setHeader(key, val);
108                     first = false;
109                 } else {
110                     resp.addHeader(key, val);
111                 }
112             }
113         }
114     }
115     
116     protected OutputStream realoutput() {
117         try {
118             return(resp.getOutputStream());
119         } catch(IOException e) {
120             /* It is not obvious why this would happen, so I'll wait
121              * until I know whatever might happen to try and implement
122              * meaningful behavior. */
123             throw(new RuntimeException(e));
124         }
125     }
126 }