Commit | Line | Data |
---|---|---|
78f5d120 FT |
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; | |
ca045757 | 17 | private MultiMap<String, String> params = null; |
1c868f4e | 18 | private Map<Object, Object> props = new HashMap<Object, Object>(); |
78f5d120 FT |
19 | |
20 | public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { | |
21 | this.cfg = cfg; | |
22 | this.req = req; | |
23 | this.resp = resp; | |
78f5d120 | 24 | { |
433ee775 FT |
25 | /* Ewwww, this is disgusting! */ |
26 | String scheme = req.getScheme(); | |
27 | int port = -1; | |
78f5d120 | 28 | String host = req.getHeader("Host"); |
433ee775 | 29 | if((host == null) || (host.length() < 1)) { |
78f5d120 | 30 | host = req.getLocalAddr(); |
433ee775 FT |
31 | port = req.getLocalPort(); |
32 | if((port == 80) && scheme.equals("http")) | |
33 | port = -1; | |
34 | else if((port == 443) && scheme.equals("https")) | |
35 | port = -1; | |
36 | } else { | |
37 | int p; | |
38 | if((host.charAt(0) == '[') && ((p = host.indexOf(']', 1)) > 1)) { | |
39 | String newhost = host.substring(1, p); | |
40 | if((p = host.indexOf(':', p + 1)) >= 0) { | |
41 | try { | |
42 | port = Integer.parseInt(host.substring(p + 1)); | |
43 | } catch(NumberFormatException e) {} | |
44 | } | |
45 | host = newhost; | |
46 | } else if((p = host.indexOf(':')) >= 0) { | |
47 | try { | |
48 | port = Integer.parseInt(host.substring(p + 1)); | |
49 | host = host.substring(0, p); | |
50 | } catch(NumberFormatException e) {} | |
51 | } | |
52 | } | |
78f5d120 FT |
53 | String pi = req.getPathInfo(); |
54 | if(pi == null) | |
55 | pi = ""; | |
56 | String q = req.getQueryString(); | |
57 | if(q != null) | |
58 | q = "?" + q; | |
59 | else | |
60 | q = ""; | |
61 | try { | |
433ee775 | 62 | url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q); |
78f5d120 FT |
63 | } catch(MalformedURLException e) { |
64 | throw(new Error(e)); | |
65 | } | |
66 | } | |
67 | method = req.getMethod().toUpperCase().intern(); | |
68 | path = req.getPathInfo(); | |
69 | while((path.length() > 0) && (path.charAt(0) == '/')) | |
70 | path = path.substring(1); | |
71 | } | |
72 | ||
1c868f4e | 73 | public Map<Object, Object> props() { |
78f5d120 FT |
74 | return(props); |
75 | } | |
76 | ||
6f1acdb2 FT |
77 | public ServerContext ctx() { |
78 | return(new J2eeContext(cfg, req, resp)); | |
79 | } | |
80 | ||
b606e86e FT |
81 | public SocketAddress remoteaddr() { |
82 | try { | |
83 | return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort())); | |
84 | } catch(UnknownHostException e) { | |
85 | /* req.getRemoteAddr should always be a valid IP address, | |
86 | * so this should never happen. */ | |
87 | throw(new Error(e)); | |
88 | } | |
89 | } | |
90 | ||
91 | public SocketAddress localaddr() { | |
92 | try { | |
93 | return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort())); | |
94 | } catch(UnknownHostException e) { | |
95 | /* req.getRemoteAddr should always be a valid IP address, | |
96 | * so this should never happen. */ | |
97 | throw(new Error(e)); | |
98 | } | |
99 | } | |
100 | ||
78f5d120 FT |
101 | public URL url() { |
102 | return(url); | |
103 | } | |
104 | ||
105 | public String method() { | |
106 | return(method); | |
107 | } | |
108 | ||
109 | public String path() { | |
110 | return(path); | |
111 | } | |
112 | ||
113 | public InputStream input() { | |
114 | try { | |
115 | return(req.getInputStream()); | |
116 | } catch(IOException e) { | |
117 | /* It is not obvious why this would happen, so I'll wait | |
118 | * until I know whatever might happen to try and implement | |
119 | * meaningful behavior. */ | |
120 | throw(new RuntimeException(e)); | |
121 | } | |
122 | } | |
123 | ||
124 | public MultiMap<String, String> inheaders() { | |
125 | MultiMap<String, String> h = new HeaderTreeMap(); | |
126 | Enumeration ki = req.getHeaderNames(); | |
127 | if(ki != null) { | |
128 | while(ki.hasMoreElements()) { | |
129 | String k = (String)ki.nextElement(); | |
130 | Enumeration vi = req.getHeaders(k); | |
131 | if(vi != null) { | |
132 | while(vi.hasMoreElements()) { | |
133 | String v = (String)vi.nextElement(); | |
134 | h.add(k, v); | |
135 | } | |
136 | } | |
137 | } | |
138 | } | |
139 | return(h); | |
140 | } | |
141 | ||
142 | public MultiMap<String, String> params() { | |
ca045757 FT |
143 | if(params == null) { |
144 | params = Params.urlparams(this); | |
145 | if(method == "POST") { | |
146 | MultiMap<String, String> pp = Params.postparams(this); | |
147 | if(pp != null) | |
148 | params.putAll(pp); | |
149 | } | |
150 | } | |
151 | return(params); | |
78f5d120 FT |
152 | } |
153 | ||
154 | protected void backflush() { | |
c5364ae2 | 155 | resp.setStatus(respcode); |
78f5d120 FT |
156 | for(String key : outheaders().keySet()) { |
157 | boolean first = true; | |
158 | for(String val : outheaders().values(key)) { | |
159 | if(first) { | |
160 | resp.setHeader(key, val); | |
161 | first = false; | |
162 | } else { | |
163 | resp.addHeader(key, val); | |
164 | } | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | protected OutputStream realoutput() { | |
170 | try { | |
171 | return(resp.getOutputStream()); | |
172 | } catch(IOException e) { | |
173 | /* It is not obvious why this would happen, so I'll wait | |
174 | * until I know whatever might happen to try and implement | |
175 | * meaningful behavior. */ | |
176 | throw(new RuntimeException(e)); | |
177 | } | |
178 | } | |
179 | } |