| 1 | package dolda.jsvc.j2ee; |
| 2 | |
| 3 | import dolda.jsvc.*; |
| 4 | import dolda.jsvc.util.*; |
| 5 | import java.io.*; |
| 6 | import java.util.*; |
| 7 | import java.net.*; |
| 8 | import javax.servlet.*; |
| 9 | import javax.servlet.http.*; |
| 10 | |
| 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>(); |
| 19 | |
| 20 | public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { |
| 21 | this.cfg = cfg; |
| 22 | this.req = req; |
| 23 | this.resp = resp; |
| 24 | { |
| 25 | /* Ewwww, this is disgusting! */ |
| 26 | String scheme = req.getScheme(); |
| 27 | int port = -1; |
| 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")) |
| 33 | port = -1; |
| 34 | else if((port == 443) && scheme.equals("https")) |
| 35 | port = -1; |
| 36 | } else { |
| 37 | int p; |
| 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) { |
| 41 | try { |
| 42 | port = Integer.parseInt(host.substring(p + 1)); |
| 43 | } catch(NumberFormatException e) {} |
| 44 | } |
| 45 | host = newhost; |
| 46 | } else if((p = host.indexOf(':')) >= 0) { |
| 47 | try { |
| 48 | port = Integer.parseInt(host.substring(p + 1)); |
| 49 | host = host.substring(0, p); |
| 50 | } catch(NumberFormatException e) {} |
| 51 | } |
| 52 | } |
| 53 | String pi = req.getPathInfo(); |
| 54 | if(pi == null) |
| 55 | pi = ""; |
| 56 | String q = req.getQueryString(); |
| 57 | if(q != null) |
| 58 | q = "?" + q; |
| 59 | else |
| 60 | q = ""; |
| 61 | try { |
| 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) { |
| 65 | throw(new Error(e)); |
| 66 | } |
| 67 | } |
| 68 | method = req.getMethod().toUpperCase().intern(); |
| 69 | path = req.getPathInfo(); |
| 70 | while((path.length() > 0) && (path.charAt(0) == '/')) |
| 71 | path = path.substring(1); |
| 72 | } |
| 73 | |
| 74 | public Map<Object, Object> props() { |
| 75 | return(props); |
| 76 | } |
| 77 | |
| 78 | public SocketAddress remoteaddr() { |
| 79 | try { |
| 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. */ |
| 84 | throw(new Error(e)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public SocketAddress localaddr() { |
| 89 | try { |
| 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. */ |
| 94 | throw(new Error(e)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public URL url() { |
| 99 | return(url); |
| 100 | } |
| 101 | |
| 102 | public URL rooturl() { |
| 103 | return(context); |
| 104 | } |
| 105 | |
| 106 | public ServerContext ctx() { |
| 107 | return(ThreadContext.current().server()); |
| 108 | } |
| 109 | |
| 110 | public String method() { |
| 111 | return(method); |
| 112 | } |
| 113 | |
| 114 | public String path() { |
| 115 | return(path); |
| 116 | } |
| 117 | |
| 118 | public InputStream input() { |
| 119 | try { |
| 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)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public MultiMap<String, String> inheaders() { |
| 130 | MultiMap<String, String> h = new HeaderTreeMap(); |
| 131 | Enumeration ki = req.getHeaderNames(); |
| 132 | if(ki != null) { |
| 133 | while(ki.hasMoreElements()) { |
| 134 | String k = (String)ki.nextElement(); |
| 135 | Enumeration vi = req.getHeaders(k); |
| 136 | if(vi != null) { |
| 137 | while(vi.hasMoreElements()) { |
| 138 | String v = (String)vi.nextElement(); |
| 139 | h.add(k, v); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | return(h); |
| 145 | } |
| 146 | |
| 147 | public MultiMap<String, String> params() { |
| 148 | if(params == null) { |
| 149 | params = Params.urlparams(this); |
| 150 | if(method == "POST") { |
| 151 | MultiMap<String, String> pp = Params.postparams(this); |
| 152 | if(pp != null) |
| 153 | params.putAll(pp); |
| 154 | } |
| 155 | } |
| 156 | return(params); |
| 157 | } |
| 158 | |
| 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)) { |
| 164 | if(first) { |
| 165 | resp.setHeader(key, val); |
| 166 | first = false; |
| 167 | } else { |
| 168 | resp.addHeader(key, val); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | protected OutputStream realoutput() { |
| 175 | try { |
| 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)); |
| 182 | } |
| 183 | } |
| 184 | } |