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

1.1       abaird      1: // JigsawHttpServletRequest.java
1.17    ! bmahe       2: // $Id: JigsawHttpServletRequest.java,v 1.16 1998/01/22 14:09:35 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.17    ! bmahe      51:   private final static int STREAM_STATE_INITIAL = 0;
        !            52:   private final static int STREAM_READER_USED = 1;
        !            53:   private final static int INPUT_STREAM_USED = 2;
        !            54: 
        !            55:   private int stream_state = STREAM_STATE_INITIAL;
        !            56: 
1.14      bmahe      57:   public final static 
1.16      bmahe      58:   String STATE_PARAMETERS = "org.w3c.jigsaw.servlet.stateParam";
1.9       bmahe      59: 
1.14      bmahe      60:   private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
                     61:   /** 
                     62:    * The initial request.
                     63:    */
                     64:   private Request request = null;
                     65:   /**
                     66:    * The attached servlet.
                     67:    */
                     68:   private Servlet servlet = null;
                     69:   /**
                     70:    * The lazyly computed queryParameters hashtable.
                     71:    */
                     72:   private Hashtable queryParameters = null;
                     73: 
                     74:   private synchronized void prepareQueryParameters()
                     75:   {
                     76:     if( queryParameters != null )
                     77:       return;
                     78:     // What kinf of parameters are we expecting ?
                     79:     if ( request.getMethod().equals("POST") ) {
                     80:       // POSTed parameters, check content type:
                     81:       if ((! request.hasContentType())
                     82:          || (type.match(request.getContentType()) < 0) ) 
                     83:        return;
                     84:       // Get and decode the request entity:
                     85:       URLDecoder dec = null;
                     86:       try {
                     87:        InputStream in = request.getInputStream() ;
                     88:        // Notify the client that we are willing to continue
                     89:        Client client = request.getClient();
                     90:        if ( client != null ) 
                     91:          client.sendContinue();
                     92:        dec = new URLDecoder (in, false);
                     93:        queryParameters = dec.parse () ;
                     94:       } catch (URLDecoderException e) {
                     95:        queryParameters = null;
                     96:       } catch (IOException ex) {
                     97:        queryParameters = null;
                     98:       }
                     99:     } else {
                    100:       // URL encoded parameters:
                    101:       String query = request.getQueryString();
                    102:       if (query != null) {
                    103:        InputStream qis = new StringBufferInputStream(query);
                    104:        try {
                    105:          queryParameters = new URLDecoder(qis, false).parse();
                    106:        } catch (Exception ex) {
                    107:          throw new RuntimeException("Java implementation bug.");
1.1       abaird    108:        }
1.14      bmahe     109:       }
1.1       abaird    110:     }
1.14      bmahe     111:     // state parameters
                    112:     Hashtable param = (Hashtable)request.getState(STATE_PARAMETERS);
                    113:     if (param != null) {
                    114:       if (queryParameters == null)
                    115:        queryParameters = param;
                    116:       else {
                    117:        Enumeration e= param.keys();
                    118:        while (e.hasMoreElements()) {
                    119:          String name = (String)e.nextElement();
1.10      bmahe     120:          Object value = queryParameters.get(name);
1.14      bmahe     121:          if (value == null)
                    122:            queryParameters.put(name,param.get(name));
                    123:          else  if (value instanceof String[]) {
                    124:            String oldValues [] = (String[])value;
                    125:            String newValues [] = new String[oldValues.length+1];
                    126:            System.arraycopy(oldValues,0,newValues,0,oldValues.length);
                    127:            newValues[oldValues.length] = (String)param.get(name);
                    128:            queryParameters.put(name,newValues);
                    129:          } else {
                    130:            String newValues [] = new String[2];
                    131:            newValues[0] = (String)param.get(name);
                    132:            newValues[1] = (String)value;
                    133:            queryParameters.put(name,newValues);
                    134:          }
1.10      bmahe     135:        }
1.14      bmahe     136:       }
1.1       abaird    137:     }
1.14      bmahe     138:   }
1.8       bmahe     139: 
1.14      bmahe     140:   /**
                    141:    * ServletRequest implementation - Get the length of request data.
                    142:    * @return An int, or <strong>-1</strong>.
                    143:    */
                    144: 
                    145:   public int getContentLength()
                    146:   {
                    147:     return request.getContentLength();
                    148:   }
                    149: 
                    150:   /**
                    151:    * ServletRequest implementation - Get the type of the request's body.
                    152:    * @return A String encoded mime type, or <strong>null</strong>.
                    153:    */
                    154: 
                    155:   public String getContentType()
                    156:   {
1.16      bmahe     157:     org.w3c.www.mime.MimeType t = request.getContentType();
1.14      bmahe     158:     return (t == null) ? null : t.toString();
                    159:   }
                    160: 
                    161:   /**
                    162:    * ServletRequest implementation - Get the protocol of that request.
                    163:    * @return A String encoded version of the protocol.
                    164:    */
                    165: 
                    166:   public String getProtocol()
                    167:   {
                    168:     return request.getVersion();
                    169:   }
                    170:     
                    171:   /**
                    172:    * ServletRequest implementation - Get the name of queried server.
                    173:    * @return Name of server, as a String.
                    174:    */
                    175: 
                    176:   public String getServerName()
                    177:   {
                    178:     return request.getClient().getServer().getHost();
                    179:   }
                    180:     
                    181:   /**
                    182:    * ServletRequest implementation - Get the port of queried server.
                    183:    * @return A port number (int).
                    184:    */
                    185: 
                    186:   public int getServerPort()
                    187:   {
                    188:     return request.getClient().getServer().getLocalPort();
                    189:   }
                    190: 
                    191:   /**
                    192:    * ServletRequest implementation - Get the IP address of requests's sender.
                    193:    * @return Numeric IP address, as a String.
                    194:    */
                    195: 
                    196:   public String getRemoteAddr()
                    197:   {
                    198:     return request.getClient().getInetAddress().getHostAddress();
                    199:   }
                    200: 
                    201:   /**
                    202:    * ServletRequest implementation - FQDN of request's sender.
                    203:    * @return Name of client's machine (FQDN).
                    204:    */
                    205: 
                    206:   public String getRemoteHost()
                    207:   {
                    208:     return request.getClient().getInetAddress().getHostName();
                    209:   }
                    210: 
                    211:   /**
                    212:    * ServletRequest implementation - Get real path.
                    213:    * Jigsaw realy has no notion of <em>translation</em> stricto
                    214:    * sensu (it has much better in fact ;-). This is a pain here.
                    215:    * @return Always <strong>null</strong>.
                    216:    */
                    217: 
                    218:   public String getRealPath(String name) {
                    219:     return null;
                    220:   }
                    221: 
                    222:   /**
                    223:    * ServletRequest interface - Get the input stream to read data.
                    224:    * @return An input stream instance (may be <strong>null</strong>).
                    225:    */
                    226:     
                    227:   protected ServletInputStream is = null;
                    228:   public ServletInputStream getInputStream()
                    229:     throws IOException
                    230:   {
1.17    ! bmahe     231:     if (stream_state == STREAM_READER_USED)
        !           232:       throw new IllegalStateException("Reader used");
        !           233:     stream_state = INPUT_STREAM_USED;
        !           234:     return getJigsawInputStream();
        !           235:   }
        !           236:     
        !           237:   protected ServletInputStream getJigsawInputStream()
        !           238:     throws IOException
        !           239:   {
1.14      bmahe     240:     // If alredy computed return:
                    241:     if ( is != null )
                    242:       return is;
                    243:     // Built it:
                    244:     InputStream stream = null;
                    245:     if ((stream = request.getInputStream()) == null)
                    246:       stream = new ContentLengthInputStream(null, 0);
                    247:     return is = new JigsawServletInputStream(stream);
                    248:   }
