KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > RequestAdapter


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.connection;
30
31 import com.caucho.server.session.SessionManager;
32 import com.caucho.server.webapp.WebApp;
33 import com.caucho.util.FreeList;
34 import com.caucho.util.L10N;
35 import com.caucho.vfs.ReadStream;
36
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.ServletRequest JavaDoc;
39 import javax.servlet.ServletRequestWrapper JavaDoc;
40 import javax.servlet.http.Cookie JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.servlet.http.HttpSession JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.HashMap JavaDoc;
46
47 /**
48  * Any requests that depends on an underlying request, like
49  * include() requests or adapters for other servlet engines.
50  */

51 public class RequestAdapter extends RequestWrapper
52   implements CauchoRequest {
53   static final L10N L = new L10N(RequestAdapter.class);
54
55   static final int MAX_DEPTH = 64;
56   
57   public static String JavaDoc REQUEST_URI = "javax.servlet.include.request_uri";
58   public static String JavaDoc CONTEXT_PATH = "javax.servlet.include.context_path";
59   public static String JavaDoc SERVLET_PATH = "javax.servlet.include.servlet_path";
60   public static String JavaDoc PATH_INFO = "javax.servlet.include.path_info";
61   public static String JavaDoc QUERY_STRING = "javax.servlet.include.query_string";
62   
63   public static String JavaDoc STATUS_CODE = "javax.servlet.error.status_code";
64   public static String JavaDoc EXCEPTION_TYPE = "javax.servlet.error.exception_type";
65   public static String JavaDoc MESSAGE = "javax.servlet.error.message";
66   public static String JavaDoc EXCEPTION = "javax.servlet.error.exception";
67   public static String JavaDoc ERROR_URI = "javax.servlet.error.request_uri";
68   public static String JavaDoc SERVLET_NAME = "javax.servlet.error.servlet_name";
69   
70   public static String JavaDoc JSP_EXCEPTION = "javax.servlet.jsp.jspException";
71   
72   public static String JavaDoc SHUTDOWN = "com.caucho.shutdown";
73
74   private static final FreeList<RequestAdapter> _freeList =
75     new FreeList<RequestAdapter>(16);
76   
77   // for real adapters
78
private WebApp _webApp;
79   private HttpServletResponse JavaDoc _response;
80
81   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _roleMap;
82
83   protected RequestAdapter()
84   {
85     super(null);
86   }
87   
88   protected RequestAdapter(HttpServletRequest JavaDoc request, WebApp app)
89   {
90     super(request);
91     
92     _webApp = app;
93   }
94
95   /**
96    * Creates a new RequestAdapter.
97    */

98   public static RequestAdapter create(HttpServletRequest JavaDoc request,
99                       WebApp app)
100   {
101     RequestAdapter reqAdapt = _freeList.allocate();
102
103     if (reqAdapt == null)
104       return new RequestAdapter(request, app);
105     else {
106       reqAdapt.setRequest(request);
107       reqAdapt._webApp = app;
108
109       return reqAdapt;
110     }
111   }
112
113   /**
114    * Creates a new RequestAdapter.
115    */

116   public static RequestAdapter create()
117   {
118     RequestAdapter reqAdapt = _freeList.allocate();
119
120     if (reqAdapt != null)
121       return reqAdapt;
122     else
123       return new RequestAdapter();
124   }
125
126   public void init(HttpServletRequest JavaDoc request,
127                    HttpServletResponse JavaDoc response,
128                    WebApp app)
129     throws ServletException JavaDoc
130   {
131     setRequest(request);
132     
133     _response = response;
134     _webApp = app;
135
136     if (request == this ||
137     request instanceof CauchoRequest &&
138     ((CauchoRequest) request).getRequestDepth(0) > MAX_DEPTH) {
139       throw new ServletException JavaDoc(L.l("too many servlet includes `{0}'",
140                                      request.getRequestURI()));
141     }
142   }
143
144   public boolean isTop()
145   {
146     return false;
147   }
148   
149   public void setWebApp(WebApp app)
150   {
151     _webApp = app;
152   }
153
154   protected HttpServletResponse JavaDoc getResponse()
155   {
156     return _response;
157   }
158
159   public void setResponse(CauchoResponse response)
160   {
161     _response = response;
162   }
163
164   /**
165    * Returns the underlying read stream.
166    */

167   public ReadStream getStream() throws IOException JavaDoc
168   {
169     if (getRequest() instanceof CauchoRequest)
170       return ((CauchoRequest) getRequest()).getStream();
171     else
172       return null;
173   }
174
175   /**
176    * Returns the URI for the current page: included or top-level.
177    */

178   public String JavaDoc getPageURI()
179   {
180     String JavaDoc uri = (String JavaDoc) getAttribute(REQUEST_URI);
181     
182     if (uri != null)
183       return uri;
184     else
185       return getRequestURI();
186   }
187
188   public static String JavaDoc getPageURI(HttpServletRequest JavaDoc request)
189   {
190     String JavaDoc uri = (String JavaDoc) request.getAttribute(REQUEST_URI);
191     
192     if (uri != null)
193       return uri;
194     else
195       return request.getRequestURI();
196   }
197
198   public String JavaDoc getPageContextPath()
199   {
200     String JavaDoc contextPath = (String JavaDoc) getAttribute(CONTEXT_PATH);
201     
202     if (contextPath != null)
203       return contextPath;
204     else
205       return getContextPath();
206   }
207
208   public static String JavaDoc getPageContextPath(HttpServletRequest JavaDoc request)
209   {
210     String JavaDoc contextPath = (String JavaDoc) request.getAttribute(CONTEXT_PATH);
211     
212     if (contextPath != null)
213       return contextPath;
214     else
215       return request.getContextPath();
216   }
217   
218   /**
219    * Returns the servlet-path for the current page, i.e. this will return the
220    * url of the include page, not the original request.
221    */

222   public String JavaDoc getPageServletPath()
223   {
224     String JavaDoc servletPath = (String JavaDoc) getAttribute(SERVLET_PATH);
225     
226     if (servletPath != null)
227       return servletPath;
228     else
229       return getServletPath();
230   }
231   
232   /**
233    * Returns the servlet-path for the current page, i.e. this will return the
234    * url of the include page, not the original request.
235    */

236   public static String JavaDoc getPageServletPath(HttpServletRequest JavaDoc request)
237   {
238     String JavaDoc servletPath = (String JavaDoc) request.getAttribute(SERVLET_PATH);
239     
240     if (servletPath != null)
241       return servletPath;
242     else
243       return request.getServletPath();
244   }
245
246   /**
247    * Returns the path-info for the current page, i.e. this will return the
248    * url of the include page, not the original request.
249    */

250   public String JavaDoc getPagePathInfo()
251   {
252     String JavaDoc uri = (String JavaDoc) getAttribute(REQUEST_URI);
253     
254     if (uri != null)
255       return (String JavaDoc) getAttribute(PATH_INFO);
256     else
257       return getPathInfo();
258   }
259
260   /**
261    * Returns the path-info for the current page, i.e. this will return the
262    * url of the include page, not the original request.
263    */

264   public static String JavaDoc getPagePathInfo(HttpServletRequest JavaDoc request)
265   {
266     String JavaDoc uri = (String JavaDoc) request.getAttribute(REQUEST_URI);
267     
268     if (uri != null)
269       return (String JavaDoc) request.getAttribute(PATH_INFO);
270     else
271       return request.getPathInfo();
272   }
273   
274   /**
275    * Returns the query-string for the current page, i.e. this will return the
276    * url of the include page, not the original request.
277    */

278   public String JavaDoc getPageQueryString()
279   {
280     String JavaDoc uri = (String JavaDoc) getAttribute(REQUEST_URI);
281     
282     if (uri != null)
283       return (String JavaDoc) getAttribute(QUERY_STRING);
284     else
285       return getQueryString();
286   }
287   
288   /**
289    * Returns the query-string for the current page, i.e. this will return the
290    * url of the include page, not the original request.
291    */

292   public static String JavaDoc getPageQueryString(HttpServletRequest JavaDoc request)
293   {
294     String JavaDoc uri = (String JavaDoc) request.getAttribute(REQUEST_URI);
295     
296     if (uri != null)
297       return (String JavaDoc) request.getAttribute(QUERY_STRING);
298     else
299       return request.getQueryString();
300   }
301
302   public int getRequestDepth(int depth)
303   {
304     if (depth > MAX_DEPTH)
305       throw new RuntimeException JavaDoc(L.l("too many request dispatchers"));
306
307     ServletRequest JavaDoc req = getRequest();
308     while (req != null) {
309       if (req instanceof CauchoRequest)
310     return ((CauchoRequest) req).getRequestDepth(depth + 1);
311       else if (req instanceof ServletRequestWrapper JavaDoc) {
312     ServletRequestWrapper JavaDoc reqWrap = (ServletRequestWrapper JavaDoc) req;
313
314     req = reqWrap.getRequest();
315       }
316       else
317     break;
318     }
319
320     return depth + 2;
321   }
322
323   public void setHeader(String JavaDoc key, String JavaDoc value)
324   {
325   }
326   
327   public WebApp getWebApp()
328   {
329     return _webApp;
330   }
331
332   public void setVaryCookie(String JavaDoc cookie)
333   {
334     // super.setVaryCookie(cookie);
335

336     if (getRequest() instanceof CauchoRequest)
337       ((CauchoRequest) getRequest()).setVaryCookie(cookie);
338   }
339
340   public String JavaDoc getVaryCookie()
341   {
342     // super.setVaryCookie(cookie);
343

344     if (getRequest() instanceof CauchoRequest)
345       return ((CauchoRequest) getRequest()).getVaryCookie();
346     else
347       return null;
348   }
349
350   public boolean getVaryCookies()
351   {
352     // super.setVaryCookie(cookie);
353

354     if (getRequest() instanceof CauchoRequest)
355       return ((CauchoRequest) getRequest()).getVaryCookies();
356     else
357       return false;
358   }
359
360   public void setHasCookie()
361   {
362     // super.setHasCookie();
363

364     if (getRequest() instanceof CauchoRequest)
365       ((CauchoRequest) getRequest()).setHasCookie();
366   }
367
368   public boolean getHasCookie()
369   {
370     // super.setHasCookie();
371

372     if (getRequest() instanceof CauchoRequest)
373       return ((CauchoRequest) getRequest()).getHasCookie();
374     else
375       return false;
376   }
377       
378   public HttpSession JavaDoc getSession(boolean create)
379   {
380     SessionManager manager = getSessionManager();
381     
382     setVaryCookie(getCookieName(manager));
383
384     HttpSession JavaDoc session = super.getSession(create);
385
386     if (session != null)
387       setHasCookie();
388     
389     return session;
390   }
391
392   public String JavaDoc getRequestedSessionId()
393   {
394     SessionManager manager = getSessionManager();
395     
396     setVaryCookie(getCookieName(manager));
397
398     String JavaDoc id = super.getRequestedSessionId();
399
400     if (id != null)
401       setHasCookie();
402
403     return id;
404   }
405
406   public boolean isRequestedSessionIdValid()
407   {
408     SessionManager manager = getSessionManager();
409     
410     setVaryCookie(getCookieName(manager));
411
412     boolean isValid = super.isRequestedSessionIdValid();
413
414     if (isValid)
415       setHasCookie();
416
417     return isValid;
418   }
419
420   public boolean isRequestedSessionIdFromCookie()
421   {
422     SessionManager manager = getSessionManager();
423     
424     setVaryCookie(getCookieName(manager));
425
426     boolean isValid = super.isRequestedSessionIdFromCookie();
427     if (isValid)
428       setHasCookie();
429
430     return isValid;
431   }
432
433   public boolean isRequestedSessionIdFromURL()
434   {
435     SessionManager manager = getSessionManager();
436
437     setVaryCookie(getCookieName(manager));
438
439     boolean isValid = super.isRequestedSessionIdFromURL();
440     
441     if (isValid)
442       setHasCookie();
443
444     return isValid;
445   }
446
447   protected final SessionManager getSessionManager()
448   {
449     WebApp app = getWebApp();
450     if (app != null)
451       return app.getSessionManager();
452     else
453       return null;
454   }
455
456   protected final String JavaDoc getCookieName(SessionManager manager)
457   {
458     if (isSecure())
459       return manager.getCookieName();
460     else
461       return manager.getSSLCookieName();
462   }
463
464   public Cookie JavaDoc []getCookies()
465   {
466     // page depends on any cookie
467
setVaryCookie(null);
468     
469     Cookie JavaDoc []cookies = super.getCookies();
470     if (cookies != null && cookies.length > 0)
471       setHasCookie();
472
473     return cookies;
474   }
475
476   public Cookie JavaDoc getCookie(String JavaDoc name)
477   {
478     // page depends on this cookie
479
setVaryCookie(name);
480
481     if (getRequest() instanceof CauchoRequest)
482       return ((CauchoRequest) getRequest()).getCookie(name);
483
484     Cookie JavaDoc []cookies = super.getCookies();
485     for (int i = 0; i < cookies.length; i++) {
486       if (cookies[i].getName().equals(name)) {
487         setHasCookie();
488         return cookies[i];
489       }
490     }
491
492     return null;
493   }
494   
495   public void killKeepalive()
496   {
497   }
498   
499   public boolean allowKeepalive()
500   {
501     return true;
502   }
503
504   /**
505    * Sets the role map.
506    */

507   public HashMap JavaDoc<String JavaDoc,String JavaDoc> setRoleMap(HashMap JavaDoc<String JavaDoc,String JavaDoc> map)
508   {
509     HashMap JavaDoc<String JavaDoc,String JavaDoc> oldMap = _roleMap;
510     _roleMap = map;
511
512     return oldMap;
513   }
514
515   /**
516    * Checks the isUserInRole.
517    */

518   public boolean isUserInRole(String JavaDoc role)
519   {
520     if (_roleMap != null) {
521       String JavaDoc newRole = _roleMap.get(role);
522       
523       if (newRole != null)
524     role = newRole;
525     }
526
527     return super.isUserInRole(role);
528   }
529   
530   public boolean authenticate()
531     throws ServletException JavaDoc, IOException JavaDoc
532   {
533     return true;
534   }
535
536   /**
537    * Frees the adapter for reuse.
538    */

539   public static void free(RequestAdapter reqAdapt)
540   {
541     reqAdapt.free();
542
543     _freeList.free(reqAdapt);
544   }
545
546   /**
547    * Clears the adapter.
548    */

549   protected void free()
550   {
551     super.free();
552     
553     _webApp = null;
554     _response = null;
555   }
556 }
557
Popular Tags