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

1.1       abaird      1: // JigsawHttpServletRequest.java
1.35    ! bmahe       2: // $Id: JigsawHttpServletRequest.java,v 1.34 1998/06/09 09:59:15 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.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) {
                    129:                InputStream qis = new StringBufferInputStream(query);
                    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) {
                    171:            InputStream qis = new StringBufferInputStream(query);
                    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:    {
                    232:        return request.getClient().getServer().getHost();
                    233:    }
                    234: 
                    235:    /**
                    236:     * ServletRequest implementation - Get the port of queried server.
                    237:     * @return A port number (int).
                    238:     */
                    239: 
                    240:    public int getServerPort()
                    241:    {
                    242:        return request.getClient().getServer().getLocalPort();
                    243:    }
                    244: 
                    245:    /**
                    246:     * ServletRequest implementation - Get the IP address of requests's sender.
                    247:     * @return Numeric IP address, as a String.
                    248:     */
                    249: 
                    250:     public String getRemoteAddr()
                    251:     {
                    252:        return request.getClient().getInetAddress().getHostAddress();
                    253:     }
                    254: 
                    255:     /**
                    256:      * ServletRequest implementation - FQDN of request's sender.
                    257:      * @return Name of client's machine (FQDN).
                    258:      */
                    259: 
                    260:     public String getRemoteHost()
                    261:     {
                    262:        return request.getClient().getInetAddress().getHostName();
                    263:     }
                    264: 
                    265:     /**
                    266:      * ServletRequest implementation - Get real path.
                    267:      * Jigsaw realy has no notion of <em>translation</em> stricto
                    268:      * sensu (it has much better in fact ;-). This is a pain here.
                    269:      * @return Always <strong>null</strong>.
                    270:      */
                    271: 
                    272:     public String getRealPath(String name) {
                    273:        return null;
                    274:     }
                    275: 
1.32      bmahe     276:     protected ServletInputStream is = null;
                    277: 
1.18      bmahe     278:     /**
1.32      bmahe     279:      * Returns an input stream for reading binary data in the request body. 
                    280:      * @exception IllegalStateException if getReader has been called on 
                    281:      * this same request. 
                    282:      * @exception IOException on other I/O related errors. 
                    283:      * @see getReader
                    284:      */    
1.18      bmahe     285:     public ServletInputStream getInputStream()
                    286:        throws IOException
                    287:     {
                    288:        if (stream_state == STREAM_READER_USED)
                    289:            throw new IllegalStateException("Reader used");
                    290:        stream_state = INPUT_STREAM_USED;
                    291:        return getJigsawInputStream();
                    292:     }
                    293:     
                    294:     protected ServletInputStream getJigsawInputStream()
                    295:        throws IOException
                    296:     {
                    297:        // If alredy computed return:
                    298:        if ( is != null )
                    299:            return is;
                    300:        // Built it:
                    301:        InputStream stream = null;
                    302:        if ((stream = request.getInputStream()) == null)
                    303:            stream = new ContentLengthInputStream(null, 0);
                    304:        return is = new JigsawServletInputStream(stream);
                    305:     }
                    306: 
                    307:     /**
                    308:      * ServletRequest implementation - Get a parameter value.
                    309:      * @return The String encoded value for the parameter.
                    310:      */
                    311: 
                    312:     public String getParameter(String name)
                    313:     {
                    314:        prepareQueryParameters();
                    315:        if ( queryParameters != null ) {
                    316:            Object value = queryParameters.get(name);
                    317:            if (value instanceof String[])
                    318:                return ((String[])value)[0];
                    319:            else return (String)value;
                    320:        }
                    321:        else
                    322:            return null;
                    323:     }
                    324: 
                    325:     /**
                    326:      * ServletRequest implementation - Get the parameters value.
                    327:      * @return The String array encoded value for the parameter.
                    328:      */
                    329:     
                    330:     public String[] getParameterValues(String parameter) {
                    331:        Vector V = new Vector(23);
                    332:        prepareQueryParameters();
                    333:        if (queryParameters == null) 
                    334:            return null;
                    335:        Object value = queryParameters.get(parameter);
                    336:        if (value == null) return null;
                    337:        if (value instanceof String[])
                    338:            return (String[])value;
                    339:        else {
                    340:            String [] parameterValues = new String[1];
                    341:            parameterValues[0] = (String)value;
                    342:            return parameterValues;
                    343:        }
                    344:     }
                    345: 
                    346:     /**
                    347:      * ServletRequest implementation - List available parameters.
                    348:      * @return An enumeration of parameter names.
                    349:      */
                    350:     
                    351:     public Enumeration getParameterNames()
                    352:     {
                    353:        prepareQueryParameters();
                    354:        return ((queryParameters == null)
                    355:                ? new EmptyEnumeration()
                    356:                : queryParameters.keys());
                    357:     }
                    358: 
                    359:     /**
                    360:      * ServletRequest implementation - Get an attribute of the request.
                    361:      * This closely match Jigsaw's notion of request state.
                    362:      * @param name The name of the attribute.
                    363:      * @return An object that gives the value of the attribute.
                    364:      */
                    365: 
                    366:     public Object getAttribute(String name) {
                    367:        return request.getState(name);
                    368:     }
                    369: 
                    370:     /**
                    371:      * HttpServletRequest implementation - Get the request's method.
                    372:      * @return A String instance.
                    373:      */
                    374: 
                    375:     public  String getMethod()
                    376:     {
                    377:        return request.getMethod();
                    378:     }
                    379: 
                    380:     /**
                    381:      * HttpServletRequest implementation - Get the request's path info.
                    382:      * @return A String instance or <strong>null</strong>.
                    383:      */
                    384: 
                    385:     public  String getPathInfo()
                    386:     {
                    387:        return (String) request.getState(ServletWrapperFrame.STATE_EXTRA_PATH);
                    388:     }
                    389:     
                    390:     /**
                    391:      * HttpServletRequest implementation - Get the request's path translated.
                    392:      * @return A String instance or <strong>null</strong>.
                    393:      */
                    394:     
                    395:     public  String getPathTranslated()
                    396:     {
                    397:        String pathinfo = getPathInfo();
                    398:        if ( pathinfo != null ) {
                    399:            httpd             server  = request.getClient().getServer();
                    400:            ResourceReference rr_root = server.getRootReference();
                    401:            try {
                    402:                LookupState       ls      = new LookupState(pathinfo);
                    403:                LookupResult      lr      = new LookupResult(rr_root);
                    404:                ResourceReference path    = null;
                    405:                
                    406:                try {
                    407:                    FramedResource root = (FramedResource) rr_root.lock();
                    408:                    if (root.lookup(ls,lr))
                    409:                        path = lr.getTarget();
                    410:                } catch (InvalidResourceException ex) {
                    411:                    path = null;
                    412:                } finally {
                    413:                    rr_root.unlock();
                    414:                }
                    415:                
                    416:                if (path != null) {
                    417:                    try {
                    418:                        Resource r = path.lock();
                    419:                        if (r instanceof FileResource)
                    420:                            return ((FileResource)
                    421:                                    r).getFile().getAbsolutePath();
                    422:                        else if (r instanceof DirectoryResource)
                    423:                            return ((DirectoryResource)
                    424:                                    r).getDirectory().getAbsolutePath();
                    425:                        else return null;
                    426:                    } catch (InvalidResourceException ex) {
                    427:                        return null;
                    428:                    } finally {
                    429:                        path.unlock();
                    430:                    }
                    431:                }
                    432:            } catch (org.w3c.tools.resources.ProtocolException ex) {
                    433:                return null;
                    434:            }
                    435:        }
                    436:        return null;
                    437:     }
                    438: 
                    439:     /**
                    440:      * HttpServletRequest implementation - Get the request's query string.
                    441:      * @return A String instance or <strong>null</strong>.
                    442:      */
                    443: 
                    444:     public  String getQueryString()
                    445:     {
                    446:        return request.getQueryString();
                    447:     }
                    448:     
                    449:     /**
                    450:      * HttpServletRequest implementation - Get the request's user (if any).
                    451:      * @return A String instance or <strong>null</strong>.
                    452:      */
                    453: 
                    454:     public String getRemoteUser()
                    455:     {
                    456:        return (String) request.getState(AuthFilter.STATE_AUTHUSER);
                    457:     }
                    458:     
                    459:     /**
                    460:      * HttpServletRequest implementation - Get the request's auth method.
                    461:      * @return A String instance or <strong>null</strong>.
                    462:      */
                    463: 
                    464:     public String getAuthType() {
                    465:        return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
                    466:     }
                    467: 
                    468:     /**
                    469:      * HttpServletRequest implementation - Get a request header as a String.
                    470:      * @return A String instance or <strong>null</strong>.
                    471:      */
                    472: 
                    473:     public String getHeader(String name) {
                    474:        return request.getValue(name);
                    475:     }
                    476: 
                    477:     /**
                    478:      * HttpServletRequest implementation - Get a request header as an int.
                    479:      * @return An int, or <strong>-1</strong>.
                    480:      */
                    481: 
                    482:     public int getIntHeader(String name) {
                    483:        HeaderValue v = request.getHeaderValue(name);
                    484:        if ( v != null ) {
                    485:            Object o = v.getValue();
                    486:            if ((o != null) && (o instanceof Integer))
                    487:                return ((Integer) o).intValue();
1.1       abaird    488:        }
1.18      bmahe     489:        return -1;
1.1       abaird    490:     }
1.18      bmahe     491: 
                    492:     /**
                    493:      * HttpServletRequest implementation - Get a request header as an date.
                    494:      * @return An long (as a number of milliseconds), or <strong>-1</strong>.
                    495:      */
                    496: 
                    497:     public long getDateHeader(String name) {
                    498:        HeaderValue v = request.getHeaderValue(name, null);
                    499:        if ( v != null ) {
                    500:            Object o = v.getValue();
                    501:            if ((o != null) && (o instanceof Long)) 
                    502:                return ((Long) o).longValue();
1.10      bmahe     503:        }
1.18      bmahe     504:        return (long) -1;
1.1       abaird    505:     }
1.8       bmahe     506: 
1.18      bmahe     507:     /**
                    508:      * HttpServletRequest implementation - Get a all header names.
                    509:      * @return An enumeration.
                    510:      */
                    511: 
                    512:     public Enumeration getHeaderNames() {
                    513:        return new HeaderNames(request.enumerateHeaderDescriptions());
                    514:     }
1.16      bmahe     515: 
1.25      bmahe     516:     /**
                    517:      * Gets, from the first line of the HTTP request, 
                    518:      * the part of this request's URI that is to the left of any query string.
                    519:      */
1.18      bmahe     520:     public  String getRequestURI()
                    521:     {
                    522:        return request.getURL().toExternalForm();
                    523:     }
                    524:     
                    525:     public  String getRequestPath()
                    526:     {
                    527:        return request.getURLPath();
                    528:     }
                    529:     
1.25      bmahe     530:     /**
                    531:      * Gets the part of this request's URI that refers to the servlet 
                    532:      * being invoked. Analogous to the CGI variable SCRIPT_NAME. 
                    533:      */
1.18      bmahe     534:     public  String getServletPath()
                    535:     {
                    536:        ResourceReference rr = request.getTargetResource();
1.16      bmahe     537:        try {
1.18      bmahe     538:            return rr.lock().getURLPath();
1.16      bmahe     539:        } catch (InvalidResourceException ex) {
1.18      bmahe     540:            return null;
1.16      bmahe     541:        } finally {
1.18      bmahe     542:            rr.unlock();
1.16      bmahe     543:        }
1.18      bmahe     544:     }
                    545:     
1.25      bmahe     546:     /**
                    547:      * @return the scheme of the URL used in this request, for example "http",
                    548:      * "https", or "ftp". Different schemes have different rules
                    549:      * for constructing URLs, as noted in RFC 1738. The URL used to create 
                    550:      * a request may be reconstructed using this scheme, the server name 
                    551:      * and port, and additional information such as URIs.
                    552:      */
1.18      bmahe     553:     public String getScheme() {
                    554:        return request.getURL().getProtocol();
                    555:     }
1.16      bmahe     556: 
1.25      bmahe     557:     /**
                    558:      * Gets the array of cookies found in this request.
                    559:      * @return the array of cookies found in this request 
                    560:      */
1.18      bmahe     561:     public Cookie[] getCookies() {
1.19      bmahe     562:        HttpCookieList cookielist = request.getCookie();
                    563:        HttpCookie[] cookies = cookielist.getCookies();
                    564:        Cookie[] Scookies = new Cookie[cookies.length];
                    565:        for (int i = 0 ; i < cookies.length ; i++ ) {
                    566:            Scookies[i] = convertCookie(cookies[i]);
                    567:        }
                    568:        return Scookies;
                    569:     }
                    570:   
                    571:     protected Cookie convertCookie(HttpCookie httpCookie) {
                    572:        Cookie cookie = new Cookie(httpCookie.getName(),
                    573:                                   httpCookie.getValue());
                    574:        cookie.setDomain(httpCookie.getDomain());
                    575:        cookie.setPath(httpCookie.getPath());
                    576:        cookie.setVersion(httpCookie.getVersion());
                    577:        return cookie;
                    578:     }
                    579: 
                    580:     protected String getRequestedSessionIdFromCookie() {
                    581:        HttpCookieList cookielist = request.getCookie();
                    582:        if (cookielist != null) {
                    583:            HttpCookie httpCookie = 
1.28      bmahe     584:                request.getCookie().getCookie(getCookieName());
1.19      bmahe     585:            if (httpCookie != null)
                    586:                return httpCookie.getValue();
                    587:        }
1.18      bmahe     588:        return null;
                    589:     }
1.19      bmahe     590: 
1.31      bmahe     591:     protected String getRequestedSessionIdFromURL() {
                    592:        return getURLParameter(getCookieName());
                    593:     }
                    594: 
1.25      bmahe     595:     /**
                    596:      * Gets the session id specified with this request. This may differ 
                    597:      * from the actual session id. For example, if the request specified an
                    598:      * id for an invalid session, then this will get a new session with a 
                    599:      * new id. 
                    600:      * @return the session id specified by this request, or null if the 
                    601:      * request did not specify a session id.
                    602:      */
1.18      bmahe     603:     public String getRequestedSessionId() {
1.31      bmahe     604:        if (requestedSessionID == null) {
1.27      bmahe     605:            requestedSessionID = getRequestedSessionIdFromCookie();
1.31      bmahe     606:            if (requestedSessionID == null)
                    607:                requestedSessionID = getRequestedSessionIdFromURL();
                    608:        }
1.19      bmahe     609:        return requestedSessionID;
                    610:     }
                    611: 
                    612:     protected synchronized JigsawHttpSessionContext getSessionContext() {
                    613:        return sessionContext;
1.15      benoit    614:     }
1.14      bmahe     615: 
1.25      bmahe     616:     /**
                    617:      * Gets the current valid session associated with this request, if create
                    618:      * is false or, if necessary, creates a new session for the request, if 
                    619:      * create is true. 
                    620:      * @return the session associated with this request or null if create 
                    621:      * was false and no valid session is associated with this request. 
                    622:      */
1.18      bmahe     623:     public HttpSession getSession(boolean create) {
1.19      bmahe     624:        if (httpSession == null) {
1.27      bmahe     625:            httpSession = (JigsawHttpSession)
                    626:                getSession(getRequestedSessionId());
1.29      bmahe     627:            if (httpSession != null) // the client join the session
                    628:                httpSession.setNoMoreNew();
1.27      bmahe     629:        }
                    630:        if (httpSession == null & create) {
                    631:            httpSession = new JigsawHttpSession(getSessionContext(), 
                    632:                                                createCookie());
                    633:            response.addCookie(httpSession.getCookie());
                    634:        } else if (httpSession != null) {
                    635:            httpSession.setLastAccessedTime();
                    636:            if (! httpSession.isValid()) {
1.19      bmahe     637:                httpSession = new JigsawHttpSession(getSessionContext(), 
                    638:                                                    createCookie());
                    639:                response.addCookie(httpSession.getCookie());
                    640:            }
                    641:        }
                    642:        return httpSession;
                    643:     }
                    644: 
1.33      bmahe     645:     protected String getCookieName() {
                    646:         ObservableProperties props =
                    647:             request.getClient().getServer().getProperties();
                    648:         return props.getString(ServletProps.SERVLET_COOKIE_NAME,
                    649:                                ServletProps.DEFAULT_COOKIE_NAME);
                    650:     }
                    651: 
                    652: 
1.19      bmahe     653:     protected Cookie createCookie() {
1.33      bmahe     654:        ObservableProperties props = 
                    655:            request.getClient().getServer().getProperties();
1.34      bmahe     656:        String name     = props.getString(ServletProps.SERVLET_COOKIE_NAME,
                    657:                                          ServletProps.DEFAULT_COOKIE_NAME);
                    658:        String path     = props.getString(ServletProps.SERVLET_COOKIE_PATH,
                    659:                                          "/");
                    660:        String domain   = props.getString(ServletProps.SERVLET_COOKIE_DOMAIN,
1.35    ! bmahe     661:                                          null);
1.34      bmahe     662:        String comment  = props.getString(ServletProps.SERVLET_COOKIE_COMMENT,
                    663:                                          null);
                    664:        int maxage      = props.getInteger(ServletProps.SERVLET_COOKIE_MAXAGE,
                    665:                                           86400);
                    666:        boolean secure  = props.getBoolean(ServletProps.SERVLET_COOKIE_SECURE,
                    667:                                           false);
1.33      bmahe     668: 
                    669:        Cookie cookie = new Cookie(name, null);
                    670:        cookie.setPath(path);
                    671:        cookie.setMaxAge(maxage);
                    672:        if ((comment != null) && (comment.length() > 0))
                    673:            cookie.setComment(comment);
                    674:        if ((domain != null) && (domain.length() > 0))
                    675:            cookie.setDomain(domain);
                    676:        cookie.setSecure(secure);
1.19      bmahe     677:        return cookie;
                    678:     }
                    679: 
                    680:     protected HttpSession getSession(String sessionId) {
                    681:        if (sessionId != null)
                    682:            return getSessionContext().getSession(sessionId);
1.18      bmahe     683:        return null;
                    684:     }
1.17      bmahe     685:   
1.25      bmahe     686:     /**
                    687:      * Checks whether this request is associated with a session that is valid 
                    688:      * in the current session context. If it is not valid, the requested
                    689:      * session will never be returned from the getSession method. 
                    690:      * @return true if this request is assocated with a session that is valid 
                    691:      * in the current session context. 
                    692:      */
1.18      bmahe     693:     public boolean isRequestedSessionIdValid() {
1.27      bmahe     694:        JigsawHttpSession session = (JigsawHttpSession) 
                    695:            getSession(getRequestedSessionId());
1.19      bmahe     696:        if (session == null)
                    697:            return false;
                    698:        return (session.isValid());
1.18      bmahe     699:     }
                    700: 
1.25      bmahe     701:     /**
                    702:      * Checks whether the session id specified by this request came in as 
                    703:      * a cookie. (The requested session may not be one returned by the 
                    704:      * getSession method.)
                    705:      * @return true if the session id specified by this request came 
                    706:      * in as a cookie; false otherwise 
                    707:      */
1.18      bmahe     708:     public boolean isRequestedSessionIdFromCookie() {
1.29      bmahe     709:        return (getRequestedSessionIdFromCookie() != null);
1.18      bmahe     710:     }
                    711: 
1.25      bmahe     712:     /**
                    713:      * Checks whether the session id specified by this request came in as 
                    714:      * part of the URL. (The requested session may not be the one returned 
                    715:      * by the getSession method.)
                    716:      * @return true if the session id specified by the request for this 
                    717:      * session came in as part of the URL; false otherwise
                    718:      */
1.18      bmahe     719:     public boolean isRequestedSessionIdFromUrl() {
1.31      bmahe     720:        return (getRequestedSessionIdFromURL() != null);
1.18      bmahe     721:     }
                    722: 
                    723:     protected BufferedReader reader = null;
1.25      bmahe     724: 
                    725:     /**
1.32      bmahe     726:      * Returns a buffered reader for reading text in the request body. 
1.25      bmahe     727:      * This translates character set encodings as appropriate.
1.32      bmahe     728:      * @exception UnsupportedEncodingException if the character set encoding 
                    729:      * is unsupported, so the text can't be correctly decoded. 
                    730:      * @exception IllegalStateException if getInputStream has been called on 
                    731:      * this same request. 
                    732:      * @exception IOException on other I/O related errors. 
                    733:      * @see getInputStream 
1.25      bmahe     734:      */
1.18      bmahe     735:     public BufferedReader getReader()
                    736:        throws IOException
                    737:     {
                    738:        if (stream_state == INPUT_STREAM_USED)
                    739:            throw new IllegalStateException("Input Stream used");
                    740:        stream_state = STREAM_READER_USED;
                    741:        if (reader == null) {
                    742:            reader = new BufferedReader(
                    743:                        new InputStreamReader( getJigsawInputStream()));
                    744:        }
                    745:        return reader;
1.19      bmahe     746:     }
                    747: 
                    748:     JigsawHttpServletRequest(Servlet servlet, 
                    749:                             Request request, 
1.29      bmahe     750:                             JigsawHttpServletResponse response,
                    751:                             JigsawHttpSessionContext sessionContext) {
                    752:        this.servlet        = servlet;
                    753:        this.request        = request;
                    754:        this.response       = response;
                    755:        this.sessionContext = sessionContext;
1.17      bmahe     756:     }
                    757: 
1.1       abaird    758: }

Webmaster