KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > WinstoneRequest


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.security.MessageDigest JavaDoc;
15 import java.security.NoSuchAlgorithmException JavaDoc;
16 import java.security.Principal JavaDoc;
17 import java.text.DateFormat JavaDoc;
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Random JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.Stack JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.TimeZone JavaDoc;
37
38 import javax.servlet.ServletInputStream JavaDoc;
39 import javax.servlet.ServletRequestAttributeEvent JavaDoc;
40 import javax.servlet.ServletRequestAttributeListener JavaDoc;
41 import javax.servlet.ServletRequestListener JavaDoc;
42 import javax.servlet.http.Cookie JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpSession JavaDoc;
45
46
47 /**
48  * Implements the request interface required by the servlet spec.
49  *
50  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
51  * @version $Id: WinstoneRequest.java,v 1.33 2006/08/29 00:55:13 rickknowles Exp $
52  */

53 public class WinstoneRequest implements HttpServletRequest JavaDoc {
54     protected static DateFormat JavaDoc headerDF = new SimpleDateFormat JavaDoc(
55             "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
56     protected static Random JavaDoc rnd = null;
57     static {
58         headerDF.setTimeZone(TimeZone.getTimeZone("GMT"));
59         rnd = new Random JavaDoc(System.currentTimeMillis());
60     }
61
62     // Request header constants
63
static final String JavaDoc CONTENT_LENGTH_HEADER = "Content-Length";
64     static final String JavaDoc CONTENT_TYPE_HEADER = "Content-Type";
65     static final String JavaDoc AUTHORIZATION_HEADER = "Authorization";
66     static final String JavaDoc LOCALE_HEADER = "Accept-Language";
67     static final String JavaDoc HOST_HEADER = "Host";
68     static final String JavaDoc IN_COOKIE_HEADER1 = "Cookie";
69     static final String JavaDoc IN_COOKIE_HEADER2 = "Cookie2";
70     static final String JavaDoc METHOD_HEAD = "HEAD";
71     static final String JavaDoc METHOD_GET = "GET";
72     static final String JavaDoc METHOD_POST = "POST";
73     static final String JavaDoc POST_PARAMETERS = "application/x-www-form-urlencoded";
74
75     protected Map JavaDoc attributes;
76     protected Map JavaDoc parameters;
77     protected Stack JavaDoc attributesStack;
78     protected Stack JavaDoc parametersStack;
79 // protected Map forwardedParameters;
80

81     protected String JavaDoc headers[];
82     protected Cookie JavaDoc cookies[];
83     
84     protected String JavaDoc method;
85     protected String JavaDoc scheme;
86     protected String JavaDoc serverName;
87     protected String JavaDoc requestURI;
88     protected String JavaDoc servletPath;
89     protected String JavaDoc pathInfo;
90     protected String JavaDoc queryString;
91     protected String JavaDoc protocol;
92     protected int contentLength;
93     protected String JavaDoc contentType;
94     protected String JavaDoc encoding;
95     
96     protected int serverPort;
97     protected String JavaDoc remoteIP;
98     protected String JavaDoc remoteName;
99     protected int remotePort;
100     protected String JavaDoc localAddr;
101     protected String JavaDoc localName;
102     protected int localPort;
103     protected Boolean JavaDoc parsedParameters;
104     protected Map JavaDoc requestedSessionIds;
105     protected Map JavaDoc currentSessionIds;
106     protected String JavaDoc deadRequestedSessionId;
107     protected List JavaDoc locales;
108     protected String JavaDoc authorization;
109     protected boolean isSecure;
110     
111     protected WinstoneInputStream inputData;
112     protected BufferedReader JavaDoc inputReader;
113     protected ServletConfiguration servletConfig;
114     protected WebAppConfiguration webappConfig;
115     protected HostGroup hostGroup;
116
117     protected AuthenticationPrincipal authenticatedUser;
118     protected ServletRequestAttributeListener JavaDoc requestAttributeListeners[];
119     protected ServletRequestListener JavaDoc requestListeners[];
120     
121     private MessageDigest JavaDoc md5Digester;
122     
123     private Set JavaDoc usedSessions;
124     
125     /**
126      * InputStream factory method.
127      */

128     public WinstoneRequest() throws IOException JavaDoc {
129         this.attributes = new Hashtable JavaDoc();
130         this.parameters = new Hashtable JavaDoc();
131         this.locales = new ArrayList JavaDoc();
132         this.attributesStack = new Stack JavaDoc();
133         this.parametersStack = new Stack JavaDoc();
134 // this.forwardedParameters = new Hashtable();
135
this.requestedSessionIds = new Hashtable JavaDoc();
136         this.currentSessionIds = new Hashtable JavaDoc();
137         this.usedSessions = new HashSet JavaDoc();
138         this.contentLength = -1;
139         this.isSecure = false;
140         try {
141             this.md5Digester = MessageDigest.getInstance("MD5");
142         } catch (NoSuchAlgorithmException JavaDoc err) {
143             throw new WinstoneException(
144                     "MD5 digester unavailable - what the ...?");
145         }
146     }
147
148     /**
149      * Resets the request to be reused
150      */

151     public void cleanUp() {
152         this.requestListeners = null;
153         this.requestAttributeListeners = null;
154         this.attributes.clear();
155         this.parameters.clear();
156         this.attributesStack.clear();
157         this.parametersStack.clear();
158 // this.forwardedParameters.clear();
159
this.usedSessions.clear();
160         this.headers = null;
161         this.cookies = null;
162         this.method = null;
163         this.scheme = null;
164         this.serverName = null;
165         this.requestURI = null;
166         this.servletPath = null;
167         this.pathInfo = null;
168         this.queryString = null;
169         this.protocol = null;
170         this.contentLength = -1;
171         this.contentType = null;
172         this.encoding = null;
173         this.inputData = null;
174         this.inputReader = null;
175         this.servletConfig = null;
176         this.webappConfig = null;
177         this.hostGroup = null;
178         this.serverPort = -1;
179         this.remoteIP = null;
180         this.remoteName = null;
181         this.remotePort = -1;
182         this.localAddr = null;
183         this.localName = null;
184         this.localPort = -1;
185         this.parsedParameters = null;
186         this.requestedSessionIds.clear();
187         this.currentSessionIds.clear();
188         this.deadRequestedSessionId = null;
189         this.locales.clear();
190         this.authorization = null;
191         this.isSecure = false;
192         this.authenticatedUser = null;
193     }
194
195     /**
196      * Steps through the header array, searching for the first header matching
197      */

198     private String JavaDoc extractFirstHeader(String JavaDoc name) {
199         for (int n = 0; n < this.headers.length; n++) {
200             if (this.headers[n].toUpperCase().startsWith(name.toUpperCase() + ':')) {
201                 return this.headers[n].substring(name.length() + 1).trim(); // 1 for colon
202
}
203         }
204         return null;
205     }
206
207     private Collection JavaDoc extractHeaderNameList() {
208         Collection JavaDoc headerNames = new HashSet JavaDoc();
209         for (int n = 0; n < this.headers.length; n++) {
210             String JavaDoc name = this.headers[n];
211             int colonPos = name.indexOf(':');
212             headerNames.add(name.substring(0, colonPos));
213         }
214         return headerNames;
215     }
216
217     public Map JavaDoc getAttributes() {
218         return this.attributes;
219     }
220
221     public Map JavaDoc getParameters() {
222         return this.parameters;
223     }
224 //
225
// public Map getForwardedParameters() {
226
// return this.forwardedParameters;
227
// }
228

229     public Stack JavaDoc getAttributesStack() {
230         return this.attributesStack;
231     }
232     
233     public Stack JavaDoc getParametersStack() {
234         return this.parametersStack;
235     }
236     
237     public Map JavaDoc getCurrentSessionIds() {
238         return this.currentSessionIds;
239     }
240     
241     public Map JavaDoc getRequestedSessionIds() {
242         return this.requestedSessionIds;
243     }
244     
245     public String JavaDoc getDeadRequestedSessionId() {
246         return this.deadRequestedSessionId;
247     }
248
249     public HostGroup getHostGroup() {
250         return this.hostGroup;
251     }
252
253     public WebAppConfiguration getWebAppConfig() {
254         return this.webappConfig;
255     }
256
257     public ServletConfiguration getServletConfig() {
258         return this.servletConfig;
259     }
260
261     public String JavaDoc getEncoding() {
262         return this.encoding;
263     }
264
265     public Boolean JavaDoc getParsedParameters() {
266         return this.parsedParameters;
267     }
268
269     public List JavaDoc getListLocales() {
270         return this.locales;
271     }
272
273     public void setInputStream(WinstoneInputStream inputData) {
274         this.inputData = inputData;
275     }
276
277     public void setHostGroup(HostGroup hostGroup) {
278         this.hostGroup = hostGroup;
279     }
280
281     public void setWebAppConfig(WebAppConfiguration webappConfig) {
282         this.webappConfig = webappConfig;
283     }
284
285     public void setServletConfig(ServletConfiguration servletConfig) {
286         this.servletConfig = servletConfig;
287     }
288
289     public void setServerPort(int port) {
290         this.serverPort = port;
291     }
292
293     public void setRemoteIP(String JavaDoc remoteIP) {
294         this.remoteIP = remoteIP;
295     }
296
297     public void setRemoteName(String JavaDoc name) {
298         this.remoteName = name;
299     }
300
301     public void setRemotePort(int port) {
302         this.remotePort = port;
303     }
304
305     public void setLocalAddr(String JavaDoc ip) {
306         this.localName = ip;
307     }
308
309     public void setLocalName(String JavaDoc name) {
310         this.localName = name;
311     }
312
313     public void setLocalPort(int port) {
314         this.localPort = port;
315     }
316
317     public void setMethod(String JavaDoc method) {
318         this.method = method;
319     }
320
321     public void setIsSecure(boolean isSecure) {
322         this.isSecure = isSecure;
323     }
324
325     public void setQueryString(String JavaDoc queryString) {
326         this.queryString = queryString;
327     }
328
329     public void setServerName(String JavaDoc name) {
330         this.serverName = name;
331     }
332
333     public void setRequestURI(String JavaDoc requestURI) {
334         this.requestURI = requestURI;
335     }
336
337     public void setScheme(String JavaDoc scheme) {
338         this.scheme = scheme;
339     }
340
341     public void setServletPath(String JavaDoc servletPath) {
342         this.servletPath = servletPath;
343     }
344
345     public void setPathInfo(String JavaDoc pathInfo) {
346         this.pathInfo = pathInfo;
347     }
348
349     public void setProtocol(String JavaDoc protocolString) {
350         this.protocol = protocolString;
351     }
352
353     public void setRemoteUser(AuthenticationPrincipal user) {
354         this.authenticatedUser = user;
355     }
356
357     public void setContentLength(int len) {
358         this.contentLength = len;
359     }
360
361     public void setContentType(String JavaDoc type) {
362         this.contentType = type;
363     }
364
365     public void setAuthorization(String JavaDoc auth) {
366         this.authorization = auth;
367     }
368
369     public void setLocales(List JavaDoc locales) {
370         this.locales = locales;
371     }
372
373     public void setCurrentSessionIds(Map JavaDoc currentSessionIds) {
374         this.currentSessionIds = currentSessionIds;
375     }
376     
377     public void setRequestedSessionIds(Map JavaDoc requestedSessionIds) {
378         this.requestedSessionIds = requestedSessionIds;
379     }
380
381     public void setDeadRequestedSessionId(String JavaDoc deadRequestedSessionId) {
382         this.deadRequestedSessionId = deadRequestedSessionId;
383     }
384
385     public void setEncoding(String JavaDoc encoding) {
386         this.encoding = encoding;
387     }
388
389     public void setParsedParameters(Boolean JavaDoc parsed) {
390         this.parsedParameters = parsed;
391     }
392
393     public void setRequestListeners(ServletRequestListener JavaDoc rl[]) {
394         this.requestListeners = rl;
395     }
396
397     public void setRequestAttributeListeners(
398             ServletRequestAttributeListener JavaDoc ral[]) {
399         this.requestAttributeListeners = ral;
400     }
401
402     /**
403      * Gets parameters from the url encoded parameter string
404      */

405     public static void extractParameters(String JavaDoc urlEncodedParams,
406             String JavaDoc encoding, Map JavaDoc outputParams, boolean overwrite) {
407         Logger.log(Logger.DEBUG, Launcher.RESOURCES,
408                 "WinstoneRequest.ParsingParameters", new String JavaDoc[] {
409                         urlEncodedParams, encoding });
410         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(urlEncodedParams, "&", false);
411         Set JavaDoc overwrittenParamNames = null;
412         while (st.hasMoreTokens()) {
413             String JavaDoc token = st.nextToken();
414             int equalPos = token.indexOf('=');
415             try {
416                 String JavaDoc decodedNameDefault = decodeURLToken(equalPos == -1 ? token
417                         : token.substring(0, equalPos));
418                 String JavaDoc decodedValueDefault = (equalPos == -1 ? ""
419                         : decodeURLToken(token.substring(equalPos + 1)));
420                 String JavaDoc decodedName = (encoding == null ? decodedNameDefault
421                         : new String JavaDoc(decodedNameDefault.getBytes("8859_1"), encoding));
422                 String JavaDoc decodedValue = (encoding == null ? decodedValueDefault
423                         : new String JavaDoc(decodedValueDefault.getBytes("8859_1"), encoding));
424
425                 Object JavaDoc already = null;
426                 if (overwrite) {
427                     if (overwrittenParamNames == null) {
428                         overwrittenParamNames = new HashSet JavaDoc();
429                     }
430                     if (!overwrittenParamNames.contains(decodedName)) {
431                         overwrittenParamNames.add(decodedName);
432                         outputParams.remove(decodedName);
433                     }
434                 }
435                 already = outputParams.get(decodedName);
436                 if (already == null) {
437                     outputParams.put(decodedName, decodedValue);
438                 } else if (already instanceof String JavaDoc) {
439                     String JavaDoc pair[] = new String JavaDoc[2];
440                     pair[0] = (String JavaDoc) already;
441                     pair[1] = decodedValue;
442                     outputParams.put(decodedName, pair);
443                 } else if (already instanceof String JavaDoc[]) {
444                     String JavaDoc alreadyArray[] = (String JavaDoc[]) already;
445                     String JavaDoc oneMore[] = new String JavaDoc[alreadyArray.length + 1];
446                     System.arraycopy(alreadyArray, 0, oneMore, 0,
447                             alreadyArray.length);
448                     oneMore[oneMore.length - 1] = decodedValue;
449                     outputParams.put(decodedName, oneMore);
450                 } else {
451                     Logger.log(Logger.WARNING, Launcher.RESOURCES,
452                             "WinstoneRequest.UnknownParameterType",
453                             decodedName + " = " + decodedValue.getClass());
454                 }
455             } catch (UnsupportedEncodingException JavaDoc err) {
456                 Logger.log(Logger.ERROR, Launcher.RESOURCES,
457                         "WinstoneRequest.ErrorParameters", err);
458             }
459         }
460     }
461
462     /**
463      * For decoding the URL encoding used on query strings
464      */

465     public static String JavaDoc decodeURLToken(String JavaDoc in) {
466         StringBuffer JavaDoc workspace = new StringBuffer JavaDoc();
467         for (int n = 0; n < in.length(); n++) {
468             char thisChar = in.charAt(n);
469             if (thisChar == '+')
470                 workspace.append(' ');
471             else if (thisChar == '%') {
472                 String JavaDoc token = in.substring(Math.min(n + 1, in.length()),
473                         Math.min(n + 3, in.length()));
474                 try {
475                     int decoded = Integer.parseInt(token, 16);
476                     workspace.append((char) decoded);
477                     n += 2;
478                 } catch (RuntimeException JavaDoc err) {
479                     Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.InvalidURLTokenChar", token);
480                     workspace.append(thisChar);
481                 }
482             } else
483                 workspace.append(thisChar);
484         }
485         return workspace.toString();
486     }
487     
488     public void discardRequestBody() {
489         if (getContentLength() > 0) {
490             try {
491                 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "WinstoneResponse.ForceBodyParsing");
492                 // If body not parsed
493
if ((this.parsedParameters == null) ||
494                         (this.parsedParameters.equals(Boolean.FALSE))) {
495                     // read full stream length
496
InputStream JavaDoc in = getInputStream();
497                     byte buffer[] = new byte[2048];
498                     while (in.read(buffer) != -1)
499                         ;
500                 }
501             } catch (IOException JavaDoc err) {
502                 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "WinstoneResponse.ErrorForceBodyParsing", err);
503             }
504         }
505     }
506
507     /**
508      * This takes the parameters in the body of the request and puts them into
509      * the parameters map.
510      */

