Actually set the HTTP status code...
[jsvc.git] / src / dolda / jsvc / j2ee / J2eeRequest.java
CommitLineData
78f5d120
FT
1package dolda.jsvc.j2ee;
2
3import dolda.jsvc.*;
4import dolda.jsvc.util.*;
5import java.io.*;
6import java.util.*;
7import java.net.*;
8import javax.servlet.*;
9import javax.servlet.http.*;
10
11public 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() {
c5364ae2 121 resp.setStatus(respcode);
78f5d120
FT
122 for(String key : outheaders().keySet()) {
123 boolean first = true;
124 for(String val : outheaders().values(key)) {
125 if(first) {
126 resp.setHeader(key, val);
127 first = false;
128 } else {
129 resp.addHeader(key, val);
130 }
131 }
132 }
133 }
134
135 protected OutputStream realoutput() {
136 try {
137 return(resp.getOutputStream());
138 } catch(IOException e) {
139 /* It is not obvious why this would happen, so I'll wait
140 * until I know whatever might happen to try and implement
141 * meaningful behavior. */
142 throw(new RuntimeException(e));
143 }
144 }
145}