Source Code Cross Referenced for UserSession.java in  » Forum » JForum-2.1.8 » net » jforum » entities » 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 » Forum » JForum 2.1.8 » net.jforum.entities 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) JForum Team
003:         * All rights reserved.
004:         * 
005:         * Redistribution and use in source and binary forms, 
006:         * with or without modification, are permitted provided 
007:         * that the following conditions are met:
008:         * 
009:         * 1) Redistributions of source code must retain the above 
010:         * copyright notice, this list of conditions and the 
011:         * following  disclaimer.
012:         * 2)  Redistributions in binary form must reproduce the 
013:         * above copyright notice, this list of conditions and 
014:         * the following disclaimer in the documentation and/or 
015:         * other materials provided with the distribution.
016:         * 3) Neither the name of "Rafael Steil" nor 
017:         * the names of its contributors may be used to endorse 
018:         * or promote products derived from this software without 
019:         * specific prior written permission.
020:         * 
021:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
022:         * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
023:         * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
024:         * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
025:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
026:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
027:         * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
028:         * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
029:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
030:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
031:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
032:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
033:         * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
034:         * IN CONTRACT, STRICT LIABILITY, OR TORT 
035:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
036:         * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
037:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038:         * 
039:         * This file creation date: 30/12/2003 / 21:40:54
040:         * The JForum Project
041:         * http://www.jforum.net
042:         */
043:        package net.jforum.entities;
044:
045:        import java.awt.image.BufferedImage;
046:        import java.io.Serializable;
047:        import java.util.Date;
048:
049:        import net.jforum.ControllerUtils;
050:        import net.jforum.JForumExecutionContext;
051:        import net.jforum.SessionFacade;
052:        import net.jforum.repository.SecurityRepository;
053:        import net.jforum.security.PermissionControl;
054:        import net.jforum.security.SecurityConstants;
055:        import net.jforum.util.Captcha;
056:        import net.jforum.util.I18n;
057:        import net.jforum.util.preferences.ConfigKeys;
058:        import net.jforum.util.preferences.SystemGlobals;
059:
060:        import com.octo.captcha.image.ImageCaptcha;
061:
062:        /**
063:         * Stores information about user's session.
064:         * 
065:         * @author Rafael Steil
066:         * @version $Id: UserSession.java,v 1.37 2007/09/10 01:17:20 rafaelsteil Exp $
067:         */
068:        public class UserSession implements  Serializable {
069:            static final long serialVersionUID = 0;
070:
071:            private long sessionTime;
072:
073:            private int userId;
074:            private int privateMessages;
075:
076:            private Date startTime;
077:            private Date lastVisit;
078:
079:            private String sessionId;
080:            private String username;
081:            private String lang;
082:            private String ip;
083:
084:            private boolean autoLogin;
085:
086:            private ImageCaptcha imageCaptcha = null;
087:
088:            public UserSession() {
089:            }
090:
091:            public UserSession(UserSession us) {
092:                if (us.getStartTime() != null) {
093:                    this .startTime = new Date(us.getStartTime().getTime());
094:                }
095:
096:                if (us.getLastVisit() != null) {
097:                    this .lastVisit = new Date(us.getLastVisit().getTime());
098:                }
099:
100:                this .sessionTime = us.getSessionTime();
101:                this .userId = us.getUserId();
102:                this .sessionId = us.getSessionId();
103:                this .username = us.getUsername();
104:                this .autoLogin = us.getAutoLogin();
105:                this .lang = us.getLang();
106:                this .privateMessages = us.getPrivateMessages();
107:                this .imageCaptcha = us.imageCaptcha;
108:                this .ip = us.getIp();
109:            }
110:
111:            public Date sessionLastUpdate() {
112:                return new Date(this .startTime.getTime() + this .sessionTime);
113:            }
114:
115:            public void setIp(String ip) {
116:                this .ip = ip;
117:            }
118:
119:            public String getIp() {
120:                return this .ip;
121:            }
122:
123:            /**
124:             * Set session's start time.
125:             * 
126:             * @param startTime  Start time in miliseconds
127:             */
128:            public void setStartTime(Date startTime) {
129:                this .startTime = startTime;
130:            }
131:
132:            /**
133:             * @return Returns the privateMessages.
134:             */
135:            public int getPrivateMessages() {
136:                return this .privateMessages;
137:            }
138:
139:            /**
140:             * @param privateMessages The privateMessages to set.
141:             */
142:            public void setPrivateMessages(int privateMessages) {
143:                this .privateMessages = privateMessages;
144:            }
145:
146:            /**
147:             * Set session last visit time.
148:             * 
149:             * @param lastVisit Time in miliseconds
150:             */
151:            public void setLastVisit(Date lastVisit) {
152:                this .lastVisit = lastVisit;
153:            }
154:
155:            /**
156:             * Set user's id
157:             * 
158:             * @param userId The user id
159:             */
160:            public void setUserId(int userId) {
161:                this .userId = userId;
162:            }
163:
164:            /**
165:             * Set user's name
166:             * 
167:             * @param username The username
168:             */
169:            public void setUsername(String username) {
170:                this .username = username;
171:            }
172:
173:            public void setSessionId(String sessionId) {
174:                this .sessionId = sessionId;
175:            }
176:
177:            public void setSessionTime(long sessionTime) {
178:                this .sessionTime = sessionTime;
179:            }
180:
181:            public void setLang(String lang) {
182:                this .lang = lang;
183:            }
184:
185:            /**
186:             * Update the session time.
187:             */
188:            public void updateSessionTime() {
189:                this .sessionTime = System.currentTimeMillis()
190:                        - this .startTime.getTime();
191:            }
192:
193:            /**
194:             * Enable or disable auto-login.
195:             * 
196:             * @param autoLogin  <code>true</code> or <code>false</code> to represent auto-login status
197:             */
198:            public void setAutoLogin(boolean autoLogin) {
199:                this .autoLogin = autoLogin;
200:            }
201:
202:            /**
203:             * Gets user's session start time
204:             * 
205:             * @return Start time in miliseconds
206:             */
207:            public Date getStartTime() {
208:                return this .startTime;
209:            }
210:
211:            public String getLang() {
212:                return this .lang;
213:            }
214:
215:            /**
216:             * Gets user's last visit time
217:             * 
218:             * @return Time in miliseconds
219:             */
220:            public Date getLastVisit() {
221:                //return new GregorianCalendar(2007, 6, 28, 15, 15, 19).getTime();
222:                return this .lastVisit;
223:            }
224:
225:            /**
226:             * Gets the session time.
227:             * 
228:             * @return The session time
229:             */
230:            public long getSessionTime() {
231:                return this .sessionTime;
232:            }
233:
234:            /**
235:             * Gets user's id
236:             * 
237:             * @return The user id
238:             */
239:            public int getUserId() {
240:                return this .userId;
241:            }
242:
243:            /**
244:             * Gets the username
245:             * 
246:             * @return The username
247:             */
248:            public String getUsername() {
249:                if (this .username == null
250:                        && this .userId == SystemGlobals
251:                                .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
252:                    this .username = I18n.getMessage("Guest");
253:                }
254:
255:                return this .username;
256:            }
257:
258:            /**
259:             * Gets auto-login status
260:             * 
261:             * @return <code>true</code> if auto-login is enabled, or <code>false</code> if disabled.
262:             */
263:            public boolean getAutoLogin() {
264:                return this .autoLogin;
265:            }
266:
267:            /**
268:             * Gets the session id related to this user session
269:             * 
270:             * @return A string with the session id
271:             */
272:            public String getSessionId() {
273:                return this .sessionId;
274:            }
275:
276:            /**
277:             * Checks if the user is an administrator
278:             * 
279:             * @return <code>true</code> if the user is an administrator
280:             */
281:            public boolean isAdmin() {
282:                return SecurityRepository.canAccess(this .userId,
283:                        SecurityConstants.PERM_ADMINISTRATION);
284:            }
285:
286:            /**
287:             * Checks if the user is a moderator
288:             * 
289:             * @return <code>true</code> if the user has moderations rights
290:             */
291:            public boolean isModerator() {
292:                return SecurityRepository.canAccess(this .userId,
293:                        SecurityConstants.PERM_MODERATION);
294:            }
295:
296:            /**
297:             * Checks if the user can moderate a forum
298:             * 
299:             * @param forumId the forum's id to check for moderation rights
300:             * @return <code>true</code> if the user has moderations rights
301:             */
302:            public boolean isModerator(int forumId) {
303:                PermissionControl pc = SecurityRepository.get(this .userId);
304:
305:                return (pc.canAccess(SecurityConstants.PERM_MODERATION))
306:                        && (pc.canAccess(
307:                                SecurityConstants.PERM_MODERATION_FORUMS,
308:                                Integer.toString(forumId)));
309:            }
310:
311:            /**
312:             * Makes the user's session "anoymous" - eg, the user. This method sets the session's start and
313:             * last visit time to the current datetime, the user id to the return of a call to
314:             * <code>SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)</code> and finally sets
315:             * session attribute named "logged" to "0" will be considered a non-authenticated / anonymous
316:             * user
317:             */
318:            public void makeAnonymous() {
319:                this .registerBasicInfo();
320:
321:                ControllerUtils.addCookie(SystemGlobals
322:                        .getValue(ConfigKeys.COOKIE_AUTO_LOGIN), null);
323:                ControllerUtils.addCookie(SystemGlobals
324:                        .getValue(ConfigKeys.COOKIE_NAME_DATA), SystemGlobals
325:                        .getValue(ConfigKeys.ANONYMOUS_USER_ID));
326:
327:                SessionFacade.makeUnlogged();
328:            }
329:
330:            /**
331:             * Sets the startup and last visit time to now, as well set the
332:             * user id to Anonymous. This method is usually called when the
333:             * user hits the forum for the first time. 
334:             */
335:            public void registerBasicInfo() {
336:                this .setStartTime(new Date(System.currentTimeMillis()));
337:                this .setLastVisit(new Date(System.currentTimeMillis()));
338:                this .setUserId(SystemGlobals
339:                        .getIntValue(ConfigKeys.ANONYMOUS_USER_ID));
340:            }
341:
342:            /**
343:             * Sets a new user session information using information from an <code>User</code> instance.
344:             * This method sets the user id, username, the number of private messages, the session's start
345:             * time ( set to the current date and time ) and the language.
346:             * 
347:             * @param user The <code>User</code> instance to get data from
348:             */
349:            public void dataToUser(User user) {
350:                this .setUserId(user.getId());
351:                this .setUsername(user.getUsername());
352:                this .setPrivateMessages(user.getPrivateMessagesCount());
353:                this .setStartTime(new Date(System.currentTimeMillis()));
354:                this .setLang(user.getLang());
355:            }
356:
357:            /**
358:             * Get the captcha image to challenge the user
359:             * 
360:             * @return BufferedImage the captcha image to challenge the user
361:             */
362:            public BufferedImage getCaptchaImage() {
363:                if (this .imageCaptcha == null) {
364:                    return null;
365:                }
366:
367:                return (BufferedImage) this .imageCaptcha.getChallenge();
368:            }
369:
370:            /**
371:             * Validate the captcha response of user
372:             * 
373:             * @param userResponse String the captcha response from user
374:             * @return boolean true if the answer is valid, otherwise return false
375:             */
376:            public boolean validateCaptchaResponse(String userResponse) {
377:                if ((SystemGlobals
378:                        .getBoolValue(ConfigKeys.CAPTCHA_REGISTRATION) || SystemGlobals
379:                        .getBoolValue(ConfigKeys.CAPTCHA_POSTS))
380:                        && this .imageCaptcha != null) {
381:
382:                    if (SystemGlobals
383:                            .getBoolValue(ConfigKeys.CAPTCHA_IGNORE_CASE)) {
384:                        userResponse = userResponse.toLowerCase();
385:                    }
386:
387:                    boolean result = this .imageCaptcha.validateResponse(
388:                            userResponse).booleanValue();
389:                    this .destroyCaptcha();
390:                    return result;
391:                }
392:
393:                return true;
394:            }
395:
396:            /**
397:             * create a new image captcha
398:             * 
399:             */
400:            public void createNewCaptcha() {
401:                this .destroyCaptcha();
402:                this .imageCaptcha = Captcha.getInstance().getNextImageCaptcha();
403:            }
404:
405:            /**
406:             * Destroy the current captcha validation is done
407:             * 
408:             */
409:            public void destroyCaptcha() {
410:                this .imageCaptcha = null;
411:            }
412:
413:            /**
414:             * @deprecated use JForumExecutionContext.getForumContext().isBot() instead
415:             *
416:             *
417:             * Checks if it's a bot
418:             * @return <code>true</code> if this user session is from any robot
419:             */
420:            public boolean isBot() {
421:                //        return Boolean.TRUE.equals(JForumExecutionContext.getRequest().getAttribute(ConfigKeys.IS_BOT));
422:                return JForumExecutionContext.getForumContext().isBot();
423:            }
424:
425:            /**
426:             * @see java.lang.Object#equals(java.lang.Object)
427:             */
428:            public boolean equals(Object o) {
429:                if (!(o instanceof  UserSession)) {
430:                    return false;
431:                }
432:
433:                return this .sessionId.equals(((UserSession) o).getSessionId());
434:            }
435:
436:            /**
437:             * @see java.lang.Object#hashCode()
438:             */
439:            public int hashCode() {
440:                return this.sessionId.hashCode();
441:            }
442:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.