KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > DispatchRequest


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.webapp;
31
32 import com.caucho.i18n.CharacterEncoding;
33 import com.caucho.log.Log;
34 import com.caucho.server.connection.CauchoRequest;
35 import com.caucho.server.connection.CauchoResponse;
36 import com.caucho.server.connection.Form;
37 import com.caucho.server.connection.RequestAdapter;
38 import com.caucho.server.connection.ServletInputStreamImpl;
39 import com.caucho.server.session.SessionImpl;
40 import com.caucho.server.session.SessionManager;
41 import com.caucho.util.Alarm;
42 import com.caucho.util.CharBuffer;
43 import com.caucho.util.FreeList;
44 import com.caucho.util.HashMapImpl;
45 import com.caucho.vfs.BufferedReaderAdapter;
46 import com.caucho.vfs.Encoding;
47 import com.caucho.vfs.ReadStream;
48
49 import javax.servlet.RequestDispatcher JavaDoc;
50 import javax.servlet.ServletException JavaDoc;
51 import javax.servlet.ServletInputStream JavaDoc;
52 import javax.servlet.http.HttpServletRequest JavaDoc;
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54 import javax.servlet.http.HttpSession JavaDoc;
55 import java.io.BufferedReader JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.util.Collections JavaDoc;
58 import java.util.Enumeration JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.logging.Logger JavaDoc;
61 /**
62  * sub-request for a include() page
63  */

