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 return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort()));
81 } catch(UnknownHostException e) {
82 /* req.getRemoteAddr should always be a valid IP address,
83 * so this should never happen. */
88 public SocketAddress localaddr() {
90 return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort()));
91 } catch(UnknownHostException e) {
92 /* req.getRemoteAddr should always be a valid IP address,
93 * so this should never happen. */
102 public URL rooturl() {
106 public ServerContext ctx() {
107 return(ThreadContext.current().server());
110 public String method() {
114 public String path() {
118 public InputStream input() {
120 return(req.getInputStream());
121 } catch(IOException e) {
122 /* It is not obvious why this would happen, so I'll wait
123 * until I know whatever might happen to try and implement
124 * meaningful behavior. */
125 throw(new RuntimeException(e));
129 public MultiMap<String, String> inheaders() {
130 MultiMap<String, String> h = new HeaderTreeMap();
131 Enumeration ki = req.getHeaderNames();
133 while(ki.hasMoreElements()) {
134 String k = (String)ki.nextElement();
135 Enumeration vi = req.getHeaders(k);
137 while(vi.hasMoreElements()) {
138 String v = (String)vi.nextElement();
147 public MultiMap<String, String> params() {
149 params = Params.urlparams(this);
150 if(method == "POST") {
151 MultiMap<String, String> pp = Params.postparams(this);
159 protected void backflush() {
160 resp.setStatus(respcode);
161 for(String key : outheaders().keySet()) {
162 boolean first = true;
163 for(String val : outheaders().values(key)) {
165 resp.setHeader(key, val);
168 resp.addHeader(key, val);
174 protected OutputStream realoutput() {
176 return(resp.getOutputStream());
177 } catch(IOException e) {
178 /* It is not obvious why this would happen, so I'll wait
179 * until I know whatever might happen to try and implement
180 * meaningful behavior. */
181 throw(new RuntimeException(e));