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

1.1       abaird      1: // JigsawHttpServletRequest.java
1.42    ! bmahe       2: // $Id: JigsawHttpServletRequest.java,v 1.41 1999/02/19 16:12:09 ylafon 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.18      bmahe      25:     // The HeaderDescription enumeration
                     26:     Enumeration e = null;
1.6       abaird     27: 
1.18      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.25      bmahe      51:     /**
                     52:      * The InputStream state codes.
                     53:      */
                     54: 
                     55:     /**
                     56:      * The initial state of the request InputStream 
                     57:      */
1.18      bmahe      58:     private final static int STREAM_STATE_INITIAL = 0;
1.25      bmahe      59: 
                     60:     /**
                     61:      * One reader has been created and is probably used.
                     62:      */
1.18      bmahe      63:     private final static int STREAM_READER_USED = 1;
1.25      bmahe      64: 
                     65:     /**
                     66:      * The input stream is used
                     67:      */
1.18      bmahe      68:     private final static int INPUT_STREAM_USED = 2;
                     69: 
1.25      bmahe      70:     /**
                     71:      * The inputstream state
                     72:      */
1.18      bmahe      73:     private int stream_state = STREAM_STATE_INITIAL;
                     74: 
                     75:     public final static 
                     76:        String STATE_PARAMETERS = "org.w3c.jigsaw.servlet.stateParam";
                     77: 
                     78:     private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
                     79:     /** 
                     80:      * The initial request.
                     81:      */
                     82:     private Request request = null;
                     83:     /**
                     84:      * The attached servlet.
                     85:      */
                     86:     private Servlet servlet = null;
                     87:     /**
                     88:      * The lazyly computed queryParameters hashtable.
                     89:      */
                     90:     private Hashtable queryParameters = null;
                     91: 
1.25      bmahe      92:     protected JigsawHttpServletResponse response = null;
                     93: 
                     94:     protected JigsawHttpSession httpSession = null;
                     95: 
1.29      bmahe      96:     protected JigsawHttpSessionContext sessionContext = null;
1.25      bmahe      97: 
                     98:     protected String requestedSessionID = null;
                     99: 
1.18      bmahe     100:     private synchronized void prepareQueryParameters()
                    101:     {
                    102:        if( queryParameters != null )
                    103:            return;
                    104:        // What kinf of parameters are we expecting ?
                    105:        if ( request.getMethod().equals("POST") ) {
                    106:            // POSTed parameters, check content type:
                    107:            if ((! request.hasContentType())
                    108:                || (type.match(request.getContentType()) < 0) ) 
                    109:                return;
                    110:            // Get and decode the request entity:
                    111:            URLDecoder dec = null;
                    112:            try {
                    113:                InputStream in = request.getInputStream() ;
                    114:                // Notify the client that we are willing to continue
                    115:                Client client = request.getClient();
                    116:                if ( client != null ) 
                    117:                    client.sendContinue();
                    118:                dec = new URLDecoder (in, false);
                    119:                queryParameters = dec.parse () ;
                    120:            } catch (URLDecoderException e) {
                    121:                queryParameters = null;
                    122:            } catch (IOException ex) {
                    123:                queryParameters = null;
                    124:            }
                    125:        } else {
                    126:            // URL encoded parameters:
                    127:            String query = request.getQueryString();
                    128:            if (query != null) {
1.42    ! bmahe     129:                Reader qis = new StringReader(query);
1.18      bmahe     130:                try {
                    131:                    queryParameters = new URLDecoder(qis, false).parse();
                    132:                } catch (Exception ex) {
                    133:                    throw new RuntimeException("Java implementation bug.");
                    134:                }
                    135:            }
                    136:        }
                    137:        // state parameters
                    138:        Hashtable param = (Hashtable)request.getState(STATE_PARAMETERS);
                    139:        if (param != null) {
                    140:            if (queryParameters == null)
                    141:                queryParameters = param;
                    142:            else {
                    143:                Enumeration e= param.keys();
                    144:                while (e.hasMoreElements()) {
                    145:                    String name = (String)e.nextElement();
                    146:                    Object value = queryParameters.get(name);
                    147:                    if (value == null)
                    148:                        queryParameters.put(name,param.get(name));
                    149:                    else  if (value instanceof String[]) {
                    150:                        String oldValues [] = (String[])value;
                    151:                        String newValues [] = new String[oldValues.length+1];
                    152:                        System.arraycopy(oldValues,0,newValues,0,
                    153:                                         oldValues.length);
                    154:                        newValues[oldValues.length] = (String)param.get(name);
                    155:                        queryParameters.put(name,newValues);
                    156:                    } else {
                    157:                        String newValues [] = new String[2];
                    158:                        newValues[0] = (String)param.get(name);
                    159:                        newValues[1] = (String)value;
                    160:                        queryParameters.put(name,newValues);
                    161:                    }
                    162:                }
                    163:            }
                    164:        }
                    165:     }
                    166: 
