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;
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 } catch(MalformedURLException e) {
67 method = req.getMethod().toUpperCase().intern();
68 path = req.getPathInfo();
69 while((path.length() > 0) && (path.charAt(0) == '/'))
70 path = path.substring(1);
73 public Map<Object, Object> props() {
77 public ServerContext ctx() {
78 return(new J2eeContext(cfg, req, resp));
81 public SocketAddress remoteaddr() {
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. */
91 public SocketAddress localaddr() {
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. */
105 public String method() {
109 public String path() {
113 public InputStream input() {
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));
124 public MultiMap<String, String> inheaders() {
125 MultiMap<String, String> h = new HeaderTreeMap();
126 Enumeration ki = req.getHeaderNames();
128 while(ki.hasMoreElements()) {
129 String k = (String)ki.nextElement();
130 Enumeration vi = req.getHeaders(k);
132 while(vi.hasMoreElements()) {
133 String v = (String)vi.nextElement();
142 public MultiMap<String, String> params() {
144 params = Params.urlparams(this);
145 if(method == "POST") {
146 MultiMap<String, String> pp = Params.postparams(this);
154 protected void backflush() {
155 resp.setStatus(respcode);
156 for(String key : outheaders().keySet()) {
157 boolean first = true;
158 for(String val : outheaders().values(key)) {
160 resp.setHeader(key, val);
163 resp.addHeader(key, val);
169 protected OutputStream realoutput() {
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));