001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jWebApp.
006: *
007: * jWebApp is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jWebApp is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jWebApp; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jwebapp.controller;
020:
021: import java.lang.reflect.Method;
022: import java.util.HashSet;
023: import java.util.Hashtable;
024: import java.util.Vector;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: @SuppressWarnings("unchecked")
029: // working to complete a Java 1.5 version
030: class DataManager {
031: protected Hashtable validationHash = new Hashtable();
032: protected Vector validationVector = new Vector();
033: protected Hashtable urlDataHash = new Hashtable();
034: protected Hashtable messageHash = new Hashtable();
035: protected Hashtable requestHash = new Hashtable();
036: protected Vector requestRegularExpressionVector = new Vector();
037: protected Hashtable requestHandlers = new Hashtable();
038:
039: boolean reload() throws Exception {
040: requestHandlers = new Hashtable();
041:
042: return loadData();
043: }
044:
045: boolean loadData() throws Exception {
046: return false;
047: }
048:
049: Hashtable getRequestHash() {
050: return requestHash;
051: }
052:
053: RequestData getRequestData(String classPrefix, String requestUri)
054: throws InstantiationException, IllegalAccessException,
055: ClassNotFoundException, NoSuchMethodException {
056: RequestData requestData = null;
057:
058: if (requestUri.endsWith("/"))
059: requestUri = requestUri.substring(0, requestUri
060: .lastIndexOf("/"));
061:
062: String tmpUri = requestUri;
063:
064: if ((requestData = (RequestData) requestHash.get(requestUri)) == null) {
065: if (tmpUri.startsWith("/"))
066: tmpUri = tmpUri.substring(1);
067:
068: // search regular expressions
069: if (requestRegularExpressionVector.size() > 0) {
070: for (int i = 0; i < requestRegularExpressionVector
071: .size()
072: && requestData == null; i++) {
073: RequestData r = (RequestData) requestRegularExpressionVector
074: .elementAt(i);
075:
076: if (r.getMatchRegExp() != null
077: && requestUri.matches(r.getMatchRegExp()))
078: requestData = (RequestData) r;
079: }
080: }
081:
082: if (requestData == null) {
083: String extension = "";
084:
085: // Strip extension
086: if (tmpUri.lastIndexOf('.') > -1) {
087: extension = tmpUri.substring(tmpUri
088: .lastIndexOf('.'));
089: tmpUri = tmpUri.substring(0, tmpUri
090: .lastIndexOf('.'));
091: }
092:
093: while (tmpUri != null && requestData == null) {
094: if ((requestData = (RequestData) requestHash
095: .get(tmpUri + extension)) == null) {
096: String handlerClass = classPrefix + "."
097: + tmpUri.replace('/', '.'), classPart = null, methodPart = null;
098:
099: try {
100: // check for class
101: if (handlerClass.lastIndexOf('.') != -1) {
102: classPart = handlerClass
103: .substring(handlerClass
104: .lastIndexOf('.') + 1);
105: classPart = Character
106: .toUpperCase(classPart
107: .charAt(0))
108: + classPart.substring(1);
109:
110: handlerClass = handlerClass.substring(
111: 0, handlerClass
112: .lastIndexOf('.'))
113: + "." + classPart;
114: } else
115: handlerClass = Character
116: .toUpperCase(handlerClass
117: .charAt(0))
118: + handlerClass.substring(1);
119:
120: requestData = new RequestData(tmpUri, null,
121: null, handlerClass, null, false,
122: false);
123: } catch (Exception e) // don't care about exception information
124: {
125: // check for class and method
126: if (handlerClass.lastIndexOf('.') != -1) {
127: methodPart = handlerClass
128: .substring(handlerClass
129: .lastIndexOf('.') + 1);
130: methodPart = Character
131: .toUpperCase(methodPart
132: .charAt(0))
133: + methodPart.substring(1);
134:
135: handlerClass = handlerClass.substring(
136: 0, handlerClass
137: .lastIndexOf('.'));
138:
139: if (handlerClass.lastIndexOf('.') != -1) {
140: classPart = handlerClass
141: .substring(handlerClass
142: .lastIndexOf('.') + 1);
143: classPart = Character
144: .toUpperCase(classPart
145: .charAt(0))
146: + classPart.substring(1);
147:
148: handlerClass = handlerClass
149: .substring(0, handlerClass
150: .lastIndexOf('.'))
151: + "." + classPart;
152: } else
153: handlerClass = Character
154: .toUpperCase(handlerClass
155: .charAt(0))
156: + handlerClass.substring(1);
157:
158: try {
159: requestData = new RequestData(
160: tmpUri, null, null,
161: handlerClass, methodPart,
162: false, false);
163: } catch (Exception e2) {
164: } // don't care about exception information
165: }
166: }
167:
168: if (requestData == null) {
169: if (tmpUri.indexOf('/') != -1)
170: tmpUri = tmpUri.substring(tmpUri
171: .indexOf('/') + 1);
172: else if (tmpUri.equals("Default"))
173: tmpUri = null;
174: else
175: tmpUri = "Default";
176: }
177: }
178: }
179: }
180:
181: if (requestData != null)
182: requestHash.put(requestUri, requestData);
183: }
184:
185: return requestData;
186: }
187:
188: Hashtable getValidationHash() {
189: return validationHash;
190: }
191:
192: Vector getValidationVector() {
193: return validationVector;
194: }
195:
196: ValidationData getValidation(String key) {
197: return (ValidationData) validationHash.get(key);
198: }
199:
200: Hashtable getMessageHash() {
201: return messageHash;
202: }
203:
204: String getMessage(String key) {
205: return (String) messageHash.get(key);
206: }
207:
208: Hashtable getUrlDataHash() {
209: return urlDataHash;
210: }
211:
212: UrlData getUrlData(String key) {
213: if (key != null)
214: return (UrlData) urlDataHash.get(key);
215:
216: return null;
217: }
218:
219: RequestHandler getRequestHandlerInstance(String handlerClassName)
220: throws ClassNotFoundException, InstantiationException,
221: IllegalAccessException {
222: RequestHandler handler = (RequestHandler) requestHandlers
223: .get(handlerClassName);//.getId());
224:
225: if (handler == null)
226: if ((handler = (RequestHandler) Class.forName(
227: handlerClassName).newInstance()) != null)
228: requestHandlers.put(handlerClassName, handler);//.getId());
229:
230: return handler;
231: }
232:
233: /**
234: * Request information.
235: */
236: public class RequestData {
237: static final int PARAM_UNDEFINED = 0;
238: static final int PARAM_NONE = 1;
239: static final int PARAM_SERVER_INTERFACE = 2;
240: static final int PARAM_SERVLET_REQUEST = 3;
241: static final int PARAM_SERVLET_RESPONSE = 4;
242: static final int PARAM_SERVLET_BOTH = 5;
243:
244: RequestMethods requestMethods;
245: private RequestHandler requestHandler;
246: private String requestId, match, regularExpression,
247: handlerClassName, handlerMethodName;
248: private boolean isSecure, isDefined;
249: private HashSet roleSet;
250: private Hashtable urlDataHash, parameterHash;
251: private Vector validationVector;
252:
253: RequestData(String id, String match, String regExp,
254: String handlerClassName, String handlerMethodName,
255: boolean isSecure, boolean isConventional)
256: throws NoSuchMethodException, ClassNotFoundException,
257: InstantiationException, IllegalAccessException {
258: this .requestId = id;
259: this .match = match;
260: this .regularExpression = regExp;
261: this .handlerClassName = handlerClassName;
262: this .handlerMethodName = handlerMethodName;
263: this .isSecure = isSecure;
264: this .isDefined = isConventional;
265:
266: if (handlerClassName != null) {
267: requestHandler = getRequestHandlerInstance(handlerClassName);
268: requestMethods = new RequestMethods(requestHandler
269: .getClass(),
270: handlerMethodName == null ? "Request"
271: : handlerMethodName);
272: }
273:
274: roleSet = new HashSet();
275: urlDataHash = new Hashtable();
276: parameterHash = new Hashtable();
277: validationVector = new Vector();
278: }
279:
280: /**
281: * Returns the request Id
282: * @return the request Id
283: */
284: public String getId() {
285: return requestId;
286: }
287:
288: /**
289: * The request requires a secure connection.
290: * @return true if the request requires a secure connection, false otherwise.
291: */
292: public boolean isSecure() {
293: return isSecure;
294: }
295:
296: /**
297: * The request is conventional.
298: * @return true if the request is conventional.
299: */
300: public boolean isDefined() {
301: return isDefined;
302: }
303:
304: /**
305: * Returns a hash set of the roles associated with the request. A user must have at least one of
306: * the defined roles. If no roles are defined, the request is unrestricted.
307: * @return an instance of HashSet
308: */
309: public HashSet getRoleSet() {
310: return roleSet;
311: }
312:
313: /**
314: * Returns a parameter associated with key.
315: * @return parameter associated with key
316: */
317: public String getParameter(String key) {
318: return (String) parameterHash.get(key);
319: }
320:
321: /**
322: * Returns a Hashtable of parameters associated with the request.
323: * @return a Hashtable of parameters associated with the request
324: */
325: public Hashtable getParameterHash() {
326: return parameterHash;
327: }
328:
329: String getMatch() {
330: return match;
331: }
332:
333: String getMatchRegExp() {
334: return regularExpression;
335: }
336:
337: String getHandlerClassName() {
338: return handlerClassName;
339: }
340:
341: String getHandlerMethodName() {
342: return handlerMethodName != null ? handlerMethodName
343: : "Request";
344: }
345:
346: Hashtable getUrlDataHash() {
347: return urlDataHash;
348: }
349:
350: String getUrlDataId(String key) {
351: return (String) urlDataHash.get(key);
352: }
353:
354: Vector getValidationVector() {
355: return validationVector;
356: }
357:
358: Method getValidationMethod() {
359: return requestMethods.getValidationMethod();
360: }
361:
362: Method getProcessingMethod() {
363: return requestMethods.getProcessingMethod();
364: }
365:
366: int getValidationMethodType() {
367: return requestMethods.getValidationMethodType();
368: }
369:
370: int getProcessingMethodType() {
371: return requestMethods.getProcessingMethodType();
372: }
373:
374: RequestHandler getRequestHandler() {
375: return requestHandler;
376: }
377:
378: void setRequestHandler(RequestHandler requestHandler) {
379: this .requestHandler = requestHandler;
380: }
381:
382: class RequestMethods {
383: private Method validationMethod, processingMethod;
384: private int validationMethodType, processingMethodType;
385:
386: RequestMethods(Class cs, String methodName)
387: throws NoSuchMethodException {
388: try {
389: getRequestMethods(cs, "validate" + methodName, true);
390: } catch (Exception e) {
391: }
392: getRequestMethods(cs, "process" + methodName, false);
393: }
394:
395: void getRequestMethods(Class cs, String methodName,
396: boolean validation) throws NoSuchMethodException {
397: Method method = null;
398: int methodType = RequestData.PARAM_UNDEFINED;
399:
400: if (methodName != null && !methodName.equals("Request")) {
401: try {
402: method = cs.getMethod(methodName,
403: new Class[] { ServerInterface.class });
404: methodType = RequestData.PARAM_SERVER_INTERFACE;
405: } catch (Exception e2) {
406: try {
407: method = cs
408: .getMethod(
409: methodName,
410: new Class[] { HttpServletRequest.class });
411: methodType = RequestData.PARAM_SERVLET_REQUEST;
412: } catch (Exception e3) {
413: try {
414: method = cs
415: .getMethod(
416: methodName,
417: new Class[] { HttpServletResponse.class });
418: methodType = RequestData.PARAM_SERVLET_RESPONSE;
419: } catch (Exception e4) {
420: try {
421: method = cs
422: .getMethod(
423: methodName,
424: new Class[] {
425: HttpServletRequest.class,
426: HttpServletResponse.class });
427: methodType = RequestData.PARAM_SERVLET_BOTH;
428: } catch (Exception e5) {
429: method = cs.getMethod(methodName,
430: new Class[] { null });
431: methodType = RequestData.PARAM_NONE;
432: }
433: }
434: }
435: }
436: } else {
437: method = cs.getMethod((validation ? "validate"
438: : "process")
439: + "Request",
440: new Class[] { ServerInterface.class });
441: methodType = RequestData.PARAM_SERVER_INTERFACE;
442: }
443:
444: if (validation) {
445: setValidationMethod(method);
446: setValidationMethodType(methodType);
447: } else {
448: setProcessingMethod(method);
449: setProcessingMethodType(methodType);
450: }
451: }
452:
453: public Method getValidationMethod() {
454: return validationMethod;
455: }
456:
457: public void setValidationMethod(Method validationMethod) {
458: this .validationMethod = validationMethod;
459: }
460:
461: public Method getProcessingMethod() {
462: return processingMethod;
463: }
464:
465: public void setProcessingMethod(Method processingMethod) {
466: this .processingMethod = processingMethod;
467: }
468:
469: public int getValidationMethodType() {
470: return validationMethodType;
471: }
472:
473: public void setValidationMethodType(int validationMethodType) {
474: this .validationMethodType = validationMethodType;
475: }
476:
477: public int getProcessingMethodType() {
478: return processingMethodType;
479: }
480:
481: public void setProcessingMethodType(int processingMethodType) {
482: this.processingMethodType = processingMethodType;
483: }
484: }
485: }
486: }
|