1.31      bmahe     167:     protected String getURLParameter(String name) {
                    168:        Hashtable urlParameters = null;
                    169:        String query = request.getQueryString();
                    170:        if (query != null) {
1.42    ! bmahe     171:            Reader qis = new StringReader(query);
1.31      bmahe     172:            try {
                    173:                urlParameters = new URLDecoder(qis, false).parse();
                    174:                return (String) urlParameters.get(name);
                    175:            } catch (Exception ex) {
                    176:                throw new RuntimeException("Java implementation bug.");
                    177:            }
                    178:        }
                    179:        return null;
                    180:     }
1.18      bmahe     181: 
                    182:     /**
                    183:      * Return the Charset parameter of content type
                    184:      * @return A String instance
                    185:      */
                    186:     public String getCharacterEncoding() {
                    187:        org.w3c.www.mime.MimeType type = request.getContentType();
                    188:        if ((type != null) && (type.hasParameter("charset"))) {
                    189:            return type.getParameterValue("charset");
                    190:        }
1.19      bmahe     191:        return "ISO-8859-1";
1.18      bmahe     192:     }
                    193: 
                    194:     /**
                    195:      * ServletRequest implementation - Get the length of request data.
                    196:      * @return An int, or <strong>-1</strong>.
                    197:      */
                    198: 
                    199:     public int getContentLength()
                    200:     {
                    201:        return request.getContentLength();
                    202:     }
                    203: 
                    204:     /**
                    205:      * ServletRequest implementation - Get the type of the request's body.
                    206:      * @return A String encoded mime type, or <strong>null</strong>.
                    207:      */
                    208: 
                    209:     public String getContentType()
                    210:     {
                    211:        org.w3c.www.mime.MimeType t = request.getContentType();
                    212:        return (t == null) ? null : t.toString();
                    213:     }
                    214:     
                    215:     /**
                    216:      * ServletRequest implementation - Get the protocol of that request.
                    217:      * @return A String encoded version of the protocol.
                    218:      */
                    219: 
                    220:     public String getProtocol()
                    221:     {
                    222:        return request.getVersion();
                    223:     }
                    224:     
                    225:    /**
                    226:     * ServletRequest implementation - Get the name of queried server.
                    227:     * @return Name of server, as a String.
                    228:     */
                    229: 
                    230:    public String getServerName()
                    231:    {
1.39      bmahe     232:        if (request.isProxy()) {
                    233:           String host = request.getHost();
                    234:           if (host != null)
                    235:               return host.substring(0, host.lastIndexOf(':'));
                    236:        }
1.18      bmahe     237:        return request.getClient().getServer().getHost();
                    238:    }
                    239: 
                    240:    /**
                    241:     * ServletRequest implementation - Get the port of queried server.
                    242:     * @return A port number (int).
                    243:     */
                    244: 
                    245:    public int getServerPort()
                    246:    {
1.39      bmahe     247:        if (request.isProxy()) {
                    248:           String host = request.getHost();
                    249:           if (host != null) {
                    250:               int idx = host.lastIndexOf(':');
                    251:               if (idx == -1)
                    252:                   return 80;
                    253:               return Integer.parseInt(host.substring(idx+1));
                    254:           }
                    255:        }
1.18      bmahe     256:        return request.getClient().getServer().getLocalPort();
                    257:    }
                    258: 
                    259:    /**
                    260:     * ServletRequest implementation - Get the IP address of requests's sender.
                    261:     * @return Numeric IP address, as a String.
                    262:     */
                    263: 
                    264:     public String getRemoteAddr()
                    265:     {
                    266:        return request.getClient().getInetAddress().getHostAddress();
                    267:     }
                    268: 
                    269:     /**
                    270:      * ServletRequest implementation - FQDN of request's sender.
                    271:      * @return Name of client's machine (FQDN).
                    272:      */
                    273: 
                    274:     public String getRemoteHost()
                    275:     {
                    276:        return request.getClient().getInetAddress().getHostName();
                    277:     }
                    278: 
                    279:     /**
                    280:      * ServletRequest implementation - Get real path.
                    281:      * Jigsaw realy has no notion of <em>translation</em> stricto
                    282:      * sensu (it has much better in fact ;-). This is a pain here.
                    283:      * @return Always <strong>null</strong>.
1.42    ! bmahe     284:      * @deprecated since jsdk1.2
1.18      bmahe     285:      */
                    286: 
                    287:     public String getRealPath(String name) {
                    288:        return null;
                    289:     }
                    290: 
