Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletRequest.java, revision 1.16

1.1       abaird      1: // JigsawHttpServletRequest.java
1.16    ! bmahe       2: // $Id: JigsawHttpServletRequest.java,v 1.1 1997/12/19 14:48:14 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: 
1.16    ! bmahe       6: package org.w3c.jigsaw.servlet;
1.1       abaird      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.16    ! bmahe      14: import org.w3c.util.*;
        !            15: import org.w3c.jigsaw.http.*;
        !            16: import org.w3c.jigsaw.forms.URLDecoder;
        !            17: import org.w3c.jigsaw.forms.URLDecoderException;
        !            18: import org.w3c.www.http.*;
        !            19: import org.w3c.www.mime.*;
        !            20: import org.w3c.jigsaw.auth.AuthFilter; // for auth infos access
1.2       abaird     21: 
1.16    ! bmahe      22: import org.w3c.tools.resources.*;
1.14      bmahe      23: 
1.6       abaird     24: class HeaderNames implements Enumeration {
1.14      bmahe      25:   // The HeaderDescription enumeration
                     26:   Enumeration e = null;
1.6       abaird     27: 
1.14      bmahe      28:   public boolean hasMoreElements() {
                     29:     return e.hasMoreElements();
                     30:   }
                     31: 
                     32:   public Object nextElement() {
                     33:     HeaderDescription d = (HeaderDescription) e.nextElement();
                     34:     return d.getName();
                     35:   }
                     36: 
                     37:   HeaderNames(Enumeration e) {
                     38:     this.e = e ;
                     39:   }
1.6       abaird     40: 
                     41: }
                     42: 
