KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > ServletHttpRequest


1 // ========================================================================
2
// $Id: ServletHttpRequest.java,v 1.65 2005/08/13 00:01:27 gregwilkins Exp $
3
// Copyright 200-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16
17 package org.mortbay.jetty.servlet;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.security.Principal JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.RequestDispatcher JavaDoc;
32 import javax.servlet.ServletInputStream JavaDoc;
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.ServletRequestWrapper JavaDoc;
35 import javax.servlet.http.Cookie JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38
39 import org.apache.commons.logging.Log;
40 import org.mortbay.log.LogFactory;
41 import org.mortbay.http.HttpConnection;
42 import org.mortbay.http.HttpFields;
43 import org.mortbay.http.HttpInputStream;
44 import org.mortbay.http.HttpRequest;
45 import org.mortbay.http.SecurityConstraint;
46 import org.mortbay.util.LazyList;
47 import org.mortbay.util.LogSupport;
48 import org.mortbay.util.Resource;
49 import org.mortbay.util.StringUtil;
50 import org.mortbay.util.URI;
51
52
53 /* ------------------------------------------------------------ */
54 /** Servlet Request Wrapper.
55  * This class wraps a Jetty HTTP request as a 2.2 Servlet
56  * request.
57  * <P>
58  * Note that this wrapper is not synchronized and if a request is to
59  * be operated on by multiple threads, then higher level
60  * synchronizations may be required.
61  *
62  * @version $Id: ServletHttpRequest.java,v 1.65 2005/08/13 00:01:27 gregwilkins Exp $
63  * @author Greg Wilkins (gregw)
64  */