1.32      bmahe     291:     protected ServletInputStream is = null;
                    292: 
1.18      bmahe     293:     /**
1.32      bmahe     294:      * Returns an input stream for reading binary data in the request body. 
                    295:      * @exception IllegalStateException if getReader has been called on 
                    296:      * this same request. 
                    297:      * @exception IOException on other I/O related errors. 
1.41      ylafon    298:      * @see JigsawHttpServletRequest#getReader
1.32      bmahe     299:      */    
1.18      bmahe     300:     public ServletInputStream getInputStream()
                    301:        throws IOException
                    302:     {
                    303:        if (stream_state == STREAM_READER_USED)
                    304:            throw new IllegalStateException("Reader used");
                    305:        stream_state = INPUT_STREAM_USED;
                    306:        return getJigsawInputStream();
                    307:     }
                    308:     
1.37      bmahe     309:     /**
1.38      bmahe     310:      * @exception IOException if an IO error occurs
1.37      bmahe     311:      */ 
1.18      bmahe     312:     protected ServletInputStream getJigsawInputStream()
                    313:        throws IOException
                    314:     {
                    315:        // If alredy computed return:
                    316:        if ( is != null )
                    317:            return is;
                    318:        // Built it:
                    319:        InputStream stream = null;
                    320:        if ((stream = request.getInputStream()) == null)
                    321:            stream = new ContentLengthInputStream(null, 0);
                    322:        return is = new JigsawServletInputStream(stream);
                    323:     }
                    324: 
                    325:     /**
                    326:      * ServletRequest implementation - Get a parameter value.
                    327:      * @return The String encoded value for the parameter.
                    328:      */
                    329: 
                    330:     public String getParameter(String name)
                    331:     {
                    332:        prepareQueryParameters();
                    333:        if ( queryParameters != null ) {
                    334:            Object value = queryParameters.get(name);
                    335:            if (value instanceof String[])
                    336:                return ((String[])value)[0];
                    337:            else return (String)value;
                    338:        }
                    339:        else
                    340:            return null;
                    341:     }
                    342: 
                    343:     /**
                    344:      * ServletRequest implementation - Get the parameters value.
                    345:      * @return The String array encoded value for the parameter.
                    346:      */
                    347:     
                    348:     public String[] getParameterValues(String parameter) {
                    349:        Vector V = new Vector(23);
                    350:        prepareQueryParameters();
                    351:        if (queryParameters == null) 
                    352:            return null;
                    353:        Object value = queryParameters.get(parameter);
                    354:        if (value == null) return null;
                    355:        if (value instanceof String[])
                    356:            return (String[])value;
                    357:        else {
                    358:            String [] parameterValues = new String[1];
                    359:            parameterValues[0] = (String)value;
                    360:            return parameterValues;
                    361:        }
                    362:     }
                    363: 
                    364:     /**
                    365:      * ServletRequest implementation - List available parameters.
                    366:      * @return An enumeration of parameter names.
                    367:      */
                    368:     
                    369:     public Enumeration getParameterNames()
                    370:     {
                    371:        prepareQueryParameters();
                    372:        return ((queryParameters == null)
                    373:                ? new EmptyEnumeration()
                    374:                : queryParameters.keys());
                    375:     }
                    376: 
                    377:     /**
                    378:      * ServletRequest implementation - Get an attribute of the request.
                    379:      * This closely match Jigsaw's notion of request state.
                    380:      * @param name The name of the attribute.
                    381:      * @return An object that gives the value of the attribute.
                    382:      */
                    383: 
                    384:     public Object getAttribute(String name) {
                    385:        return request.getState(name);
                    386:     }
                    387: 
