001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: RequestAttributes.java 1707 2007-09-28 15:35:48Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons.attributes;
025:
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032:
033: import org.makumba.AttributeNotFoundException;
034: import org.makumba.Attributes;
035: import org.makumba.LogicException;
036: import org.makumba.UnauthenticatedException;
037: import org.makumba.UnauthorizedException;
038: import org.makumba.commons.DbConnectionProvider;
039: import org.makumba.controller.Logic;
040: import org.makumba.providers.TransactionProvider;
041:
042: /**
043: * Implementation of the Makumba {@link Attributes}
044: *
045: * @author Cristian Bogdan
046: * @version $Id: RequestAttributes.java 1707 2007-09-28 15:35:48Z manuel_gay $
047: *
048: */
049: public class RequestAttributes implements Attributes {
050: public static final String PARAMETERS_NAME = "makumba.parameters";
051:
052: public static final String ATTRIBUTES_NAME = "makumba.attributes";
053:
054: public static final String CONTROLLER_NAME = "makumba.controller";
055:
056: HttpServletRequest request;
057:
058: Object controller;
059:
060: private TransactionProvider tp = new TransactionProvider();
061:
062: public String getRequestDatabase() {
063: return tp.getDefaultDataSourceName();
064: }
065:
066: public Object getRequestController() {
067: return controller;
068: }
069:
070: public static RequestAttributes getAttributes(HttpServletRequest req)
071: throws LogicException {
072: if (req.getAttribute(ATTRIBUTES_NAME) == null)
073: req.setAttribute(ATTRIBUTES_NAME,
074: new RequestAttributes(req));
075: return (RequestAttributes) req.getAttribute(ATTRIBUTES_NAME);
076: }
077:
078: RequestAttributes(HttpServletRequest req) throws LogicException {
079: this (Logic.getLogic(req.getServletPath()), req, null);
080: }
081:
082: public RequestAttributes(Object controller, HttpServletRequest req,
083: String db) throws LogicException {
084: if (db == null)
085: db = getRequestDatabase();
086: this .request = req;
087: this .controller = controller;
088:
089: if (req.getAttribute(CONTROLLER_NAME
090: + controller.getClass().getName()) == null) {
091: req.setAttribute(CONTROLLER_NAME
092: + controller.getClass().getName(), controller);
093: try {
094: Logic.doInit(controller, this , db,
095: getConnectionProvider(req));
096: } catch (UnauthorizedException e) {
097: // if we are not in the login page
098: if (!req.getServletPath().endsWith("login.jsp"))
099: throw e;
100: }
101: }
102: }
103:
104: static final public String PROVIDER_ATTRIBUTE = "org.makumba.providerAttribute";
105:
106: /**
107: * Gives a provider to get connection to the database
108: *
109: * @param req
110: * the http request corresponding to the current access
111: * @return A {@link DbConnectionProvider} providing the database connection service
112: */
113: public static DbConnectionProvider getConnectionProvider(
114: HttpServletRequest req) {
115: DbConnectionProvider prov = (DbConnectionProvider) req
116: .getAttribute(PROVIDER_ATTRIBUTE);
117: if (prov == null) {
118: prov = new DbConnectionProvider();
119: req.setAttribute(PROVIDER_ATTRIBUTE, prov);
120: }
121: return prov;
122: }
123:
124: public static HttpParameters getParameters(HttpServletRequest req) {
125: if (req.getAttribute(PARAMETERS_NAME) == null)
126: req.setAttribute(PARAMETERS_NAME, makeParameters(req));
127: return (HttpParameters) req.getAttribute(PARAMETERS_NAME);
128: }
129:
130: public static HttpParameters makeParameters(HttpServletRequest req) {
131: if (req.getContentType() != null
132: && req.getContentType().indexOf("multipart") != -1)
133: return new MultipartHttpParameters(req);
134: return new HttpParameters(req);
135: }
136:
137: static public void setAttribute(HttpServletRequest req, String var,
138: Object o) {
139: if (o != null) {
140: req.setAttribute(var, o);
141: req.removeAttribute(var + "_null");
142: } else {
143: req.removeAttribute(var);
144: req.setAttribute(var + "_null", "null");
145: }
146: }
147:
148: public static final Object notFound = "not found";
149:
150: /**
151: * @see org.makumba.Attributes#setAttribute(java.lang.String, java.lang.Object)
152: */
153: public Object setAttribute(String s, Object o) {
154: String snull = s + "_null";
155: HttpSession ss = request.getSession(true);
156:
157: Object value = ss.getAttribute(s);
158: ss.setAttribute(s, o);
159: if (o == null)
160: ss.setAttribute(snull, "null");
161: else
162: ss.removeAttribute(snull);
163: return value;
164: }
165:
166: /**
167: * @see org.makumba.Attributes#removeAttribute(java.lang.String)
168: */
169: public void removeAttribute(String s) throws LogicException {
170: request.getSession(true).removeAttribute(s);
171: }
172:
173: /**
174: * @see org.makumba.Attributes#hasAttribute(java.lang.String)
175: */
176: public boolean hasAttribute(String s) {
177: try {
178: return (checkSessionForAttribute(s) != RequestAttributes.notFound
179: || checkServletLoginForAttribute(s) != RequestAttributes.notFound
180: || checkLogicForAttribute(s) != RequestAttributes.notFound || checkParameterForAttribute(s) != RequestAttributes.notFound);
181: } catch (LogicException e) {
182: return false;
183: }
184: }
185:
186: /**
187: * @see java.lang.Object#toString()
188: */
189: public String toString() {
190: String s = "Makumba Atributes:\n";
191: s += "\tSession: {";
192: HttpSession ss = request.getSession(true);
193: Enumeration enumSession = ss.getAttributeNames();
194: while (enumSession.hasMoreElements()) {
195: String key = (String) enumSession.nextElement();
196: s += key + "=";
197: Object value = ss.getAttribute(key);
198: if (value instanceof RequestAttributes) { // don't print if type is from this class --> avoid endless loop
199: s += value.getClass();
200: } else {
201: s += value;
202: }
203: if (enumSession.hasMoreElements()) {
204: s += ", ";
205: }
206: }
207: s += "}\n";
208:
209: Enumeration enumRequest = request.getAttributeNames();
210: s += "\tRequest: {";
211: while (enumRequest.hasMoreElements()) {
212: String key = (String) enumRequest.nextElement();
213: s += key + "=";
214: Object value = request.getAttribute(key);
215: if (value instanceof RequestAttributes) { // don't print if type is from this class --> avoid endless loop
216: s += value.getClass();
217: } else {
218: s += value;
219: }
220: if (enumRequest.hasMoreElements()) {
221: s += ", ";
222: }
223: }
224: s += "}\n";
225:
226: Enumeration enumParams = request.getParameterNames();
227: s += "\tParameters: {";
228: while (enumParams.hasMoreElements()) {
229: String key = (String) enumParams.nextElement();
230: s += key + "=" + request.getParameter(key);
231: if (enumParams.hasMoreElements()) {
232: s += ", ";
233: }
234: }
235: s += "}";
236: return s;
237: }
238:
239: /**
240: * @see org.makumba.Attributes#getAttribute(java.lang.String)
241: */
242: public Object getAttribute(String s) throws LogicException {
243: Object o = checkSessionForAttribute(s);
244: if (o != notFound)
245: return o;
246:
247: o = checkServletLoginForAttribute(s);
248: if (o != notFound)
249: return o;
250:
251: o = checkLogicForAttribute(s);
252: if (o != notFound)
253: return o;
254:
255: o = checkParameterForAttribute(s);
256: if (o != notFound)
257: return o;
258:
259: throw new AttributeNotFoundException(s, true);
260: }
261:
262: public Object checkSessionForAttribute(String s) {
263: String snull = s + "_null";
264: HttpSession ss = request.getSession(true);
265:
266: Object value = ss.getAttribute(s);
267: if (value != null)
268: return value;
269: if (ss.getAttribute(snull) != null)
270: return null;
271: value = request.getAttribute(s);
272: if (value != null)
273: return value;
274: if (request.getAttribute(snull) != null)
275: return null;
276: return notFound;
277: }
278:
279: public Object checkServletLoginForAttribute(String s) {
280: if (request.getRemoteUser() != null && request.isUserInRole(s))
281: return request.getRemoteUser();
282: return notFound;
283: }
284:
285: public Object checkLogicForAttribute(String s)
286: throws LogicException {
287: String snull = s + "_null";
288: HttpSession ss = request.getSession(true);
289: boolean nullValue = false;
290: Object value = null;
291: try {
292: value = Logic.getAttribute(getRequestController(), s, this ,
293: getRequestDatabase(),
294: getConnectionProvider(request));
295: if (value == null)
296: nullValue = true;
297: } catch (NoSuchMethodException e) {
298: } catch (UnauthenticatedException ue) {
299: ue.setAttributeName(s);
300: throw ue;
301: }
302: // FIXME: should check HTTP argument illegalities
303:
304: if (value != null) {
305: ss.setAttribute(s, value);
306: return value;
307: }
308: if (nullValue) {
309: ss.removeAttribute(s);
310: ss.setAttribute(snull, "x");
311: return null;
312: }
313: return notFound;
314: }
315:
316: public Object checkParameterForAttribute(String s) {
317: Object value = getParameters(request).getParameter(s);
318: if (value != null)
319: return value;
320: return notFound;
321: }
322:
323: /**
324: * Computes a Map that holds all Attributes (meaning, session attributes, request attributes and parameters)
325: *
326: * FIXME should also take into account all the rest (BL, login, ...)
327: */
328: public Map toMap() {
329: HttpSession ss = request.getSession(true);
330: Enumeration<String> enumSession = ss.getAttributeNames();
331: Enumeration<String> enumRequest = request.getAttributeNames();
332: Enumeration<String> enumParams = request.getParameterNames();
333:
334: Map<String, Object> allAttributes = new HashMap<String, Object>();
335: while (enumSession.hasMoreElements()) {
336: String attrName = enumSession.nextElement();
337: allAttributes.put(attrName, ss.getAttribute(attrName));
338: }
339: while (enumRequest.hasMoreElements()) {
340: String attrName = enumRequest.nextElement();
341: allAttributes.put(attrName, ss.getAttribute(attrName));
342: }
343: while (enumParams.hasMoreElements()) {
344: String attrName = enumParams.nextElement();
345: allAttributes.put(attrName, ss.getAttribute(attrName));
346: }
347:
348: return allAttributes;
349: }
350: }
|