64 class DispatchRequest extends RequestAdapter {
65   protected static final Logger JavaDoc log = Log.open(DispatchRequest.class);
66
67   private static final FreeList<DispatchRequest> _freeList =
68     new FreeList<DispatchRequest>(32);
69
70   private WebApp _webApp;
71   private WebApp _oldWebApp;
72   private Form _formParser;
73   private HashMapImpl<String JavaDoc,String JavaDoc[]> _form;
74   protected ReadStream _readStream;
75   protected ServletInputStreamImpl _is;
76   // Reader for post contents
77
private BufferedReaderAdapter _bufferedReader;
78   private String JavaDoc _method;
79   private String JavaDoc _uri;
80   private String JavaDoc _servletPath;
81   private String JavaDoc _pathInfo;
82   private String JavaDoc _queryString;
83   private String JavaDoc _addedQuery;
84   private SessionImpl _session;
85
86   private String JavaDoc _pageUri;
87   private String JavaDoc _pageContextPath;
88   private String JavaDoc _pageServletPath;
89   private String JavaDoc _pagePathInfo;
90   private String JavaDoc _pageQueryString;
91
92   protected DispatchRequest()
93   {
94   }
95
96   /**
97    * Creates a dispatch request.
98    */

99   public static DispatchRequest createDispatch()
100   {
101     DispatchRequest req = _freeList.allocate();
102     if (req == null)
103       req = new DispatchRequest();
104
105     return req;
106   }
107
108   void init(WebApp webApp,
109             WebApp oldWebApp,
110             HttpServletRequest JavaDoc request,
111             HttpServletResponse JavaDoc response,
112         String JavaDoc method, String JavaDoc uri,
113         String JavaDoc servletPath, String JavaDoc pathInfo,
114         String JavaDoc queryString, String JavaDoc addedQuery)
115     throws ServletException JavaDoc
116   {
117     super.init(request, response, webApp);
118
119     _webApp = webApp;
120     _oldWebApp = oldWebApp;
121
122     _form = null;
123
124     _readStream = null;
125     _is = null;
126
127     _bufferedReader = null;
128
129     _method = method;
130     _uri = uri;
131     _servletPath = servletPath;
132     _pathInfo = pathInfo;
133     _queryString = queryString;
134     _addedQuery = addedQuery;
135
136     _pageUri = null;
137     _pageContextPath = null;
138     _pageServletPath = null;
139     _pagePathInfo = null;
140     _pageQueryString = null;
141
142     _session = null;
143   }
144
145   void setStream(ReadStream readStream)
146   {
147     _readStream = readStream;
148   }
149
150   public WebApp getWebApp()
151   {
152     return _webApp;
153   }
154
155   public String JavaDoc getMethod()
156   {
157     return _method;
158   }
159
160   public String JavaDoc getRequestURI()
161   {
162     return _uri;
163   }
164
165   /**
166    * Returns the URL for the request
167    */

168   public StringBuffer JavaDoc getRequestURL()
169   {
170     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
171
172     sb.append(getScheme());
173     sb.append("://");
174     sb.append(getServerName());
175     if (getServerPort() > 0 &&
176         getServerPort() != 80 &&
177         getServerPort() != 443) {
178       sb.append(":");
179       sb.append(getServerPort());
180     }
181     sb.append(getRequestURI());
182
183     return sb;
184   }
185
186   void setPageURI(String JavaDoc uri)
187   {
188     _pageUri = uri;
189   }
190
191   public String JavaDoc getPageURI()
192   {
193     return _pageUri;
194   }
195
196   /**
197    * Returns the servlet context prefix of the webApp.
198    */

199   public String JavaDoc getContextPath()
200   {
201     if (_webApp != null)
202       return _webApp.getContextPath();
203     else
204       return "/";
205   }
206
207   /**
208    * Sets the servlet context prefix of page.
209    */

210   void setPageContextPath(String JavaDoc contextPath)
211   {
212     _pageContextPath = contextPath;
213   }
214
215   /**
216    * Gets the servlet context prefix of page.
217    */

218   public String JavaDoc getPageContextPath()
219   {
220     return _pageContextPath;
221   }
222
223   public String JavaDoc getServletPath()
224   {
225     return _servletPath;
226   }
227
228   public String JavaDoc getPageServletPath()
229   {
230     return _pageServletPath;
231   }
232
233   void setPageServletPath(String JavaDoc servletPath)
234   {
235     _pageServletPath = servletPath;
236   }
237
238   public String JavaDoc getPathInfo()
239   {
240     return _pathInfo;
241   }
242
243   public String JavaDoc getPagePathInfo()
244   {
245     return _pagePathInfo;
246   }
247
248   void setPagePathInfo(String JavaDoc pathInfo)
249   {
250     _pagePathInfo = pathInfo;
251   }
252
253   public String JavaDoc getQueryString()
254   {
255     return _queryString;
256   }
257
258   void setPageQueryString(String JavaDoc queryString)
259   {
260     _pageQueryString = queryString;
261   }
262
263   public String JavaDoc getPageQueryString()
264   {
265     return _pageQueryString;
266   }
267
268   public Map JavaDoc getParameterMap()
269   {
270     if (_form == null)
271       _form = parseQuery();
272
273     return _form;
274   }
275
276   public Enumeration JavaDoc getParameterNames()
277   {
278     if (_form == null)
279       _form = parseQuery();
280
281     return Collections.enumeration(_form.keySet());
282   }
283
284   public String JavaDoc []getParameterValues(String JavaDoc name)
285   {
286     if (_form == null)
287       _form = parseQuery();
288
289     return _form.get(name);
290   }
291
292   public String JavaDoc getParameter(String JavaDoc name)
293   {
294     String JavaDoc []values = getParameterValues(name);
295     if (values == null || values.length == 0)
296       return null;
297
298     return values[0];
299   }
300
301   private HashMapImpl<String JavaDoc,String JavaDoc[]> parseQuery()
302   {
303     HashMapImpl<String JavaDoc,String JavaDoc[]> table = new HashMapImpl<String JavaDoc,String JavaDoc[]>();
304
305     String JavaDoc defaultEncoding = CharacterEncoding.getLocalEncoding();
306     String JavaDoc charEncoding = getCharacterEncoding();
307     if (charEncoding == null)
308       charEncoding = defaultEncoding;
309     String JavaDoc javaEncoding = Encoding.getJavaName(charEncoding);
310
311     if (_addedQuery != null) {
312       try {
313     if (_formParser == null)
314       _formParser = new Form();
315     _formParser.parseQueryString(table, _addedQuery, javaEncoding, false);
316       } catch (Exception JavaDoc e) {
317       }
318     }
319
320     Enumeration JavaDoc en = super.getParameterNames();
321     while (en.hasMoreElements()) {
322       String JavaDoc key = (String JavaDoc) en.nextElement();
323       String JavaDoc []oldValues = (String JavaDoc []) super.getParameterValues(key);
324       String JavaDoc []newValues = (String JavaDoc []) table.get(key);
325
326       if (oldValues == null) {
327       }
328       else if (newValues == null)
329     table.put(key, oldValues);
330       else {
331     String JavaDoc []next = new String JavaDoc[oldValues.length + newValues.length];
332     System.arraycopy(newValues, 0, next, 0, newValues.length);
333     System.arraycopy(oldValues, 0, next, newValues.length,
334              oldValues.length);
335     table.put(key, next);
336       }
337     }
338
339     return table;
340   }
341
342   public String JavaDoc getRealPath(String JavaDoc path)
343   {
344     return _webApp.getRealPath(path);
345   }
346
347   public String JavaDoc getPathTranslated()
348   {
349     if (_pathInfo != null)
350       return getRealPath(_pathInfo);
351     else
352       return null;
353   }
354
355   public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path)
356   {
357     if (path.startsWith("/"))
358       return _webApp.getRequestDispatcher(path);
359     else
360       return _webApp.getRequestDispatcher(getPwd() + path);
361   }
362
363   public String JavaDoc getPwd()
364   {
365     CharBuffer cb = CharBuffer.allocate();
366
367     String JavaDoc servletPath = getPageServletPath();
368     if (servletPath != null)
369       cb.append(servletPath);
370     String JavaDoc pathInfo = getPagePathInfo();
371     if (pathInfo != null)
372       cb.append(pathInfo);
373
374     int p = cb.lastIndexOf('/');
375     if (p >= 0)
376       cb.setLength(p);
377     cb.append('/');
378
379     return cb.close();
380   }
381
382   public ReadStream getStream()
383     throws IOException JavaDoc
384   {
385     if (_readStream == null && getRequest() instanceof CauchoRequest)
386       return ((CauchoRequest) getRequest()).getStream();
387     else
388       return _readStream;
389   }
390
391   public ServletInputStream JavaDoc getInputStream()
392     throws IOException JavaDoc
393   {
394     if (_readStream == null)
395       return super.getInputStream();
396
397     if (_is == null)
398       _is = new ServletInputStreamImpl();
399     _is.init(_readStream);
400
401     return _is;
402   }
403
404   public BufferedReader JavaDoc getReader()
405     throws IOException JavaDoc
406   {
407     if (_readStream == null)
408       return super.getReader();
409
410     if (_bufferedReader == null)
411       _bufferedReader = new BufferedReaderAdapter(getStream());
412
413     // bufferedReader is just an adapter to get the signature right.
414
_bufferedReader.init(getStream());
415
416     return _bufferedReader;
417   }
418
419   /**
420    * Returns the current session.
421    *
422    * @param create true if a new session should be created
423    *
424    * @return the current session
425    */

