Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletRequest.java, revision 1.14
1.1 abaird 1: // JigsawHttpServletRequest.java
1.14 ! bmahe 2: // $Id: JigsawHttpServletRequest.java,v 1.13 1997/09/09 09:38:45 bmahe 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:
1.8 bmahe 11: import javax.servlet.*;
12: import javax.servlet.http.*;
1.1 abaird 13:
1.7 abaird 14: import w3c.util.*;
1.1 abaird 15: import w3c.jigsaw.http.*;
16: import w3c.jigsaw.forms.*;
17: import w3c.www.http.*;
1.6 abaird 18: import w3c.www.mime.*;
1.1 abaird 19: import w3c.jigsaw.auth.AuthFilter; // for auth infos access
1.2 abaird 20:
1.14 ! bmahe 21: import w3c.jigsaw.resources.*;
! 22:
1.6 abaird 23: class HeaderNames implements Enumeration {
1.14 ! bmahe 24: // The HeaderDescription enumeration
! 25: Enumeration e = null;
1.6 abaird 26:
1.14 ! bmahe 27: public boolean hasMoreElements() {
! 28: return e.hasMoreElements();
! 29: }
! 30:
! 31: public Object nextElement() {
! 32: HeaderDescription d = (HeaderDescription) e.nextElement();
! 33: return d.getName();
! 34: }
! 35:
! 36: HeaderNames(Enumeration e) {
! 37: this.e = e ;
! 38: }
1.6 abaird 39:
40: }
41:
1.2 abaird 42: /**
43: * @author Alexandre Rafalovitch <alex@access.com.au>
44: * @author Anselm Baird-Smith <abaird@w3.org>
1.12 bmahe 45: * @author Benoit Mahe <bmahe@sophia.inria.fr>
1.2 abaird 46: */
1.1 abaird 47:
48: public class JigsawHttpServletRequest implements HttpServletRequest {
1.9 bmahe 49:
1.14 ! bmahe 50: public final static
! 51: String STATE_PARAMETERS = "w3c.jigsaw.servlet.stateParam";
1.9 bmahe 52:
1.14 ! bmahe 53: private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
! 54: /**
! 55: * The initial request.
! 56: */
! 57: private Request request = null;
! 58: /**
! 59: * The attached servlet.
! 60: */
! 61: private Servlet servlet = null;
! 62: /**
! 63: * The lazyly computed queryParameters hashtable.
! 64: */
! 65: private Hashtable queryParameters = null;
! 66:
! 67: private synchronized void prepareQueryParameters()
! 68: {
! 69: if( queryParameters != null )
! 70: return;
! 71: // What kinf of parameters are we expecting ?
! 72: if ( request.getMethod().equals("POST") ) {
! 73: // POSTed parameters, check content type:
! 74: if ((! request.hasContentType())
! 75: || (type.match(request.getContentType()) < 0) )
! 76: return;
! 77: // Get and decode the request entity:
! 78: URLDecoder dec = null;
! 79: try {
! 80: InputStream in = request.getInputStream() ;
! 81: // Notify the client that we are willing to continue
! 82: Client client = request.getClient();
! 83: if ( client != null )
! 84: client.sendContinue();
! 85: // dec = new URLDecoder (in, true);
! 86: dec = new URLDecoder (in, false);
! 87: queryParameters = dec.parse () ;
! 88: } catch (URLDecoderException e) {
! 89: queryParameters = null;
! 90: } catch (IOException ex) {
! 91: queryParameters = null;
! 92: }
! 93: } else {
! 94: // URL encoded parameters:
! 95: String query = request.getQueryString();
! 96: if (query != null) {
! 97: InputStream qis = new StringBufferInputStream(query);
! 98: try {
! 99: queryParameters = new URLDecoder(qis, false).parse();
! 100: } catch (Exception ex) {
! 101: throw new RuntimeException("Java implementation bug.");
1.1 abaird 102: }
1.14 ! bmahe 103: }
1.1 abaird 104: }
1.14 ! bmahe 105: // state parameters
! 106: Hashtable param = (Hashtable)request.getState(STATE_PARAMETERS);
! 107: if (param != null) {
! 108: if (queryParameters == null)
! 109: queryParameters = param;
! 110: else {
! 111: Enumeration e= param.keys();
! 112: while (e.hasMoreElements()) {
! 113: String name = (String)e.nextElement();
1.10 bmahe 114: Object value = queryParameters.get(name);
1.14 ! bmahe 115: if (value == null)
! 116: queryParameters.put(name,param.get(name));
! 117: else if (value instanceof String[]) {
! 118: String oldValues [] = (String[])value;
! 119: String newValues [] = new String[oldValues.length+1];
! 120: System.arraycopy(oldValues,0,newValues,0,oldValues.length);
! 121: newValues[oldValues.length] = (String)param.get(name);
! 122: queryParameters.put(name,newValues);
! 123: } else {
! 124: String newValues [] = new String[2];
! 125: newValues[0] = (String)param.get(name);
! 126: newValues[1] = (String)value;
! 127: queryParameters.put(name,newValues);
! 128: }
1.10 bmahe 129: }
1.14 ! bmahe 130: }
1.1 abaird 131: }
1.14 ! bmahe 132: }
1.8 bmahe 133:
1.14 ! bmahe 134: /**
! 135: * ServletRequest implementation - Get the length of request data.
! 136: * @return An int, or <strong>-1</strong>.
! 137: */
! 138:
! 139: public int getContentLength()
! 140: {
! 141: return request.getContentLength();
! 142: }
! 143:
! 144: /**
! 145: * ServletRequest implementation - Get the type of the request's body.
! 146: * @return A String encoded mime type, or <strong>null</strong>.
! 147: */
! 148:
! 149: public String getContentType()
! 150: {
! 151: w3c.www.mime.MimeType t = request.getContentType();
! 152: return (t == null) ? null : t.toString();
! 153: }
! 154:
! 155: /**
! 156: * ServletRequest implementation - Get the protocol of that request.
! 157: * @return A String encoded version of the protocol.
! 158: */
! 159:
! 160: public String getProtocol()
! 161: {
! 162: return request.getVersion();
! 163: }
! 164:
! 165: /**
! 166: * ServletRequest implementation - Get the name of queried server.
! 167: * @return Name of server, as a String.
! 168: */
! 169:
! 170: public String getServerName()
! 171: {
! 172: return request.getClient().getServer().getHost();
! 173: }
! 174:
! 175: /**
! 176: * ServletRequest implementation - Get the port of queried server.
! 177: * @return A port number (int).
! 178: */
! 179:
! 180: public int getServerPort()
! 181: {
! 182: return request.getClient().getServer().getLocalPort();
! 183: }
! 184:
! 185: /**
! 186: * ServletRequest implementation - Get the IP address of requests's sender.
! 187: * @return Numeric IP address, as a String.
! 188: */
! 189:
! 190: public String getRemoteAddr()
! 191: {
! 192: return request.getClient().getInetAddress().getHostAddress();
! 193: }
! 194:
! 195: /**
! 196: * ServletRequest implementation - FQDN of request's sender.
! 197: * @return Name of client's machine (FQDN).
! 198: */
! 199:
! 200: public String getRemoteHost()
! 201: {
! 202: return request.getClient().getInetAddress().getHostName();
! 203: }
! 204:
! 205: /**
! 206: * ServletRequest implementation - Get real path.
! 207: * Jigsaw realy has no notion of <em>translation</em> stricto
! 208: * sensu (it has much better in fact ;-). This is a pain here.
! 209: * @return Always <strong>null</strong>.
! 210: */
! 211:
! 212: public String getRealPath(String name) {
! 213: return null;
! 214: }
! 215:
! 216: /**
! 217: * ServletRequest interface - Get the input stream to read data.
! 218: * @return An input stream instance (may be <strong>null</strong>).
! 219: */
! 220:
! 221: protected ServletInputStream is = null;
! 222: public ServletInputStream getInputStream()
! 223: throws IOException
! 224: {
! 225: // If alredy computed return:
! 226: if ( is != null )
! 227: return is;
! 228: // Built it:
! 229: InputStream stream = null;
! 230: if ((stream = request.getInputStream()) == null)
! 231: stream = new ContentLengthInputStream(null, 0);
! 232: return is = new JigsawServletInputStream(stream);
! 233: }
! 234:
! 235: /**
! 236: * ServletRequest implementation - Get a parameter value.
! 237: * @return The String encoded value for the parameter.
! 238: */
! 239:
! 240: public String getParameter(String name)
! 241: {
! 242: prepareQueryParameters();
! 243: if ( queryParameters != null ) {
! 244: Object value = queryParameters.get(name);
1.10 bmahe 245: if (value instanceof String[])
1.14 ! bmahe 246: return ((String[])value)[0];
! 247: else return (String)value;
1.1 abaird 248: }
1.14 ! bmahe 249: else
! 250: return null;
! 251: }
! 252:
! 253: /**
! 254: * ServletRequest implementation - Get the parameters value.
! 255: * @return The String array encoded value for the parameter.
! 256: */
! 257:
! 258: public String[] getParameterValues(String parameter) {
! 259: Vector V = new Vector(23);
! 260: prepareQueryParameters();
! 261: if (queryParameters == null)
! 262: return null;
! 263: Object value = queryParameters.get(parameter);
! 264: if (value == null) return null;
! 265: if (value instanceof String[])
! 266: return (String[])value;
! 267: else {
! 268: String [] parameterValues = new String[1];
! 269: parameterValues[0] = (String)value;
! 270: return parameterValues;
! 271: }
! 272: }
! 273:
! 274: /**
! 275: * ServletRequest implementation - List available parameters.
! 276: * @return An enumeration of parameter names.
! 277: */
! 278:
! 279: public Enumeration getParameterNames()
! 280: {
! 281: prepareQueryParameters();
! 282: return ((queryParameters == null)
! 283: ? new EmptyEnumeration()
! 284: : queryParameters.keys());
! 285: }
! 286:
! 287: /**
! 288: * ServletRequest implementation - Get an attribute of the request.
! 289: * This closely match Jigsaw's notion of request state.
! 290: * @param name The name of the attribute.
! 291: * @return An object that gives the value of the attribute.
! 292: */
! 293:
! 294: public Object getAttribute(String name) {
! 295: return request.getState(name);
! 296: }
! 297:
! 298: /**
! 299: * HttpServletRequest implementation - Get the request's method.
! 300: * @return A String instance.
! 301: */
! 302:
! 303: public String getMethod()
! 304: {
! 305: return request.getMethod();
! 306: }
! 307:
! 308: /**
! 309: * HttpServletRequest implementation - Get the request's path info.
! 310: * @return A String instance or <strong>null</strong>.
! 311: */
! 312:
! 313: public String getPathInfo()
! 314: {
! 315: return (String) request.getState(ServletWrapper.STATE_EXTRA_PATH);
! 316: }
! 317:
! 318: /**
! 319: * HttpServletRequest implementation - Get the request's path translated.
! 320: * @return A String instance or <strong>null</strong>.
! 321: */
! 322:
! 323: public String getPathTranslated2()
! 324: {
! 325: String pathinfo = getPathInfo();
! 326: if ( pathinfo != null ) {
! 327: ServletWrapper sw = (ServletWrapper) servlet.getServletConfig();
! 328: File dir = sw.getServletDirectory();
! 329: if (dir == null)
1.1 abaird 330: return null;
1.14 ! bmahe 331: if (pathinfo.charAt(0) == '/')
! 332: pathinfo = pathinfo.substring(1);
! 333: File transpath = new File(sw.getServletDirectory(),pathinfo);
! 334: return transpath.getAbsolutePath();
! 335: }
! 336: return null;
! 337: }
! 338:
! 339: public String getPathTranslated()
! 340: {
! 341: String pathinfo = getPathInfo();
! 342: if ( pathinfo != null ) {
! 343: // FIXME
! 344: //return null;
! 345: httpd server = request.getClient().getServer();
! 346: HTTPResource root = server.root;
! 347: try {
! 348: LookupState ls = new LookupState(pathinfo);
! 349: LookupResult lr = new LookupResult(root);
! 350: HTTPResource path = null;
! 351: if (root.lookup(ls,lr))
! 352: path = lr.getTarget();
! 353: if (path != null)
! 354: if (path instanceof FileResource)
! 355: return ((FileResource)path).getFile().getAbsolutePath();
! 356: else if (path instanceof DirectoryResource)
! 357: return ((DirectoryResource)path).getDirectory().getAbsolutePath();
! 358: else return null;
! 359: else return null;
! 360: }
! 361: catch (HTTPException ex) {return null;}
1.6 abaird 362:
1.14 ! bmahe 363: } //end if
! 364: return null;
! 365: }
! 366:
! 367: /**
! 368: * HttpServletRequest implementation - Get the request's query string.
! 369: * @return A String instance or <strong>null</strong>.
! 370: */
! 371:
! 372: public String getQueryString()
! 373: {
! 374: return request.getQueryString();
! 375: }
! 376:
! 377: /**
! 378: * HttpServletRequest implementation - Get the request's user (if any).
! 379: * @return A String instance or <strong>null</strong>.
! 380: */
! 381:
! 382: public String getRemoteUser()
! 383: {
! 384: return (String) request.getState(AuthFilter.STATE_AUTHUSER);
! 385: }
! 386:
! 387: /**
! 388: * HttpServletRequest implementation - Get the request's auth method.
! 389: * @return A String instance or <strong>null</strong>.
! 390: */
! 391:
! 392: public String getAuthType() {
! 393: return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
! 394: }
! 395:
! 396: /**
! 397: * HttpServletRequest implementation - Get a request header as a String.
! 398: * @return A String instance or <strong>null</strong>.
! 399: */
! 400:
! 401: public String getHeader(String name) {
! 402: return request.getValue(name);
! 403: }
! 404:
! 405: /**
! 406: * HttpServletRequest implementation - Get a request header as an int.
! 407: * @return An int, or <strong>-1</strong>.
! 408: */
! 409:
! 410: public int getIntHeader(String name) {
! 411: HeaderValue v = request.getHeaderValue(name);
! 412: if ( v != null ) {
! 413: Object o = v.getValue();
! 414: if ((o != null) && (o instanceof Integer))
! 415: return ((Integer) o).intValue();
! 416: }
! 417: return -1;
! 418: }
! 419:
! 420: /**
! 421: * HttpServletRequest implementation - Get a request header as an date.
! 422: * @return An long (as a number of milliseconds), or <strong>-1</strong>.
! 423: */
! 424:
! 425: public long getDateHeader(String name) {
! 426: HeaderValue v = request.getHeaderValue(name, null);
! 427: if ( v != null ) {
! 428: Object o = v.getValue();
! 429: if ((o != null) && (o instanceof Long))
! 430: return ((Long) o).longValue();
! 431: }
! 432: return (long) -1;
! 433: }
! 434:
! 435: /**
! 436: * HttpServletRequest implementation - Get a all header names.
! 437: * @return An enumeration.
! 438: */
! 439:
! 440: public Enumeration getHeaderNames() {
! 441: return new HeaderNames(request.enumerateHeaderDescriptions());
! 442: }
! 443:
! 444: public String getRequestURI()
! 445: {
! 446: return request.getURL().toExternalForm();
! 447: }
! 448:
! 449: public String getRequestPath()
! 450: {
! 451: return request.getURLPath();
! 452: }
! 453:
! 454: public String getServletPath()
! 455: {
! 456: return request.getTargetResource().getURLPath();
! 457: }
! 458:
! 459: JigsawHttpServletRequest(Servlet servlet, Request request) {
! 460: this.servlet = servlet;
! 461: this.request = request;
! 462: }
1.1 abaird 463:
1.8 bmahe 464: // method added
465:
466: public String getScheme() {
467: return request.getURL().getProtocol();
468: }
469:
1.1 abaird 470: }
1.8 bmahe 471:
472:
Webmaster