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