1.42    ! bmahe     388:     public void setAttribute(String name, Object object) {
        !           389:        request.setState(name, object);
        !           390:     }
        !           391: 
        !           392:     public Enumeration getAttributeNames() {
        !           393:        return request.getStateNames();
        !           394:     }
        !           395: 
1.18      bmahe     396:     /**
                    397:      * HttpServletRequest implementation - Get the request's method.
                    398:      * @return A String instance.
                    399:      */
                    400: 
                    401:     public  String getMethod()
                    402:     {
                    403:        return request.getMethod();
                    404:     }
                    405: 
                    406:     /**
                    407:      * HttpServletRequest implementation - Get the request's path info.
                    408:      * @return A String instance or <strong>null</strong>.
                    409:      */
                    410: 
                    411:     public  String getPathInfo()
                    412:     {
                    413:        return (String) request.getState(ServletWrapperFrame.STATE_EXTRA_PATH);
                    414:     }
                    415:     
                    416:     /**
                    417:      * HttpServletRequest implementation - Get the request's path translated.
                    418:      * @return A String instance or <strong>null</strong>.
                    419:      */
                    420:     
                    421:     public  String getPathTranslated()
                    422:     {
                    423:        String pathinfo = getPathInfo();
                    424:        if ( pathinfo != null ) {
                    425:            httpd             server  = request.getClient().getServer();
                    426:            ResourceReference rr_root = server.getRootReference();
                    427:            try {
                    428:                LookupState       ls      = new LookupState(pathinfo);
                    429:                LookupResult      lr      = new LookupResult(rr_root);
                    430:                ResourceReference path    = null;
                    431:                
                    432:                try {
                    433:                    FramedResource root = (FramedResource) rr_root.lock();
                    434:                    if (root.lookup(ls,lr))
                    435:                        path = lr.getTarget();
                    436:                } catch (InvalidResourceException ex) {
                    437:                    path = null;
                    438:                } finally {
                    439:                    rr_root.unlock();
                    440:                }
                    441:                
                    442:                if (path != null) {
                    443:                    try {
                    444:                        Resource r = path.lock();
                    445:                        if (r instanceof FileResource)
                    446:                            return ((FileResource)
                    447:                                    r).getFile().getAbsolutePath();
                    448:                        else if (r instanceof DirectoryResource)
                    449:                            return ((DirectoryResource)
                    450:                                    r).getDirectory().getAbsolutePath();
                    451:                        else return null;
                    452:                    } catch (InvalidResourceException ex) {
                    453:                        return null;
                    454:                    } finally {
                    455:                        path.unlock();
                    456:                    }
                    457:                }
                    458:            } catch (org.w3c.tools.resources.ProtocolException ex) {
                    459:                return null;
                    460:            }
                    461:        }
                    462:        return null;
                    463:     }
                    464: 
                    465:     /**
                    466:      * HttpServletRequest implementation - Get the request's query string.
                    467:      * @return A String instance or <strong>null</strong>.
                    468:      */
                    469: 
                    470:     public  String getQueryString()
                    471:     {
                    472:        return request.getQueryString();
                    473:     }
                    474:     
                    475:     /**
                    476:      * HttpServletRequest implementation - Get the request's user (if any).
                    477:      * @return A String instance or <strong>null</strong>.
                    478:      */
                    479: 
                    480:     public String getRemoteUser()
                    481:     {
                    482:        return (String) request.getState(AuthFilter.STATE_AUTHUSER);
                    483:     }
                    484:     
                    485:     /**
                    486:      * HttpServletRequest implementation - Get the request's auth method.
                    487:      * @return A String instance or <strong>null</strong>.
                    488:      */
                    489: 
                    490:     public String getAuthType() {
                    491:        return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
                    492:     }
                    493: 
                    494:     /**
                    495:      * HttpServletRequest implementation - Get a request header as a String.
                    496:      * @return A String instance or <strong>null</strong>.
                    497:      */
                    498: 
                    499:     public String getHeader(String name) {
                    500:        return request.getValue(name);
                    501:     }
                    502: 
                    503:     /**
                    504:      * HttpServletRequest implementation - Get a request header as an int.
                    505:      * @return An int, or <strong>-1</strong>.
                    506:      */
                    507: 
                    508:     public int getIntHeader(String name) {
                    509:        HeaderValue v = request.getHeaderValue(name);
                    510:        if ( v != null ) {
                    511:            Object o = v.getValue();
                    512:            if ((o != null) && (o instanceof Integer))
                    513:                return ((Integer) o).intValue();
1.1       abaird    514:        }
1.18      bmahe     515:        return -1;
1.1       abaird    516:     }
1.18      bmahe     517: 
                    518:     /**
                    519:      * HttpServletRequest implementation - Get a request header as an date.
                    520:      * @return An long (as a number of milliseconds), or <strong>-1</strong>.
                    521:      */
                    522: 
                    523:     public long getDateHeader(String name) {
                    524:        HeaderValue v = request.getHeaderValue(name, null);
                    525:        if ( v != null ) {
                    526:            Object o = v.getValue();
                    527:            if ((o != null) && (o instanceof Long)) 
                    528:                return ((Long) o).longValue();
1.10      bmahe     529:        }
1.18      bmahe     530:        return (long) -1;
1.1       abaird    531:     }
1.8       bmahe     532: 
1.18      bmahe     533:     /**
                    534:      * HttpServletRequest implementation - Get a all header names.
                    535:      * @return An enumeration.
                    536:      */
                    537: 
                    538:     public Enumeration getHeaderNames() {
                    539:        return new HeaderNames(request.enumerateHeaderDescriptions());
                    540:     }
