1 package dolda.jsvc.j2ee;
4 import dolda.jsvc.util.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
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, context;
17 private MultiMap<String, String> params = null;
18 private Map<Object, Object> props = new HashMap<Object, Object>();
20 public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
25 /* Ewwww, this is disgusting! */
26 String scheme = req.getScheme();
28 String host = req.getHeader("Host");
29 if((host == null) || (host.length() < 1)) {
30 host = req.getLocalAddr();
31 port = req.getLocalPort();
32 if((port == 80) && scheme.equals("http"))
34 else if((port == 443) && scheme.equals("https"))
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) {
42 port = Integer.parseInt(host.substring(p + 1));
43 } catch(NumberFormatException e) {}
46 } else if((p = host.indexOf(':')) >= 0) {
48 port = Integer.parseInt(host.substring(p + 1));
49 host = host.substring(0, p);
50 } catch(NumberFormatException e) {}
53 String pi = req.getPathInfo();
56 String q = req.getQueryString();
62 url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q);
63 context = new URL(scheme, host, port, req.getContextPath());
64 } catch(MalformedURLException e) {
68 method = req.getMethod().toUpperCase().intern();
69 path = req.getPathInfo();
70 while((path.length() > 0) && (path.charAt(0) == '/'))
71 path = path.substring(1);
74 public Map<Object, Object> props() {
78 public SocketAddress remoteaddr() {
80 /* Apparently getRemotePort returns -1 when running on Tomcat over AJP. */
81 int port = req.getRemotePort();
84 return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), port));
85 } catch(UnknownHostException e) {
86 /* req.getRemoteAddr should always be a valid IP address,
87 * so this should never happen. */
92 public SocketAddress localaddr() {
94 return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort()));
95 } catch(UnknownHostException e) {
96 /* req.getRemoteAddr should always be a valid IP address,
97 * so this should never happen. */
106 public URL rooturl() {
110 public ServerContext ctx() {
111 return(ThreadContext.current().server());
114 public String method() {
118 public String path() {
122 public InputStream input() {
124 return(req.getInputStream());
125 } catch(IOException e) {
126 /* It is not obvious why this would happen, so I'll wait
127 * until I know whatever might happen to try and implement
128 * meaningful behavior. */
129 throw(new RuntimeException(e));
133 public MultiMap<String, String> inheaders() {
134 MultiMap<String, String> h = new HeaderTreeMap();
135 Enumeration ki = req.getHeaderNames();
137 while(ki.hasMoreElements()) {
138 String k = (String)ki.nextElement();
139 Enumeration vi = req.getHeaders(k);
141 while(vi.hasMoreElements()) {
142 String v = (String)vi.nextElement();
151 public MultiMap<String, String> params() {
153 params = Params.urlparams(this);
154 if(method == "POST") {
155 MultiMap<String, String> pp = Params.postparams(this);
163 protected void backflush() {
164 resp.setStatus(respcode);
165 for(String key : outheaders().keySet()) {
166 boolean first = true;
167 for(String val : outheaders().values(key)) {
169 resp.setHeader(key, val);
172 resp.addHeader(key, val);
178 protected OutputStream realoutput() {
180 return(resp.getOutputStream());
181 } catch(IOException e) {
182 /* It is not obvious why this would happen, so I'll wait
183 * until I know whatever might happen to try and implement
184 * meaningful behavior. */
185 throw(new RuntimeException(e));