KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > control > RequestManager


1 /*
2  * $Id: RequestManager.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.control;
26
27 import java.io.Serializable JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilValidate;
37
38 /**
39  * RequestManager - Manages request, config and view mappings.
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 2.0
44  */

45 public class RequestManager implements Serializable JavaDoc {
46
47     public static final String JavaDoc module = RequestManager.class.getName();
48     public static final int VIEW_HANDLER_KEY = 1;
49     public static final int EVENT_HANDLER_KEY = 0;
50
51     private URL JavaDoc configFileUrl;
52
53     public RequestManager(ServletContext JavaDoc context) {
54
55         /** Loads the site configuration from servlet context parameter. */
56         try {
57             configFileUrl = context.getResource("/WEB-INF/controller.xml");
58         } catch (Exception JavaDoc e) {
59             Debug.logError(e, "[RequestManager.constructor] Error Finding XML Config File: " +
60                 "/WEB-INF/controller.xml", module);
61         }
62         // do quick inits:
63
ConfigXMLReader.getConfigMap(configFileUrl);
64         ConfigXMLReader.getHandlerMap(configFileUrl);
65         ConfigXMLReader.getRequestMap(configFileUrl);
66         ConfigXMLReader.getViewMap(configFileUrl);
67     }
68
69     /** Gets the entire handler mapping */
70     public Map JavaDoc getHandlerMap() {
71         return (Map JavaDoc) ConfigXMLReader.getHandlerMap(configFileUrl);
72     }
73
74     /** Gets the class name of the named handler */
75     public String JavaDoc getHandlerClass(String JavaDoc name, int type) {
76         Map JavaDoc map = getHandlerMap();
77         Map JavaDoc hMap = null;
78
79         if (type == 1) {
80             hMap = (Map JavaDoc) map.get("view");
81         } else {
82             hMap = (Map JavaDoc) map.get("event");
83         }
84
85         if (!hMap.containsKey(name)) {
86             return null;
87         } else {
88             return (String JavaDoc) hMap.get(name);
89         }
90     }
91
92     public List JavaDoc getHandlerKeys(int type) {
93         Map JavaDoc map = getHandlerMap();
94         Map JavaDoc hMap = null;
95
96         if (type == 1) {
97             hMap = (Map JavaDoc) map.get("view");
98         } else {
99             hMap = (Map JavaDoc) map.get("event");
100         }
101
102         if (hMap != null) {
103             return new LinkedList JavaDoc(hMap.keySet());
104         } else {
105             return null;
106         }
107     }
108
109     public Map JavaDoc getRequestMapMap(String JavaDoc uriStr) {
110         if (UtilValidate.isNotEmpty(uriStr)) {
111             return (Map JavaDoc) ConfigXMLReader.getRequestMap(configFileUrl).get(uriStr);
112         } else {
113             return null;
114         }
115     }
116
117     public String JavaDoc getRequestAttribute(String JavaDoc uriStr, String JavaDoc attribute) {
118         Map JavaDoc uri = getRequestMapMap(uriStr);
119
120         if (uri != null && attribute != null) {
121             return (String JavaDoc) uri.get(attribute);
122         } else {
123             Debug.logInfo("[RequestManager.getRequestAttribute] Value for attribute \"" + attribute +
124                 "\" of uri \"" + uriStr + "\" not found", module);
125             return null;
126         }
127     }
128
129     /** Gets the event class from the requestMap */
130     public String JavaDoc getEventPath(String JavaDoc uriStr) {
131         Map JavaDoc uri = getRequestMapMap(uriStr);
132
133         if (uri != null)
134             return (String JavaDoc) uri.get(ConfigXMLReader.EVENT_PATH);
135         else {
136             Debug.logWarning("[RequestManager.getEventPath] Path of event for request \"" + uriStr +
137                 "\" not found", module);
138             return null;
139         }
140     }
141
142     /** Gets the event type from the requestMap */
143     public String JavaDoc getEventType(String JavaDoc uriStr) {
144         Map JavaDoc uri = getRequestMapMap(uriStr);
145
146         if (uri != null)
147             return (String JavaDoc) uri.get(ConfigXMLReader.EVENT_TYPE);
148         else {
149             Debug.logWarning("[RequestManager.getEventType] Type of event for request \"" + uriStr +
150                 "\" not found", module);
151             return null;
152         }
153     }
154
155     /** Gets the event method from the requestMap */
156     public String JavaDoc getEventMethod(String JavaDoc uriStr) {
157         Map JavaDoc uri = getRequestMapMap(uriStr);
158
159         if (uri != null) {
160             return (String JavaDoc) uri.get(ConfigXMLReader.EVENT_METHOD);
161         } else {
162             Debug.logWarning("[RequestManager.getEventMethod] Method of event for request \"" +
163                 uriStr + "\" not found", module);
164             return null;
165         }
166     }
167
168     /** Gets the event global-transaction from the requestMap */
169     public boolean getEventGlobalTransaction(String JavaDoc uriStr) {
170         Map JavaDoc uri = getRequestMapMap(uriStr);
171
172         if (uri != null) {
173             return new Boolean JavaDoc((String JavaDoc) uri.get(ConfigXMLReader.EVENT_GLOBAL_TRANSACTION)).booleanValue();
174         } else {
175             if (Debug.verboseOn()) {
176                 Debug.logWarning("[RequestManager.getEventGlobalTransaction] Global-transaction of event for request \"" +
177                     uriStr + "\" not found, defaulting to true", module);
178             }
179             return false;
180         }
181     }
182
183     /** Gets the view name from the requestMap */
184     public String JavaDoc getViewName(String JavaDoc uriStr) {
185         Map JavaDoc uri = getRequestMapMap(uriStr);
186
187         if (uri != null)
188             return (String JavaDoc) uri.get(ConfigXMLReader.NEXT_PAGE);
189         else {
190             Debug.logWarning("[RequestManager.getViewName] View name for uri \"" + uriStr + "\" not found", module);
191             return null;
192         }
193     }
194
195     /** Gets the next page (jsp) from the viewMap */
196     public String JavaDoc getViewPage(String JavaDoc viewStr) {
197         if (viewStr != null && viewStr.startsWith("view:")) viewStr = viewStr.substring(viewStr.indexOf(':') + 1);
198         Map JavaDoc page = (Map JavaDoc) ConfigXMLReader.getViewMap(configFileUrl).get(viewStr);
199
200         if (page != null) {
201             return (String JavaDoc) page.get(ConfigXMLReader.VIEW_PAGE);
202         } else {
203             Debug.logWarning("[RequestManager.getViewPage] View with name \"" + viewStr + "\" not found", module);
204             return null;
205         }
206     }
207
208     /** Gets the type of this view */
209     public String JavaDoc getViewType(String JavaDoc viewStr) {
210         Map JavaDoc view = (Map JavaDoc) ConfigXMLReader.getViewMap(configFileUrl).get(viewStr);
211
212         if (view != null) {
213             return (String JavaDoc) view.get(ConfigXMLReader.VIEW_TYPE);
214         } else {
215             Debug.logWarning("[RequestManager.getViewType] View with name \"" + viewStr + "\" not found", module);
216             return null;
217         }
218     }
219
220     /** Gets the info of this view */
221     public String JavaDoc getViewInfo(String JavaDoc viewStr) {
222         Map JavaDoc view = (Map JavaDoc) ConfigXMLReader.getViewMap(configFileUrl).get(viewStr);
223
224         if (view != null) {
225             return (String JavaDoc) view.get(ConfigXMLReader.VIEW_INFO);
226         } else {
227             Debug.logWarning("[RequestManager.getViewInfo] View with name \"" + viewStr + "\" not found", module);
228             return null;
229         }
230     }
231     
232     /** Gets the content-type of this view */
233     public String JavaDoc getViewContentType(String JavaDoc viewStr) {
234         Map JavaDoc view = (Map JavaDoc) ConfigXMLReader.getViewMap(configFileUrl).get(viewStr);
235
236         if (view != null) {
237             return (String JavaDoc) view.get(ConfigXMLReader.VIEW_CONTENT_TYPE);
238         } else {
239             Debug.logWarning("[RequestManager.getViewInfo] View with name \"" + viewStr + "\" not found", module);
240             return null;
241         }
242     }
243     
244     /** Gets the content-type of this view */
245     public String JavaDoc getViewEncoding(String JavaDoc viewStr) {
246         Map JavaDoc view = (Map JavaDoc) ConfigXMLReader.getViewMap(configFileUrl).get(viewStr);
247
248         if (view != null) {
249             return (String JavaDoc) view.get(ConfigXMLReader.VIEW_ENCODING);
250         } else {
251             Debug.logWarning("[RequestManager.getViewInfo] View with name \"" + viewStr + "\" not found", module);
252             return null;
253         }
254     }
255
256     /** Gets the error page from the requestMap, if none uses the default */
257     public String JavaDoc getErrorPage(String JavaDoc uriStr) {
258         //Debug.logInfo("uriStr is: " + uriStr, module);
259
Map JavaDoc uri = getRequestMapMap(uriStr);
260         //Debug.logInfo("RequestMapMap is: " + uri, module);
261

262         if (uri != null) {
263             String JavaDoc errorViewUri = (String JavaDoc) uri.get(ConfigXMLReader.ERROR_PAGE);
264             //Debug.logInfo("errorViewUri is: " + errorViewUri, module);
265
String JavaDoc returnPage = getViewPage(errorViewUri);
266             //Debug.logInfo("Got returnPage for ErrorPage: " + returnPage, module);
267

268             if (returnPage != null) {
269                 return returnPage;
270             } else {
271                 return getDefaultErrorPage();
272             }
273         } else {
274             return getDefaultErrorPage();
275         }
276     }
277
278     /** Gets the default error page from the configMap or static site default */
279     public String JavaDoc getDefaultErrorPage() {
280         String JavaDoc errorPage = null;
281         errorPage = (String JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get(ConfigXMLReader.DEFAULT_ERROR_PAGE);
282         //Debug.logInfo("For DefaultErrorPage got errorPage: " + errorPage, module);
283
if (errorPage != null) return errorPage;
284         return "/error/error.jsp";
285     }
286
287     public boolean requiresAuth(String JavaDoc uriStr) {
288         Map JavaDoc uri = getRequestMapMap(uriStr);
289
290         if (uri != null) {
291             String JavaDoc value = (String JavaDoc) uri.get(ConfigXMLReader.SECURITY_AUTH);
292
293             //if (Debug.verboseOn()) Debug.logVerbose("Require Auth: " + value, module);
294
if ("true".equalsIgnoreCase(value))
295                 return true;
296             else
297                 return false;
298         } else
299             return false;
300     }
301
302     public boolean requiresHttps(String JavaDoc uriStr) {
303         Map JavaDoc uri = getRequestMapMap(uriStr);
304
305         if (uri != null) {
306             String JavaDoc value = (String JavaDoc) uri.get(ConfigXMLReader.SECURITY_HTTPS);
307
308             //if (Debug.verboseOn()) Debug.logVerbose("Requires HTTPS: " + value, module);
309
if ("true".equalsIgnoreCase(value))
310                 return true;
311             else
312                 return false;
313         } else
314             return false;
315     }
316
317     public boolean allowExtView(String JavaDoc uriStr) {
318         Map JavaDoc uri = getRequestMapMap(uriStr);
319
320         if (uri != null) {
321             String JavaDoc value = (String JavaDoc) uri.get(ConfigXMLReader.SECURITY_EXTVIEW);
322
323             //if (Debug.verboseOn()) Debug.logVerbose("Allow External View: " + value, module);
324
if ("false".equalsIgnoreCase(value))
325                 return false;
326             else
327                 return true;
328         } else
329             return true;
330     }
331
332     public boolean allowDirectRequest(String JavaDoc uriStr) {
333         Map JavaDoc uri = getRequestMapMap(uriStr);
334
335         if (uri != null) {
336             String JavaDoc value = (String JavaDoc) uri.get(ConfigXMLReader.SECURITY_DIRECT);
337
338             //if (Debug.verboseOn()) Debug.logVerbose("Allow Direct Request: " + value, module);
339
if ("false".equalsIgnoreCase(value))
340                 return false;
341             else
342                 return true;
343         } else
344             return false;
345     }
346
347     public Collection JavaDoc getFirstVisitEvents() {
348         Collection JavaDoc c = (Collection JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get(ConfigXMLReader.FIRSTVISIT);
349         return c;
350     }
351
352     public Collection JavaDoc getPreProcessor() {
353         Collection JavaDoc c = (Collection JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get(ConfigXMLReader.PREPROCESSOR);
354         return c;
355     }
356
357     public Collection JavaDoc getPostProcessor() {
358         Collection JavaDoc c = (Collection JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get(ConfigXMLReader.POSTPROCESSOR);
359         return c;
360     }
361
362     public List JavaDoc getAfterLoginEventList() {
363         List JavaDoc lst = (List JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get("after-login");
364         return lst;
365     }
366
367     public List JavaDoc getBeforeLogoutEventList() {
368         List JavaDoc lst = (List JavaDoc) ConfigXMLReader.getConfigMap(configFileUrl).get("before-logout");
369         return lst;
370     }
371 }
372
Popular Tags