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;
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 context = new URL(scheme, host, port, req.getContextPath());
63 } catch(MalformedURLException e) {
67 method = req.getMethod().toUpperCase().intern();
68 path = req.getPathInfo();
69 while((path.length() > 0) && (path.charAt(0) == '/'))
70 path = path.substring(1);
73 public SocketAddress remoteaddr() {
75 /* Apparently getRemotePort returns -1 when running on Tomcat over AJP. */
76 int port = req.getRemotePort();
79 return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), port));
80 } catch(UnknownHostException e) {
81 /* req.getRemoteAddr should always be a valid IP address,
82 * so this should never happen. */
87 public SocketAddress localaddr() {
89 return(new InetSocketAddress(InetAddress.getByName(req.getLocalAddr()), req.getLocalPort()));
90 } catch(UnknownHostException e) {
91 /* req.getRemoteAddr should always be a valid IP address,
92 * so this should never happen. */
101 public URL rooturl() {
105 public ServerContext ctx() {
106 return(ThreadContext.current().server());
109 public String method() {
113 public String path() {
117 public InputStream input() {
119 return(req.getInputStream());
120 } catch(IOException e) {
121 /* It is not obvious why this would happen, so I'll wait
122 * until I know whatever might happen to try and implement
123 * meaningful behavior. */
124 throw(new RuntimeException(e));
128 public MultiMap<String, String> inheaders() {
129 MultiMap<String, String> h = new HeaderTreeMap();
130 Enumeration ki = req.getHeaderNames();
132 while(ki.hasMoreElements()) {
133 String k = (String)ki.nextElement();
134 Enumeration vi = req.getHeaders(k);
136 while(vi.hasMoreElements()) {
137 String v = (String)vi.nextElement();
146 public MultiMap<String, String> params() {
148 params = Params.urlparams(this);
149 if(method == "POST") {
150 MultiMap<String, String> pp = Params.postparams(this);
158 protected void backflush() {
159 resp.setStatus(respcode);
160 for(String key : outheaders().keySet()) {
161 boolean first = true;
162 for(String val : outheaders().values(key)) {
164 resp.setHeader(key, val);
167 resp.addHeader(key, val);
173 protected OutputStream realoutput() {
175 return(resp.getOutputStream());
176 } catch(IOException e) {
177 /* It is not obvious why this would happen, so I'll wait
178 * until I know whatever might happen to try and implement
179 * meaningful behavior. */
180 throw(new RuntimeException(e));