Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletRequest.java, revision 1.6
1.1 abaird 1: // JigsawHttpServletRequest.java
1.6 ! abaird 2: // $Id: JigsawHttpServletRequest.java,v 1.5 1997/02/24 08:57:39 abaird Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package w3c.jigsaw.servlet;
7:
8: import java.io.*;
9: import java.util.*;
10:
11: import java.servlet.*;
12: import java.servlet.http.*;
13:
14: import w3c.jigsaw.http.*;
15: import w3c.jigsaw.forms.*;
16: import w3c.www.http.*;
1.6 ! abaird 17: import w3c.www.mime.*;
1.1 abaird 18: import w3c.jigsaw.auth.AuthFilter; // for auth infos access
1.2 abaird 19:
1.6 ! abaird 20: class HeaderNames implements Enumeration {
! 21: // The HeaderDescription enumeration
! 22: Enumeration e = null;
! 23:
! 24: public boolean hasMoreElements() {
! 25: return e.hasMoreElements();
! 26: }
! 27:
! 28: public Object nextElement() {
! 29: HeaderDescription d = (HeaderDescription) e.nextElement();
! 30: return d.getName();
! 31: }
! 32:
! 33: HeaderNames(Enumeration e) {
! 34: this.e = e ;
! 35: }
! 36:
! 37: }
! 38:
1.2 abaird 39: /**
40: * @author Alexandre Rafalovitch <alex@access.com.au>
41: * @author Anselm Baird-Smith <abaird@w3.org>
42: */
1.1 abaird 43:
44: public class JigsawHttpServletRequest implements HttpServletRequest {
1.6 ! abaird 45: private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
1.1 abaird 46: /**
47: * The initial request.
48: */
1.6 ! abaird 49: private Request request = null;
! 50: /**
! 51: * The attached servlet.
! 52: */
! 53: private Servlet servlet = null;
1.1 abaird 54: /**
55: * The lazyly computed queryParameters hashtable.
56: */
57: private Hashtable queryParameters = null;
58:
59: private void prepareQueryParameters()
60: {
61: if( queryParameters != null )
62: return;
1.6 ! abaird 63: // What kinf of parameters are we expecting ?
! 64: if ( request.getMethod().equals("POST") ) {
! 65: // POSTed parameters, check content type:
! 66: if ((! request.hasContentType())
! 67: || (type.match(request.getContentType()) < 0) )
! 68: return;
! 69: // Get and decode the request entity:
! 70: URLDecoder dec = null;
! 71: try {
! 72: InputStream in = request.getInputStream() ;
! 73: // Notify the client that we are willing to continue
! 74: Client client = request.getClient();
! 75: if ( client != null )
! 76: client.sendContinue();
! 77: dec = new URLDecoder (in, true);
! 78: queryParameters = dec.parse () ;
! 79: } catch (URLDecoderException e) {
! 80: queryParameters = null;
! 81: } catch (IOException ex) {
! 82: queryParameters = null;
! 83: }
! 84: } else {
! 85: // URL encoded parameters:
! 86: String query = request.getQueryString();
! 87: if (query == null)
! 88: return ;
! 89: InputStream qis = new StringBufferInputStream(query);
! 90: try {
! 91: queryParameters = new URLDecoder(qis).parse();
! 92: } catch (Exception ex) {
! 93: throw new RuntimeException("Java implementation bug.");
! 94: }
1.1 abaird 95: }
96: }
97:
1.6 ! abaird 98: /**
! 99: * ServletRequest implementation - Get the length of request data.
! 100: * @return An int, or <strong>-1</strong>.
! 101: */
! 102:
1.1 abaird 103: public int getContentLength()
104: {
105: return request.getContentLength();
106: }
1.6 ! abaird 107:
! 108: /**
! 109: * ServletRequest implementation - Get the type of the request's body.
! 110: * @return A String encoded mime type, or <strong>null</strong>.
! 111: */
! 112:
1.1 abaird 113: public String getContentType()
114: {
1.5 abaird 115: w3c.www.mime.MimeType t = request.getContentType();
116: return (t == null) ? null : t.toString();
1.1 abaird 117: }
1.6 ! abaird 118:
! 119: /**
! 120: * ServletRequest implementation - Get the protocol of that request.
! 121: * @return A String encoded version of the protocol.
! 122: */
! 123:
1.1 abaird 124: public String getProtocol()
125: {
126: return request.getVersion();
127: }
128:
1.6 ! abaird 129: /**
! 130: * ServletRequest implementation - Get the name of queried server.
! 131: * @return Name of server, as a String.
! 132: */
1.1 abaird 133:
134: public String getServerName()
135: {
136: return request.getClient().getServer().getHost();
137: }
138:
1.6 ! abaird 139: /**
! 140: * ServletRequest implementation - Get the port of queried server.
! 141: * @return A port number (int).
! 142: */
! 143:
1.1 abaird 144: public int getServerPort()
145: {
146: return request.getClient().getServer().getLocalPort();
147: }
148:
149: /**
1.6 ! abaird 150: * ServletRequest implementation - Get the IP address of requests's sender.
! 151: * @return Numeric IP address, as a String.
1.1 abaird 152: */
153:
1.6 ! abaird 154: public String getRemoteAddr()
1.1 abaird 155: {
1.6 ! abaird 156: return request.getClient().getInetAddress().getHostAddress();
1.1 abaird 157: }
1.6 ! abaird 158:
! 159: /**
! 160: * ServletRequest implementation - FQDN of request's sender.
! 161: * @return Name of client's machine (FQDN).
! 162: */
! 163:
! 164: public String getRemoteHost()
1.1 abaird 165: {
1.6 ! abaird 166: return request.getClient().getInetAddress().getHostName();
1.1 abaird 167: }
168:
1.6 ! abaird 169: /**
! 170: * ServletRequest implementation - Get real path.
! 171: * Jigsaw realy has no notion of <em>translation</em> stricto
! 172: * sensu (it has much better in fact ;-). This is a pain here.
! 173: * @return Always <strong>null</strong>.
! 174: */
! 175:
! 176: public String getRealPath(String name) {
1.1 abaird 177: return null;
178: }
179:
1.6 ! abaird 180: /**
! 181: * ServletRequest interface - Get the input stream to read data.
! 182: * @return An input stream instance (may be <strong>null</strong>).
! 183: */
! 184:
! 185: protected ServletInputStream is = null;
! 186: public ServletInputStream getInputStream()
! 187: throws IOException
1.1 abaird 188: {
1.6 ! abaird 189: // If alredy computed return:
! 190: if ( is != null )
! 191: return is;
! 192: // Built it:
! 193: InputStream stream = null;
! 194: if ((stream = request.getInputStream()) == null)
! 195: stream = new ContentLengthInputStream(null, 0);
! 196: return is = new JigsawServletInputStream(stream);
1.1 abaird 197: }
1.6 ! abaird 198:
! 199: /**
! 200: * ServletRequest implementation - Get a parameter value.
! 201: * @return The String encoded value for the parameter.
! 202: */
1.1 abaird 203:
1.6 ! abaird 204: public String getParameter(String name)
1.1 abaird 205: {
1.6 ! abaird 206: prepareQueryParameters();
! 207: if ( queryParameters != null )
! 208: return (String) queryParameters.get(name);
! 209: else
! 210: return null;
1.1 abaird 211: }
1.6 ! abaird 212:
! 213: /**
! 214: * ServletRequest implementation - List available parameters.
! 215: * @return An enumeration of parameter names.
! 216: */
1.1 abaird 217:
1.6 ! abaird 218: public Enumeration getParameterNames()
1.1 abaird 219: {
1.6 ! abaird 220: prepareQueryParameters();
! 221: return queryParameters.keys();
1.1 abaird 222: }
223:
1.6 ! abaird 224: /**
! 225: * ServletRequest implementation - Get an attribute of the request.
! 226: * This closely match Jigsaw's notion of request state.
! 227: * @param name The name of the attribute.
! 228: * @return An object that gives the value of the attribute.
! 229: */
! 230:
! 231: public Object getAttribute(String name) {
! 232: return request.getState(name);
! 233: }
! 234:
! 235: /**
! 236: * HttpServletRequest implementation - Get the request's method.
! 237: * @return A String instance.
! 238: */
! 239:
1.1 abaird 240: public String getMethod()
241: {
242: return request.getMethod();
243: }
244:
1.6 ! abaird 245: /**
! 246: * HttpServletRequest implementation - Get the request's path info.
! 247: * @return A String instance or <strong>null</strong>.
! 248: */
! 249:
1.1 abaird 250: public String getPathInfo()
251: {
252: return (String) request.getState(ServletWrapper.STATE_EXTRA_PATH);
253: }
254:
1.6 ! abaird 255: /**
! 256: * HttpServletRequest implementation - Get the request's path translated.
! 257: * @return A String instance or <strong>null</strong>.
! 258: */
! 259:
1.1 abaird 260: public String getPathTranslated()
261: {
1.6 ! abaird 262: String pathinfo = getPathInfo();
! 263: if ( pathinfo != null )
! 264: // FIXME
! 265: return null;
1.1 abaird 266: return null;
267: }
268:
1.6 ! abaird 269: /**
! 270: * HttpServletRequest implementation - Get the request's query string.
! 271: * @return A String instance or <strong>null</strong>.
! 272: */
! 273:
1.1 abaird 274: public String getQueryString()
275: {
276: return request.getQueryString();
277: }
278:
1.6 ! abaird 279: /**
! 280: * HttpServletRequest implementation - Get the request's user (if any).
! 281: * @return A String instance or <strong>null</strong>.
! 282: */
! 283:
1.1 abaird 284: public String getRemoteUser()
285: {
286: return (String) request.getState(AuthFilter.STATE_AUTHUSER);
287: }
288:
1.6 ! abaird 289: /**
! 290: * HttpServletRequest implementation - Get the request's auth method.
! 291: * @return A String instance or <strong>null</strong>.
! 292: */
! 293:
! 294: public String getAuthType() {
! 295: return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
! 296: }
! 297:
! 298: /**
! 299: * HttpServletRequest implementation - Get a request header as a String.
! 300: * @return A String instance or <strong>null</strong>.
! 301: */
! 302:
! 303: public String getHeader(String name) {
! 304: return request.getValue(name);
! 305: }
! 306:
! 307: /**
! 308: * HttpServletRequest implementation - Get a request header as an int.
! 309: * @return An int, or <strong>-1</strong>.
! 310: */
! 311:
! 312: public int getIntHeader(String name) {
! 313: HeaderValue v = request.getHeaderValue(name);
! 314: if ( v != null ) {
! 315: Object o = v.getValue();
! 316: if ((o != null) && (o instanceof Integer))
! 317: return ((Integer) o).intValue();
! 318: }
! 319: return -1;
! 320: }
! 321:
! 322: /**
! 323: * HttpServletRequest implementation - Get a request header as an date.
! 324: * @return An long (as a number of milliseconds), or <strong>-1</strong>.
! 325: */
! 326:
! 327: public long getDateHeader(String name) {
! 328: HeaderValue v = request.getHeaderValue(name, null);
! 329: if ( v != null ) {
! 330: Object o = v.getValue();
! 331: if ((o != null) && (o instanceof Long))
! 332: return ((Long) o).longValue();
! 333: }
! 334: return (long) -1;
! 335: }
! 336:
! 337: /**
! 338: * HttpServletRequest implementation - Get a all header names.
! 339: * @return An enumeration.
! 340: */
! 341:
! 342: public Enumeration getHeaderNames() {
! 343: return new HeaderNames(request.enumerateHeaderDescriptions());
! 344: }
! 345:
1.1 abaird 346: public String getRequestURI()
347: {
348: return request.getURL().toExternalForm();
349: }
350:
351: public String getRequestPath()
352: {
353: return request.getURLPath();
354: }
355:
356: public String getServletPath()
357: {
1.4 abaird 358: return request.getTargetResource().getURLPath();
1.1 abaird 359: }
360:
1.6 ! abaird 361: JigsawHttpServletRequest(Servlet servlet, Request request) {
! 362: this.servlet = servlet;
1.1 abaird 363: this.request = request;
364: }
365:
366:
367: }
Webmaster