511     public void parseRequestParameters() {
512         if ((parsedParameters != null) && !parsedParameters.booleanValue()) {
513             Logger.log(Logger.WARNING, Launcher.RESOURCES,
514                     "WinstoneRequest.BothMethods");
515             this.parsedParameters = Boolean.TRUE;
516         } else if (parsedParameters == null) {
517             Map JavaDoc workingParameters = new HashMap JavaDoc();
518             try {
519                 // Parse query string from request
520
if ((method.equals(METHOD_GET) || method.equals(METHOD_HEAD) ||
521                         method.equals(METHOD_POST))
522                         && (this.queryString != null)) {
523                     extractParameters(this.queryString, this.encoding, workingParameters, false);
524                     Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
525                             "WinstoneRequest.ParamLine", "" + workingParameters);
526                 }
527                 
528                 if (method.equals(METHOD_POST) && (contentType != null)
529                         && contentType.equals(POST_PARAMETERS)) {
530                     Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
531                             "WinstoneRequest.ParsingBodyParameters");
532
533                     // Parse params
534
byte paramBuffer[] = new byte[contentLength];
535                     int readCount = this.inputData.read(paramBuffer);
536                     if (readCount != contentLength)
537                         Logger.log(Logger.WARNING, Launcher.RESOURCES,
538                                 "WinstoneRequest.IncorrectContentLength",
539                                 new String JavaDoc[] { contentLength + "",
540                                         readCount + "" });
541                     String JavaDoc paramLine = (this.encoding == null ? new String JavaDoc(
542                             paramBuffer) : new String JavaDoc(paramBuffer,
543                             this.encoding));
544                     extractParameters(paramLine.trim(), this.encoding, workingParameters, false);
545                     Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
546                             "WinstoneRequest.ParamLine", "" + workingParameters);
547                 }
548                 
549                 this.parameters.putAll(workingParameters);
550                 this.parsedParameters = Boolean.TRUE;
551             } catch (Throwable JavaDoc err) {
552                 Logger.log(Logger.ERROR, Launcher.RESOURCES,
553                         "WinstoneRequest.ErrorBodyParameters", err);
554                 this.parsedParameters = null;
555             }
556         }
557     }
558
559     /**
560      * Go through the list of headers, and build the headers/cookies arrays for
561      * the request object.
562      */

