Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawHttpServletRequest.java, revision 1.46.4.10
1.1 abaird 1: // JigsawHttpServletRequest.java
1.46.4.10! bmahe 2: // $Id: JigsawHttpServletRequest.java,v 1.51 2000/03/13 17:28:51 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.*;
1.46.4.4 bmahe 10: import java.security.Principal;
1.1 abaird 11:
1.8 bmahe 12: import javax.servlet.*;
13: import javax.servlet.http.*;
1.1 abaird 14:
1.16 bmahe 15: import org.w3c.util.*;
16: import org.w3c.jigsaw.http.*;
17: import org.w3c.jigsaw.forms.URLDecoder;
18: import org.w3c.jigsaw.forms.URLDecoderException;
1.46 bmahe 19: import org.w3c.jigsaw.resources.VirtualHostResource;
1.16 bmahe 20: import org.w3c.www.http.*;
21: import org.w3c.www.mime.*;
22: import org.w3c.jigsaw.auth.AuthFilter; // for auth infos access
1.2 abaird 23:
1.16 bmahe 24: import org.w3c.tools.resources.*;
1.14 bmahe 25:
1.6 abaird 26: class HeaderNames implements Enumeration {
1.18 bmahe 27: // The HeaderDescription enumeration
28: Enumeration e = null;
1.6 abaird 29:
1.18 bmahe 30: public boolean hasMoreElements() {
31: return e.hasMoreElements();
32: }
33:
34: public Object nextElement() {
35: HeaderDescription d = (HeaderDescription) e.nextElement();
36: return d.getName();
37: }
38:
39: HeaderNames(Enumeration e) {
40: this.e = e ;
41: }
1.6 abaird 42:
43: }
44:
1.2 abaird 45: /**
46: * @author Alexandre Rafalovitch <alex@access.com.au>
47: * @author Anselm Baird-Smith <abaird@w3.org>
1.12 bmahe 48: * @author Benoit Mahe <bmahe@sophia.inria.fr>
1.2 abaird 49: */
1.1 abaird 50:
51: public class JigsawHttpServletRequest implements HttpServletRequest {
1.9 bmahe 52:
1.25 bmahe 53: /**
54: * The InputStream state codes.
55: */
56:
57: /**
58: * The initial state of the request InputStream
59: */
1.18 bmahe 60: private final static int STREAM_STATE_INITIAL = 0;
1.25 bmahe 61:
62: /**
63: * One reader has been created and is probably used.
64: */
1.18 bmahe 65: private final static int STREAM_READER_USED = 1;
1.25 bmahe 66:
67: /**
68: * The input stream is used
69: */
1.18 bmahe 70: private final static int INPUT_STREAM_USED = 2;
71:
1.25 bmahe 72: /**
73: * The inputstream state
74: */
1.18 bmahe 75: private int stream_state = STREAM_STATE_INITIAL;
76:
77: public final static
78: String STATE_PARAMETERS = "org.w3c.jigsaw.servlet.stateParam";
79:
80: private static MimeType type = MimeType.APPLICATION_X_WWW_FORM_URLENCODED ;
81: /**
82: * The initial request.
83: */
84: private Request request = null;
85: /**
86: * The attached servlet.
87: */
88: private Servlet servlet = null;
89: /**
90: * The lazyly computed queryParameters hashtable.
91: */
92: private Hashtable queryParameters = null;
93:
1.25 bmahe 94: protected JigsawHttpServletResponse response = null;
95:
96: protected JigsawHttpSession httpSession = null;
97:
1.29 bmahe 98: protected JigsawHttpSessionContext sessionContext = null;
1.25 bmahe 99:
100: protected String requestedSessionID = null;
101:
1.45 bmahe 102: private Hashtable mergeParameters(Hashtable source, Hashtable dest) {
103: if (source == null)
104: return dest;
105: if (dest != null) {
106: Enumeration e = source.keys();
107: while (e.hasMoreElements()) {
108: String name = (String)e.nextElement();
109: Object value = dest.get(name);
110: if (value == null)
111: dest.put(name, source.get(name));
112: else if (value instanceof String[]) {
113: String oldValues [] = (String[])value;
114: String newValues [] = new String[oldValues.length+1];
115: System.arraycopy(oldValues,0,newValues,0,
116: oldValues.length);
117: newValues[oldValues.length] = (String)source.get(name);
118: dest.put(name,newValues);
119: } else {
120: String newValues [] = new String[2];
121: newValues[0] = (String)source.get(name);
122: newValues[1] = (String)value;
123: dest.put(name,newValues);
124: }
125: }
126: return dest;
127: } else {
128: return source;
129: }
130: }
131:
132: private synchronized void prepareQueryParameters() {
1.18 bmahe 133: if( queryParameters != null )
134: return;
1.45 bmahe 135: Hashtable postParameters = null;
1.18 bmahe 136: // What kinf of parameters are we expecting ?
137: if ( request.getMethod().equals("POST") ) {
1.45 bmahe 138: postParameters = new Hashtable(2);
1.18 bmahe 139: // POSTed parameters, check content type:
140: if ((! request.hasContentType())
141: || (type.match(request.getContentType()) < 0) )
142: return;
143: // Get and decode the request entity:
144: URLDecoder dec = null;
145: try {
146: InputStream in = request.getInputStream() ;
147: // Notify the client that we are willing to continue
148: Client client = request.getClient();
149: if ( client != null )
150: client.sendContinue();
151: dec = new URLDecoder (in, false);
1.45 bmahe 152: postParameters = dec.parse () ;
1.18 bmahe 153: } catch (URLDecoderException e) {
1.45 bmahe 154: postParameters = null;
1.18 bmahe 155: } catch (IOException ex) {
1.45 bmahe 156: postParameters = null;
1.18 bmahe 157: }
1.45 bmahe 158: }
159: // URL encoded parameters:
1.46.4.10! bmahe 160: String query = getQueryString();
1.45 bmahe 161: if (query != null) {
162: Reader qis = new StringReader(query);
163: try {
164: queryParameters = new URLDecoder(qis, false).parse();
165: } catch (Exception ex) {
166: throw new RuntimeException("Java implementation bug.");
1.18 bmahe 167: }
168: }
1.45 bmahe 169: queryParameters = mergeParameters(postParameters, queryParameters);
1.18 bmahe 170: // state parameters
171: Hashtable param = (Hashtable)request.getState(STATE_PARAMETERS);
1.45 bmahe 172: queryParameters = mergeParameters(param, queryParameters);
1.18 bmahe 173: }
174:
1.31 bmahe 175: protected String getURLParameter(String name) {
176: Hashtable urlParameters = null;
1.46.4.10! bmahe 177: String query = getQueryString();
1.31 bmahe 178: if (query != null) {
1.42 bmahe 179: Reader qis = new StringReader(query);
1.31 bmahe 180: try {
181: urlParameters = new URLDecoder(qis, false).parse();
182: return (String) urlParameters.get(name);
183: } catch (Exception ex) {
184: throw new RuntimeException("Java implementation bug.");
185: }
186: }
187: return null;
188: }
1.18 bmahe 189:
190: /**
191: * Return the Charset parameter of content type
192: * @return A String instance
193: */
194: public String getCharacterEncoding() {
195: org.w3c.www.mime.MimeType type = request.getContentType();
196: if ((type != null) && (type.hasParameter("charset"))) {
197: return type.getParameterValue("charset");
198: }
1.19 bmahe 199: return "ISO-8859-1";
1.18 bmahe 200: }
201:
202: /**
203: * ServletRequest implementation - Get the length of request data.
204: * @return An int, or <strong>-1</strong>.
205: */
206:
1.46.4.10! bmahe 207: public int getContentLength() {
1.18 bmahe 208: return request.getContentLength();
209: }
210:
211: /**
212: * ServletRequest implementation - Get the type of the request's body.
213: * @return A String encoded mime type, or <strong>null</strong>.
214: */
215:
1.46.4.10! bmahe 216: public String getContentType() {
1.18 bmahe 217: org.w3c.www.mime.MimeType t = request.getContentType();
218: return (t == null) ? null : t.toString();
219: }
220:
221: /**
222: * ServletRequest implementation - Get the protocol of that request.
223: * @return A String encoded version of the protocol.
224: */
225:
1.46.4.10! bmahe 226: public String getProtocol() {
1.18 bmahe 227: return request.getVersion();
228: }
229:
1.46.4.4 bmahe 230: protected httpd getServer() {
231: return request.getClient().getServer();
232: }
233:
1.18 bmahe 234: /**
235: * ServletRequest implementation - Get the name of queried server.
236: * @return Name of server, as a String.
237: */
238:
1.46.4.10! bmahe 239: public String getServerName() {
! 240: String host = request.getHost();
! 241: if (host != null)
! 242: return host.substring(0, host.lastIndexOf(':'));
! 243: else
! 244: return getServer().getHost();
1.18 bmahe 245: }
246:
247: /**
248: * ServletRequest implementation - Get the port of queried server.
249: * @return A port number (int).
250: */
251:
1.46.4.10! bmahe 252: public int getServerPort() {
1.39 bmahe 253: if (request.isProxy()) {
254: String host = request.getHost();
255: if (host != null) {
256: int idx = host.lastIndexOf(':');
257: if (idx == -1)
258: return 80;
259: return Integer.parseInt(host.substring(idx+1));
260: }
261: }
1.46.4.4 bmahe 262: return getServer().getLocalPort();
1.18 bmahe 263: }
264:
265: /**
266: * ServletRequest implementation - Get the IP address of requests's sender.
267: * @return Numeric IP address, as a String.
268: */
269:
1.46.4.10! bmahe 270: public String getRemoteAddr() {
1.18 bmahe 271: return request.getClient().getInetAddress().getHostAddress();
272: }
273:
274: /**
275: * ServletRequest implementation - FQDN of request's sender.
276: * @return Name of client's machine (FQDN).
277: */
278:
1.46.4.10! bmahe 279: public String getRemoteHost() {
1.18 bmahe 280: return request.getClient().getInetAddress().getHostName();
281: }
282:
283: /**
284: * ServletRequest implementation - Get real path.
285: * Jigsaw realy has no notion of <em>translation</em> stricto
286: * sensu (it has much better in fact ;-). This is a pain here.
1.46 bmahe 287: * @return the real path.
1.42 bmahe 288: * @deprecated since jsdk1.2
1.18 bmahe 289: */
290:
291: public String getRealPath(String name) {
1.46.4.4 bmahe 292: httpd server = getServer();
1.46 bmahe 293: ResourceReference rr_root = server.getRootReference();
1.46.4.8 bmahe 294: return JigsawServletContext.getRealPath(name,
295: rr_root,
296: request.getTargetResource());
1.18 bmahe 297: }
298:
1.32 bmahe 299: protected ServletInputStream is = null;
300:
1.18 bmahe 301: /**
1.32 bmahe 302: * Returns an input stream for reading binary data in the request body.
303: * @exception IllegalStateException if getReader has been called on
304: * this same request.
305: * @exception IOException on other I/O related errors.
1.41 ylafon 306: * @see JigsawHttpServletRequest#getReader
1.32 bmahe 307: */
1.18 bmahe 308: public ServletInputStream getInputStream()
309: throws IOException
310: {
311: if (stream_state == STREAM_READER_USED)
312: throw new IllegalStateException("Reader used");
313: stream_state = INPUT_STREAM_USED;
314: return getJigsawInputStream();
315: }
316:
1.37 bmahe 317: /**
1.38 bmahe 318: * @exception IOException if an IO error occurs
1.37 bmahe 319: */
1.18 bmahe 320: protected ServletInputStream getJigsawInputStream()
321: throws IOException
322: {
323: // If alredy computed return:
324: if ( is != null )
325: return is;
326: // Built it:
327: InputStream stream = null;
328: if ((stream = request.getInputStream()) == null)
329: stream = new ContentLengthInputStream(null, 0);
330: return is = new JigsawServletInputStream(stream);
331: }
332:
333: /**
334: * ServletRequest implementation - Get a parameter value.
335: * @return The String encoded value for the parameter.
336: */
337:
1.46.4.10! bmahe 338: public String getParameter(String name) {
1.18 bmahe 339: prepareQueryParameters();
340: if ( queryParameters != null ) {
341: Object value = queryParameters.get(name);
342: if (value instanceof String[])
343: return ((String[])value)[0];
344: else return (String)value;
345: }
346: else
347: return null;
348: }
349:
350: /**
351: * ServletRequest implementation - Get the parameters value.
352: * @return The String array encoded value for the parameter.
353: */
354:
355: public String[] getParameterValues(String parameter) {
356: Vector V = new Vector(23);
357: prepareQueryParameters();
358: if (queryParameters == null)
359: return null;
360: Object value = queryParameters.get(parameter);
361: if (value == null) return null;
362: if (value instanceof String[])
363: return (String[])value;
364: else {
365: String [] parameterValues = new String[1];
366: parameterValues[0] = (String)value;
367: return parameterValues;
368: }
369: }
370:
371: /**
372: * ServletRequest implementation - List available parameters.
373: * @return An enumeration of parameter names.
374: */
375:
1.46.4.10! bmahe 376: public Enumeration getParameterNames() {
1.18 bmahe 377: prepareQueryParameters();
378: return ((queryParameters == null)
379: ? new EmptyEnumeration()
380: : queryParameters.keys());
381: }
382:
383: /**
384: * ServletRequest implementation - Get an attribute of the request.
385: * This closely match Jigsaw's notion of request state.
386: * @param name The name of the attribute.
387: * @return An object that gives the value of the attribute.
388: */
389:
390: public Object getAttribute(String name) {
391: return request.getState(name);
392: }
393:
1.42 bmahe 394: public void setAttribute(String name, Object object) {
395: request.setState(name, object);
396: }
397:
1.46.4.1 bmahe 398: /**
399: * Removes an attribute from this request. This method is not
400: * generally needed as attributes only persist as long as the request
401: * is being handled.
402: *
403: * <p>Attribute names should follow the same conventions as
404: * package names. Names beginning with <code>java.*</code>,
405: * <code>javax.*</code>, and <code>com.sun.*</code>, are
406: * reserved for use by Sun Microsystems.
407: *
408: * @param name a <code>String</code> specifying
409: * the name of the attribute to remove
410: */
411: public void removeAttribute(String name) {
1.46.4.2 bmahe 412: request.delState(name);
1.46.4.1 bmahe 413: }
414:
1.42 bmahe 415: public Enumeration getAttributeNames() {
416: return request.getStateNames();
417: }
418:
1.18 bmahe 419: /**
1.46.4.1 bmahe 420: * Returns the preferred <code>Locale</code> that the client will
421: * accept content in, based on the Accept-Language header.
422: * If the client request doesn't provide an Accept-Language header,
423: * this method returns the default locale for the server.
424: *
425: * @return the preferred <code>Locale</code> for the client
426: */
427: public Locale getLocale() {
1.46.4.2 bmahe 428: return (Locale)getLocales().nextElement();
1.46.4.1 bmahe 429: }
430:
431: /**
432: * Returns an <code>Enumeration</code> of <code>Locale</code> objects
433: * indicating, in decreasing order starting with the preferred locale, the
434: * locales that are acceptable to the client based on the Accept-Language
435: * header.
436: * If the client request doesn't provide an Accept-Language header,
437: * this method returns an <code>Enumeration</code> containing one
438: * <code>Locale</code>, the default locale for the server.
439: *
440: * @return an <code>Enumeration</code> of preferred
441: * <code>Locale</code> objects for the client
442: */
443: public Enumeration getLocales() {
1.46.4.2 bmahe 444: HttpAcceptLanguage languages[] = request.getAcceptLanguage();
445: if (languages == null) {
446: Vector def = new Vector();
447: def.addElement(Locale.getDefault());
448: return def.elements();
449: }
450:
1.46.4.6 bmahe 451: //LinkedList is better, but we must be JDK1.1 compliant
452: Vector locales = new Vector();
1.46.4.2 bmahe 453:
454: for (int i = 0 ; i < languages.length ; i++) {
455: HttpAcceptLanguage language = languages[i];
456: double quality = language.getQuality();
457: String lang = language.getLanguage();
458: String country = "";
459: int idx = lang.indexOf('-');
460: if (idx > -1) {
461: country = lang.substring(idx + 1).trim();
462: lang = lang.substring(0, idx).trim();
463: }
464: // insert the Locale in ordered list
1.46.4.6 bmahe 465: int qidx = 0;
466: int size = locales.size();
467: if (size > 0) {
468: QLocale ql = (QLocale) locales.firstElement();
469: while ((qidx < size) && (ql.getLanguageQuality() >= quality)) {
470: try {
471: ql = (QLocale) locales.elementAt(++qidx);
472: } catch (ArrayIndexOutOfBoundsException ex) {
473: //end of vector, so append
474: }
475: }
476: locales.insertElementAt(new QLocale(lang, country, quality),
477: qidx);
478: } else {
479: locales.addElement(new QLocale(lang, country, quality));
1.46.4.2 bmahe 480: }
481: }
482: // because Locale is final :(
483: int size = locales.size();
484: Vector vlocale = new Vector(size);
485: for (int i = 0 ; i < size ; i ++) {
1.46.4.6 bmahe 486: vlocale.addElement(((QLocale)locales.elementAt(i)).getLocale());
1.46.4.2 bmahe 487: }
488: return vlocale.elements();
1.46.4.1 bmahe 489: }
490:
491: /**
492: * Returns a boolean indicating whether this request was made using a
493: * secure channel, such as HTTPS.
494: *
495: * @return a boolean indicating if the request was made using a
496: * secure channel
497: */
498:
499: public boolean isSecure() {
1.46.4.3 bmahe 500: // only https secure?
501: return (request.getURL().getProtocol().equalsIgnoreCase("https"));
1.46.4.1 bmahe 502: }
503:
504: /**
1.18 bmahe 505: * HttpServletRequest implementation - Get the request's method.
506: * @return A String instance.
507: */
508:
1.46.4.10! bmahe 509: public String getMethod() {
1.18 bmahe 510: return request.getMethod();
511: }
512:
513: /**
514: * HttpServletRequest implementation - Get the request's path info.
515: * @return A String instance or <strong>null</strong>.
516: */
517:
1.46.4.10! bmahe 518: public String getPathInfo() {
! 519: if (request.hasState(JigsawRequestDispatcher.PATH_INFO_P)) {
! 520: String pathinfo =
! 521: (String) request.getState(JigsawRequestDispatcher.PATH_INFO_P);
! 522: return (pathinfo.equals("/")) ? null : pathinfo;
1.46.4.8 bmahe 523: }
1.46.4.10! bmahe 524: return null;
1.18 bmahe 525: }
526:
527: /**
528: * HttpServletRequest implementation - Get the request's path translated.
529: * @return A String instance or <strong>null</strong>.
530: */
531:
1.46.4.10! bmahe 532: public String getPathTranslated() {
1.18 bmahe 533: String pathinfo = getPathInfo();
1.46 bmahe 534: if ( pathinfo != null )
535: return getRealPath(pathinfo);
1.18 bmahe 536: return null;
537: }
538:
539: /**
1.46.4.1 bmahe 540: * Returns the portion of the request URI that indicates the context
541: * of the request. The context path always comes first in a request
542: * URI. The path starts with a "/" character but does not end with a "/"
543: * character. For servlets in the default (root) context, this method
544: * returns "".
545: * @return a <code>String</code> specifying the portion of the request
546: * URI that indicates the context of the request
547: */
548: public String getContextPath() {
1.46.4.5 bmahe 549: throw new RuntimeException("Not Yet Implemented");
1.46.4.1 bmahe 550: }
551:
1.46.4.10! bmahe 552: public boolean hasQueryString() {
! 553: if (request.hasState(JigsawRequestDispatcher.QUERY_STRING_P)) {
! 554: return true;
! 555: } else {
! 556: return request.hasQueryString();
! 557: }
! 558: }
! 559:
1.46.4.1 bmahe 560: /**
1.18 bmahe 561: * HttpServletRequest implementation - Get the request's query string.
562: * @return A String instance or <strong>null</strong>.
563: */
1.46.4.10! bmahe 564: public String getQueryString() {
! 565: if (request.hasState(JigsawRequestDispatcher.QUERY_STRING_P)) {
! 566: return (String)
! 567: request.getState(JigsawRequestDispatcher.QUERY_STRING_P);
! 568: } else {
! 569: return request.getQueryString();
! 570: }
1.18 bmahe 571: }
572:
573: /**
574: * HttpServletRequest implementation - Get the request's user (if any).
575: * @return A String instance or <strong>null</strong>.
576: */
577:
1.46.4.10! bmahe 578: public String getRemoteUser() {
1.18 bmahe 579: return (String) request.getState(AuthFilter.STATE_AUTHUSER);
580: }
581:
582: /**
1.46.4.1 bmahe 583: * Returns a boolean indicating whether the authenticated user is included
584: * in the specified logical "role". Roles and role membership can be
585: * defined using deployment descriptors. If the user has not been
586: * authenticated, the method returns <code>false</code>.
587: *
588: * @param role a <code>String</code> specifying the name of the role
589: * @return a <code>boolean</code> indicating whether the user making this
590: * request belongs to a given role; <code>false</code> if the user has not
591: * been authenticated
592: */
593:
594: public boolean isUserInRole(String role) {
1.46.4.5 bmahe 595: throw new RuntimeException("Not Yet Implemented");
1.46.4.1 bmahe 596: }
597:
598: /**
599: * Returns a <code>java.security.Principal</code> object containing
600: * the name of the current authenticated user. If the user has not been
601: * authenticated, the method returns <code>null</code>.
602: *
603: * @return a <code>java.security.Principal</code> containing
604: * the name of the user making this request; <code>null</code> if the
605: * user has not been authenticated
606: */
1.46.4.4 bmahe 607: public Principal getUserPrincipal() {
608: return new PrincipalImpl(getRemoteUser());
1.46.4.1 bmahe 609: }
610:
611: /**
1.18 bmahe 612: * HttpServletRequest implementation - Get the request's auth method.
613: * @return A String instance or <strong>null</strong>.
614: */
615:
616: public String getAuthType() {
617: return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
618: }
619:
620: /**
621: * HttpServletRequest implementation - Get a request header as a String.
622: * @return A String instance or <strong>null</strong>.
623: */
624:
625: public String getHeader(String name) {
626: return request.getValue(name);
627: }
628:
629: /**
1.46.4.1 bmahe 630: * Returns all the values of the specified request header
631: * as an <code>Enumeration</code> of <code>String</code> objects.
632: *
633: * <p>Some headers, such as <code>Accept-Language</code> can be sent
634: * by clients as several headers each with a different value rather than
635: * sending the header as a comma separated list.
636: *
1.46.4.4 bmahe 637: * <p>WARNING, this can't happen with Jigsaw, all multiple values are
638: * grouped in one, and only one, header. So, this method always return
639: * ONE header value.
640: *
1.46.4.1 bmahe 641: * <p>If the request did not include any headers
642: * of the specified name, this method returns an empty
643: * <code>Enumeration</code>.
644: * The header name is case insensitive. You can use
645: * this method with any request header.
646: *
647: * @param name a <code>String</code> specifying the header name
648: * @return a <code>Enumeration</code> containing the values of the
649: * requested header, or <code>null</code> if the request does not
650: * have any headers of that name
651: */
652:
653: public Enumeration getHeaders(String name) {
1.46.4.4 bmahe 654: String value = getHeader(name);
655: String array[] = { value };
656: return new ArrayEnumeration(array);
1.46.4.1 bmahe 657: }
658:
659: /**
1.18 bmahe 660: * HttpServletRequest implementation - Get a request header as an int.
661: * @return An int, or <strong>-1</strong>.
662: */
663:
664: public int getIntHeader(String name) {
665: HeaderValue v = request.getHeaderValue(name);
666: if ( v != null ) {
667: Object o = v.getValue();
668: if ((o != null) && (o instanceof Integer))
669: return ((Integer) o).intValue();
1.1 abaird 670: }
1.18 bmahe 671: return -1;
1.1 abaird 672: }
1.18 bmahe 673:
674: /**
675: * HttpServletRequest implementation - Get a request header as an date.
676: * @return An long (as a number of milliseconds), or <strong>-1</strong>.
677: */
678:
679: public long getDateHeader(String name) {
680: HeaderValue v = request.getHeaderValue(name, null);
681: if ( v != null ) {
682: Object o = v.getValue();
683: if ((o != null) && (o instanceof Long))
684: return ((Long) o).longValue();
1.10 bmahe 685: }
1.18 bmahe 686: return (long) -1;
1.1 abaird 687: }
1.8 bmahe 688:
1.18 bmahe 689: /**
690: * HttpServletRequest implementation - Get a all header names.
691: * @return An enumeration.
692: */
693:
694: public Enumeration getHeaderNames() {
695: return new HeaderNames(request.enumerateHeaderDescriptions());
696: }
1.16 bmahe 697:
1.25 bmahe 698: /**
699: * Gets, from the first line of the HTTP request,
700: * the part of this request's URI that is to the left of any query string.
701: */
1.46.4.10! bmahe 702: public String getRequestURI() {
1.39 bmahe 703: String uri = null;
1.46.4.10! bmahe 704: if (request.hasState(JigsawRequestDispatcher.REQUEST_URI_P)) {
! 705: uri = (String)
! 706: request.getState(JigsawRequestDispatcher.REQUEST_URI_P);
1.39 bmahe 707: } else {
1.46.4.10! bmahe 708: //fixme test
! 709: if (request.isProxy()) {
! 710: uri = request.getURL().toExternalForm();
! 711: } else {
! 712: uri = request.getURLPath();
! 713: }
! 714: if (hasQueryString()) {
! 715: String query = getQueryString();
! 716: int idx = uri.lastIndexOf(query);
! 717: uri = uri.substring(0, idx-1);
! 718: }
1.39 bmahe 719: }
720: return uri;
1.18 bmahe 721: }
722:
1.46.4.1 bmahe 723: /**
724: * Returns a {@link RequestDispatcher} object that acts as a wrapper for
725: * the resource located at the given path.
726: * A <code>RequestDispatcher</code> object can be used to forward
727: * a request to the resource or to include the resource in a response.
728: * The resource can be dynamic or static.
729: *
730: * <p>The pathname specified may be relative, although it cannot extend
731: * outside the current servlet context. If the path begins with
732: * a "/" it is interpreted as relative to the current context root.
733: * This method returns <code>null</code> if the servlet container
734: * cannot return a <code>RequestDispatcher</code>.
735: *
736: * <p>The difference between this method and {@link
737: * ServletContext#getRequestDispatcher} is that this method can take a
738: * relative path.
739: *
740: * @param path a <code>String</code> specifying the pathname
741: * to the resource
742: * @return a <code>RequestDispatcher</code> object that acts as a
743: * wrapper for the resource at the specified path
744: * @see RequestDispatcher
745: * @see ServletContext#getRequestDispatcher
746: */
747: public RequestDispatcher getRequestDispatcher(String path) {
1.46.4.4 bmahe 748: if (path == null) {
749: throw new IllegalArgumentException("null");
750: }
1.46.4.10! bmahe 751: String urlpath = null;
! 752: ResourceReference rr = request.getTargetResource();
! 753: if (! path.startsWith("/")) {
! 754: String uri = null;
1.46.4.4 bmahe 755: try {
1.46.4.10! bmahe 756: ResourceReference rrp = rr.lock().getParent();
! 757: try {
! 758: Resource r = rrp.lock();
! 759: uri = r.getURLPath();
! 760: } catch (InvalidResourceException irex) {
! 761: return null;
! 762: } finally {
! 763: rrp.unlock();
! 764: }
! 765: } catch (InvalidResourceException ex) {
1.46.4.4 bmahe 766: return null;
767: } finally {
1.46.4.10! bmahe 768: rr.unlock();
1.46.4.4 bmahe 769: }
1.46.4.10! bmahe 770: urlpath = ( uri.endsWith("/") ? uri+path : uri+"/"+path );
! 771: } else {
! 772: urlpath = path;
1.46.4.4 bmahe 773: }
774: return JigsawRequestDispatcher.getRequestDispatcher(urlpath,
1.46.4.10! bmahe 775: getServer(),
! 776: rr);
1.46.4.1 bmahe 777: }
778:
1.25 bmahe 779: /**
780: * Gets the part of this request's URI that refers to the servlet
781: * being invoked. Analogous to the CGI variable SCRIPT_NAME.
782: */
1.46.4.10! bmahe 783: public String getServletPath() {
! 784: if (request.hasState(JigsawRequestDispatcher.SERVLET_PATH_P)) {
! 785: return (String)
! 786: request.getState(JigsawRequestDispatcher.SERVLET_PATH_P);
! 787: } else {
! 788: ResourceReference rr = request.getTargetResource();
! 789: try {
! 790: return rr.lock().getURLPath();
! 791: } catch (InvalidResourceException ex) {
! 792: return null;
! 793: } finally {
! 794: rr.unlock();
! 795: }
1.16 bmahe 796: }
1.18 bmahe 797: }
798:
1.25 bmahe 799: /**
800: * @return the scheme of the URL used in this request, for example "http",
801: * "https", or "ftp". Different schemes have different rules
802: * for constructing URLs, as noted in RFC 1738. The URL used to create
803: * a request may be reconstructed using this scheme, the server name
804: * and port, and additional information such as URIs.
805: */
1.18 bmahe 806: public String getScheme() {
807: return request.getURL().getProtocol();
808: }
1.16 bmahe 809:
1.25 bmahe 810: /**
811: * Gets the array of cookies found in this request.
1.36 bmahe 812: * @return the array of cookies found in this request or
813: * <strong>null</strong> if there is no cookie.
1.25 bmahe 814: */
1.18 bmahe 815: public Cookie[] getCookies() {
1.19 bmahe 816: HttpCookieList cookielist = request.getCookie();
1.36 bmahe 817: Cookie[] Scookies = null;
818: if (cookielist != null) {
819: HttpCookie[] cookies = cookielist.getCookies();
820: Scookies = new Cookie[cookies.length];
821: for (int i = 0 ; i < cookies.length ; i++ ) {
822: Scookies[i] = convertCookie(cookies[i]);
823: }
1.19 bmahe 824: }
825: return Scookies;
826: }
827:
828: protected Cookie convertCookie(HttpCookie httpCookie) {
829: Cookie cookie = new Cookie(httpCookie.getName(),
830: httpCookie.getValue());
1.40 bmahe 831: String val = null;
832: if ((val = httpCookie.getDomain()) != null)
833: cookie.setDomain(val);
834: if ((val = httpCookie.getPath()) != null)
835: cookie.setPath(val);
1.19 bmahe 836: cookie.setVersion(httpCookie.getVersion());
837: return cookie;
838: }
839:
840: protected String getRequestedSessionIdFromCookie() {
841: HttpCookieList cookielist = request.getCookie();
842: if (cookielist != null) {
843: HttpCookie httpCookie =
1.28 bmahe 844: request.getCookie().getCookie(getCookieName());
1.19 bmahe 845: if (httpCookie != null)
846: return httpCookie.getValue();
847: }
1.18 bmahe 848: return null;
849: }
1.19 bmahe 850:
1.31 bmahe 851: protected String getRequestedSessionIdFromURL() {
852: return getURLParameter(getCookieName());
853: }
854:
1.25 bmahe 855: /**
856: * Gets the session id specified with this request. This may differ
857: * from the actual session id. For example, if the request specified an
858: * id for an invalid session, then this will get a new session with a
859: * new id.
860: * @return the session id specified by this request, or null if the
861: * request did not specify a session id.
862: */
1.18 bmahe 863: public String getRequestedSessionId() {
1.31 bmahe 864: if (requestedSessionID == null) {
1.27 bmahe 865: requestedSessionID = getRequestedSessionIdFromCookie();
1.31 bmahe 866: if (requestedSessionID == null)
867: requestedSessionID = getRequestedSessionIdFromURL();
868: }
1.19 bmahe 869: return requestedSessionID;
870: }
871:
872: protected synchronized JigsawHttpSessionContext getSessionContext() {
873: return sessionContext;
1.15 benoit 874: }
1.14 bmahe 875:
1.25 bmahe 876: /**
877: * Gets the current valid session associated with this request, if create
878: * is false or, if necessary, creates a new session for the request, if
879: * create is true.
880: * @return the session associated with this request or null if create
881: * was false and no valid session is associated with this request.
882: */
1.18 bmahe 883: public HttpSession getSession(boolean create) {
1.19 bmahe 884: if (httpSession == null) {
1.27 bmahe 885: httpSession = (JigsawHttpSession)
886: getSession(getRequestedSessionId());
1.29 bmahe 887: if (httpSession != null) // the client join the session
888: httpSession.setNoMoreNew();
1.27 bmahe 889: }
890: if (httpSession == null & create) {
891: httpSession = new JigsawHttpSession(getSessionContext(),
892: createCookie());
893: response.addCookie(httpSession.getCookie());
894: } else if (httpSession != null) {
895: httpSession.setLastAccessedTime();
896: if (! httpSession.isValid()) {
1.19 bmahe 897: httpSession = new JigsawHttpSession(getSessionContext(),
898: createCookie());
899: response.addCookie(httpSession.getCookie());
900: }
901: }
902: return httpSession;
903: }
904:
1.42 bmahe 905: /**
906: * Gets the current valid session associated with this request.
907: * @return the session associated with this request.
908: */
909: public HttpSession getSession() {
910: return getSession(true);
911: }
912:
1.33 bmahe 913: protected String getCookieName() {
914: ObservableProperties props =
915: request.getClient().getServer().getProperties();
916: return props.getString(ServletProps.SERVLET_COOKIE_NAME,
917: ServletProps.DEFAULT_COOKIE_NAME);
918: }
919:
920:
1.19 bmahe 921: protected Cookie createCookie() {
1.33 bmahe 922: ObservableProperties props =
923: request.getClient().getServer().getProperties();
1.34 bmahe 924: String name = props.getString(ServletProps.SERVLET_COOKIE_NAME,
925: ServletProps.DEFAULT_COOKIE_NAME);
926: String path = props.getString(ServletProps.SERVLET_COOKIE_PATH,
927: "/");
928: String domain = props.getString(ServletProps.SERVLET_COOKIE_DOMAIN,
1.35 bmahe 929: null);
1.34 bmahe 930: String comment = props.getString(ServletProps.SERVLET_COOKIE_COMMENT,
931: null);
932: int maxage = props.getInteger(ServletProps.SERVLET_COOKIE_MAXAGE,
933: 86400);
934: boolean secure = props.getBoolean(ServletProps.SERVLET_COOKIE_SECURE,
935: false);
1.33 bmahe 936:
937: Cookie cookie = new Cookie(name, null);
938: cookie.setPath(path);
939: cookie.setMaxAge(maxage);
940: if ((comment != null) && (comment.length() > 0))
941: cookie.setComment(comment);
942: if ((domain != null) && (domain.length() > 0))
943: cookie.setDomain(domain);
944: cookie.setSecure(secure);
1.19 bmahe 945: return cookie;
946: }
947:
948: protected HttpSession getSession(String sessionId) {
949: if (sessionId != null)
950: return getSessionContext().getSession(sessionId);
1.18 bmahe 951: return null;
952: }
1.17 bmahe 953:
1.25 bmahe 954: /**
955: * Checks whether this request is associated with a session that is valid
956: * in the current session context. If it is not valid, the requested
957: * session will never be returned from the getSession method.
958: * @return true if this request is assocated with a session that is valid
959: * in the current session context.
960: */
1.18 bmahe 961: public boolean isRequestedSessionIdValid() {
1.27 bmahe 962: JigsawHttpSession session = (JigsawHttpSession)
963: getSession(getRequestedSessionId());
1.19 bmahe 964: if (session == null)
965: return false;
966: return (session.isValid());
1.18 bmahe 967: }
968:
1.25 bmahe 969: /**
970: * Checks whether the session id specified by this request came in as
971: * a cookie. (The requested session may not be one returned by the
972: * getSession method.)
973: * @return true if the session id specified by this request came
974: * in as a cookie; false otherwise
975: */
1.18 bmahe 976: public boolean isRequestedSessionIdFromCookie() {
1.29 bmahe 977: return (getRequestedSessionIdFromCookie() != null);
1.18 bmahe 978: }
979:
1.25 bmahe 980: /**
981: * Checks whether the session id specified by this request came in as
982: * part of the URL. (The requested session may not be the one returned
983: * by the getSession method.)
984: * @return true if the session id specified by the request for this
985: * session came in as part of the URL; false otherwise
1.42 bmahe 986: * @deprecated since jsdk2.1
1.25 bmahe 987: */
1.18 bmahe 988: public boolean isRequestedSessionIdFromUrl() {
1.31 bmahe 989: return (getRequestedSessionIdFromURL() != null);
1.18 bmahe 990: }
991:
1.42 bmahe 992: /**
993: * Checks whether the session id specified by this request came in as
994: * part of the URL. (The requested session may not be the one returned
995: * by the getSession method.)
996: * @return true if the session id specified by the request for this
997: * session came in as part of the URL; false otherwise
998: */
999: public boolean isRequestedSessionIdFromURL() {
1000: return (getRequestedSessionIdFromURL() != null);
1001: }
1002:
1.18 bmahe 1003: protected BufferedReader reader = null;
1.25 bmahe 1004:
1005: /**
1.32 bmahe 1006: * Returns a buffered reader for reading text in the request body.
1.25 bmahe 1007: * This translates character set encodings as appropriate.
1.32 bmahe 1008: * @exception UnsupportedEncodingException if the character set encoding
1009: * is unsupported, so the text can't be correctly decoded.
1010: * @exception IllegalStateException if getInputStream has been called on
1011: * this same request.
1012: * @exception IOException on other I/O related errors.
1.41 ylafon 1013: * @see JigsawHttpServletRequest#getInputStream
1.25 bmahe 1014: */
1.18 bmahe 1015: public BufferedReader getReader()
1016: throws IOException
1017: {
1018: if (stream_state == INPUT_STREAM_USED)
1019: throw new IllegalStateException("Input Stream used");
1020: stream_state = STREAM_READER_USED;
1021: if (reader == null) {
1.43 ylafon 1022: String enc[] = request.getContentEncoding();
1.44 ylafon 1023: InputStream is = getJigsawInputStream();
1.43 ylafon 1024: if (enc != null) {
1025: InputStreamReader isr = null;
1026: for (int i=0; i < enc.length; i++) {
1027: try {
1028: isr = new InputStreamReader(is, enc[i]);
1029: break;
1030: } catch (UnsupportedEncodingException ex) {
1031: // not a valid encoding, skip it
1032: isr = null;
1033: }
1034: }
1035: if (isr != null)
1036: reader = new BufferedReader(isr);
1037: else
1038: reader = new BufferedReader(new InputStreamReader(is));
1039: } else {
1040: reader = new BufferedReader(new InputStreamReader(is));
1041: }
1.18 bmahe 1042: }
1043: return reader;
1.42 bmahe 1044: }
1045:
1046: /**
1.46.4.4 bmahe 1047: * Get the wrapped Jigsaw Request.
1048: * @return the request
1.42 bmahe 1049: */
1050: protected Request getRequest() {
1051: return request;
1.19 bmahe 1052: }
1053:
1054: JigsawHttpServletRequest(Servlet servlet,
1055: Request request,
1.29 bmahe 1056: JigsawHttpServletResponse response,
1057: JigsawHttpSessionContext sessionContext) {
1058: this.servlet = servlet;
1059: this.request = request;
1060: this.response = response;
1061: this.sessionContext = sessionContext;
1.17 bmahe 1062: }
1063:
1.1 abaird 1064: }
Webmaster