1.17    ! bmahe     249: 
1.14      bmahe     250:   /**
                    251:    * ServletRequest implementation - Get a parameter value.
                    252:    * @return The String encoded value for the parameter.
                    253:    */
                    254: 
                    255:   public String getParameter(String name)
                    256:   {
                    257:     prepareQueryParameters();
                    258:     if ( queryParameters != null ) {
                    259:       Object value = queryParameters.get(name);
1.10      bmahe     260:       if (value instanceof String[])
1.14      bmahe     261:        return ((String[])value)[0];
                    262:       else return (String)value;
1.1       abaird    263:     }
1.14      bmahe     264:     else
                    265:       return null;
                    266:   }
                    267: 
                    268:   /**
                    269:    * ServletRequest implementation - Get the parameters value.
                    270:    * @return The String array encoded value for the parameter.
                    271:    */
                    272: 
                    273:   public String[] getParameterValues(String parameter) {
                    274:     Vector V = new Vector(23);
                    275:     prepareQueryParameters();
                    276:     if (queryParameters == null) 
                    277:       return null;
                    278:     Object value = queryParameters.get(parameter);
                    279:     if (value == null) return null;
                    280:     if (value instanceof String[])
                    281:       return (String[])value;
                    282:     else {
                    283:       String [] parameterValues = new String[1];
                    284:       parameterValues[0] = (String)value;
                    285:       return parameterValues;
                    286:     }
                    287:   }
                    288: 
                    289:   /**
                    290:    * ServletRequest implementation - List available parameters.
                    291:    * @return An enumeration of parameter names.
                    292:    */
                    293: 
                    294:   public Enumeration getParameterNames()
                    295:   {
                    296:     prepareQueryParameters();
                    297:     return ((queryParameters == null)
                    298:            ? new EmptyEnumeration()
                    299:            : queryParameters.keys());
                    300:   }
                    301:     
                    302:   /**
                    303:    * ServletRequest implementation - Get an attribute of the request.
                    304:    * This closely match Jigsaw's notion of request state.
                    305:    * @param name The name of the attribute.
                    306:    * @return An object that gives the value of the attribute.
                    307:    */
                    308: 
                    309:   public Object getAttribute(String name) {
                    310:     return request.getState(name);
                    311:   }
                    312: 
                    313:   /**
                    314:    * HttpServletRequest implementation - Get the request's method.
                    315:    * @return A String instance.
                    316:    */
                    317: 
                    318:   public  String getMethod()
                    319:   {
                    320:     return request.getMethod();
                    321:   }
                    322:     
                    323:   /**
                    324:    * HttpServletRequest implementation - Get the request's path info.
                    325:    * @return A String instance or <strong>null</strong>.
                    326:    */
                    327: 
                    328:   public  String getPathInfo()
                    329:   {
1.16      bmahe     330:     return (String) request.getState(ServletWrapperFrame.STATE_EXTRA_PATH);
1.14      bmahe     331:   }
                    332:     
                    333:   /**
                    334:    * HttpServletRequest implementation - Get the request's path translated.
                    335:    * @return A String instance or <strong>null</strong>.
                    336:    */
                    337:     
                    338:   public  String getPathTranslated()
                    339:   {
                    340:     String pathinfo = getPathInfo();
                    341:     if ( pathinfo != null ) {
1.16      bmahe     342:       httpd             server  = request.getClient().getServer();
                    343:       ResourceReference rr_root = server.getRootReference();
1.14      bmahe     344:       try {
1.16      bmahe     345:        LookupState       ls      = new LookupState(pathinfo);
                    346:        LookupResult      lr      = new LookupResult(rr_root);
                    347:        ResourceReference path    = null;
                    348: 
                    349:        try {
                    350:          FramedResource root = (FramedResource) rr_root.lock();
                    351:          if (root.lookup(ls,lr))
                    352:            path = lr.getTarget();
                    353:        } catch (InvalidResourceException ex) {
                    354:          path = null;
                    355:        } finally {
                    356:          rr_root.unlock();
                    357:        }
                    358: 
                    359:        if (path != null) {
                    360:          try {
                    361:            Resource r = path.lock();
                    362:            if (r instanceof FileResource)
                    363:              return ((FileResource)r).getFile().getAbsolutePath();
                    364:            else if (r instanceof DirectoryResource)
                    365:              return ((DirectoryResource)r).getDirectory().getAbsolutePath();
                    366:            else return null;
                    367:          } catch (InvalidResourceException ex) {
                    368:            return null;
                    369:          } finally {
                    370:            path.unlock();
                    371:          }
                    372:        }
                    373:       } catch (org.w3c.tools.resources.ProtocolException ex) {
                    374:        return null;
1.14      bmahe     375:       }
1.15      benoit    376:     }
1.14      bmahe     377:     return null;
                    378:   }
                    379: 
                    380:   /**
                    381:    * HttpServletRequest implementation - Get the request's query string.
                    382:    * @return A String instance or <strong>null</strong>.
                    383:    */
                    384: 
                    385:   public  String getQueryString()
                    386:   {
                    387:     return request.getQueryString();
                    388:   }
                    389:     
                    390:   /**
                    391:    * HttpServletRequest implementation - Get the request's user (if any).
                    392:    * @return A String instance or <strong>null</strong>.
                    393:    */
                    394: 
                    395:   public String getRemoteUser()
                    396:   {
                    397:     return (String) request.getState(AuthFilter.STATE_AUTHUSER);
                    398:   }
                    399:     
                    400:   /**
                    401:    * HttpServletRequest implementation - Get the request's auth method.
                    402:    * @return A String instance or <strong>null</strong>.
                    403:    */
                    404: 
                    405:   public String getAuthType() {
                    406:     return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
                    407:   }
                    408: 
                    409:   /**
                    410:    * HttpServletRequest implementation - Get a request header as a String.
                    411:    * @return A String instance or <strong>null</strong>.
                    412:    */
                    413: 
                    414:   public String getHeader(String name) {
                    415:     return request.getValue(name);
                    416:   }
                    417: 
                    418:   /**
                    419:    * HttpServletRequest implementation - Get a request header as an int.
                    420:    * @return An int, or <strong>-1</strong>.
                    421:    */
                    422: 
                    423:   public int getIntHeader(String name) {
                    424:     HeaderValue v = request.getHeaderValue(name);
                    425:     if ( v != null ) {
                    426:       Object o = v.getValue();
                    427:       if ((o != null) && (o instanceof Integer))
                    428:        return ((Integer) o).intValue();
                    429:     }
                    430:     return -1;
                    431:   }
                    432: 
                    433:   /**
                    434:    * HttpServletRequest implementation - Get a request header as an date.
                    435:    * @return An long (as a number of milliseconds), or <strong>-1</strong>.
                    436:    */
                    437: 
                    438:   public long getDateHeader(String name) {
                    439:     HeaderValue v = request.getHeaderValue(name, null);
                    440:     if ( v != null ) {
                    441:       Object o = v.getValue();
                    442:       if ((o != null) && (o instanceof Long)) 
                    443:        return ((Long) o).longValue();
                    444:     }
                    445:     return (long) -1;
                    446:   }
                    447: 
                    448:   /**
                    449:    * HttpServletRequest implementation - Get a all header names.
                    450:    * @return An enumeration.
                    451:    */
                    452: 
                    453:   public Enumeration getHeaderNames() {
                    454:     return new HeaderNames(request.enumerateHeaderDescriptions());
                    455:   }
                    456: 
                    457:   public  String getRequestURI()
                    458:   {
                    459:     return request.getURL().toExternalForm();
                    460:   }
                    461:     
                    462:   public  String getRequestPath()
                    463:   {
                    464:     return request.getURLPath();
                    465:   }
                    466:     
                    467:   public  String getServletPath()
                    468:   {
1.16      bmahe     469:     ResourceReference rr = request.getTargetResource();
                    470:     try {
                    471:       return rr.lock().getURLPath();
                    472:     } catch (InvalidResourceException ex) {
                    473:       return null;
                    474:     } finally {
                    475:       rr.unlock();
                    476:     }
1.14      bmahe     477:   }
                    478:     
                    479:   JigsawHttpServletRequest(Servlet servlet, Request request) {
                    480:     this.servlet = servlet;
                    481:     this.request = request;
                    482:   }
