KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ========================================================================
2
// $Id: Dispatcher.java,v 1.92 2005/12/12 18:03:31 gregwilkins Exp $
3
// Copyright 199-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 package org.mortbay.jetty.servlet;
17
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.ServletOutputStream JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40
41 import org.apache.commons.logging.Log;
42 import org.mortbay.log.LogFactory;
43 import org.mortbay.http.HttpConnection;
44 import org.mortbay.http.PathMap;
45 import org.mortbay.util.LazyList;
46 import org.mortbay.util.LogSupport;
47 import org.mortbay.util.MultiMap;
48 import org.mortbay.util.StringMap;
49 import org.mortbay.util.URI;
50 import org.mortbay.util.UrlEncoded;
51 import org.mortbay.util.WriterOutputStream;
52
53 /* ------------------------------------------------------------ */
54 /** Servlet RequestDispatcher.
55  *
56  * @version $Id: Dispatcher.java,v 1.92 2005/12/12 18:03:31 gregwilkins Exp $
57  * @author Greg Wilkins (gregw)
58  */

59 public class Dispatcher implements RequestDispatcher JavaDoc
60 {
61     static Log log = LogFactory.getLog(Dispatcher.class);
62
63
64     /* ------------------------------------------------------------ */
65     /** Dispatch types */
66     public static final int __DEFAULT=0;
67     public static final int __REQUEST=1;
68     public static final int __FORWARD=2;
69     public static final int __INCLUDE=4;
70     public static final int __ERROR=8;
71     public static final int __ALL=15;
72     
73     /** Dispatch include attribute names */
74     public final static String JavaDoc __INCLUDE_REQUEST_URI= "javax.servlet.include.request_uri";
75     public final static String JavaDoc __INCLUDE_CONTEXT_PATH= "javax.servlet.include.context_path";
76     public final static String JavaDoc __INCLUDE_SERVLET_PATH= "javax.servlet.include.servlet_path";
77     public final static String JavaDoc __INCLUDE_PATH_INFO= "javax.servlet.include.path_info";
78     public final static String JavaDoc __INCLUDE_QUERY_STRING= "javax.servlet.include.query_string";
79
80     /** Dispatch include attribute names */
81     public final static String JavaDoc __FORWARD_REQUEST_URI= "javax.servlet.forward.request_uri";
82     public final static String JavaDoc __FORWARD_CONTEXT_PATH= "javax.servlet.forward.context_path";
83     public final static String JavaDoc __FORWARD_SERVLET_PATH= "javax.servlet.forward.servlet_path";
84     public final static String JavaDoc __FORWARD_PATH_INFO= "javax.servlet.forward.path_info";
85     public final static String JavaDoc __FORWARD_QUERY_STRING= "javax.servlet.forward.query_string";
86
87     
88     public final static StringMap __managedAttributes = new StringMap();
89     static
90     {
91         __managedAttributes.put(__INCLUDE_REQUEST_URI,__INCLUDE_REQUEST_URI);
92         __managedAttributes.put(__INCLUDE_CONTEXT_PATH,__INCLUDE_CONTEXT_PATH);
93         __managedAttributes.put(__INCLUDE_SERVLET_PATH,__INCLUDE_SERVLET_PATH);
94         __managedAttributes.put(__INCLUDE_PATH_INFO,__INCLUDE_PATH_INFO);
95         __managedAttributes.put(__INCLUDE_QUERY_STRING,__INCLUDE_QUERY_STRING);
96         
97         __managedAttributes.put(__FORWARD_REQUEST_URI,__FORWARD_REQUEST_URI);
98         __managedAttributes.put(__FORWARD_CONTEXT_PATH,__FORWARD_CONTEXT_PATH);
99         __managedAttributes.put(__FORWARD_SERVLET_PATH,__FORWARD_SERVLET_PATH);
100         __managedAttributes.put(__FORWARD_PATH_INFO,__FORWARD_PATH_INFO);
101         __managedAttributes.put(__FORWARD_QUERY_STRING,__FORWARD_QUERY_STRING);
102     }
103     
104     ServletHandler _servletHandler;
105     ServletHolder _holder=null;
106     String JavaDoc _pathSpec;
107     String JavaDoc _uriInContext;
108     String JavaDoc _pathInContext;
109     String JavaDoc _query;
110
111     
112     /* ------------------------------------------------------------ */
113     /** Constructor.
114     /** Constructor.
115      * @param servletHandler
116      * @param uriInContext Encoded uriInContext
117      * @param pathInContext Encoded pathInContext
118      * @param query
119      * @exception IllegalStateException
120      */

121     Dispatcher(ServletHandler servletHandler,
122                String JavaDoc uriInContext,
123                String JavaDoc pathInContext,
124                String JavaDoc query,
125                Map.Entry JavaDoc entry)
126         throws IllegalStateException JavaDoc
127     {
128         if(log.isDebugEnabled())log.debug("Dispatcher for "+servletHandler+","+uriInContext+","+query);
129         
130         _servletHandler=servletHandler;
131         _uriInContext=uriInContext;
132         _pathInContext=pathInContext;
133         _query=query;
134         _pathSpec=(String JavaDoc)entry.getKey();
135         _holder = (ServletHolder)entry.getValue();
136     }
137     
138     /* ------------------------------------------------------------ */
139     /** Constructor.
140      * @param servletHandler
141      * @param name
142      */

143     Dispatcher(ServletHandler servletHandler,String JavaDoc name)
144         throws IllegalStateException JavaDoc
145     {
146         _servletHandler=servletHandler;
147         _holder=_servletHandler.getServletHolder(name);
148         if (_holder==null)
149             throw new IllegalStateException JavaDoc("No named servlet handler in context");
150     }
151
152     /* ------------------------------------------------------------ */
153     public boolean isNamed()
154     {
155         return _pathInContext==null;
156     }
157     
158     /* ------------------------------------------------------------ */
159     public void include(ServletRequest JavaDoc servletRequest,
160                         ServletResponse JavaDoc servletResponse)
161         throws ServletException JavaDoc, IOException JavaDoc
162     {
163         dispatch(servletRequest,servletResponse,Dispatcher.__INCLUDE);
164     }
165     
166     /* ------------------------------------------------------------ */
167     public void forward(ServletRequest JavaDoc servletRequest,
168                         ServletResponse JavaDoc servletResponse)
169         throws ServletException JavaDoc,IOException JavaDoc
170     {
171         dispatch(servletRequest,servletResponse,Dispatcher.__FORWARD);
172     }
173     
174     /* ------------------------------------------------------------ */
175     void error(ServletRequest JavaDoc servletRequest,
176                         ServletResponse JavaDoc servletResponse)
177         throws ServletException JavaDoc,IOException JavaDoc
178     {
179         dispatch(servletRequest,servletResponse,Dispatcher.__ERROR);
180     }
181     
182     /* ------------------------------------------------------------ */
183     void dispatch(ServletRequest JavaDoc servletRequest,
184                   ServletResponse JavaDoc servletResponse,
185                   int type)
186         throws ServletException JavaDoc,IOException JavaDoc
187     {
188         HttpServletRequest JavaDoc httpServletRequest=(HttpServletRequest JavaDoc)servletRequest;
189         HttpServletResponse JavaDoc httpServletResponse=(HttpServletResponse JavaDoc)servletResponse;
190
191         HttpConnection httpConnection=
192             _servletHandler.getHttpContext().getHttpConnection();
193         ServletHttpRequest servletHttpRequest=
194             (ServletHttpRequest)httpConnection.getRequest().getWrapper();
195         
196         // wrap the request and response
197
DispatcherRequest request = new DispatcherRequest(httpServletRequest,
198                                                           servletHttpRequest,
199                                                           type);
200         DispatcherResponse response = new DispatcherResponse(request,
201                                                              httpServletResponse);
202         
203         if (type==Dispatcher.__FORWARD)
204             servletResponse.resetBuffer();
205         
206         // Merge parameters
207
String JavaDoc query=_query;
208         MultiMap parameters=null;
209         if (query!=null)
210         {
211             Map JavaDoc old_params = httpServletRequest.getParameterMap();
212             
213             // Add the parameters
214
parameters=new MultiMap();
215             UrlEncoded.decodeTo(query,parameters,request.getCharacterEncoding());
216             
217             if (old_params!=null && old_params.size()>0)
218             {
219                 // Merge old parameters.
220
Iterator JavaDoc iter = old_params.entrySet().iterator();
221                 while (iter.hasNext())
222                 {
223                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
224                     String JavaDoc name=(String JavaDoc)entry.getKey();
225                     String JavaDoc[] values=(String JavaDoc[])entry.getValue();
226                     for (int i=0;i<values.length;i++)
227                         parameters.add(name, values[i]);
228                 }
229             }
230
231             request.setParameters(parameters);
232             
233             String JavaDoc old_query=httpServletRequest.getQueryString();
234             if (old_query!=null)
235                 request.setQuery(query+"&"+old_query);
236             else
237                 request.setQuery(query);
238         }
239         
240         Object JavaDoc old_scope = null;
241         try
242         {
243             if (request.crossContext())
244             {
245                 // Setup new _context
246
old_scope=
247                     _servletHandler.getHttpContext()
248                     .enterContextScope(httpConnection.getRequest(),httpConnection.getResponse());
249             }
250         
251             if (isNamed())
252             {
253                 // No further modifications required.
254
if (_servletHandler instanceof WebApplicationHandler)
255                 {
256                     JSR154Filter filter = ((WebApplicationHandler)_servletHandler).getJsr154Filter();
257                     if (filter!=null && filter.isUnwrappedDispatchSupported())
258                     {
259                         filter.setDispatch(request, response);
260                         _servletHandler.dispatch(null,httpServletRequest,httpServletResponse,_holder, type);
261                     }
262                     else
263                         _servletHandler.dispatch(null,request,response,_holder, type);
264                 }
265                 else
266                     _servletHandler.dispatch(null,request,response,_holder, type);
267             }
268             else
269             {
270                 // Adjust servlet paths
271
request.setPaths(_servletHandler.getHttpContext().getContextPath(),
272                                  PathMap.pathMatch(_pathSpec,_pathInContext),
273                                  PathMap.pathInfo(_pathSpec,_pathInContext));
274         
275
276                 // are we wrap over or wrap under
277
if (_servletHandler instanceof WebApplicationHandler)
278                 {
279                     JSR154Filter filter = ((WebApplicationHandler)_servletHandler).getJsr154Filter();
280                     if (filter!=null && filter.isUnwrappedDispatchSupported())
281                     {
282                         filter.setDispatch(request, response);
283                         _servletHandler.dispatch(_pathInContext,httpServletRequest,httpServletResponse,_holder, type);
284                     }
285                     else
286                         _servletHandler.dispatch(_pathInContext,request,response,_holder, type);
287                 }
288                 else
289                     _servletHandler.dispatch(_pathInContext,request,response,_holder, type);
290                 
291                 
292                 if (type!=Dispatcher.__INCLUDE)
293                     response.close();
294                 else if (response.isFlushNeeded())
295                     response.flushBuffer();
296             }
297         }
298         finally
299         {
300             // restore _context
301
if (request.crossContext())
302                 _servletHandler.getHttpContext()
303                     .leaveContextScope(httpConnection.getRequest(),
304                                        httpConnection.getResponse(),
305                                        old_scope);
306         }
307     }
308
309     /* ------------------------------------------------------------ */
310     public String JavaDoc toString()
311     {
312         return "Dispatcher["+_pathSpec+","+_holder+"]";
313     }
314         
315
316     /* ------------------------------------------------------------ */
317     /** Dispatch type from name
318      */

319     public static int type(String JavaDoc type)
320     {
321         if ("request".equalsIgnoreCase(type))
322             return __REQUEST;
323         if ("forward".equalsIgnoreCase(type))
324             return __FORWARD;
325         if ("include".equalsIgnoreCase(type))
326             return __INCLUDE;
327         if ("error".equalsIgnoreCase(type))
328             return __ERROR;
329         throw new IllegalArgumentException JavaDoc(type);
330     }
331
332
333     /* ------------------------------------------------------------ */
334     /* ------------------------------------------------------------ */
335     /* ------------------------------------------------------------ */
336     class DispatcherRequest extends HttpServletRequestWrapper JavaDoc
337     {
338         DispatcherResponse _response;
339         int _filterType;
340         String JavaDoc _contextPath;
341         String JavaDoc _servletPath;
342         String JavaDoc _pathInfo;
343         MultiMap _parameters;
344         HashMap JavaDoc _attributes;
345         boolean _xContext;
346         HttpSession JavaDoc _xSession;
347         ServletHttpRequest _servletHttpRequest;
348         String JavaDoc _query;
349         
350         /* ------------------------------------------------------------ */
351         DispatcherRequest(HttpServletRequest JavaDoc httpServletRequest,
352                           ServletHttpRequest servletHttpRequest,
353                           int filterType)
354         {
355             super(httpServletRequest);
356             _servletHttpRequest=servletHttpRequest;
357             _filterType=filterType;
358             
359             // Is this being dispatched to a different context?
360
_xContext=
361                 servletHttpRequest.getServletHandler()!=_servletHandler;
362             if (_xContext)
363             {
364                 // Look for an existing or requested session ID.
365
HttpSession JavaDoc session=httpServletRequest.getSession(false);
366                 String JavaDoc session_id=(session==null)
367                     ?httpServletRequest.getRequestedSessionId()
368                     :session.getId();
369
370                 // Look for that session in new context to access it.
371
if (session_id!=null)
372                 {
373                     _xSession=_servletHandler.getHttpSession(session_id);
374                     if (_xSession!=null)
375                         ((SessionManager.Session)_xSession).access();
376                 }
377             }
378         }
379
380         /* ------------------------------------------------------------ */
381         boolean crossContext()
382         {
383             return _xContext;
384         }
385         
386         /* ------------------------------------------------------------ */
387         void setPaths(String JavaDoc cp,String JavaDoc sp, String JavaDoc pi)
388         {
389             _contextPath = (cp.length()==1 && cp.charAt(0)=='/')?"":cp;
390             _servletPath=sp;
391             _pathInfo=pi;
392         }
393
394         /* ------------------------------------------------------------ */
395         void setQuery(String JavaDoc q)
396         {
397             this._query=q;
398         }
399         
400         /* ------------------------------------------------------------ */
401         int getFilterType()
402         {
403             return _filterType;
404         }
405
406         /* ------------------------------------------------------------ */
407         String JavaDoc getPathInContext()
408         {
409             if (_pathInContext!=null)
410                 return _pathInContext;
411             else
412                 return URI.addPaths(getServletPath(),getPathInfo());
413         }
414         
415         /* ------------------------------------------------------------ */
416         public String JavaDoc getRequestURI()
417         {
418             if (_filterType==Dispatcher.__INCLUDE || isNamed())
419                 return super.getRequestURI();
420             return URI.addPaths(_contextPath,_uriInContext);
421         }
422         
423         /* ------------------------------------------------------------ */
424         public StringBuffer JavaDoc getRequestURL()
425         {
426             if (_filterType==Dispatcher.__INCLUDE || isNamed())
427                 return super.getRequestURL();
428             StringBuffer JavaDoc buf = getRootURL();
429             if (_contextPath.length()>0)
430                 buf.append(_contextPath);
431             buf.append(_uriInContext);
432             return buf;
433         }
434
435         
436         /* ------------------------------------------------------------ */
437         public String JavaDoc getPathTranslated()
438         {
439             String JavaDoc info=getPathInfo();
440             if (info==null)
441                 return null;
442             return getRealPath(info);
443         }
444         
445         /* ------------------------------------------------------------ */
446         StringBuffer JavaDoc getRootURL()
447         {
448             StringBuffer JavaDoc buf = super.getRequestURL();
449             int d=3;
450             for (int i=0;i<buf.length();i++)
451             {
452                 if (buf.charAt(i)=='/' && --d==0)
453                 {
454                     buf.setLength(i);
455                     break;
456                 }
457             }
458             return buf;
459         }
460         
461         /* ------------------------------------------------------------ */
462         public String JavaDoc getContextPath()
463         {
464             return(_filterType==Dispatcher.__INCLUDE||isNamed())?super.getContextPath():_contextPath;
465         }
466         
467         /* ------------------------------------------------------------ */
468         public String JavaDoc getServletPath()
469         {
470             return(_filterType==Dispatcher.__INCLUDE||isNamed())?super.getServletPath():_servletPath;
471         }
472         
473         /* ------------------------------------------------------------ */
474         public String JavaDoc getPathInfo()
475         {
476             return(_filterType==Dispatcher.__INCLUDE||isNamed())?super.getPathInfo():_pathInfo;
477         }
478         
479         /* ------------------------------------------------------------ */
480         public String JavaDoc getQueryString()
481         {
482             if (this._query==null)
483                 return super.getQueryString();
484             return this._query;
485         }
486
487         /* ------------------------------------------------------------ */
488         void setParameters(MultiMap parameters)
489         {
490             _parameters=parameters;
491         }
492         
493         /* -------------------------------------------------------------- */
494         public Enumeration JavaDoc getParameterNames()
495         {
496             if (_parameters==null)
497                 return super.getParameterNames();
498
499             return Collections.enumeration(_parameters.keySet());
500         }
501         
502         /* -------------------------------------------------------------- */
503         public String JavaDoc getParameter(String JavaDoc name)
504         {
505             if (_parameters==null)
506                 return super.getParameter(name);
507             return (String JavaDoc)_parameters.getValue(name,0);
508         }
509         
510         /* -------------------------------------------------------------- */
511         public String JavaDoc[] getParameterValues(String JavaDoc name)
512         {
513             if (_parameters==null)
514                 return super.getParameterValues(name);
515             List JavaDoc l =_parameters.getValues(name);
516             if (l==null)
517                 return null;
518             return (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
519         }
520         
521         /* -------------------------------------------------------------- */
522         public Map JavaDoc getParameterMap()
523         {
524             if (_parameters==null)
525                 return super.getParameterMap();
526             
527             return _parameters.toStringArrayMap();
528         }
529
530         /* ------------------------------------------------------------ */
531         public void setAttribute(String JavaDoc name, Object JavaDoc value)
532         {
533             if (__managedAttributes.containsKey(name))
534             {
535                 if (_attributes==null)
536                     _attributes=new HashMap JavaDoc(3);
537                 _attributes.put(name,value);
538             }
539             else
540                 super.setAttribute(name,value);
541         }
542         
543         /* ------------------------------------------------------------ */
544         public Object JavaDoc getAttribute(String JavaDoc name)
545         {
546             if (_attributes!=null && _attributes.containsKey(name))
547                 return _attributes.get(name);
548                 
549             if (_filterType==Dispatcher.__INCLUDE && !isNamed())
550             {
551                 if (name.equals(__INCLUDE_PATH_INFO)) return _pathInfo;
552                 if (name.equals(__INCLUDE_REQUEST_URI)) return URI.addPaths(_contextPath,_uriInContext);
553                 if (name.equals(__INCLUDE_SERVLET_PATH)) return _servletPath;
554                 if (name.equals(__INCLUDE_CONTEXT_PATH)) return _contextPath;
555                 if (name.equals(__INCLUDE_QUERY_STRING)) return Dispatcher.this._query;
556             }
557             else
558             {
559                 if (name.equals(__INCLUDE_PATH_INFO)) return null;
560                 if (name.equals(__INCLUDE_REQUEST_URI)) return null;
561                 if (name.equals(__INCLUDE_SERVLET_PATH)) return null;
562                 if (name.equals(__INCLUDE_CONTEXT_PATH)) return null;
563                 if (name.equals(__INCLUDE_QUERY_STRING)) return null;
564             }
565
566             if (_filterType!=Dispatcher.__INCLUDE && !isNamed())
567             {
568                 if (name.equals(__FORWARD_PATH_INFO))
569                     return _servletHttpRequest.getPathInfo();
570                 if (name.equals(__FORWARD_REQUEST_URI))
571                     return _servletHttpRequest.getRequestURI();
572                 if (name.equals(__FORWARD_SERVLET_PATH))
573                     return _servletHttpRequest.getServletPath();
574                 if (name.equals(__FORWARD_CONTEXT_PATH))
575                     return _servletHttpRequest.getContextPath();
576                 if (name.equals(__FORWARD_QUERY_STRING))
577                     return _servletHttpRequest.getQueryString();
578             }
579             
580             return super.getAttribute(name);
581         }
582         
583         /* ------------------------------------------------------------ */
584         public Enumeration JavaDoc getAttributeNames()
585         {
586             HashSet JavaDoc set=new HashSet JavaDoc();
587             Enumeration JavaDoc e=super.getAttributeNames();
588             while (e.hasMoreElements())
589                 set.add(e.nextElement());
590             
591             if (_filterType==Dispatcher.__INCLUDE && !isNamed())
592             {
593                 if (_pathInfo!=null)
594                     set.add(__INCLUDE_PATH_INFO);
595                 else
596                     set.remove(__INCLUDE_PATH_INFO);
597                 set.add(__INCLUDE_REQUEST_URI);
598                 set.add(__INCLUDE_SERVLET_PATH);
599                 set.add(__INCLUDE_CONTEXT_PATH);
600                 if (Dispatcher.this._query!=null)
601                     set.add(__INCLUDE_QUERY_STRING);
602                 else
603                     set.remove(__INCLUDE_QUERY_STRING);
604                     
605             }
606             else
607             {
608                 set.remove(__INCLUDE_PATH_INFO);
609                 set.remove(__INCLUDE_REQUEST_URI);
610                 set.remove(__INCLUDE_SERVLET_PATH);
611                 set.remove(__INCLUDE_CONTEXT_PATH);
612                 set.remove(__INCLUDE_QUERY_STRING);
613             }
614
615             if (_filterType!=Dispatcher.__INCLUDE && !isNamed())
616             {
617                 if (_servletHttpRequest.getPathInfo()!=null)
618                     set.add(__FORWARD_PATH_INFO);
619                 else
620                     set.remove(__FORWARD_PATH_INFO);
621                     
622                 set.add(__FORWARD_REQUEST_URI);
623                 set.add(__FORWARD_SERVLET_PATH);
624                 set.add(__FORWARD_CONTEXT_PATH);
625                 if (_servletHttpRequest.getQueryString()!=null)
626                     set.add(__FORWARD_QUERY_STRING);
627                 else
628                     set.remove(__FORWARD_QUERY_STRING);
629             }
630             
631             if (_attributes!=null)
632                 set.addAll(_attributes.keySet());
633             
634             return Collections.enumeration(set);
635         }
636         
637         /* ------------------------------------------------------------ */
638         /**
639          * If the request attribute "org.mortbay.jetty.servlet.Dispatcher.shared_session" is set, then
640          * sessions are shared in cross context dispatch. Watch out for class loading issues!
641          */

642         public HttpSession JavaDoc getSession(boolean create)
643         {
644             if (_xContext)
645             {
646                 if (_xSession==null)
647                 {
648                     if (getAttribute("org.mortbay.jetty.servlet.Dispatcher.shared_session") != null)
649                         _xSession= super.getSession(create);
650                     else
651                     {
652                         log.debug("Ctx dispatch session");
653                         
654                         String JavaDoc rsid=getRequestedSessionId();
655                         if (rsid==null)
656                         {
657                             HttpSession JavaDoc session=super.getSession(false);
658                             if (session!=null)
659                                 rsid=session.getId();
660                         }
661                         _xSession=_servletHandler.getHttpSession(rsid);
662                         if (create && _xSession==null)
663                         {
664                             _xSession=_servletHandler.newHttpSession(this);
665                             Cookie JavaDoc cookie = _servletHandler.getSessionManager().getSessionCookie(_xSession, isSecure());
666                             if (cookie!=null)
667                                 _servletHttpRequest.getHttpRequest().getHttpResponse().addSetCookie(cookie);
668                         }
669                     }
670                     
671                 }
672                 return _xSession;
673             }
674             else
675                 return super.getSession(create);
676         }
677
678         /* ------------------------------------------------------------ */
679         public boolean isRequestedSessionIdValid()
680         {
681             if (_xContext)
682             {
683                 String JavaDoc requestedSessionid = super.getRequestedSessionId();
684                 if (requestedSessionid != null)
685                 {
686                     HttpSession JavaDoc session = getSession(false);
687                     if (session != null)
688                     {
689                         return ((AbstractSessionManager.Session)session).isValid() && requestedSessionid.equals(session.getId());
690                     }
691             }
692             return false;
693             }
694             else
695             {
696                 return super.isRequestedSessionIdValid();
697             }
698         }
699
700         /* ------------------------------------------------------------ */
701         public HttpSession JavaDoc getSession()
702         {
703             return getSession(true);
704         }
705         
706         /* ------------------------------------------------------------ */
707         public String JavaDoc getRealPath(String JavaDoc path)
708         {
709             return _servletHandler.getServletContext().getRealPath(path);
710         }
711         
712         /* ------------------------------------------------------------ */
713         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc url)
714         {
715             if (url == null)
716                 return null;
717             
718             if (!url.startsWith("/"))
719             {
720                 String JavaDoc relTo=URI.addPaths(getServletPath(),getPathInfo());
721                 int slash=relTo.lastIndexOf("/");
722                 if (slash>1)
723                     relTo=relTo.substring(0,slash+1);
724                 else
725                     relTo="/";
726                 url=URI.addPaths(relTo,url);
727             }
728             
729             return _servletHandler.getServletContext().getRequestDispatcher(url);
730         }
731         
732         public String JavaDoc getMethod()
733         {
734             if (this._filterType==Dispatcher.__ERROR)
735                 return org.mortbay.http.HttpRequest.__GET;
736             return super.getMethod();
737         }
738     }
739     
740     /* ------------------------------------------------------------ */
741     /* ------------------------------------------------------------ */
742     /* ------------------------------------------------------------ */
743     class DispatcherResponse extends HttpServletResponseWrapper JavaDoc
744     {
745         DispatcherRequest _request;
746         private ServletOutputStream JavaDoc _out=null;
747         private PrintWriter JavaDoc _writer=null;
748         private boolean _flushNeeded=false;
749         private boolean _include;
750         
751         /* ------------------------------------------------------------ */
752         DispatcherResponse(DispatcherRequest request, HttpServletResponse JavaDoc response)
753         {
754             super(response);
755             _request=request;
756             request._response=this;
757             _include=_request._filterType==Dispatcher.__INCLUDE;
758             
759         }
760
761         /* ------------------------------------------------------------ */
762         public ServletOutputStream JavaDoc getOutputStream()
763             throws IOException JavaDoc
764         {
765             if (_writer!=null)
766                 throw new IllegalStateException JavaDoc("getWriter called");
767
768             if (_out==null)
769             {
770                 try {_out=super.getOutputStream();}
771                 catch(IllegalStateException JavaDoc e)
772                 {
773                     LogSupport.ignore(log,e);
774                     _flushNeeded=true;
775                     _out=new ServletOut(new WriterOutputStream(super.getWriter()));
776                 }
777             }
778
779             if (_include)
780                 _out=new DontCloseServletOut(_out);
781             
782             return _out;
783         }
784       
785         /* ------------------------------------------------------------ */
786         public PrintWriter JavaDoc getWriter()
787             throws IOException JavaDoc
788         {
789             if (_out!=null)
790                 throw new IllegalStateException JavaDoc("getOutputStream called");
791
792             if (_writer==null)
793             {
794                 try{_writer=super.getWriter();}
795                 catch(IllegalStateException JavaDoc e)
796                 {
797                     LogSupport.ignore(log, e);
798                     _flushNeeded=true;
799                     _writer = new ServletWriter(super.getOutputStream(),
800                                                 getCharacterEncoding());
801                 }
802             }
803
804             if (_include)
805                 _writer=new DontCloseWriter(_writer);
806             return _writer;
807         }
808
809         /* ------------------------------------------------------------ */
810         boolean isFlushNeeded()
811         {
812             return _flushNeeded;
813         }
814         
815         /* ------------------------------------------------------------ */
816         public void flushBuffer()
817             throws IOException JavaDoc
818         {
819             if (_writer!=null)
820                 _writer.flush();
821             if (_out!=null)
822                 _out.flush();
823             super.flushBuffer();
824         }
825         
826         /* ------------------------------------------------------------ */
827         public void close()
828             throws IOException JavaDoc
829         {
830             if (_writer!=null)
831                 _writer.close();
832             if (_out!=null)
833                 _out.close();
834         }
835         
836         /* ------------------------------------------------------------ */
837         public void setLocale(Locale JavaDoc locale)
838         {
839             if (!_include) super.setLocale(locale);
840         }
841         
842         /* ------------------------------------------------------------ */
843         public void sendError(int status, String JavaDoc message)
844             throws IOException JavaDoc
845         {
846             if (_request._filterType!=Dispatcher.__ERROR && !_include)
847                 super.sendError(status,message);
848         }
849         
850         /* ------------------------------------------------------------ */
851         public void sendError(int status)
852             throws IOException JavaDoc
853         {
854             if (_request._filterType!=Dispatcher.__ERROR && !_include)
855                 super.sendError(status);
856         }
857         
858         /* ------------------------------------------------------------ */
859         public void sendRedirect(String JavaDoc url)
860             throws IOException JavaDoc
861         {
862             if (!_include)
863             {
864                 if (!url.startsWith("http:/")&&!url.startsWith("https:/"))
865                 {
866                     StringBuffer JavaDoc buf = _request.getRootURL();
867                     
868                     if (url.startsWith("/"))
869                         buf.append(URI.canonicalPath(url));
870                     else
871                         buf.append(URI.canonicalPath(URI.addPaths(URI.parentPath(_request.getRequestURI()),url)));
872                     url=buf.toString();
873                 }
874                 
875                 super.sendRedirect(url);
876             }
877         }
878         
879         /* ------------------------------------------------------------ */
880         public void setDateHeader(String JavaDoc name, long value)
881         {
882             if (!_include) super.setDateHeader(name,value);
883         }
884         
885         /* ------------------------------------------------------------ */
886         public void setHeader(String JavaDoc name, String JavaDoc value)
887         {
888             if (!_include) super.setHeader(name,value);
889         }
890         
891         /* ------------------------------------------------------------ */
892         public void setIntHeader(String JavaDoc name, int value)
893         {
894             if (!_include) super.setIntHeader(name,value);
895         }
896         
897         /* ------------------------------------------------------------ */
898         public void addHeader(String JavaDoc name, String JavaDoc value)
899         {
900             if (!_include) super.addHeader(name,value);
901         }
902         
903         /* ------------------------------------------------------------ */
904         public void addDateHeader(String JavaDoc name, long value)
905         {
906             if (!_include) super.addDateHeader(name,value);
907         }
908         
909         /* ------------------------------------------------------------ */
910         public void addIntHeader(String JavaDoc name, int value)
911         {
912             if (!_include) super.addIntHeader(name,value);
913         }
914         
915         /* ------------------------------------------------------------ */
916         public void setStatus(int status)
917         {
918             if (_request._filterType!=Dispatcher.__ERROR && !_include)
919                 super.setStatus(status);
920         }
921         
922         /* ------------------------------------------------------------ */
923         /**
924         * The default behavior of this method is to call setStatus(int sc, String sm)
925         * on the wrapped response object.
926         *
927         * @deprecated As of version 2.1 of the Servlet spec.
928         * To set a status code
929         * use <code>setStatus(int)</code>, to send an error with a description
930         * use <code>sendError(int, String)</code>.
931         *
932         * @param status the status code
933         * @param message the status message
934         */

935         public void setStatus(int status, String JavaDoc message)
936         {
937             if (_request._filterType!=Dispatcher.__ERROR && !_include)
938                 super.setStatus(status,message);
939         }
940         
941         /* ------------------------------------------------------------ */
942         public void setContentLength(int len)
943         {
944             if (!_include) super.setContentLength(len);
945         }
946         
947         /* ------------------------------------------------------------ */
948         public void setContentType(String JavaDoc contentType)
949         {
950             if (!_include) super.setContentType(contentType);
951         }
952
953         /* ------------------------------------------------------------ */
954         public void addCookie(Cookie JavaDoc cookie)
955         {
956             if (!_include) super.addCookie(cookie);
957         }
958     }
959
960
961     /* ------------------------------------------------------------ */
962     /* ------------------------------------------------------------ */
963     /* ------------------------------------------------------------ */
964     private class DontCloseWriter extends PrintWriter JavaDoc
965     {
966         DontCloseWriter(PrintWriter JavaDoc writer)
967         {
968             super(writer);
969         }
970
971         public void close()
972         {}
973     }
974
975     /* ------------------------------------------------------------ */
976     /* ------------------------------------------------------------ */
977     /* ------------------------------------------------------------ */
978     private class DontCloseServletOut extends ServletOut
979     {
980         DontCloseServletOut(ServletOutputStream JavaDoc output)
981         {
982             super(output);
983         }
984
985         public void close()
986             throws IOException JavaDoc
987         {}
988     }
989 };
990
Popular Tags