1.16      bmahe     541: 
1.25      bmahe     542:     /**
                    543:      * Gets, from the first line of the HTTP request, 
                    544:      * the part of this request's URI that is to the left of any query string.
                    545:      */
1.18      bmahe     546:     public  String getRequestURI()
                    547:     {
1.39      bmahe     548:        String uri = null;
                    549:        //fixme test
                    550:        if (request.isProxy()) {
                    551:            uri = request.getURL().toExternalForm();
                    552:        } else {
                    553:            uri = request.getURLPath();
                    554:        }
                    555:        if (request.hasQueryString()) {
                    556:            String query = request.getQueryString();
                    557:            int idx = uri.lastIndexOf(query);
                    558:            uri = uri.substring(0, idx-1);
                    559:        }
                    560:        return uri;
1.18      bmahe     561:     }
                    562:     
1.25      bmahe     563:     /**
                    564:      * Gets the part of this request's URI that refers to the servlet 
                    565:      * being invoked. Analogous to the CGI variable SCRIPT_NAME. 
                    566:      */
1.18      bmahe     567:     public  String getServletPath()
                    568:     {
                    569:        ResourceReference rr = request.getTargetResource();
1.16      bmahe     570:        try {
1.18      bmahe     571:            return rr.lock().getURLPath();
1.16      bmahe     572:        } catch (InvalidResourceException ex) {
1.18      bmahe     573:            return null;
1.16      bmahe     574:        } finally {
1.18      bmahe     575:            rr.unlock();
1.16      bmahe     576:        }
1.18      bmahe     577:     }
                    578:     
