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