Source Code Cross Referenced for RequestAttributes.java in  » Web-Framework » makumba » org » makumba » commons » attributes » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » makumba » org.makumba.commons.attributes 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.