65 public class ServletHttpRequest
66     implements HttpServletRequest JavaDoc
67 {
68     private static Log log = LogFactory.getLog(ServletHttpRequest.class);
69
70     /* -------------------------------------------------------------- */
71     public static final String JavaDoc
72         __SESSIONID_NOT_CHECKED = "not checked",
73         __SESSIONID_URL = "url",
74         __SESSIONID_COOKIE = "cookie",
75         __SESSIONID_NONE = "none";
76
77     private static final Enumeration JavaDoc __emptyEnum =
78         Collections.enumeration(Collections.EMPTY_LIST);
79     private static final Collection JavaDoc __defaultLocale =
80         Collections.singleton(Locale.getDefault());
81
82     private ServletHandler _servletHandler;
83     private HttpRequest _httpRequest;
84     private ServletHttpResponse _servletHttpResponse;
85
86     private String JavaDoc _contextPath=null;
87     private String JavaDoc _servletPath=null;
88     private String JavaDoc _pathInfo=null;
89     private String JavaDoc _query=null;
90     private String JavaDoc _pathTranslated=null;
91     private String JavaDoc _requestedSessionId=null;
92     private HttpSession JavaDoc _session=null;
93     private String JavaDoc _sessionIdState=__SESSIONID_NOT_CHECKED;
94     private ServletIn _in =null;
95     private BufferedReader JavaDoc _reader=null;
96     private int _inputState=0;
97     private ServletHolder _servletHolder;
98     private String JavaDoc _pathInContext;
99     
100     /* ------------------------------------------------------------ */
101     /** Constructor.
102      */

103     public ServletHttpRequest(ServletHandler servletHandler,
104                        String JavaDoc pathInContext,
105                        HttpRequest request)
106     {
107         _servletHandler=servletHandler;
108         _pathInContext=pathInContext;
109         _contextPath=_servletHandler.getHttpContext().getContextPath();
110         if (_contextPath.length()<=1)
111             _contextPath="";
112
113         _httpRequest=request;
114     }
115
116     /* ------------------------------------------------------------ */
117     void recycle(ServletHandler servletHandler,String JavaDoc pathInContext)
118     {
119         _servletHandler=servletHandler;
120         _pathInContext=pathInContext;
121         _servletPath=null;
122         _pathInfo=null;
123         _query=null;
124         _pathTranslated=null;
125         _requestedSessionId=null;
126         _session=null;
127         _sessionIdState=__SESSIONID_NOT_CHECKED;
128         _in=null;
129         _reader=null;
130         _inputState=0;
131         _servletHolder=null;
132
133         if (servletHandler!=null)
134             _contextPath=_servletHandler.getHttpContext().getContextPath();
135         if (_contextPath!=null&&_contextPath.length()<=1)
136                 _contextPath="";
137     }
138     
139     
140     /* ------------------------------------------------------------ */
141     ServletHandler getServletHandler()
142     {
143         return _servletHandler;
144     }
145
146     /* ------------------------------------------------------------ */
147     void setServletHandler(ServletHandler servletHandler)
148     {
149         _servletHandler=servletHandler;
150     }
151     
152     /* ------------------------------------------------------------ */
153     /** Set servletpath and pathInfo.
154      * Called by the Handler before passing a request to a particular
155      * holder to split the context path into a servlet path and path info.
156      * @param servletPath
157      * @param pathInfo
158      */

159     void setServletPaths(String JavaDoc servletPath,
160                          String JavaDoc pathInfo,
161                          ServletHolder holder)
162     {
163         _servletPath=servletPath;
164         _pathInfo=pathInfo;
165         _servletHolder=holder;
166     }
167     
168     /* ------------------------------------------------------------ */
169     ServletHolder getServletHolder()
170     {
171         return _servletHolder;
172     }
173     
174     /* ------------------------------------------------------------ */
175     String JavaDoc getPathInContext()
176     {
177         return _pathInContext;
178     }
179     
180     /* ------------------------------------------------------------ */
181     HttpRequest getHttpRequest()
182     {
183         return _httpRequest;
184     }
185     
186     /* ------------------------------------------------------------ */
187     public ServletHttpResponse getServletHttpResponse()
188     {
189         return _servletHttpResponse;
190     }
191     
192     /* ------------------------------------------------------------ */
193     void setServletHttpResponse(ServletHttpResponse response)
194     {
195         _servletHttpResponse = response;
196     }
197
198     /* ------------------------------------------------------------ */
199     public Locale JavaDoc getLocale()
200     {
201         Enumeration JavaDoc enm = _httpRequest.getFieldValues(HttpFields.__AcceptLanguage,
202                                                        HttpFields.__separators);
203
204         // handle no locale
205
if (enm == null || !enm.hasMoreElements())
206             return Locale.getDefault();
207         
208         // sort the list in quality order
209
List JavaDoc acceptLanguage = HttpFields.qualityList(enm);
210         if (acceptLanguage.size()==0)
211             return Locale.getDefault();
212
213         int size=acceptLanguage.size();
214         
215         // convert to locals
216
for (int i=0; i<size; i++)
217         {
218             String JavaDoc language = (String JavaDoc)acceptLanguage.get(i);
219             language=HttpFields.valueParameters(language,null);
220             String JavaDoc country = "";
221             int dash = language.indexOf('-');
222             if (dash > -1)
223             {
224                 country = language.substring(dash + 1).trim();
225                 language = language.substring(0,dash).trim();
226             }
227             return new Locale JavaDoc(language,country);
228         }
229
230         return Locale.getDefault();
231     }
232     
233     /* ------------------------------------------------------------ */
234     public Enumeration JavaDoc getLocales()
235     {
236         Enumeration JavaDoc enm = _httpRequest.getFieldValues(HttpFields.__AcceptLanguage,
237                                                        HttpFields.__separators);
238
239         // handle no locale
240
if (enm == null || !enm.hasMoreElements())
241             return Collections.enumeration(__defaultLocale);
242         
243         // sort the list in quality order
244
List JavaDoc acceptLanguage = HttpFields.qualityList(enm);
245         
246         if (acceptLanguage.size()==0)
247             return
248                 Collections.enumeration(__defaultLocale);
249
250         Object JavaDoc langs = null;
251         int size=acceptLanguage.size();
252         
253         // convert to locals
254
for (int i=0; i<size; i++)
255         {
256             String JavaDoc language = (String JavaDoc)acceptLanguage.get(i);
257             language=HttpFields.valueParameters(language,null);
258             String JavaDoc country = "";
259             int dash = language.indexOf('-');
260             if (dash > -1)
261             {
262                 country = language.substring(dash + 1).trim();
263                 language = language.substring(0,dash).trim();
264             }
265             langs=LazyList.ensureSize(langs,size);
266             langs=LazyList.add(langs,new Locale JavaDoc(language,country));
267         }
268
269         if (LazyList.size(langs)==0)
270             return Collections.enumeration(__defaultLocale);
271             
272         return Collections.enumeration(LazyList.getList(langs));
273     }
274     
275     /* ------------------------------------------------------------ */
276     public boolean isSecure()
277     {
278         return _httpRequest.isConfidential();
279     }
280     
281     /* ------------------------------------------------------------ */
282     public Cookie JavaDoc[] getCookies()
283     {
284         Cookie JavaDoc[] cookies = _httpRequest.getCookies();
285         if (cookies.length==0)
286             return null;
287         return cookies;
288     }
289     
290     /* ------------------------------------------------------------ */
291     public long getDateHeader(String JavaDoc name)
292     {
293         return _httpRequest.getDateField(name);
294     }
295     
296     /* ------------------------------------------------------------ */
297     public Enumeration JavaDoc getHeaderNames()
298     {
299         return _httpRequest.getFieldNames();
300     }
301     
302     /* ------------------------------------------------------------ */
303     public String JavaDoc getHeader(String JavaDoc name)
304     {
305         return _httpRequest.getField(name);
306     }
307     
308     /* ------------------------------------------------------------ */
309     public Enumeration JavaDoc getHeaders(String JavaDoc s)
310     {
311         Enumeration JavaDoc enm=_httpRequest.getFieldValues(s);
312         if (enm==null)
313             return __emptyEnum;
314         return enm;
315     }
316     
317     /* ------------------------------------------------------------ */
318     public int getIntHeader(String JavaDoc name)
319         throws NumberFormatException JavaDoc
320     {
321         return _httpRequest.getIntField(name);
322     }
323     
324     /* ------------------------------------------------------------ */
325     public String JavaDoc getMethod()
326     {
327         return _httpRequest.getMethod();
328     }
329     
330     /* ------------------------------------------------------------ */
331     public String JavaDoc getContextPath()
332     {
333         return _contextPath;
334     }
335     
336     /* ------------------------------------------------------------ */
337     public String JavaDoc getPathInfo()
338     {
339         if (_servletPath==null)
340             return null;
341         return _pathInfo;
342     }
343     
344     /* ------------------------------------------------------------ */
345     public String JavaDoc getPathTranslated()
346     {
347         if (_pathInfo==null || _pathInfo.length()==0)
348             return null;
349         if (_pathTranslated==null)
350         {
351             Resource resource =
352                 _servletHandler.getHttpContext().getBaseResource();
353
354             if (resource==null)
355                 return null;
356
357             try
358             {
359                 resource=resource.addPath(_pathInfo);
360                 File JavaDoc file = resource.getFile();
361                 if (file==null)
362                     return null;
363                 _pathTranslated=file.getAbsolutePath();
364             }
365             catch(Exception JavaDoc e)
366             {
367                 log.debug(LogSupport.EXCEPTION,e);
368             }
369         }
370         
371         return _pathTranslated;
372     }
373     
374     /* ------------------------------------------------------------ */
375     public String JavaDoc getQueryString()
376     {
377         if (_query==null)
378             _query =_httpRequest.getQuery();
379         return _query;
380     }
381     
382     /* ------------------------------------------------------------ */
383     public String JavaDoc getAuthType()
384     {
385         String JavaDoc at= _httpRequest.getAuthType();
386         if (at==SecurityConstraint.__BASIC_AUTH)
387             return HttpServletRequest.BASIC_AUTH;
388         if (at==SecurityConstraint.__FORM_AUTH)
389             return HttpServletRequest.FORM_AUTH;
390         if (at==SecurityConstraint.__DIGEST_AUTH)
391             return HttpServletRequest.DIGEST_AUTH;
392         if (at==SecurityConstraint.__CERT_AUTH)
393             return HttpServletRequest.CLIENT_CERT_AUTH;
394         if (at==SecurityConstraint.__CERT_AUTH2)
395             return HttpServletRequest.CLIENT_CERT_AUTH;
396         return at;
397     }
398
399     /* ------------------------------------------------------------ */
400     public String JavaDoc getRemoteUser()
401     {
402         return _httpRequest.getAuthUser();
403     }
404
405     /* ------------------------------------------------------------ */
406     public boolean isUserInRole(String JavaDoc role)
407     {
408         if (_servletHolder!=null)
409             role=_servletHolder.getUserRoleLink(role);
410         return _httpRequest.isUserInRole(role);
411     }
412
413     /* ------------------------------------------------------------ */
414     public Principal JavaDoc getUserPrincipal()
415     {
416         return _httpRequest.getUserPrincipal();
417     }
418     
419     /* ------------------------------------------------------------ */
420     void setRequestedSessionId(String JavaDoc pathParams)
421     {
422         _requestedSessionId=null;
423         
424         // try cookies first
425
if (_servletHandler.isUsingCookies())
426         {
427             Cookie JavaDoc[] cookies=_httpRequest.getCookies();
428             if (cookies!=null && cookies.length>0)
429             {
430                 for (int i=0;i<cookies.length;i++)
431                 {
432                     if (SessionManager.__SessionCookie.equalsIgnoreCase(cookies[i].getName()))
433                     {
434                         if (_requestedSessionId!=null)
435                         {
436                             // Multiple jsessionid cookies. Probably due to
437
// multiple paths and/or domains. Pick the first
438
// known session or the last defined cookie.
439
SessionManager manager = _servletHandler.getSessionManager();
440                             if (manager!=null && manager.getHttpSession(_requestedSessionId)!=null)
441                                 break;
442                             log.debug("multiple session cookies");
443                         }
444                         
445                         _requestedSessionId=cookies[i].getValue();
446                         _sessionIdState = __SESSIONID_COOKIE;
447                         if(log.isDebugEnabled())log.debug("Got Session "+_requestedSessionId+" from cookie");
448                     }
449                 }
450             }
451         }
452             
453         // check if there is a url encoded session param.
454
if (pathParams!=null && pathParams.startsWith(SessionManager.__SessionURL))
455         {
456             String JavaDoc id =
457                 pathParams.substring(SessionManager.__SessionURL.length()+1);
458             if(log.isDebugEnabled())log.debug("Got Session "+id+" from URL");
459             
460             if (_requestedSessionId==null)
461             {
462                 _requestedSessionId=id;
463                 _sessionIdState = __SESSIONID_URL;
464             }
465             else if (!id.equals(_requestedSessionId))
466                 log.debug("Mismatched session IDs");
467         }
468         
469         if (_requestedSessionId == null)
470             _sessionIdState = __SESSIONID_NONE;
471     }
472     
473     /* ------------------------------------------------------------ */
474     public String JavaDoc getRequestedSessionId()
475     {
476         return _requestedSessionId;
477     }
478     
479     /* ------------------------------------------------------------ */
480     public String JavaDoc getRequestURI()
481     {
482         return _httpRequest.getEncodedPath();
483     }
484     
485     /* ------------------------------------------------------------ */
486     public StringBuffer JavaDoc getRequestURL()
487     {
488         StringBuffer JavaDoc buf = _httpRequest.getRootURL();
489         buf.append(getRequestURI());
490         return buf;
491     }
492     
493     /* ------------------------------------------------------------ */
494     public String JavaDoc getServletPath()
495     {
496         if (_servletPath==null)
497             return _pathInContext;
498         return _servletPath;
499     }
500     
501     /* ------------------------------------------------------------ */
502     public HttpSession JavaDoc getSession(boolean create)
503     {
504         if (_session != null && ((SessionManager.Session)_session).isValid())
505             return _session;
506         
507         _session=null;
508         
509         String JavaDoc id = getRequestedSessionId();
510         
511         if (id != null)
512         {
513             _session=_servletHandler.getHttpSession(id);
514             if (_session == null && !create)
515                 return null;
516         }
517         
518         if (_session == null && create)
519         {
520             _session=newSession();
521         }
522         
523         return _session;
524     }
525
526     /* ------------------------------------------------------------ */
527     /* Create a new HttpSession.
528      * If cookies are being used a set cookie is added to the response.
529      */

530     HttpSession JavaDoc newSession()
531     {
532         HttpSession JavaDoc session=_servletHandler.newHttpSession(this);
533         Cookie JavaDoc cookie=_servletHandler.getSessionManager().getSessionCookie(session,isSecure());
534         if (cookie!=null)
535             _servletHttpResponse.getHttpResponse().addSetCookie(cookie);
536         return session;
537     }
538     
539     /* ------------------------------------------------------------ */
540     public HttpSession JavaDoc getSession()
541     {
542         HttpSession JavaDoc session = getSession(true);
543         return session;
544     }
545     
546     /* ------------------------------------------------------------ */
547     public boolean isRequestedSessionIdValid()
548     {
549         return _requestedSessionId != null && getSession(false) != null;
550     }
551     
552     /* -------------------------------------------------------------- */
553     public boolean isRequestedSessionIdFromCookie()
554     {
555         return _sessionIdState == __SESSIONID_COOKIE;
556     }
557     
558     /* -------------------------------------------------------------- */
559     public boolean isRequestedSessionIdFromURL()
560     {
561         return _sessionIdState == __SESSIONID_URL;
562     }
563     
564     /* -------------------------------------------------------------- */
565     /**
566      * @deprecated
567      */

568     public boolean isRequestedSessionIdFromUrl()
569     {
570         return isRequestedSessionIdFromURL();
571     }
572     
573     /* -------------------------------------------------------------- */
574     public Enumeration JavaDoc getAttributeNames()
575     {
576         return _httpRequest.getAttributeNames();
577     }
578     
579     /* -------------------------------------------------------------- */
580     public Object JavaDoc getAttribute(String JavaDoc name)
581     {
582         return _httpRequest.getAttribute(name);
583     }
584     
585     /* -------------------------------------------------------------- */
586     public void setAttribute(String JavaDoc name, Object JavaDoc value)
587     {
588         _httpRequest.setAttribute(name,value);
589     }
590     
591     /* -------------------------------------------------------------- */
592     public void removeAttribute(String JavaDoc name)
593     {
594         _httpRequest.removeAttribute(name);
595     }
596     
597     /* -------------------------------------------------------------- */
598     public void setCharacterEncoding(String JavaDoc encoding)
599         throws UnsupportedEncodingException JavaDoc
600     {
601         if (_inputState!=0)
602             throw new IllegalStateException JavaDoc("getReader() or getInputStream() called");
603         "".getBytes(encoding);
604         _httpRequest.setCharacterEncoding(encoding,false);
605     }
606     
607     /* -------------------------------------------------------------- */
608     public String JavaDoc getCharacterEncoding()
609     {
610         return _httpRequest.getCharacterEncoding();
611     }
612     
613     /* -------------------------------------------------------------- */
614     public int getContentLength()
615     {
616         return _httpRequest.getContentLength();
617     }
618     
619     /* -------------------------------------------------------------- */
620     public String JavaDoc getContentType()
621     {
622         return _httpRequest.getContentType();
623     }
624     
625     /* -------------------------------------------------------------- */
626     public ServletInputStream JavaDoc getInputStream()
627     {
628         if (_inputState!=0 && _inputState!=1)
629             throw new IllegalStateException JavaDoc();
630         if (_in==null)
631             _in = new ServletIn((HttpInputStream)_httpRequest.getInputStream());
632         _inputState=1;
633         _reader=null;
634         return _in;
635     }
636     
637     /* -------------------------------------------------------------- */
638     /**
639      * This method is not recommended as it forces the generation of a
640      * non-optimal data structure.
641      */

642     public Map JavaDoc getParameterMap()
643     {
644         return Collections.unmodifiableMap(_httpRequest.getParameterStringArrayMap());
645     }
646     
647     /* -------------------------------------------------------------- */
648     public String JavaDoc getParameter(String JavaDoc name)
649     {
650         return _httpRequest.getParameter(name);
651     }
652     
653     /* -------------------------------------------------------------- */
654     public Enumeration JavaDoc getParameterNames()
655     {
656         return Collections.enumeration(_httpRequest.getParameterNames());
657     }
658     
659     /* -------------------------------------------------------------- */
660     public String JavaDoc[] getParameterValues(String JavaDoc name)
661     {
662         List JavaDoc v=_httpRequest.getParameterValues(name);
663         if (v==null)
664             return null;
665         String JavaDoc[]a=new String JavaDoc[v.size()];
666         return (String JavaDoc[])v.toArray(a);
667     }
668     
669     /* -------------------------------------------------------------- */
670     public String JavaDoc getProtocol()
671     {
672         return _httpRequest.getVersion();
673     }
674     
675     /* -------------------------------------------------------------- */
676     public String JavaDoc getScheme()
677     {
678         return _httpRequest.getScheme();
679     }
680     
681     /* -------------------------------------------------------------- */
682     public String JavaDoc getServerName()
683     {
684         return _httpRequest.getHost();
685     }
686     
687     /* -------------------------------------------------------------- */
688     public int getServerPort()
689     {
690         int port = _httpRequest.getPort();
691         if (port==0)
692         {
693             if (getScheme().equalsIgnoreCase("https"))
694                 return 443;
695             return 80;
696         }
697         return port;
698     }
699
700     /* -------------------------------------------------------------- */
701     public int getRemotePort()
702     {
703         HttpConnection connection= _httpRequest.getHttpConnection();
704         if (connection!=null)
705             return connection.getRemotePort();
706         return 0;
707     }
708     
709     /* -------------------------------------------------------------- */
710     public String JavaDoc getLocalName()
711     {
712         HttpConnection connection= _httpRequest.getHttpConnection();
713         if (connection!=null)
714             return connection.getServerName();
715         return null;
716     }
717     
718     /* -------------------------------------------------------------- */
719     public String JavaDoc getLocalAddr()
720     {
721         HttpConnection connection= _httpRequest.getHttpConnection();
722         if (connection!=null)
723             return connection.getServerAddr();
724         return null;
725     }
726     
727     /* -------------------------------------------------------------- */
728     public int getLocalPort()
729     {
730         HttpConnection connection= _httpRequest.getHttpConnection();
731         if (connection!=null)
732             return connection.getServerPort();
733         return 0;
734     }
735     
736     /* -------------------------------------------------------------- */
737     public BufferedReader JavaDoc getReader()
738         throws UnsupportedEncodingException JavaDoc
739     {
740         if (_inputState!=0 && _inputState!=2)
741             throw new IllegalStateException JavaDoc();
742         if (_reader==null)
743         {
744             String JavaDoc encoding=getCharacterEncoding();
745             if (encoding==null)
746                 encoding=StringUtil.__ISO_8859_1;
747             _reader=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getInputStream(),encoding));
748             
749         }
750         _inputState=2;
751         return _reader;
752     }
753     
754     /* -------------------------------------------------------------- */
755     public String JavaDoc getRemoteAddr()
756     {
757         return _httpRequest.getRemoteAddr();
758     }
759     
760     /* -------------------------------------------------------------- */
761     public String JavaDoc getRemoteHost()
762     {
763         if (_httpRequest.getHttpConnection()==null)
764             return null;
765         return _httpRequest.getRemoteHost();
766     }
767
768     /* -------------------------------------------------------------- */
769     /**
770      * @deprecated As of Version 2.1 of the Java Servlet API,
771      * use {@link javax.servlet.ServletContext#getRealPath} instead.
772      */

