KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > scoping > internal > ScopedRequestDispatcher


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.scoping.internal;
19
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.ServletResponse JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
30 import org.apache.log4j.Logger;
31
32
33 /**
34  * A request dispatcher that doesn't actually forward (but keeps track of the attempted
35  * forward), and which does some extra work to do server includes into our ScopedRequest
36  * and ScopedResponse.
37  *
38  * @see ScopedRequestImpl
39  * @see ScopedResponseImpl
40  */

41 public class ScopedRequestDispatcher
42         implements RequestDispatcher JavaDoc
43 {
44     private String JavaDoc _uri;
45
46     private static final String JavaDoc REQUEST_URI_INCLUDE = "javax.servlet.include.request_uri";
47
48     private static final Logger logger = Logger.getLogger( ScopedRequestDispatcher.class );
49     
50     
51     /**
52      * Constructor.
53      *
54      * @param uri the URI to which we'll "forward" or include.
55      */

56     public ScopedRequestDispatcher( String JavaDoc uri )
57     {
58         _uri = uri;
59     }
60
61     /**
62      * Does not actually cause a server forward of the request, but informs the ScopedRequest
63      * object that a forward was attempted for a particular URI.
64      *
65      * @param request the ScopedRequest, or a wrapper (ServletRequestWrapper) around it.
66      * @param response the ScopedResponse, or a wrapper (ServletResponseWrapper) around it.
67      */

68     public void forward( ServletRequest JavaDoc request, ServletResponse JavaDoc response )
69             throws ServletException JavaDoc, IOException JavaDoc
70     {
71         ScopedRequestImpl scopedRequest = ( ScopedRequestImpl ) ScopedServletUtils.unwrapRequest( request );
72         assert scopedRequest != null : request.getClass().getName();
73         scopedRequest.setForwardedURI( _uri );
74         scopedRequest.doForward();
75     }
76
77     /**
78      * Does a server include of the stored URI into the given ScopedRequest and ScopedResponse.
79      *
80      * @param request the ScopedRequest, or a wrapper (ServletRequestWrapper) around it.
81      * @param response the ScopedResponse, or a wrapper (ServletResponseWrapper) around it.
82      */

83     public void include( ServletRequest JavaDoc request, ServletResponse JavaDoc response )
84             throws ServletException JavaDoc, IOException JavaDoc
85     {
86         assert request instanceof HttpServletRequest JavaDoc : request.getClass().getName();
87         HttpServletRequest JavaDoc httpRequest = ( HttpServletRequest JavaDoc ) request;
88         
89         //
90
// First, unwrap the request and response, looking for our ScopedRequest and ScopedResponse.
91
//
92
HttpServletRequest JavaDoc outerRequest = ScopedServletUtils.getOuterRequest( httpRequest );
93         
94         //
95
// Need to set the "javax.servlet.include.request_uri" attribute on the outer request
96
// before forwarding with a request dispatcher. This attribute is used to keep track of
97
// the included URI.
98
//
99
outerRequest.setAttribute( REQUEST_URI_INCLUDE, httpRequest.getRequestURI());
100         
101         if ( logger.isDebugEnabled() )
102         {
103             logger.debug( "Delegating to RequestDispatcher for URI " + _uri );
104         }
105         
106         try
107         {
108             RequestDispatcher JavaDoc realDispatcher = outerRequest.getRequestDispatcher( _uri );
109             
110             if ( realDispatcher == null )
111             {
112                 assert response instanceof HttpServletResponse JavaDoc : response.getClass().getName();
113                 ( ( HttpServletResponse JavaDoc ) response ).setStatus( HttpServletResponse.SC_NOT_FOUND );
114                 logger.error( "Could not get RequestDispatcher for URI " + _uri );
115             }
116             else
117             {
118                 realDispatcher.include( request, response );
119             }
120         }
121         catch ( ServletException JavaDoc e )
122         {
123             logger.error( "Exception during RequestDispatcher.include().", e.getRootCause() );
124             
125             throw e;
126         }
127     }
128 }
129
Popular Tags