1.2       abaird     43: /**
                     44:  *  @author Alexandre Rafalovitch <alex@access.com.au>
                     45:  *  @author Anselm Baird-Smith <abaird@w3.org>
1.12      bmahe      46:  *  @author Benoit Mahe <bmahe@sophia.inria.fr>
1.2       abaird     47:  */
1.1       abaird     48: 
                     49: public class JigsawHttpServletRequest implements HttpServletRequest {
1.9       bmahe      50: 
1.14      bmahe      51:   public final static 
1.16    ! bmahe      52:   String STATE_PARAMETERS = "org.w3c.jigsaw.servlet.stateParam";
1.9       bmahe      53: 
1.14      bmahe      54:   private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
                     55:   /** 
                     56:    * The initial request.
                     57:    */
                     58:   private Request request = null;
                     59:   /**
                     60:    * The attached servlet.
                     61:    */
                     62:   private Servlet servlet = null;
                     63:   /**
                     64:    * The lazyly computed queryParameters hashtable.
                     65:    */
                     66:   private Hashtable queryParameters = null;
                     67: 
                     68:   private synchronized void prepareQueryParameters()
                     69:   {
                     70:     if( queryParameters != null )
                     71:       return;
                     72:     // What kinf of parameters are we expecting ?
                     73:     if ( request.getMethod().equals("POST") ) {
                     74:       // POSTed parameters, check content type:
                     75:       if ((! request.hasContentType())
                     76:          || (type.match(request.getContentType()) < 0) ) 
                     77:        return;
                     78:       // Get and decode the request entity:
                     79:       URLDecoder dec = null;
                     80:       try {
                     81:        InputStream in = request.getInputStream() ;
                     82:        // Notify the client that we are willing to continue
                     83:        Client client = request.getClient();
                     84:        if ( client != null ) 
                     85:          client.sendContinue();
                     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:   {
1.16    ! bmahe     151:     org.w3c.www.mime.MimeType t = request.getContentType();
1.14      bmahe     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:   {
1.16    ! bmahe     315:     return (String) request.getState(ServletWrapperFrame.STATE_EXTRA_PATH);
1.14      bmahe     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 getPathTranslated()
                    324:   {
                    325:     String pathinfo = getPathInfo();
                    326:     if ( pathinfo != null ) {
1.16    ! bmahe     327:       httpd             server  = request.getClient().getServer();
        !           328:       ResourceReference rr_root = server.getRootReference();
1.14      bmahe     329:       try {
1.16    ! bmahe     330:        LookupState       ls      = new LookupState(pathinfo);
        !           331:        LookupResult      lr      = new LookupResult(rr_root);
        !           332:        ResourceReference path    = null;
        !           333: 
        !           334:        try {
        !           335:          FramedResource root = (FramedResource) rr_root.lock();
        !           336:          if (root.lookup(ls,lr))
        !           337:            path = lr.getTarget();
        !           338:        } catch (InvalidResourceException ex) {
        !           339:          path = null;
        !           340:        } finally {
        !           341:          rr_root.unlock();
        !           342:        }
        !           343: 
        !           344:        if (path != null) {
        !           345:          try {
        !           346:            Resource r = path.lock();
        !           347:            if (r instanceof FileResource)
        !           348:              return ((FileResource)r).getFile().getAbsolutePath();
        !           349:            else if (r instanceof DirectoryResource)
        !           350:              return ((DirectoryResource)r).getDirectory().getAbsolutePath();
        !           351:            else return null;
        !           352:          } catch (InvalidResourceException ex) {
        !           353:            return null;
        !           354:          } finally {
        !           355:            path.unlock();
        !           356:          }
        !           357:        }
        !           358:       } catch (org.w3c.tools.resources.ProtocolException ex) {
        !           359:        return null;
1.14      bmahe     360:       }
1.15      benoit    361:     }
1.14      bmahe     362:     return null;
                    363:   }
                    364: 
                    365:   /**
                    366:    * HttpServletRequest implementation - Get the request's query string.
                    367:    * @return A String instance or <strong>null</strong>.
                    368:    */
                    369: 
                    370:   public  String getQueryString()
                    371:   {
                    372:     return request.getQueryString();
                    373:   }
                    374:     
                    375:   /**
                    376:    * HttpServletRequest implementation - Get the request's user (if any).
                    377:    * @return A String instance or <strong>null</strong>.
                    378:    */
                    379: 
                    380:   public String getRemoteUser()
                    381:   {
                    382:     return (String) request.getState(AuthFilter.STATE_AUTHUSER);
                    383:   }
                    384:     
                    385:   /**
                    386:    * HttpServletRequest implementation - Get the request's auth method.
                    387:    * @return A String instance or <strong>null</strong>.
                    388:    */
                    389: 
                    390:   public String getAuthType() {
                    391:     return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
                    392:   }
                    393: 
                    394:   /**
                    395:    * HttpServletRequest implementation - Get a request header as a String.
                    396:    * @return A String instance or <strong>null</strong>.
                    397:    */
                    398: 
                    399:   public String getHeader(String name) {
                    400:     return request.getValue(name);
                    401:   }
                    402: 
                    403:   /**
                    404:    * HttpServletRequest implementation - Get a request header as an int.
                    405:    * @return An int, or <strong>-1</strong>.
                    406:    */
                    407: 
                    408:   public int getIntHeader(String name) {
                    409:     HeaderValue v = request.getHeaderValue(name);
                    410:     if ( v != null ) {
                    411:       Object o = v.getValue();
                    412:       if ((o != null) && (o instanceof Integer))
                    413:        return ((Integer) o).intValue();
                    414:     }
                    415:     return -1;
                    416:   }
                    417: 
                    418:   /**
                    419:    * HttpServletRequest implementation - Get a request header as an date.
                    420:    * @return An long (as a number of milliseconds), or <strong>-1</strong>.
                    421:    */
                    422: 
                    423:   public long getDateHeader(String name) {
                    424:     HeaderValue v = request.getHeaderValue(name, null);
                    425:     if ( v != null ) {
                    426:       Object o = v.getValue();
                    427:       if ((o != null) && (o instanceof Long)) 
                    428:        return ((Long) o).longValue();
                    429:     }
                    430:     return (long) -1;
                    431:   }
                    432: 
                    433:   /**
                    434:    * HttpServletRequest implementation - Get a all header names.
                    435:    * @return An enumeration.
                    436:    */
                    437: 
                    438:   public Enumeration getHeaderNames() {
                    439:     return new HeaderNames(request.enumerateHeaderDescriptions());
                    440:   }
                    441: 
                    442:   public  String getRequestURI()
                    443:   {
                    444:     return request.getURL().toExternalForm();
                    445:   }
                    446:     
                    447:   public  String getRequestPath()
                    448:   {
                    449:     return request.getURLPath();
                    450:   }
                    451:     
                    452:   public  String getServletPath()
                    453:   {
1.16    ! bmahe     454:     ResourceReference rr = request.getTargetResource();
        !           455:     try {
        !           456:       return rr.lock().getURLPath();
        !           457:     } catch (InvalidResourceException ex) {
        !           458:       return null;
        !           459:     } finally {
        !           460:       rr.unlock();
        !           461:     }
1.14      bmahe     462:   }
                    463:     
                    464:   JigsawHttpServletRequest(Servlet servlet, Request request) {
                    465:     this.servlet = servlet;
                    466:     this.request = request;
                    467:   }
1.1       abaird    468: 
1.8       bmahe     469:   // method added 
                    470: 
                    471:   public String getScheme() {
                    472:     return request.getURL().getProtocol();
                    473:   }
                    474:   
1.1       abaird    475: }
1.8       bmahe     476: 
                    477: 

Webmaster