1.25      bmahe     579:     /**
                    580:      * @return the scheme of the URL used in this request, for example "http",
                    581:      * "https", or "ftp". Different schemes have different rules
                    582:      * for constructing URLs, as noted in RFC 1738. The URL used to create 
                    583:      * a request may be reconstructed using this scheme, the server name 
                    584:      * and port, and additional information such as URIs.
                    585:      */
1.18      bmahe     586:     public String getScheme() {
                    587:        return request.getURL().getProtocol();
                    588:     }
1.16      bmahe     589: 
1.25      bmahe     590:     /**
                    591:      * Gets the array of cookies found in this request.
1.36      bmahe     592:      * @return the array of cookies found in this request or
                    593:      * <strong>null</strong> if there is no cookie.
1.25      bmahe     594:      */
1.18      bmahe     595:     public Cookie[] getCookies() {
1.19      bmahe     596:        HttpCookieList cookielist = request.getCookie();
1.36      bmahe     597:        Cookie[] Scookies = null;
                    598:        if (cookielist != null) {
                    599:            HttpCookie[] cookies = cookielist.getCookies();
                    600:            Scookies = new Cookie[cookies.length];
                    601:            for (int i = 0 ; i < cookies.length ; i++ ) {
                    602:                Scookies[i] = convertCookie(cookies[i]);
                    603:            }
1.19      bmahe     604:        }
                    605:        return Scookies;
                    606:     }
                    607:   
                    608:     protected Cookie convertCookie(HttpCookie httpCookie) {
                    609:        Cookie cookie = new Cookie(httpCookie.getName(),
                    610:                                   httpCookie.getValue());
1.40      bmahe     611:        String val = null;
                    612:        if ((val = httpCookie.getDomain()) != null)
                    613:            cookie.setDomain(val);
                    614:        if ((val = httpCookie.getPath()) != null)
                    615:            cookie.setPath(val);
1.19      bmahe     616:        cookie.setVersion(httpCookie.getVersion());
                    617:        return cookie;
                    618:     }
                    619: 
                    620:     protected String getRequestedSessionIdFromCookie() {
                    621:        HttpCookieList cookielist = request.getCookie();
                    622:        if (cookielist != null) {
                    623:            HttpCookie httpCookie = 
1.28      bmahe     624:                request.getCookie().getCookie(getCookieName());
1.19      bmahe     625:            if (httpCookie != null)
                    626:                return httpCookie.getValue();
                    627:        }
1.18      bmahe     628:        return null;
                    629:     }
1.19      bmahe     630: 
1.31      bmahe     631:     protected String getRequestedSessionIdFromURL() {
                    632:        return getURLParameter(getCookieName());
                    633:     }
                    634: 
1.25      bmahe     635:     /**
                    636:      * Gets the session id specified with this request. This may differ 
                    637:      * from the actual session id. For example, if the request specified an
                    638:      * id for an invalid session, then this will get a new session with a 
                    639:      * new id. 
                    640:      * @return the session id specified by this request, or null if the 
                    641:      * request did not specify a session id.
                    642:      */
1.18      bmahe     643:     public String getRequestedSessionId() {
1.31      bmahe     644:        if (requestedSessionID == null) {
1.27      bmahe     645:            requestedSessionID = getRequestedSessionIdFromCookie();
1.31      bmahe     646:            if (requestedSessionID == null)
                    647:                requestedSessionID = getRequestedSessionIdFromURL();
                    648:        }
1.19      bmahe     649:        return requestedSessionID;
                    650:     }
                    651: 
                    652:     protected synchronized JigsawHttpSessionContext getSessionContext() {
                    653:        return sessionContext;
1.15      benoit    654:     }
1.14      bmahe     655: 
1.25      bmahe     656:     /**
                    657:      * Gets the current valid session associated with this request, if create
                    658:      * is false or, if necessary, creates a new session for the request, if 
                    659:      * create is true. 
                    660:      * @return the session associated with this request or null if create 
                    661:      * was false and no valid session is associated with this request. 
                    662:      */