426   @Override JavaDoc
427   public HttpSession JavaDoc getSession(boolean create)
428   {
429     SessionManager manager = _webApp.getSessionManager();
430
431     setVaryCookie(manager.getCookieName());
432
433     if (_session != null && _session.isValid()) {
434       setHasCookie();
435
436       return _session;
437     }
438
439     if (_webApp == _oldWebApp) {
440       HttpSession JavaDoc hSession = super.getSession(create);
441       if (hSession != null)
442         setHasCookie();
443
444       return hSession;
445     }
446     else {
447       SessionImpl oldSession = _session;
448       _session = createSession(create, oldSession != null);
449     }
450
451     if (_session != null)
452       setHasCookie();
453
454     return _session;
455   }
456
457   /**
458    * Returns the current session.
459    *
460    * XXX: duplicated in AbstractHttpRequest
461    *
462    * @param create true if a new session should be created
463    *
464    * @return the current session
465    */

466   private SessionImpl createSession(boolean create, boolean hasOldSession)
467   {
468     SessionManager manager = getSessionManager();
469
470     String JavaDoc id = getRequestedSessionId();
471
472     long now = Alarm.getCurrentTime();
473
474     SessionImpl session;
475
476     if (id != null && id.length() > 6) {
477       session = manager.getSession(id, now, create,
478                    isRequestedSessionIdFromCookie());
479       if (session == null) {
480       }
481       else if (session.isValid()) {
482         if (session != null)
483           setHasCookie();
484         if (! session.getId().equals(id) && manager.enableSessionCookies()) {
485       HttpServletResponse JavaDoc response = getResponse();
486
487       if (response instanceof CauchoResponse)
488         ((CauchoResponse) getResponse()).setSessionId(session.getId());
489     }
490
491         return session;
492       }
493     }
494     else
495       id = null;
496
497     if (! create)
498       return null;
499
500     // Must accept old ids because different webApps in the same
501
// server must share the same cookie
502
//
503
// But, if the session group doesn't match, then create a new
504
// session.
505

506     session = manager.createSession(id, now, this,
507                     isRequestedSessionIdFromCookie());
508
509     if (session != null)
510       setHasCookie();
511
512     if (session.getId().equals(id))
513       return session;
514
515     if (manager.enableSessionCookies()) {
516       HttpServletResponse JavaDoc response = getResponse();
517
518       if (response instanceof CauchoResponse)
519     ((CauchoResponse) getResponse()).setSessionId(session.getId());
520     }
521
522     return session;
523   }
524
525   public boolean authenticate()
526     throws ServletException JavaDoc, IOException JavaDoc
527   {
528     if (! (getRequest() instanceof CauchoRequest))
529       return false;
530     else
531       return ((CauchoRequest) getRequest()).authenticate();
532   }
533
534   /**
535    * Cleans up at the end of the request
536    */

537   public void finish()
538     throws IOException JavaDoc
539   {
540     SessionImpl session = _session;
541     _session = null;
542
543     if (session != null)
544       session.finish();
545   }
546
547   /**
548    * Frees the request.
549    */

550   public static void free(DispatchRequest req)
551   {
552     req.free();
553
554     _freeList.free(req);
555   }
556
557   /**
558    * Clears variables.
559    */

560   protected void free()
561   {
562     super.free();
563
564     _session = null;
565     _webApp = null;
566     _oldWebApp = null;
567     _readStream = null;
568
569     if (_is != null)
570       _is.free();
571   }
572
573   public String JavaDoc toString()
574   {
575     return "DispatchRequest[" + getRequest() + "]";
576   }
577 }
578
Popular Tags