KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > RequestDispatcherWrapper


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41
42 package org.jahia.services.applications;
43
44 import java.util.*;
45
46 import org.jahia.data.applications.ServletBean;
47
48 import javax.servlet.RequestDispatcher JavaDoc;
49 import javax.servlet.ServletException JavaDoc;
50 import javax.servlet.ServletRequest JavaDoc;
51 import javax.servlet.ServletResponse JavaDoc;
52
53 /**
54  * This wrapper is used to detect dispatching operations by the applications.
55  *
56  * @author Khue Nguyen <a HREF="mailto:khue@jahia.com">khue@jahia.com</a>
57  * @version 1.0
58  */

59 public class RequestDispatcherWrapper implements RequestDispatcher JavaDoc {
60
61     private static org.apache.log4j.Logger logger =
62             org.apache.log4j.Logger.getLogger (RequestDispatcherWrapper.class);
63
64     // @todo : Have a pool of dispatcher processors loaded from config file
65
private StrutsRequestDispatcherProcessor strutsRDP;
66
67     private RequestDispatcher JavaDoc requestDispatcher;
68
69     private boolean dispatcherObtainedByGetNamedDispatcher = false;
70
71     public static final String JavaDoc INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri" ;
72     public static final String JavaDoc INCLUDE_CONTEXT_PATH = "javax.servlet.include.context_path";
73     public static final String JavaDoc INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
74     public static final String JavaDoc INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
75     public static final String JavaDoc INCLUDE_QUERY_STRING = "javax.servlet.include.query_string";
76
77     public static final String JavaDoc FORWARD_REQUEST_URI = "javax.servlet.forward.request_uri" ;
78     public static final String JavaDoc FORWARD_CONTEXT_PATH = "javax.servlet.forward.context_path";
79     public static final String JavaDoc FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path";
80     public static final String JavaDoc FORWARD_PATH_INFO = "javax.servlet.forward.path_info";
81     public static final String JavaDoc FORWARD_QUERY_STRING = "javax.servlet.forward.query_string";
82
83     public static String JavaDoc[] SERVLET_DISPACHER_REQUEST_ATTRIBUTES =
84         {INCLUDE_REQUEST_URI,INCLUDE_CONTEXT_PATH,INCLUDE_SERVLET_PATH,
85         INCLUDE_PATH_INFO, INCLUDE_QUERY_STRING, FORWARD_REQUEST_URI,
86         FORWARD_CONTEXT_PATH, FORWARD_SERVLET_PATH, FORWARD_PATH_INFO,
87         FORWARD_QUERY_STRING};
88
89     public static final String JavaDoc EMPTY_ATTRIBUTE = "RequestDispatcherWrapper.EMPTY_ATTRIBUTE";
90
91     //--------------------------------------------------------------------------
92
/**
93      * @param requestDispatcher the request dispatcher
94      */

95     public RequestDispatcherWrapper (RequestDispatcher JavaDoc requestDispatcher,
96                                      boolean dispatcherObtainedByGetNamedDispatcher) {
97
98         logger.debug ("Creating a new wrapper.");
99
100         this.requestDispatcher = requestDispatcher;
101         this.dispatcherObtainedByGetNamedDispatcher =
102             dispatcherObtainedByGetNamedDispatcher;
103         this.strutsRDP = new StrutsRequestDispatcherProcessor ();
104     }
105
106     //--------------------------------------------------------------------------
107
public void forward (ServletRequest JavaDoc request, ServletResponse JavaDoc response)
108             throws ServletException JavaDoc, java.io.IOException JavaDoc {
109
110         this.strutsRDP.preForward ((ServletIncludeRequestWrapper) request,
111                 (ServletIncludeResponseWrapper) response);
112
113         String JavaDoc requestURI = ((ServletIncludeRequestWrapper) request).getRequestURI ();
114         logger.debug("requestWrapper.getRequestURI() returned : " + requestURI);
115         String JavaDoc contextPath = ((ServletIncludeRequestWrapper) request).getContextPath();
116         logger.debug("requestWrapper.getContextPath() returned : " + contextPath);
117         String JavaDoc servletPath = ((ServletIncludeRequestWrapper) request).getServletPath();
118         logger.debug("requestWrapper.getServletPath() returned : " + servletPath);
119         String JavaDoc pathInfo = ((ServletIncludeRequestWrapper) request).getPathInfo();
120         logger.debug("requestWrapper.getPathInfo() returned : " + pathInfo);
121         String JavaDoc queryString = ((ServletIncludeRequestWrapper) request).getQueryString();
122         logger.debug("requestWrapper.getQueryString() returned : " + queryString);
123
124         //logger.debug ("Request URI : " + requestURI);
125
PersistantServletRequest appRequest = ((ServletIncludeRequestWrapper) request).getPersistantServletRequest ();
126         if (appRequest != null) {
127             if (appRequest.getWebAppType () == ServletBean.JSP_TYPE
128                     && requestURI != null
129                     && (requestURI.toLowerCase ().indexOf (".jsp") == -1)) {
130                 // do an include with static resource !
131
if ( !this.dispatcherObtainedByGetNamedDispatcher ){
132                     request.setAttribute(RequestDispatcherWrapper.INCLUDE_REQUEST_URI,requestURI);
133                     request.setAttribute(RequestDispatcherWrapper.INCLUDE_CONTEXT_PATH,contextPath);
134                     request.setAttribute(RequestDispatcherWrapper.INCLUDE_SERVLET_PATH,servletPath);
135                     request.setAttribute(RequestDispatcherWrapper.INCLUDE_PATH_INFO,pathInfo);
136                     request.setAttribute(RequestDispatcherWrapper.INCLUDE_QUERY_STRING,queryString);
137                 } else {
138                     /*
139                     request.removeAttribute(this.INCLUDE_REQUEST_URI);
140                     request.removeAttribute(this.INCLUDE_CONTEXT_PATH);
141                     request.removeAttribute(this.INCLUDE_SERVLET_PATH);
142                     request.removeAttribute(this.INCLUDE_PATH_INFO);
143                     request.removeAttribute(this.INCLUDE_QUERY_STRING);*/

144                 }
145
146                 this.include (request, response);
147
148                 this.strutsRDP.postForward ((ServletIncludeRequestWrapper) request,
149                         (ServletIncludeResponseWrapper) response);
150                 return;
151             }
152         }
153         if ( !this.dispatcherObtainedByGetNamedDispatcher ){
154             request.setAttribute(RequestDispatcherWrapper.FORWARD_REQUEST_URI,requestURI);
155             request.setAttribute(RequestDispatcherWrapper.FORWARD_CONTEXT_PATH,contextPath);
156             request.setAttribute(RequestDispatcherWrapper.FORWARD_SERVLET_PATH,servletPath);
157             request.setAttribute(RequestDispatcherWrapper.FORWARD_PATH_INFO,pathInfo);
158             request.setAttribute(RequestDispatcherWrapper.FORWARD_QUERY_STRING,queryString);
159         } else {
160             /*
161             request.removeAttribute(this.FORWARD_REQUEST_URI);
162             request.removeAttribute(this.FORWARD_CONTEXT_PATH);
163             request.removeAttribute(this.FORWARD_SERVLET_PATH);
164             request.removeAttribute(this.FORWARD_PATH_INFO);
165             request.removeAttribute(this.FORWARD_QUERY_STRING);*/

166         }
167
168         this.requestDispatcher.forward (request, response);
169
170         this.strutsRDP.postForward ((ServletIncludeRequestWrapper) request,
171                 (ServletIncludeResponseWrapper) response);
172
173 /*
174 // FIXME HOLLIS
175 // STRUTS APPLICATION ISSUE :
176 // One of the reasons we encounter problems with forwarding to Struts portlets
177 // is that there is a ModuleConfig in the request and it seems to create
178 // some trouble here.
179 // So I temporarily remove it from the request, then restore it once the
180 // application forward is done.
181
182 // 1) remove from request
183         Object moduleConfig = request.getAttribute (org.apache.struts.Globals.MODULE_KEY);
184         request.removeAttribute (org.apache.struts.Globals.MODULE_KEY);
185
186         Object messageKey = request.getAttribute(org.apache.struts.Globals.MESSAGE_KEY);
187         request.removeAttribute (org.apache.struts.Globals.MESSAGES_KEY);
188
189         this.requestDispatcher.forward (request, response);
190
191 // restore
192         request.setAttribute (org.apache.struts.Globals.MODULE_KEY, moduleConfig);
193         request.setAttribute (org.apache.struts.Globals.MESSAGES_KEY, messageKey);
194 */

195
196     }
197
198     //--------------------------------------------------------------------------
199
public void include (ServletRequest JavaDoc request, ServletResponse JavaDoc response)
200             throws ServletException JavaDoc, java.io.IOException JavaDoc {
201 //String requestURI = ((ServletIncludeRequestWrapper)request).getRequestURI();
202
//logger.debug("Request URI : " + ((ServletIncludeRequestWrapper)request).getRequestURI() );
203

204         this.strutsRDP.preInclude ((ServletIncludeRequestWrapper) request,
205                 (ServletIncludeResponseWrapper) response);
206
207         String JavaDoc requestURI = ((ServletIncludeRequestWrapper) request).getRequestURI ();
208         logger.debug("requestWrapper.getRequestURI() returned : " + requestURI);
209         String JavaDoc contextPath = ((ServletIncludeRequestWrapper) request).getContextPath();
210         logger.debug("requestWrapper.getContextPath() returned : " + contextPath);
211         String JavaDoc servletPath = ((ServletIncludeRequestWrapper) request).getServletPath();
212         logger.debug("requestWrapper.getServletPath() returned : " + servletPath);
213         String JavaDoc pathInfo = ((ServletIncludeRequestWrapper) request).getPathInfo();
214         logger.debug("requestWrapper.getPathInfo() returned : " + pathInfo);
215         String JavaDoc queryString = ((ServletIncludeRequestWrapper) request).getQueryString();
216         logger.debug("requestWrapper.getQueryString() returned : " + queryString);
217
218         if ( !this.dispatcherObtainedByGetNamedDispatcher ){
219             request.setAttribute(RequestDispatcherWrapper.INCLUDE_REQUEST_URI,requestURI);
220             request.setAttribute(RequestDispatcherWrapper.INCLUDE_CONTEXT_PATH,contextPath);
221             request.setAttribute(RequestDispatcherWrapper.INCLUDE_SERVLET_PATH,servletPath);
222             request.setAttribute(RequestDispatcherWrapper.INCLUDE_PATH_INFO,pathInfo);
223             request.setAttribute(RequestDispatcherWrapper.INCLUDE_QUERY_STRING,queryString);
224         } else {
225             /*
226             request.removeAttribute(this.INCLUDE_REQUEST_URI);
227             request.removeAttribute(this.INCLUDE_CONTEXT_PATH);
228             request.removeAttribute(this.INCLUDE_SERVLET_PATH);
229             request.removeAttribute(this.INCLUDE_PATH_INFO);
230             request.removeAttribute(this.INCLUDE_QUERY_STRING);*/

231         }
232
233         this.requestDispatcher.include (request, response);
234
235         this.strutsRDP.postInclude ((ServletIncludeRequestWrapper) request,
236                 (ServletIncludeResponseWrapper) response);
237
238     }
239
240
241     public static HashMap backupServletDispatcherRequestAttributes(ServletRequest JavaDoc request){
242
243       HashMap attributes = new HashMap();
244
245       for ( int i=0; i<SERVLET_DISPACHER_REQUEST_ATTRIBUTES.length; i++ ){
246         String JavaDoc val = (String JavaDoc)request.getAttribute(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i]);
247         if ( val == null ){
248           val = EMPTY_ATTRIBUTE;
249         }
250         attributes.put(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i],val);
251         request.removeAttribute(SERVLET_DISPACHER_REQUEST_ATTRIBUTES[i]);
252       }
253       return attributes;
254     }
255
256     public static void restoreServletDispatcherRequestAttributes(ServletRequest JavaDoc request,
257         HashMap attributes){
258
259       Iterator iterator = attributes.keySet().iterator();
260       while ( iterator.hasNext() ){
261         String JavaDoc key = (String JavaDoc)iterator.next();
262         String JavaDoc val = (String JavaDoc)attributes.get(key);
263         if ( EMPTY_ATTRIBUTE.equals(val) ){
264           request.removeAttribute(key);
265         } else {
266           request.setAttribute(key, val);
267         }
268       }
269     }
270
271 }
272
Popular Tags