1.18      bmahe     663:     public HttpSession getSession(boolean create) {
1.19      bmahe     664:        if (httpSession == null) {
1.27      bmahe     665:            httpSession = (JigsawHttpSession)
                    666:                getSession(getRequestedSessionId());
1.29      bmahe     667:            if (httpSession != null) // the client join the session
                    668:                httpSession.setNoMoreNew();
1.27      bmahe     669:        }
                    670:        if (httpSession == null & create) {
                    671:            httpSession = new JigsawHttpSession(getSessionContext(), 
                    672:                                                createCookie());
                    673:            response.addCookie(httpSession.getCookie());
                    674:        } else if (httpSession != null) {
                    675:            httpSession.setLastAccessedTime();
                    676:            if (! httpSession.isValid()) {
1.19      bmahe     677:                httpSession = new JigsawHttpSession(getSessionContext(), 
                    678:                                                    createCookie());
                    679:                response.addCookie(httpSession.getCookie());
                    680:            }
                    681:        }
                    682:        return httpSession;
                    683:     }
                    684: 
1.42    ! bmahe     685:     /**
        !           686:      * Gets the current valid session associated with this request.
        !           687:      * @return the session associated with this request.
        !           688:      */
        !           689:     public HttpSession getSession() {
        !           690:        return getSession(true);
        !           691:     }
        !           692: 
1.33      bmahe     693:     protected String getCookieName() {
                    694:         ObservableProperties props =
                    695:             request.getClient().getServer().getProperties();
                    696:         return props.getString(ServletProps.SERVLET_COOKIE_NAME,
                    697:                                ServletProps.DEFAULT_COOKIE_NAME);
                    698:     }
                    699: 
                    700: 
1.19      bmahe     701:     protected Cookie createCookie() {
1.33      bmahe     702:        ObservableProperties props = 
                    703:            request.getClient().getServer().getProperties();
1.34      bmahe     704:        String name     = props.getString(ServletProps.SERVLET_COOKIE_NAME,
                    705:                                          ServletProps.DEFAULT_COOKIE_NAME);
                    706:        String path     = props.getString(ServletProps.SERVLET_COOKIE_PATH,
                    707:                                          "/");
                    708:        String domain   = props.getString(ServletProps.SERVLET_COOKIE_DOMAIN,
1.35      bmahe     709:                                          null);
1.34      bmahe     710:        String comment  = props.getString(ServletProps.SERVLET_COOKIE_COMMENT,
                    711:                                          null);
                    712:        int maxage      = props.getInteger(ServletProps.SERVLET_COOKIE_MAXAGE,
                    713:                                           86400);
                    714:        boolean secure  = props.getBoolean(ServletProps.SERVLET_COOKIE_SECURE,
                    715:                                           false);
1.33      bmahe     716: 
                    717:        Cookie cookie = new Cookie(name, null);
                    718:        cookie.setPath(path);
                    719:        cookie.setMaxAge(maxage);
                    720:        if ((comment != null) && (comment.length() > 0))
                    721:            cookie.setComment(comment);
                    722:        if ((domain != null) && (domain.length() > 0))
                    723:            cookie.setDomain(domain);
                    724:        cookie.setSecure(secure);
1.19      bmahe     725:        return cookie;
                    726:     }
                    727: 
                    728:     protected HttpSession getSession(String sessionId) {
                    729:        if (sessionId != null)
                    730:            return getSessionContext().getSession(sessionId);
1.18      bmahe     731:        return null;
                    732:     }
1.17      bmahe     733:   
1.25      bmahe     734:     /**
                    735:      * Checks whether this request is associated with a session that is valid 
                    736:      * in the current session context. If it is not valid, the requested
                    737:      * session will never be returned from the getSession method. 
                    738:      * @return true if this request is assocated with a session that is valid 
                    739:      * in the current session context. 
                    740:      */
