| 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<?, ?> props = new HashMap(); |
| 18 | |
| 19 | public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { |
| 20 | this.cfg = cfg; |
| 21 | this.req = req; |
| 22 | this.resp = resp; |
| 23 | { |
| 24 | String host = req.getHeader("Host"); |
| 25 | if((host == null) || (host.length() < 1)) |
| 26 | host = req.getLocalAddr(); |
| 27 | String pi = req.getPathInfo(); |
| 28 | if(pi == null) |
| 29 | pi = ""; |
| 30 | String q = req.getQueryString(); |
| 31 | if(q != null) |
| 32 | q = "?" + q; |
| 33 | else |
| 34 | q = ""; |
| 35 | try { |
| 36 | url = new URL(req.getScheme(), host, req.getServerPort(), req.getContextPath() + req.getServletPath() + pi + q); |
| 37 | } catch(MalformedURLException e) { |
| 38 | throw(new Error(e)); |
| 39 | } |
| 40 | } |
| 41 | method = req.getMethod().toUpperCase().intern(); |
| 42 | path = req.getPathInfo(); |
| 43 | while((path.length() > 0) && (path.charAt(0) == '/')) |
| 44 | path = path.substring(1); |
| 45 | } |
| 46 | |
| 47 | public Map<?, ?> props() { |
| 48 | return(props); |
| 49 | } |
| 50 | |
| 51 | public ServerContext ctx() { |
| 52 | return(new J2eeContext(cfg, req, resp)); |
| 53 | } |
| 54 | |
| 55 | public URL url() { |
| 56 | return(url); |
| 57 | } |
| 58 | |
| 59 | public String method() { |
| 60 | return(method); |
| 61 | } |
| 62 | |
| 63 | public String path() { |
| 64 | return(path); |
| 65 | } |
| 66 | |
| 67 | public InputStream input() { |
| 68 | try { |
| 69 | return(req.getInputStream()); |
| 70 | } catch(IOException e) { |
| 71 | /* It is not obvious why this would happen, so I'll wait |
| 72 | * until I know whatever might happen to try and implement |
| 73 | * meaningful behavior. */ |
| 74 | throw(new RuntimeException(e)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public MultiMap<String, String> inheaders() { |
| 79 | MultiMap<String, String> h = new HeaderTreeMap(); |
| 80 | Enumeration ki = req.getHeaderNames(); |
| 81 | if(ki != null) { |
| 82 | while(ki.hasMoreElements()) { |
| 83 | String k = (String)ki.nextElement(); |
| 84 | Enumeration vi = req.getHeaders(k); |
| 85 | if(vi != null) { |
| 86 | while(vi.hasMoreElements()) { |
| 87 | String v = (String)vi.nextElement(); |
| 88 | h.add(k, v); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return(h); |
| 94 | } |
| 95 | |
| 96 | public MultiMap<String, String> params() { |
| 97 | return(null); |
| 98 | } |
| 99 | |
| 100 | protected void backflush() { |
| 101 | for(String key : outheaders().keySet()) { |
| 102 | boolean first = true; |
| 103 | for(String val : outheaders().values(key)) { |
| 104 | if(first) { |
| 105 | resp.setHeader(key, val); |
| 106 | first = false; |
| 107 | } else { |
| 108 | resp.addHeader(key, val); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | protected OutputStream realoutput() { |
| 115 | try { |
| 116 | return(resp.getOutputStream()); |
| 117 | } catch(IOException e) { |
| 118 | /* It is not obvious why this would happen, so I'll wait |
| 119 | * until I know whatever might happen to try and implement |
| 120 | * meaningful behavior. */ |
| 121 | throw(new RuntimeException(e)); |
| 122 | } |
| 123 | } |
| 124 | } |