563     public void parseHeaders(List JavaDoc headerList) {
564         // Iterate through headers
565
List JavaDoc outHeaderList = new ArrayList JavaDoc();
566         List JavaDoc cookieList = new ArrayList JavaDoc();
567         for (Iterator JavaDoc i = headerList.iterator(); i.hasNext();) {
568             String JavaDoc header = (String JavaDoc) i.next();
569             int colonPos = header.indexOf(':');
570             String JavaDoc name = header.substring(0, colonPos);
571             String JavaDoc value = header.substring(colonPos + 1).trim();
572
573             // Add it to out headers if it's not a cookie
574
outHeaderList.add(header);
575 // if (!name.equalsIgnoreCase(IN_COOKIE_HEADER1)
576
// && !name.equalsIgnoreCase(IN_COOKIE_HEADER2))
577

578             if (name.equalsIgnoreCase(AUTHORIZATION_HEADER))
579                 this.authorization = value;
580             else if (name.equalsIgnoreCase(LOCALE_HEADER))
581                 this.locales = parseLocales(value);
582             else if (name.equalsIgnoreCase(CONTENT_LENGTH_HEADER))
583                 this.contentLength = Integer.parseInt(value);
584             else if (name.equalsIgnoreCase(HOST_HEADER)) {
585                 int nextColonPos = value.indexOf(':');
586                 if ((nextColonPos == -1) || (nextColonPos == value.length() - 1)) {
587                     this.serverName = value;
588                     if (this.scheme != null) {
589                         if (this.scheme.equals("http")) {
590                             this.serverPort = 80;
591                         } else if (this.scheme.equals("https")) {
592                             this.serverPort = 443;
593                         }
594                     }
595                 } else {
596                     this.serverName = value.substring(0, nextColonPos);
597                     this.serverPort = Integer.parseInt(value.substring(nextColonPos + 1));
598                 }
599             }
600             else if (name.equalsIgnoreCase(CONTENT_TYPE_HEADER)) {
601                 this.contentType = value;
602                 int semicolon = value.lastIndexOf(';');
603                 if (semicolon != -1) {
604                     String JavaDoc encodingClause = value.substring(semicolon + 1).trim();
605                     if (encodingClause.startsWith("charset="))
606                         this.encoding = encodingClause.substring(8);
607                 }
608             } else if (name.equalsIgnoreCase(IN_COOKIE_HEADER1)
609                     || name.equalsIgnoreCase(IN_COOKIE_HEADER2))
610                 parseCookieLine(value, cookieList);
611         }
612         this.headers = (String JavaDoc[]) outHeaderList.toArray(new String JavaDoc[0]);
613         if (cookieList.isEmpty()) {
614             this.cookies = null;
615         } else {
616             this.cookies = (Cookie JavaDoc[]) cookieList.toArray(new Cookie JavaDoc[0]);
617         }
618     }
619
620     private static String JavaDoc nextToken(StringTokenizer JavaDoc st) {
621         if (st.hasMoreTokens()) {
622             return st.nextToken();
623         } else {
624             return null;
625         }
626     }
627     
628     private void parseCookieLine(String JavaDoc headerValue, List JavaDoc cookieList) {
629         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(headerValue, ";", false);
630         int version = 0;
631         String JavaDoc cookieLine = nextToken(st);
632
633         // check cookie version flag
634
if (cookieLine.startsWith("$Version=")) {
635             int equalPos = cookieLine.indexOf('=');
636             try {
637                 version = Integer.parseInt(extractFromQuotes(
638                         cookieLine.substring(equalPos + 1).trim()));
639             } catch (NumberFormatException JavaDoc err) {
640                 version = 0;
641             }
642             cookieLine = nextToken(st);
643         }
644
645         // process remainder - parameters
646
while (cookieLine != null) {
647             cookieLine = cookieLine.trim();
648             int equalPos = cookieLine.indexOf('=');
649             if (equalPos == -1) {
650                 // next token
651
cookieLine = nextToken(st);
652             } else {
653                 String JavaDoc name = cookieLine.substring(0, equalPos);
654                 String JavaDoc value = extractFromQuotes(cookieLine.substring(equalPos + 1));
655                 Cookie JavaDoc thisCookie = new Cookie JavaDoc(name, value);
656                 thisCookie.setVersion(version);
657                 thisCookie.setSecure(isSecure());
658                 cookieList.add(thisCookie);
659
660                 // check for path / domain / port
661
cookieLine = nextToken(st);
662                 while ((cookieLine != null) && cookieLine.trim().startsWith("$")) {
663                     cookieLine = cookieLine.trim();
664                     equalPos = cookieLine.indexOf('=');
665                     String JavaDoc attrValue = equalPos == -1 ? "" : cookieLine
666                             .substring(equalPos + 1).trim();
667                     if (cookieLine.startsWith("$Path")) {
668                         thisCookie.setPath(extractFromQuotes(attrValue));
669                     } else if (cookieLine.startsWith("$Domain")) {
670                         thisCookie.setDomain(extractFromQuotes(attrValue));
671                     }
672                     cookieLine = nextToken(st);
673                 }
674
675                 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
676                         "WinstoneRequest.CookieFound", thisCookie.toString());
677                 if (thisCookie.getName().equals(WinstoneSession.SESSION_COOKIE_NAME)) {
678                     // Find a context that manages this key
679
HostConfiguration hostConfig = this.hostGroup.getHostByName(this.serverName);
680                     WebAppConfiguration ownerContext = hostConfig.getWebAppBySessionKey(thisCookie.getValue());
681                     if (ownerContext != null) {
682                         this.requestedSessionIds.put(ownerContext.getContextPath(),
683                                 thisCookie.getValue());
684                         this.currentSessionIds.put(ownerContext.getContextPath(),
685                                 thisCookie.getValue());
686                     }
687                     // If not found, it was probably dead
688
else {
689                         this.deadRequestedSessionId = thisCookie.getValue();
690                     }
691 // this.requestedSessionId = thisCookie.getValue();
692
// this.currentSessionId = thisCookie.getValue();
693
Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
694                             "WinstoneRequest.SessionCookieFound",
695                             new String JavaDoc[] {thisCookie.getValue(),
696                             ownerContext == null ? "" : "prefix:" + ownerContext.getContextPath()});
697                 }
698             }
699         }
700     }
701     
702     private static String JavaDoc extractFromQuotes(String JavaDoc input) {
703         if ((input != null) && input.startsWith("\"") && input.endsWith("\"")) {
704             return input.substring(1, input.length() - 1);
705         } else {
706             return input;
707         }
708     }
709
710     private List JavaDoc parseLocales(String JavaDoc header) {
711         // Strip out the whitespace
712
StringBuffer JavaDoc lb = new StringBuffer JavaDoc();
713         for (int n = 0; n < header.length(); n++) {
714             char c = header.charAt(n);
715             if (!Character.isWhitespace(c))
716                 lb.append(c);
717         }
718
719         // Tokenize by commas
720
Map JavaDoc localeEntries = new HashMap JavaDoc();
721         StringTokenizer JavaDoc commaTK = new StringTokenizer JavaDoc(lb.toString(), ",", false);
722         for (; commaTK.hasMoreTokens();) {
723             String JavaDoc clause = commaTK.nextToken();
724
725             // Tokenize by semicolon
726
Float JavaDoc quality = new Float JavaDoc(1);
727             if (clause.indexOf(";q=") != -1) {
728                 int pos = clause.indexOf(";q=");
729                 try {
730                     quality = new Float JavaDoc(clause.substring(pos + 3));
731                 } catch (NumberFormatException JavaDoc err) {
732                     quality = new Float JavaDoc(0);
733                 }
734                 clause = clause.substring(0, pos);
735             }
736
737             // Build the locale
738
String JavaDoc language = "";
739             String JavaDoc country = "";
740             String JavaDoc variant = "";
741             int dpos = clause.indexOf('-');
742             if (dpos == -1)
743                 language = clause;
744             else {
745                 language = clause.substring(0, dpos);
746                 String JavaDoc remainder = clause.substring(dpos + 1);
747                 int d2pos = remainder.indexOf('-');
748                 if (d2pos == -1)
749                     country = remainder;
750                 else {
751                     country = remainder.substring(0, d2pos);
752                     variant = remainder.substring(d2pos + 1);
753                 }
754             }
755             Locale JavaDoc loc = new Locale JavaDoc(language, country, variant);
756
757             // Put into list by quality
758
List JavaDoc localeList = (List JavaDoc) localeEntries.get(quality);
759             if (localeList == null) {
760                 localeList = new ArrayList JavaDoc();
761                 localeEntries.put(quality, localeList);
762             }
763             localeList.add(loc);
764         }
765
766         // Extract and build the list
767
Float JavaDoc orderKeys[] = (Float JavaDoc[]) localeEntries.keySet().toArray(new Float JavaDoc[0]);
768         Arrays.sort(orderKeys);
769         List JavaDoc outputLocaleList = new ArrayList JavaDoc();
770         for (int n = 0; n < orderKeys.length; n++) {
771             // Skip backwards through the list of maps and add to the output list
772
int reversedIndex = (orderKeys.length - 1) - n;
773             if ((orderKeys[reversedIndex].floatValue() <= 0)
774                     || (orderKeys[reversedIndex].floatValue() > 1))
775                 continue;
776             List JavaDoc localeList = (List JavaDoc) localeEntries.get(orderKeys[reversedIndex]);
777             for (Iterator JavaDoc i = localeList.iterator(); i.hasNext();)
778                 outputLocaleList.add(i.next());
779         }
780
781         return outputLocaleList;
782     }
783
784     public void addIncludeQueryParameters(String JavaDoc queryString) {
785         Map JavaDoc lastParams = new Hashtable JavaDoc();
786         if (!this.parametersStack.isEmpty()) {
787             lastParams.putAll((Map JavaDoc) this.parametersStack.peek());
788         }
789         Map JavaDoc newQueryParams = new HashMap JavaDoc();
790         if (queryString != null) {
791             extractParameters(queryString, this.encoding, newQueryParams, false);
792         }
793         lastParams.putAll(newQueryParams);
794         this.parametersStack.push(lastParams);
795     }
796
797     public void addIncludeAttributes(String JavaDoc requestURI, String JavaDoc contextPath,
798             String JavaDoc servletPath, String JavaDoc pathInfo, String JavaDoc queryString) {
799         Map JavaDoc includeAttributes = new HashMap JavaDoc();
800         if (requestURI != null) {
801             includeAttributes.put(RequestDispatcher.INCLUDE_REQUEST_URI, requestURI);
802         }
803         if (contextPath != null) {
804             includeAttributes.put(RequestDispatcher.INCLUDE_CONTEXT_PATH, contextPath);
805         }
806         if (servletPath != null) {
807             includeAttributes.put(RequestDispatcher.INCLUDE_SERVLET_PATH, servletPath);
808         }
809         if (pathInfo != null) {
810             includeAttributes.put(RequestDispatcher.INCLUDE_PATH_INFO, pathInfo);
811         }
812         if (queryString != null) {
813             includeAttributes.put(RequestDispatcher.INCLUDE_QUERY_STRING, queryString);
814         }
815         this.attributesStack.push(includeAttributes);
816     }
817     
818     public void removeIncludeQueryString() {
819         if (!this.parametersStack.isEmpty()) {
820             this.parametersStack.pop();
821         }
822     }
823     
824     public void clearIncludeStackForForward() {
825         this.parametersStack.clear();
826         this.attributesStack.clear();
827     }
828     
829     public void setForwardQueryString(String JavaDoc forwardQueryString) {
830 // this.forwardedParameters.clear();
831

832         // Parse query string from include / forward
833
if (forwardQueryString != null) {
834             String JavaDoc oldQueryString = this.queryString == null ? "" : this.queryString;
835             boolean needJoiner = !forwardQueryString.equals("") && !oldQueryString.equals("");
836             this.queryString = forwardQueryString + (needJoiner ? "&" : "") + oldQueryString;
837             
838             if (this.parsedParameters != null) {
839                 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
840                         "WinstoneRequest.ParsingParameters", new String JavaDoc[] {
841                         forwardQueryString, this.encoding });
842                 extractParameters(forwardQueryString, this.encoding, this.parameters, true);
843                 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES,
844                         "WinstoneRequest.ParamLine", "" + this.parameters);
845             }
846         }
847
848     }
849     
850     public void removeIncludeAttributes() {
851         if (!this.attributesStack.isEmpty()) {
852             this.attributesStack.pop();
853         }
854     }
855     
856     // Implementation methods for the servlet request stuff
857
public Object JavaDoc getAttribute(String JavaDoc name) {
858         if (!this.attributesStack.isEmpty()) {
859             Map JavaDoc includedAttributes = (Map JavaDoc) this.attributesStack.peek();
860             Object JavaDoc value = includedAttributes.get(name);
861             if (value != null) {
862                 return value;
863             }
864         }
865         return this.attributes.get(name);
866     }
867
868     public Enumeration JavaDoc getAttributeNames() {
869         Map JavaDoc attributes = new HashMap JavaDoc(this.attributes);
870         if (!this.attributesStack.isEmpty()) {
871             Map JavaDoc includedAttributes = (Map JavaDoc) this.attributesStack.peek();
872             attributes.putAll(includedAttributes);
873         }
874         return Collections.enumeration(attributes.keySet());
875     }
876
877     public void removeAttribute(String JavaDoc name) {
878         Object JavaDoc value = attributes.get(name);
879         if (value == null)
880             return;
881
882         // fire event
883
if (this.requestAttributeListeners != null) {
884             for (int n = 0; n < this.requestAttributeListeners.length; n++) {
885                 ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
886                 Thread.currentThread().setContextClassLoader(getWebAppConfig().getLoader());
887                 this.requestAttributeListeners[n].attributeRemoved(
888                         new ServletRequestAttributeEvent JavaDoc(this.webappConfig,
889                                 this, name, value));
890                 Thread.currentThread().setContextClassLoader(cl);
891             }
892         }
893         
894         this.attributes.remove(name);
895     }
896
897     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
898         if ((name != null) && (o != null)) {
899             Object JavaDoc oldValue = attributes.get(name);
900             attributes.put(name, o); // make sure it's set at the top level
901

902             // fire event
903
if (this.requestAttributeListeners != null) {
904                 if (oldValue == null) {
905                     for (int n = 0; n < this.requestAttributeListeners.length; n++) {
906                         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
907                         Thread.currentThread().setContextClassLoader(getWebAppConfig().getLoader());
908                         this.requestAttributeListeners[n].attributeAdded(
909                                 new ServletRequestAttributeEvent JavaDoc(this.webappConfig,
910                                         this, name, o));
911                         Thread.currentThread().setContextClassLoader(cl);
912                     }
913                 } else {
914                     for (int n = 0; n < this.requestAttributeListeners.length; n++) {
915                         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
916                         Thread.currentThread().setContextClassLoader(getWebAppConfig().getLoader());
917                         this.requestAttributeListeners[n]
918                                 .attributeReplaced(new ServletRequestAttributeEvent JavaDoc(
919                                         this.webappConfig, this, name, oldValue));
920                         Thread.currentThread().setContextClassLoader(cl);
921                     }
922                 }
923             }
924         } else if (name != null) {
925             removeAttribute(name);
926         }
927     }
928
929     public String JavaDoc getCharacterEncoding() {
930         return this.encoding;
931     }
932
933     public void setCharacterEncoding(String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
934         "blah".getBytes(encoding); // throws an exception if the encoding is unsupported
935
if (this.inputReader == null) {
936             Logger.log(Logger.DEBUG, Launcher.RESOURCES, "WinstoneRequest.SetCharEncoding",
937                     new String JavaDoc[] { this.encoding, encoding });
938             this.encoding = encoding;
939         }
940     }
941
942     public int getContentLength() {
943         return this.contentLength;
944     }
945
946     public String JavaDoc getContentType() {
947         return this.contentType;
948     }
949
950     public Locale JavaDoc getLocale() {
951         return this.locales.isEmpty() ? Locale.getDefault()
952                 : (Locale JavaDoc) this.locales.get(0);
953     }
954
955     public Enumeration JavaDoc getLocales() {
956         List JavaDoc sendLocales = this.locales;
957         if (sendLocales.isEmpty())
958             sendLocales.add(Locale.getDefault());
959         return Collections.enumeration(sendLocales);
960     }
961
962     public String JavaDoc getProtocol() {
963         return this.protocol;
964     }
965
966     public String JavaDoc getScheme() {
967         return this.scheme;
968     }
969
970     public boolean isSecure() {
971         return this.isSecure;
972     }
973
974     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
975         if (this.inputReader != null) {
976             return this.inputReader;
977         } else {
978             if (this.parsedParameters != null) {
979                 if (this.parsedParameters.equals(Boolean.TRUE)) {
980                     Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.BothMethodsReader");
981                 } else {
982                     throw new IllegalStateException JavaDoc(Launcher.RESOURCES.getString(
983                             "WinstoneRequest.CalledReaderAfterStream"));
984                 }
985             }
986             if (this.encoding != null) {
987                 this.inputReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
988                         this.inputData, this.encoding));
989             } else {
990                 this.inputReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
991                         this.inputData));
992             }
993             this.parsedParameters = new Boolean JavaDoc(false);
994             return this.inputReader;
995         }
996     }
997
998     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
999         if (this.inputReader != null) {
1000            throw new IllegalStateException JavaDoc(Launcher.RESOURCES.getString(
1001                    "WinstoneRequest.CalledStreamAfterReader"));
1002        }
1003        if (this.parsedParameters != null) {
1004            if (this.parsedParameters.equals(Boolean.TRUE)) {
1005                Logger.log(Logger.WARNING, Launcher.RESOURCES, "WinstoneRequest.BothMethods");
1006            }
1007        }
1008        this.parsedParameters = new Boolean JavaDoc(false);
1009        return this.inputData;
1010    }
1011
1012    public String JavaDoc getParameter(String JavaDoc name) {
1013        parseRequestParameters();
1014        Object JavaDoc param = null;
1015        if (!this.parametersStack.isEmpty()) {
1016            param = ((Map JavaDoc) this.parametersStack.peek()).get(name);
1017        }
1018// if ((param == null) && this.forwardedParameters.get(name) != null) {
1019
// param = this.forwardedParameters.get(name);
1020
// }
1021
if (param == null) {
1022            param = this.parameters.get(name);
1023        }
1024        if (param == null)
1025            return null;
1026        else if (param instanceof String JavaDoc)
1027            return (String JavaDoc) param;
1028        else if (param instanceof String JavaDoc[])
1029            return ((String JavaDoc[]) param)[0];
1030        else
1031            return param.toString();
1032    }
1033
1034    public Enumeration JavaDoc getParameterNames() {
1035        parseRequestParameters();
1036        Set JavaDoc parameterKeys = new HashSet JavaDoc(this.parameters.keySet());
1037// parameterKeys.addAll(this.forwardedParameters.keySet());
1038
if (!this.parametersStack.isEmpty()) {
1039            parameterKeys.addAll(((Map JavaDoc) this.parametersStack.peek()).keySet());
1040        }
1041        return Collections.enumeration(parameterKeys);
1042    }
1043
1044    public String JavaDoc[] getParameterValues(String JavaDoc name) {
1045        parseRequestParameters();
1046        Object JavaDoc param = null;
1047        if (!this.parametersStack.isEmpty()) {
1048            param = ((Map JavaDoc) this.parametersStack.peek()).get(name);
1049        }
1050// if ((param == null) && this.forwardedParameters.get(name) != null) {
1051
// param = this.forwardedParameters.get(name);
1052
// }
1053
if (param == null) {
1054            param = this.parameters.get(name);
1055        }
1056        if (param == null)
1057            return null;
1058        else if (param instanceof String JavaDoc) {
1059            return new String JavaDoc[] {(String JavaDoc) param};
1060        } else if (param instanceof String JavaDoc[])
1061            return (String JavaDoc[]) param;
1062        else
1063            throw new WinstoneException(Launcher.RESOURCES.getString(
1064                    "WinstoneRequest.UnknownParameterType", name + " - "
1065                            + param.getClass()));
1066    }
1067
1068    public Map JavaDoc getParameterMap() {
1069        Hashtable JavaDoc paramMap = new Hashtable JavaDoc();
1070        for (Enumeration JavaDoc names = this.getParameterNames(); names
1071                .hasMoreElements();) {
1072            String JavaDoc name = (String JavaDoc) names.nextElement();
1073            paramMap.put(name, getParameterValues(name));
1074        }
1075        return paramMap;
1076    }
1077
1078    public String JavaDoc getServerName() {
1079        return this.serverName;
1080    }
1081
1082    public int getServerPort() {
1083        return this.serverPort;
1084    }
1085
1086    public String JavaDoc getRemoteAddr() {
1087        return this.remoteIP;
1088    }
1089
1090    public String JavaDoc getRemoteHost() {
1091        return this.remoteName;
1092    }
1093
1094    public int getRemotePort() {
1095        return this.remotePort;
1096    }
1097
1098    public String JavaDoc getLocalAddr() {
1099        return this.localAddr;
1100    }
1101
1102    public String JavaDoc getLocalName() {
1103        return this.localName;
1104    }
1105
1106    public int getLocalPort() {
1107        return this.localPort;
1108    }
1109
1110    public javax.servlet.RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
1111        if (path.startsWith("/"))
1112            return this.webappConfig.getRequestDispatcher(path);
1113
1114        // Take the servlet path + pathInfo, and make an absolute path
1115
String JavaDoc fullPath = getServletPath()
1116                + (getPathInfo() == null ? "" : getPathInfo());
1117        int lastSlash = fullPath.lastIndexOf('/');
1118        String JavaDoc currentDir = (lastSlash == -1 ? "/" : fullPath.substring(0,
1119                lastSlash + 1));
1120        return this.webappConfig.getRequestDispatcher(currentDir + path);
1121    }
1122
1123    // Now the stuff for HttpServletRequest
1124
public String JavaDoc getContextPath() {
1125        return this.webappConfig.getContextPath();
1126    }
1127
1128    public Cookie JavaDoc[] getCookies() {
1129        return this.cookies;
1130    }
1131
1132    public long getDateHeader(String JavaDoc name) {
1133        String JavaDoc dateHeader = getHeader(name);
1134        if (dateHeader == null) {
1135            return -1;
1136        } else try {
1137            Date JavaDoc date = null;
1138            synchronized (headerDF) {
1139                date = headerDF.parse(dateHeader);
1140            }
1141            return date.getTime();
1142        } catch (java.text.ParseException JavaDoc err) {
1143            throw new IllegalArgumentException JavaDoc(Launcher.RESOURCES.getString(
1144                    "WinstoneRequest.BadDate", dateHeader));
1145        }
1146    }
1147
1148    public int getIntHeader(String JavaDoc name) {
1149        String JavaDoc header = getHeader(name);
1150        return header == null ? -1 : Integer.parseInt(header);
1151    }
1152
1153    public String JavaDoc getHeader(String JavaDoc name) {
1154        return extractFirstHeader(name);
1155    }
1156
1157    public Enumeration JavaDoc getHeaderNames() {
1158        return Collections.enumeration(extractHeaderNameList());
1159    }
1160
1161    public Enumeration JavaDoc getHeaders(String JavaDoc name) {
1162        List JavaDoc headers = new ArrayList JavaDoc();
1163        for (int n = 0; n < this.headers.length; n++)
1164            if (this.headers[n].toUpperCase().startsWith(
1165                    name.toUpperCase() + ':'))
1166                headers
1167                        .add(this.headers[n].substring(name.length() + 1)
1168                                .trim()); // 1 for colon
1169
return Collections.enumeration(headers);
1170    }
1171
1172    public String JavaDoc getMethod() {
1173        return this.method;
1174    }
1175
1176    public String JavaDoc getPathInfo() {
1177        return this.pathInfo;
1178    }
1179
1180    public String JavaDoc getPathTranslated() {
1181        return this.webappConfig.getRealPath(this.pathInfo);
1182    }
1183
1184    public String JavaDoc getQueryString() {
1185        return this.queryString;
1186    }
1187
1188    public String JavaDoc getRequestURI() {
1189        return this.requestURI;
1190    }
1191
1192    public String JavaDoc getServletPath() {
1193        return this.servletPath;
1194    }
1195
1196    public String JavaDoc getRequestedSessionId() {
1197        String JavaDoc actualSessionId = (String JavaDoc) this.requestedSessionIds.get(this.webappConfig.getContextPath());
1198        if (actualSessionId != null) {
1199            return actualSessionId;
1200        } else {
1201            return this.deadRequestedSessionId;
1202        }
1203    }
1204
1205    public StringBuffer JavaDoc getRequestURL() {
1206        StringBuffer JavaDoc url = new StringBuffer JavaDoc();
1207        url.append(getScheme()).append("://");
1208        url.append(getServerName());
1209        if (!((getServerPort() == 80) && getScheme().equals("http"))
1210                && !((getServerPort() == 443) && getScheme().equals("https")))
1211            url.append(':').append(getServerPort());
1212        url.append(this.webappConfig.getContextPath());
1213        url.append(getServletPath());
1214        if (getPathInfo() != null)
1215            url.append(getPathInfo());
1216        return url;
1217    }
1218
1219    public Principal JavaDoc getUserPrincipal() {
1220        return this.authenticatedUser;
1221    }
1222
1223    public boolean isUserInRole(String JavaDoc role) {
1224        if (this.authenticatedUser == null)
1225            return false;
1226        else if (this.servletConfig.getSecurityRoleRefs() == null)
1227            return this.authenticatedUser.isUserIsInRole(role);
1228        else {
1229            String JavaDoc replacedRole = (String JavaDoc) this.servletConfig.getSecurityRoleRefs().get(role);
1230            return this.authenticatedUser
1231                    .isUserIsInRole(replacedRole == null ? role : replacedRole);
1232        }
1233    }
1234
1235    public String JavaDoc getAuthType() {
1236        return this.authenticatedUser == null ? null : this.authenticatedUser
1237                .getAuthType();
1238    }
1239
1240    public String JavaDoc getRemoteUser() {
1241        return this.authenticatedUser == null ? null : this.authenticatedUser
1242                .getName();
1243    }
1244
1245    public boolean isRequestedSessionIdFromCookie() {
1246        return (getRequestedSessionId() != null);
1247    }
1248
1249    public boolean isRequestedSessionIdFromURL() {
1250        return false;
1251    }
1252
1253    public boolean isRequestedSessionIdValid() {
1254        String JavaDoc requestedId = getRequestedSessionId();
1255        if (requestedId == null) {
1256            return false;
1257        }
1258        WinstoneSession ws = this.webappConfig.getSessionById(requestedId, false);
1259        return (ws != null);
1260// if (ws == null) {
1261
// return false;
1262
// } else {
1263
// return (validationCheck(ws, System.currentTimeMillis(), false) != null);
1264
// }
1265
}
1266
1267    public HttpSession JavaDoc getSession() {
1268        return getSession(true);
1269    }
1270
1271    public HttpSession JavaDoc getSession(boolean create) {
1272        String JavaDoc cookieValue = (String JavaDoc) this.currentSessionIds.get(this.webappConfig.getContextPath());
1273
1274        // Handle the null case
1275
if (cookieValue == null) {
1276            if (!create) {
1277                return null;
1278            } else {
1279                cookieValue = makeNewSession().getId();
1280            }
1281        }
1282
1283        // Now get the session object
1284
WinstoneSession session = this.webappConfig.getSessionById(cookieValue, false);
1285        if (session != null) {
1286// long nowDate = System.currentTimeMillis();
1287
// session = validationCheck(session, nowDate, create);
1288
// if (session == null) {
1289
// this.currentSessionIds.remove(this.webappConfig.getContextPath());
1290
// }
1291
}
1292        if (create && (session == null)) {
1293            session = makeNewSession();
1294        }
1295        if (session != null) {
1296            this.usedSessions.add(session);
1297            session.addUsed(this);
1298        }
1299        return session;
1300    }
1301
1302    /**
1303     * Make a new session, and return the id
1304     */