1.18      bmahe     741:     public boolean isRequestedSessionIdValid() {
1.27      bmahe     742:        JigsawHttpSession session = (JigsawHttpSession) 
                    743:            getSession(getRequestedSessionId());
1.19      bmahe     744:        if (session == null)
                    745:            return false;
                    746:        return (session.isValid());
1.18      bmahe     747:     }
                    748: 
1.25      bmahe     749:     /**
                    750:      * Checks whether the session id specified by this request came in as 
                    751:      * a cookie. (The requested session may not be one returned by the 
                    752:      * getSession method.)
                    753:      * @return true if the session id specified by this request came 
                    754:      * in as a cookie; false otherwise 
                    755:      */
1.18      bmahe     756:     public boolean isRequestedSessionIdFromCookie() {
1.29      bmahe     757:        return (getRequestedSessionIdFromCookie() != null);
1.18      bmahe     758:     }
                    759: 
1.25      bmahe     760:     /**
                    761:      * Checks whether the session id specified by this request came in as 
                    762:      * part of the URL. (The requested session may not be the one returned 
                    763:      * by the getSession method.)
                    764:      * @return true if the session id specified by the request for this 
                    765:      * session came in as part of the URL; false otherwise
1.42    ! bmahe     766:      * @deprecated since jsdk2.1
1.25      bmahe     767:      */
1.18      bmahe     768:     public boolean isRequestedSessionIdFromUrl() {
1.31      bmahe     769:        return (getRequestedSessionIdFromURL() != null);
1.18      bmahe     770:     }
                    771: 
1.42    ! bmahe     772:     /**
        !           773:      * Checks whether the session id specified by this request came in as 
        !           774:      * part of the URL. (The requested session may not be the one returned 
        !           775:      * by the getSession method.)
        !           776:      * @return true if the session id specified by the request for this 
        !           777:      * session came in as part of the URL; false otherwise
        !           778:      */
        !           779:     public boolean isRequestedSessionIdFromURL() {
        !           780:        return (getRequestedSessionIdFromURL() != null);
        !           781:     }
        !           782: 
1.18      bmahe     783:     protected BufferedReader reader = null;
1.25      bmahe     784: 
                    785:     /**
1.32      bmahe     786:      * Returns a buffered reader for reading text in the request body. 
1.25      bmahe     787:      * This translates character set encodings as appropriate.
1.32      bmahe     788:      * @exception UnsupportedEncodingException if the character set encoding 
                    789:      * is unsupported, so the text can't be correctly decoded. 
                    790:      * @exception IllegalStateException if getInputStream has been called on 
                    791:      * this same request. 
                    792:      * @exception IOException on other I/O related errors. 
1.41      ylafon    793:      * @see JigsawHttpServletRequest#getInputStream 
1.25      bmahe     794:      */
1.18      bmahe     795:     public BufferedReader getReader()
                    796:        throws IOException
                    797:     {
                    798:        if (stream_state == INPUT_STREAM_USED)
                    799:            throw new IllegalStateException("Input Stream used");
                    800:        stream_state = STREAM_READER_USED;
                    801:        if (reader == null) {
                    802:            reader = new BufferedReader(
                    803:                        new InputStreamReader( getJigsawInputStream()));
                    804:        }
                    805:        return reader;
1.42    ! bmahe     806:     }
        !           807: 
        !           808:     /**
        !           809:      * @@FIXME
        !           810:      */
        !           811:     protected Request getRequest() {
        !           812:        return request;
1.19      bmahe     813:     }
                    814: 
                    815:     JigsawHttpServletRequest(Servlet servlet, 
                    816:                             Request request, 
1.29      bmahe     817:                             JigsawHttpServletResponse response,
                    818:                             JigsawHttpSessionContext sessionContext) {
                    819:        this.servlet        = servlet;
                    820:        this.request        = request;
                    821:        this.response       = response;
                    822:        this.sessionContext = sessionContext;
1.17      bmahe     823:     }
                    824: 
1.1       abaird    825: }

Webmaster