01: package com.jat.presentation.parameter;
02:
03: import java.util.Enumeration;
04: import java.util.Hashtable;
05: import javax.servlet.http.HttpServletRequest;
06:
07: import com.jat.business.BusinessObject;
08: import com.jat.core.config.Config;
09: import com.jat.core.log.LogManager;
10: import com.jat.presentation.PresentationException;
11:
12: /**
13: * <p>Title: JAT</p>
14: * <p>Description: </p>
15: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
16: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
17: * @author stf
18: * @version 1.0
19: * @since 1.2
20: */
21:
22: public class HttpRequestParameters extends BusinessObject {
23:
24: public HttpRequestParameters(HttpServletRequest request,
25: String dataSourceName) {
26: super (dataSourceName);
27: this .init(request);
28: }
29:
30: public String getParameter(String name) {
31: String value = super .getField(name);
32: if (value != null)
33: return value.trim();
34: return null;
35: }
36:
37: public String getParameter(String name, boolean mandatory)
38: throws PresentationException {
39: String value = this .getParameter(name);
40: if (mandatory && (value == null || value.equals("")))
41: throw new PresentationException("Mandatory parameter "
42: + name + " not found in request");
43: return value;
44: }
45:
46: public Hashtable checkParameters(String section, String fieldsKey)
47: throws Exception {
48: Hashtable errors = new Hashtable();
49: for (Enumeration e = Config.getCurrent().getSubKeys(section,
50: fieldsKey).elements(); e.hasMoreElements();) {
51: String key = (String) e.nextElement();
52: String name = Config.getCurrent().getValue(section,
53: key + ".name");
54: String label = Config.getCurrent().getValue(section,
55: key + ".label");
56: CheckParameterPlugin check = null;
57: try {
58: String cp = Config.getCurrent().getValue(section,
59: key + ".check");
60: check = (CheckParameterPlugin) Class.forName(cp)
61: .newInstance();
62: } catch (Exception ex1) {
63: LogManager
64: .sendDebug(this .getClass().getName()
65: + "::checkParameters: exception loading CheckParameter class name for parameter '"
66: + name + "': " + ex1);
67: check = new DefaultCheckParameter();
68: }
69: try {
70: String value = this .getParameter(name);
71: Object obj = check.check(label, value, this
72: .getProperties());
73: } catch (CheckParameterException pe) {
74: LogManager.sendDebug(this .getClass().getName()
75: + "checkParameters: exception: " + pe);
76: errors.put(name, pe.getMessage());
77: }
78: }
79: return errors;
80: }
81:
82: protected void init(HttpServletRequest request) {
83: for (Enumeration e = request.getParameterNames(); e
84: .hasMoreElements();) {
85: String name = (String) e.nextElement();
86: super.putField(name, request.getParameter(name));
87: }
88: }
89: }
|