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; | |
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; | |
78f5d120 FT |
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 | ||
6f1acdb2 FT |
51 | public ServerContext ctx() { |
52 | return(new J2eeContext(cfg, req, resp)); | |
53 | } | |
54 | ||
b606e86e FT |
55 | public SocketAddress remoteaddr() { |
56 | try { | |
57 | return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort())); | |
58 | } catch(UnknownHostException e) { | |
59 | /* req.getRemoteAddr should always be a valid IP address, | |
60 | * so this should never happen. */ | |
61 | throw(new Error(e)); | |
62 | } | |
63 | } | |
64 | ||
65 | public SocketAddress localaddr() { | |
66 | try { | |
67 | return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort())); | |
68 | } catch(UnknownHostException e) { | |
69 | /* req.getRemoteAddr should always be a valid IP address, | |
70 | * so this should never happen. */ | |
71 | throw(new Error(e)); | |
72 | } | |
73 | } | |
74 | ||
78f5d120 FT |
75 | public URL url() { |
76 | return(url); | |
77 | } | |
78 | ||
79 | public String method() { | |
80 | return(method); | |
81 | } | |
82 | ||
83 | public String path() { | |
84 | return(path); | |
85 | } | |
86 | ||
87 | public InputStream input() { | |
88 | try { | |
89 | return(req.getInputStream()); | |
90 | } catch(IOException e) { | |
91 | /* It is not obvious why this would happen, so I'll wait | |
92 | * until I know whatever might happen to try and implement | |
93 | * meaningful behavior. */ | |
94 | throw(new RuntimeException(e)); | |
95 | } | |
96 | } | |
97 | ||
98 | public MultiMap<String, String> inheaders() { | |
99 | MultiMap<String, String> h = new HeaderTreeMap(); | |
100 | Enumeration ki = req.getHeaderNames(); | |
101 | if(ki != null) { | |
102 | while(ki.hasMoreElements()) { | |
103 | String k = (String)ki.nextElement(); | |
104 | Enumeration vi = req.getHeaders(k); | |
105 | if(vi != null) { | |
106 | while(vi.hasMoreElements()) { | |
107 | String v = (String)vi.nextElement(); | |
108 | h.add(k, v); | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
113 | return(h); | |
114 | } | |
115 | ||
116 | public MultiMap<String, String> params() { | |
117 | return(null); | |
118 | } | |
119 | ||
120 | protected void backflush() { | |
121 | for(String key : outheaders().keySet()) { | |
122 | boolean first = true; | |
123 | for(String val : outheaders().values(key)) { | |
124 | if(first) { | |
125 | resp.setHeader(key, val); | |
126 | first = false; | |
127 | } else { | |
128 | resp.addHeader(key, val); | |
129 | } | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | protected OutputStream realoutput() { | |
135 | try { | |
136 | return(resp.getOutputStream()); | |
137 | } catch(IOException e) { | |
138 | /* It is not obvious why this would happen, so I'll wait | |
139 | * until I know whatever might happen to try and implement | |
140 | * meaningful behavior. */ | |
141 | throw(new RuntimeException(e)); | |
142 | } | |
143 | } | |
144 | } |