001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.webapp.control;
019:
020: import java.io.Serializable;
021: import java.net.URL;
022: import java.net.MalformedURLException;
023: import java.util.Collection;
024: import java.util.Map;
025: import java.util.List;
026: import java.util.LinkedList;
027: import javax.servlet.ServletContext;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.base.util.UtilValidate;
031:
032: /**
033: * RequestManager - Manages request, config and view mappings.
034: */
035: public class RequestManager implements Serializable {
036:
037: public static final String module = RequestManager.class.getName();
038: public static final int VIEW_HANDLER_KEY = 1;
039: public static final int EVENT_HANDLER_KEY = 0;
040:
041: private URL configFileUrl;
042: private URL webInfUrl;
043:
044: public RequestManager(ServletContext context) {
045:
046: /** Loads the site configuration from servlet context parameter. */
047: try {
048: configFileUrl = context
049: .getResource("/WEB-INF/controller.xml");
050: webInfUrl = context.getResource("/WEB-INF");
051: } catch (Exception e) {
052: Debug.logError(e,
053: "[RequestManager.constructor] Error Finding XML Config File: "
054: + "/WEB-INF/controller.xml", module);
055: }
056: // do quick inits:
057: ConfigXMLReader.getConfigMap(configFileUrl);
058: ConfigXMLReader.getHandlerMap(configFileUrl);
059: ConfigXMLReader.getRequestMap(configFileUrl);
060: ConfigXMLReader.getViewMap(configFileUrl);
061: }
062:
063: /** Gets the entire handler mapping */
064: public Map getHandlerMap() {
065: return ConfigXMLReader.getHandlerMap(configFileUrl);
066: }
067:
068: /** Gets the class name of the named handler */
069: public String getHandlerClass(String name, int type) {
070: Map map = getHandlerMap();
071: Map hMap;
072:
073: if (type == 1) {
074: hMap = (Map) map.get("view");
075: } else {
076: hMap = (Map) map.get("event");
077: }
078:
079: if (!hMap.containsKey(name)) {
080: return null;
081: } else {
082: return (String) hMap.get(name);
083: }
084: }
085:
086: public List getHandlerKeys(int type) {
087: Map map = getHandlerMap();
088: Map hMap;
089:
090: if (type == 1) {
091: hMap = (Map) map.get("view");
092: } else {
093: hMap = (Map) map.get("event");
094: }
095:
096: if (hMap != null) {
097: return new LinkedList(hMap.keySet());
098: } else {
099: return null;
100: }
101: }
102:
103: public Map getRequestMapMap(String uriStr) {
104: if (UtilValidate.isNotEmpty(uriStr)) {
105: return (Map) ConfigXMLReader.getRequestMap(configFileUrl)
106: .get(uriStr);
107: } else {
108: return null;
109: }
110: }
111:
112: public String getRequestAttribute(String uriStr, String attribute) {
113: Map uri = getRequestMapMap(uriStr);
114:
115: if (uri != null && attribute != null) {
116: return (String) uri.get(attribute);
117: } else {
118: Debug.logInfo(
119: "[RequestManager.getRequestAttribute] Value for attribute \""
120: + attribute + "\" of uri \"" + uriStr
121: + "\" not found", module);
122: return null;
123: }
124: }
125:
126: /** Gets the event class from the requestMap */
127: public String getEventPath(String uriStr) {
128: Map uri = getRequestMapMap(uriStr);
129:
130: if (uri != null)
131: return (String) uri.get(ConfigXMLReader.EVENT_PATH);
132: else {
133: Debug.logWarning(
134: "[RequestManager.getEventPath] Path of event for request \""
135: + uriStr + "\" not found", module);
136: return null;
137: }
138: }
139:
140: /** Gets the event type from the requestMap */
141: public String getEventType(String uriStr) {
142: Map uri = getRequestMapMap(uriStr);
143:
144: if (uri != null)
145: return (String) uri.get(ConfigXMLReader.EVENT_TYPE);
146: else {
147: Debug.logWarning(
148: "[RequestManager.getEventType] Type of event for request \""
149: + uriStr + "\" not found", module);
150: return null;
151: }
152: }
153:
154: /** Gets the event method from the requestMap */
155: public String getEventMethod(String uriStr) {
156: Map uri = getRequestMapMap(uriStr);
157:
158: if (uri != null) {
159: return (String) uri.get(ConfigXMLReader.EVENT_METHOD);
160: } else {
161: Debug.logWarning(
162: "[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 uriStr) {
170: Map uri = getRequestMapMap(uriStr);
171:
172: if (uri != null) {
173: return Boolean
174: .valueOf(
175: (String) uri
176: .get(ConfigXMLReader.EVENT_GLOBAL_TRANSACTION))
177: .booleanValue();
178: } else {
179: if (Debug.verboseOn()) {
180: Debug
181: .logWarning(
182: "[RequestManager.getEventGlobalTransaction] Global-transaction of event for request \""
183: + uriStr
184: + "\" not found, defaulting to true",
185: module);
186: }
187: return false;
188: }
189: }
190:
191: /** Gets the view name from the requestMap */
192: public String getViewName(String uriStr) {
193: Map uri = getRequestMapMap(uriStr);
194:
195: if (uri != null)
196: return (String) uri.get(ConfigXMLReader.NEXT_PAGE);
197: else {
198: Debug.logWarning(
199: "[RequestManager.getViewName] View name for uri \""
200: + uriStr + "\" not found", module);
201: return null;
202: }
203: }
204:
205: /** Gets the next page (jsp) from the viewMap */
206: public String getViewPage(String viewStr) {
207: if (viewStr != null && viewStr.startsWith("view:"))
208: viewStr = viewStr.substring(viewStr.indexOf(':') + 1);
209: Map page = (Map) ConfigXMLReader.getViewMap(configFileUrl).get(
210: viewStr);
211:
212: if (page != null) {
213: return (String) page.get(ConfigXMLReader.VIEW_PAGE);
214: } else {
215: Debug.logWarning(
216: "[RequestManager.getViewPage] View with name \""
217: + viewStr + "\" not found", module);
218: return null;
219: }
220: }
221:
222: /** Gets the type of this view */
223: public String getViewType(String viewStr) {
224: Map view = (Map) ConfigXMLReader.getViewMap(configFileUrl).get(
225: viewStr);
226:
227: if (view != null) {
228: return (String) view.get(ConfigXMLReader.VIEW_TYPE);
229: } else {
230: Debug.logWarning(
231: "[RequestManager.getViewType] View with name \""
232: + viewStr + "\" not found", module);
233: return null;
234: }
235: }
236:
237: /** Gets the info of this view */
238: public String getViewInfo(String viewStr) {
239: Map view = (Map) ConfigXMLReader.getViewMap(configFileUrl).get(
240: viewStr);
241:
242: if (view != null) {
243: return (String) view.get(ConfigXMLReader.VIEW_INFO);
244: } else {
245: Debug.logWarning(
246: "[RequestManager.getViewInfo] View with name \""
247: + viewStr + "\" not found", module);
248: return null;
249: }
250: }
251:
252: /** Gets the content-type of this view */
253: public String getViewContentType(String viewStr) {
254: Map view = (Map) ConfigXMLReader.getViewMap(configFileUrl).get(
255: viewStr);
256:
257: if (view != null) {
258: return (String) view.get(ConfigXMLReader.VIEW_CONTENT_TYPE);
259: } else {
260: Debug.logWarning(
261: "[RequestManager.getViewInfo] View with name \""
262: + viewStr + "\" not found", module);
263: return null;
264: }
265: }
266:
267: /** Gets the content-type of this view */
268: public String getViewEncoding(String viewStr) {
269: Map view = (Map) ConfigXMLReader.getViewMap(configFileUrl).get(
270: viewStr);
271:
272: if (view != null) {
273: return (String) view.get(ConfigXMLReader.VIEW_ENCODING);
274: } else {
275: Debug.logWarning(
276: "[RequestManager.getViewInfo] View with name \""
277: + viewStr + "\" not found", module);
278: return null;
279: }
280: }
281:
282: /** Gets the error page from the requestMap, if none uses the default */
283: public String getErrorPage(String uriStr) {
284: //Debug.logInfo("uriStr is: " + uriStr, module);
285: Map uri = getRequestMapMap(uriStr);
286: //Debug.logInfo("RequestMapMap is: " + uri, module);
287:
288: if (uri != null) {
289: String errorViewUri = (String) uri
290: .get(ConfigXMLReader.ERROR_PAGE);
291: //Debug.logInfo("errorViewUri is: " + errorViewUri, module);
292: String returnPage = getViewPage(errorViewUri);
293: //Debug.logInfo("Got returnPage for ErrorPage: " + returnPage, module);
294:
295: if (returnPage != null) {
296: return returnPage;
297: } else {
298: return getDefaultErrorPage();
299: }
300: } else {
301: return getDefaultErrorPage();
302: }
303: }
304:
305: /** Gets the default error page from the configMap or static site default */
306: public String getDefaultErrorPage() {
307: String errorPage;
308: errorPage = (String) ConfigXMLReader
309: .getConfigMap(configFileUrl).get(
310: ConfigXMLReader.DEFAULT_ERROR_PAGE);
311: //Debug.logInfo("For DefaultErrorPage got errorPage: " + errorPage, module);
312: if (errorPage != null)
313: return errorPage;
314: return "/error/error.jsp";
315: }
316:
317: public boolean requiresAuth(String uriStr) {
318: Map uri = getRequestMapMap(uriStr);
319:
320: if (uri != null) {
321: String value = (String) uri
322: .get(ConfigXMLReader.SECURITY_AUTH);
323:
324: //if (Debug.verboseOn()) Debug.logVerbose("Require Auth: " + value, module);
325: return "true".equalsIgnoreCase(value);
326: } else
327: return false;
328: }
329:
330: public boolean requiresHttps(String uriStr) {
331: Map uri = getRequestMapMap(uriStr);
332:
333: if (uri != null) {
334: String value = (String) uri
335: .get(ConfigXMLReader.SECURITY_HTTPS);
336:
337: //if (Debug.verboseOn()) Debug.logVerbose("Requires HTTPS: " + value, module);
338: return "true".equalsIgnoreCase(value);
339: } else
340: return false;
341: }
342:
343: public boolean requiresHttpsClientCert(String uriStr) {
344: Map uri = getRequestMapMap(uriStr);
345:
346: if (uri != null) {
347: String value = (String) uri
348: .get(ConfigXMLReader.SECURITY_CERT);
349:
350: //if (Debug.verboseOn()) Debug.logVerbose("Requires x.509 Cert: " + value, module);
351: return "true".equalsIgnoreCase(value);
352: } else
353: return false;
354:
355: }
356:
357: public boolean allowExtView(String uriStr) {
358: Map uri = getRequestMapMap(uriStr);
359:
360: if (uri != null) {
361: String value = (String) uri
362: .get(ConfigXMLReader.SECURITY_EXTVIEW);
363:
364: //if (Debug.verboseOn()) Debug.logVerbose("Allow External View: " + value, module);
365: return !"false".equalsIgnoreCase(value);
366: } else
367: return true;
368: }
369:
370: public boolean allowDirectRequest(String uriStr) {
371: Map uri = getRequestMapMap(uriStr);
372:
373: if (uri != null) {
374: String value = (String) uri
375: .get(ConfigXMLReader.SECURITY_DIRECT);
376:
377: //if (Debug.verboseOn()) Debug.logVerbose("Allow Direct Request: " + value, module);
378: return !"false".equalsIgnoreCase(value);
379: } else
380: return false;
381: }
382:
383: public String getDefaultRequest() {
384: return ConfigXMLReader.getDefaultRequest(configFileUrl);
385: }
386:
387: public Collection getFirstVisitEvents() {
388: return (Collection) ConfigXMLReader.getConfigMap(configFileUrl)
389: .get(ConfigXMLReader.FIRSTVISIT);
390: }
391:
392: public Collection getPreProcessor() {
393: return (Collection) ConfigXMLReader.getConfigMap(configFileUrl)
394: .get(ConfigXMLReader.PREPROCESSOR);
395: }
396:
397: public Collection getPostProcessor() {
398: return (Collection) ConfigXMLReader.getConfigMap(configFileUrl)
399: .get(ConfigXMLReader.POSTPROCESSOR);
400: }
401:
402: public List getAfterLoginEventList() {
403: return (List) ConfigXMLReader.getConfigMap(configFileUrl).get(
404: "after-login");
405: }
406:
407: public List getBeforeLogoutEventList() {
408: return (List) ConfigXMLReader.getConfigMap(configFileUrl).get(
409: "before-logout");
410: }
411: }
|