| 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; |
| 17 | private Map<Object, Object> props = new HashMap<Object, Object>(); |
| 18 | |
| 19 | public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { |
| 20 | this.cfg = cfg; |
| 21 | this.req = req; |
| 22 | this.resp = resp; |
| 23 | { |
| 24 | /* Ewwww, this is disgusting! */ |
| 25 | String scheme = req.getScheme(); |
| 26 | int port = -1; |
| 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")) |
| 32 | port = -1; |
| 33 | else if((port == 443) && scheme.equals("https")) |
| 34 | port = -1; |
| 35 | } else { |
| 36 | int p; |
| 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) { |
| 40 | try { |
| 41 | port = Integer.parseInt(host.substring(p + 1)); |
| 42 | } catch(NumberFormatException e) {} |
| 43 | } |
| 44 | host = newhost; |
| 45 | } else if((p = host.indexOf(':')) >= 0) { |
| 46 | try { |
| 47 | port = Integer.parseInt(host.substring(p + 1)); |
| 48 | host = host.substring(0, p); |
| 49 | } catch(NumberFormatException e) {} |
| 50 | } |
| 51 | } |
| 52 | String pi = req.getPathInfo(); |
| 53 | if(pi == null) |
| 54 | pi = ""; |
| 55 | String q = req.getQueryString(); |
| 56 | if(q != null) |
| 57 | q = "?" + q; |
| 58 | else |
| 59 | q = ""; |
| 60 | try { |
| 61 | url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q); |
| 62 | } catch(MalformedURLException e) { |
| 63 | throw(new Error(e)); |
| 64 | } |
| 65 | } |
| 66 | method = req.getMethod().toUpperCase().intern(); |
| 67 | path = req.getPathInfo(); |
| 68 | while((path.length() > 0) && (path.charAt(0) == '/')) |
| 69 | path = path.substring(1); |
| 70 | } |
| 71 | |
| 72 | public Map<Object, Object> props() { |
| 73 | return(props); |
| 74 | } |
| 75 | |
| 76 | public ServerContext ctx() { |
| 77 | return(new J2eeContext(cfg, req, resp)); |
| 78 | } |
| 79 | |
| 80 | public SocketAddress remoteaddr() { |
| 81 | try { |
| 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. */ |
| 86 | throw(new Error(e)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public SocketAddress localaddr() { |
| 91 | try { |
| 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. */ |
| 96 | throw(new Error(e)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public URL url() { |
| 101 | return(url); |
| 102 | } |
| 103 | |
| 104 | public String method() { |
| 105 | return(method); |
| 106 | } |
| 107 | |
| 108 | public String path() { |
| 109 | return(path); |
| 110 | } |
| 111 | |
| 112 | public InputStream input() { |
| 113 | try { |
| 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)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public MultiMap<String, String> inheaders() { |
| 124 | MultiMap<String, String> h = new HeaderTreeMap(); |
| 125 | Enumeration ki = req.getHeaderNames(); |
| 126 | if(ki != null) { |
| 127 | while(ki.hasMoreElements()) { |
| 128 | String k = (String)ki.nextElement(); |
| 129 | Enumeration vi = req.getHeaders(k); |
| 130 | if(vi != null) { |
| 131 | while(vi.hasMoreElements()) { |
| 132 | String v = (String)vi.nextElement(); |
| 133 | h.add(k, v); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return(h); |
| 139 | } |
| 140 | |
| 141 | public MultiMap<String, String> params() { |
| 142 | return(null); |
| 143 | } |
| 144 | |
| 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)) { |
| 150 | if(first) { |
| 151 | resp.setHeader(key, val); |
| 152 | first = false; |
| 153 | } else { |
| 154 | resp.addHeader(key, val); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | protected OutputStream realoutput() { |
| 161 | try { |
| 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)); |
| 168 | } |
| 169 | } |
| 170 | } |