1.1       abaird    483: 
1.8       bmahe     484:   // method added 
                    485: 
                    486:   public String getScheme() {
                    487:     return request.getURL().getProtocol();
                    488:   }
1.17    ! bmahe     489: 
        !           490: 
        !           491:   //API 1.2 :
        !           492: 
        !           493:   public Cookie[] getCookies() {
        !           494:     return null;
        !           495:   }
        !           496:   
        !           497:   public String getRequestedSessionId() {
        !           498:     return null;
        !           499:   }
        !           500: 
        !           501:   public HttpSession getSession(boolean create) {
        !           502:     return null;
        !           503:   }
1.8       bmahe     504:   
1.17    ! bmahe     505:   public boolean isRequestedSessionIdValid() {
        !           506:     return false;
        !           507:   }
        !           508: 
        !           509:   public boolean isRequestedSessionIdFromCookie() {
        !           510:     return false;
        !           511:   }
        !           512: 
        !           513:   public boolean isRequestedSessionIdFromUrl() {
        !           514:     return false;
        !           515:   }
        !           516: 
        !           517:   protected BufferedReader reader = null;
        !           518:   public BufferedReader getReader()
        !           519:     throws IOException
        !           520:   {
        !           521:     if (stream_state == INPUT_STREAM_USED)
        !           522:       throw new IllegalStateException("Input Stream used");
        !           523:     stream_state = STREAM_READER_USED;
        !           524:     if (reader == null) {
        !           525:       reader = new BufferedReader(
        !           526:                    new InputStreamReader( getJigsawInputStream()));
        !           527:     }
        !           528:     return reader;
        !           529:   }
        !           530: 
1.1       abaird    531: }
1.8       bmahe     532: 
                    533: 

Webmaster