1305    private WinstoneSession makeNewSession() {
1306        String JavaDoc cookieValue = "Winstone_" + this.remoteIP + "_"
1307                + this.serverPort + "_" + System.currentTimeMillis() + rnd.nextLong();
1308        byte digestBytes[] = this.md5Digester.digest(cookieValue.getBytes());
1309
1310        // Write out in hex format
1311
char outArray[] = new char[32];
1312        for (int n = 0; n < digestBytes.length; n++) {
1313            int hiNibble = (digestBytes[n] & 0xFF) >> 4;
1314            int loNibble = (digestBytes[n] & 0xF);
1315            outArray[2 * n] = (hiNibble > 9 ? (char) (hiNibble + 87)
1316                    : (char) (hiNibble + 48));
1317            outArray[2 * n + 1] = (loNibble > 9 ? (char) (loNibble + 87)
1318                    : (char) (loNibble + 48));
1319        }
1320
1321        String JavaDoc newSessionId = new String JavaDoc(outArray);
1322        this.currentSessionIds.put(this.webappConfig.getContextPath(), newSessionId);
1323        return this.webappConfig.makeNewSession(newSessionId);
1324    }
1325
1326    public void markSessionsAsRequestFinished(long lastAccessedTime, boolean saveSessions) {
1327        for (Iterator JavaDoc i = this.usedSessions.iterator(); i.hasNext(); ) {
1328            WinstoneSession session = (WinstoneSession) i.next();
1329            session.setLastAccessedDate(lastAccessedTime);
1330            session.removeUsed(this);
1331            if (saveSessions) {
1332                session.saveToTemp();
1333            }
1334        }
1335        this.usedSessions.clear();
1336    }
1337    
1338    /**
1339     * @deprecated
1340     */

1341    public String JavaDoc getRealPath(String JavaDoc path) {
1342        return this.webappConfig.getRealPath(path);
1343    }
1344
1345    /**
1346     * @deprecated
1347     */

1348    public boolean isRequestedSessionIdFromUrl() {
1349        return isRequestedSessionIdFromURL();
1350    }
1351
1352}
1353
Popular Tags