001: package com.jat.presentation.controller;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import java.util.Vector;
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009: import javax.servlet.http.HttpSession;
010:
011: import com.jat.business.BusinessException;
012: import com.jat.business.BusinessObjectProperties;
013: import com.jat.business.JatUser;
014: import com.jat.core.config.Config;
015: import com.jat.core.log.LogManager;
016: import com.jat.presentation.PresentationException;
017: import com.jat.presentation.parameter.CheckParameterException;
018: import com.jat.presentation.parameter.CheckParameterPlugin;
019: import com.jat.presentation.parameter.DefaultCheckParameter;
020: import com.jat.presentation.parameter.HttpRequestParameters;
021: import com.jat.presentation.PresentationServlet;
022:
023: /**
024: * <p>Title: JAT</p>
025: * <p>Description: </p>
026: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
027: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
028: * @author stf
029: * @version 1.0
030: * @since 1.2
031: */
032:
033: public abstract class Action extends PresentationServlet {
034:
035: public final static String ACTION_PARAMETER = "action";
036: public final static String JAT_PAGES_FLOW = "JAT_PAGES_FLOW";
037: public final static String CHECK_PARAMETERS_ERRORS = "CHECK_PARAMETERS_ERRORS";
038: public final static String BUSINESS_OBJECT_LIST_RESULT = "BUSINESS_OBJECT_LIST_RESULT";
039:
040: public final static String CHECK_PARAMETERS_DEFAULT_SECTION = "presentation";
041: public final static String CHECK_PARAMETERS_SECTION_KEY = "section";
042: public final static String CHECK_PARAMETERS_FIELDS_KEY = "fields_key";
043:
044: //public abstract void doAction(HttpServletRequest request, HttpServletResponse response) throws PresentationException, ServletException;
045:
046: public boolean isNavigable() {
047: return false;
048: }
049:
050: public String getName() {
051: return this .name;
052: }
053:
054: public void setName(String name) {
055: this .name = name;
056: }
057:
058: public String getNextPage() {
059: return this .nextPage;
060: }
061:
062: public void setNextPage(String nextPage) {
063: this .nextPage = nextPage;
064: }
065:
066: public String getErrorPage() {
067: return this .errorPage;
068: }
069:
070: public void setErrorPage(String errorPage) {
071: this .errorPage = errorPage;
072: }
073:
074: public String getPrivilege() {
075: return this .privilege;
076: }
077:
078: public void setPrivilege(String privilege) {
079: this .privilege = privilege;
080: }
081:
082: public boolean isLogRequired() {
083: return this .logRequired;
084: }
085:
086: public void setLogRequired(boolean logRequired) {
087: this .logRequired = logRequired;
088: }
089:
090: public Vector getMethods() {
091: return this .methods;
092: }
093:
094: public void addMethod(String method) {
095: this .methods.addElement(method);
096: }
097:
098: public boolean removeMethod(String method) {
099: return this .methods.remove(method);
100: }
101:
102: public Vector getPreviousPages() {
103: return this .previousPages;
104: }
105:
106: public void addPreviousPages(String previousPage) {
107: this .previousPages.addElement(previousPage);
108: }
109:
110: public boolean removePreviousPages(String previousPage) {
111: return this .previousPages.remove(previousPage);
112: }
113:
114: public void setMethods(Vector methods) {
115: this .methods = methods;
116: }
117:
118: public void setPreviousPages(Vector previousPages) {
119: this .previousPages = previousPages;
120: }
121:
122: public BusinessObjectProperties checkParameters(
123: HttpServletRequest request) throws CheckParameterException,
124: Exception {
125: // CHECK_PARAMETERS_FIELDS_KEY is mandatory
126: String fieldsKey = request
127: .getParameter(CHECK_PARAMETERS_FIELDS_KEY);
128: if (fieldsKey == null || fieldsKey.equals(""))
129: throw new PresentationException("Parameter '"
130: + CHECK_PARAMETERS_FIELDS_KEY
131: + "' not found in request");
132: // CHECK_PARAMETERS_SECTION_KEY is optional
133: String section = request
134: .getParameter(CHECK_PARAMETERS_SECTION_KEY);
135: if (section == null || section.equals(""))
136: section = CHECK_PARAMETERS_DEFAULT_SECTION;
137:
138: HttpRequestParameters parameters = new HttpRequestParameters(
139: request, null);
140: Hashtable errors = parameters.checkParameters(section,
141: fieldsKey);
142: if (errors.size() > 0) {
143: request.setAttribute(CHECK_PARAMETERS_ERRORS, errors);
144: throw new CheckParameterException(errors);
145: }
146: return parameters.getProperties();
147: }
148:
149: public String toString() {
150: String ret = this .getClass().getName() + "{";
151: ret += "[name=" + this .getName() + "],";
152: ret += "[next_page=" + this .getNextPage() + "],";
153: ret += "[error_page=" + this .getErrorPage() + "],";
154: ret += "[log_required=" + this .isLogRequired() + "],";
155: ret += "[privilege=" + this .getPrivilege() + "],";
156: ret += "[methods:{" + methods + "}],";
157: ret += "[previous_pages:{" + previousPages + "}]";
158: return ret + "}";
159: }
160:
161: protected boolean checkMethod(HttpServletRequest request) {
162: if (this .methods.size() < 1)
163: return true;
164: String method = request.getMethod();
165: return methods.contains(method.toUpperCase());
166: }
167:
168: protected boolean checkFlow(HttpSession session) {
169: if (previousPages.size() < 1)
170: return true;
171: String previousPage = (String) session
172: .getAttribute(JAT_PAGES_FLOW);
173: return this .previousPages.contains(previousPage);
174: }
175:
176: protected boolean checkUserLogged(HttpSession session)
177: throws PrivilegeException {
178: if (!this .isLogRequired())
179: return true;
180: JatUser user = (JatUser) session.getAttribute(JatUser.JAT_USER);
181: if (user == null)
182: return false;
183: if (this .getPrivilege() == null)
184: return true;
185: try {
186: return user.hasPrivilege(this .getPrivilege(), null);
187: } catch (BusinessException ex) {
188: throw new PrivilegeException("User " + user.getUsername()
189: + "has no privileges to access to "
190: + this .getPrivilege());
191: }
192: }
193:
194: private String name;
195: private String nextPage;
196: private String errorPage;
197: private String privilege;
198: private boolean logRequired;
199: private Vector methods = new Vector();
200: private Vector previousPages = new Vector();
201: }
|