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 Map<?, ?> props = new HashMap();
19 public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
24 /* Ewwww, this is disgusting! */
25 String scheme = req.getScheme();
27 String host = req.getHeader("Host");
28 if((host == null) || (host.length() < 1)) {
29 host = req.getLocalAddr();
30 port = req.getLocalPort();
31 if((port == 80) && scheme.equals("http"))
33 else if((port == 443) && scheme.equals("https"))
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) {
41 port = Integer.parseInt(host.substring(p + 1));
42 } catch(NumberFormatException e) {}
45 } else if((p = host.indexOf(':')) >= 0) {
47 port = Integer.parseInt(host.substring(p + 1));
48 host = host.substring(0, p);
49 } catch(NumberFormatException e) {}
52 String pi = req.getPathInfo();
55 String q = req.getQueryString();
61 url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q);
62 } catch(MalformedURLException e) {
66 method = req.getMethod().toUpperCase().intern();
67 path = req.getPathInfo();
68 while((path.length() > 0) && (path.charAt(0) == '/'))
69 path = path.substring(1);
72 public Map<?, ?> props() {
76 public ServerContext ctx() {
77 return(new J2eeContext(cfg, req, resp));
80 public SocketAddress remoteaddr() {
82 return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort()));
83 } catch(UnknownHostException e) {
84 /* req.getRemoteAddr should always be a valid IP address,
85 * so this should never happen. */
90 public SocketAddress localaddr() {
92 return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort()));
93 } catch(UnknownHostException e) {
94 /* req.getRemoteAddr should always be a valid IP address,
95 * so this should never happen. */
104 public String method() {
108 public String path() {
112 public InputStream input() {
114 return(req.getInputStream());
115 } catch(IOException e) {
116 /* It is not obvious why this would happen, so I'll wait
117 * until I know whatever might happen to try and implement
118 * meaningful behavior. */
119 throw(new RuntimeException(e));
123 public MultiMap<String, String> inheaders() {
124 MultiMap<String, String> h = new HeaderTreeMap();
125 Enumeration ki = req.getHeaderNames();
127 while(ki.hasMoreElements()) {
128 String k = (String)ki.nextElement();
129 Enumeration vi = req.getHeaders(k);
131 while(vi.hasMoreElements()) {
132 String v = (String)vi.nextElement();
141 public MultiMap<String, String> params() {
145 protected void backflush() {
146 resp.setStatus(respcode);
147 for(String key : outheaders().keySet()) {
148 boolean first = true;
149 for(String val : outheaders().values(key)) {
151 resp.setHeader(key, val);
154 resp.addHeader(key, val);
160 protected OutputStream realoutput() {
162 return(resp.getOutputStream());
163 } catch(IOException e) {
164 /* It is not obvious why this would happen, so I'll wait
165 * until I know whatever might happen to try and implement
166 * meaningful behavior. */
167 throw(new RuntimeException(e));