773     public String JavaDoc getRealPath(String JavaDoc path)
774     {
775         return _servletHandler.getServletContext().getRealPath(path);
776     }
777
778     /* ------------------------------------------------------------ */
779     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc url)
780     {
781         if (url == null)
782             return null;
783
784         if (!url.startsWith("/"))
785         {
786             String JavaDoc relTo=URI.addPaths(_servletPath,_pathInfo);
787             int slash=relTo.lastIndexOf("/");
788             if (slash>1)
789                 relTo=relTo.substring(0,slash+1);
790             else
791                 relTo="/";
792             url=URI.addPaths(relTo,url);
793         }
794     
795         return _servletHandler.getServletContext().getRequestDispatcher(url);
796     }
797
798     /* ------------------------------------------------------------ */
799     public String JavaDoc toString()
800     {
801         return
802             getContextPath()+"+"+getServletPath()+"+"+getPathInfo()+"\n"+
803             _httpRequest.toString();
804     }
805
806     
807     /* ------------------------------------------------------------ */
808     /** Unwrap a ServletRequest.
809      *
810      * @see javax.servlet.ServletRequestWrapper
811      * @see javax.servlet.http.HttpServletRequestWrapper
812      * @param request
813      * @return The core ServletHttpRequest which must be the
814      * underlying request object
815      */

816     public static ServletHttpRequest unwrap(ServletRequest JavaDoc request)
817     {
818         while (!(request instanceof ServletHttpRequest))
819         {
820             if (request instanceof ServletRequestWrapper JavaDoc)
821             {
822                 ServletRequestWrapper JavaDoc wrapper =
823                     (ServletRequestWrapper JavaDoc)request;
824                 request=wrapper.getRequest();
825             }
826             else
827                 throw new IllegalArgumentException JavaDoc("Does not wrap ServletHttpRequest");
828         }
829
830         return (ServletHttpRequest)request;
831     }
832 }
833
834
835
836
837
838
839
Popular Tags