001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.framework.core;
018:
019: import helma.objectmodel.INode;
020: import helma.framework.UploadStatus;
021:
022: import java.io.Serializable;
023: import java.util.Date;
024:
025: /**
026: * The SessionBean wraps a <code>Session</code> object and
027: * exposes it to the scripting framework.
028: */
029: public class SessionBean implements Serializable {
030: // the wrapped session object
031: Session session;
032:
033: /**
034: * Creates a new SessionBean around a Session object.
035: *
036: * @param session ...
037: */
038: public SessionBean(Session session) {
039: this .session = session;
040: }
041:
042: /**
043: *
044: *
045: * @return ...
046: */
047: public String toString() {
048: return session.toString();
049: }
050:
051: /**
052: * Attempts to log in a user with the given username/password credentials.
053: * If username and password match, the user node is associated with the session
054: * and bound to the session.user property.
055: *
056: * @param username the username
057: * @param password the password
058: *
059: * @return true if the user exists and the password matches the user's password property.
060: */
061: public boolean login(String username, String password) {
062: return session.login(username, password);
063: }
064:
065: /**
066: * Directly associates the session with a user object without requiring
067: * a username/password pair. This is for applications that use their own
068: * authentication mechanism.
069: *
070: * @param userNode the HopObject node representing the user.
071: */
072: public void login(INode userNode) {
073: session.login(userNode);
074: }
075:
076: /**
077: * Disassociate this session from any user object it may have been associated with.
078: */
079: public void logout() {
080: session.logout();
081: }
082:
083: /**
084: * Touching the session marks it as active, avoiding session timeout.
085: * Usually, sessions are touched when the user associated with it sends
086: * a request. This method may be used to artificially keep a session alive.
087: */
088: public void touch() {
089: session.touch();
090: }
091:
092: /**
093: * Returns the time this session was last touched.
094: *
095: * @return ...
096: */
097: public Date lastActive() {
098: return new Date(session.lastTouched());
099: }
100:
101: /**
102: * Returns the time this session was created.
103: *
104: * @return ...
105: */
106: public Date onSince() {
107: return new Date(session.onSince());
108: }
109:
110: // property-related methods:
111:
112: /**
113: * Get the cache/data node for this session. This object may be used
114: * to store transient per-session data. It is reflected to the scripting
115: * environment as session.data.
116: */
117: public INode getData() {
118: return session.getCacheNode();
119: }
120:
121: /**
122: * Gets the user object for this session. This method returns null unless
123: * one of the session.login methods was previously invoked.
124: *
125: * @return ...
126: */
127: public INode getUser() {
128: return session.getUserNode();
129: }
130:
131: /**
132: * Returns the unique identifier for a session object (session cookie).
133: *
134: * @return ...
135: */
136: public String get_id() {
137: return session.getSessionId();
138: }
139:
140: /**
141: * Returns the unique identifier for a session object (session cookie).
142: *
143: * @return ...
144: */
145: public String getCookie() {
146: return session.getSessionId();
147: }
148:
149: /**
150: * Returns the time this session was last touched.
151: *
152: * @return ...
153: */
154: public Date getLastActive() {
155: return new Date(session.lastTouched());
156: }
157:
158: /**
159: * Returns a date object representing the time a user's session was started.
160: *
161: * @return ...
162: */
163: public Date getOnSince() {
164: return new Date(session.onSince());
165: }
166:
167: /**
168: * Gets the date at which the session was created or a login or
169: * logout was performed the last time.
170: *
171: * @return ...
172: */
173: public Date getLastModified() {
174: return new Date(session.lastModified());
175: }
176:
177: /**
178: * Sets the date at which the session was created or a login or
179: * logout was performed the last time.
180: *
181: * @param date ...
182: */
183: public void setLastModified(Date date) {
184: session.setLastModified(date);
185: }
186:
187: /**
188: * Return the message that is to be displayed upon the next
189: * request within this session.
190: *
191: * @return the message, or null if none was set.
192: */
193: public String getMessage() {
194: return session.message;
195: }
196:
197: /**
198: * Set a message to be displayed to this session's user. This
199: * can be used to save a message over to the next request when
200: * the current request can't be used to display a user visible
201: * message.
202: *
203: * @param msg
204: */
205: public void setMessage(String msg) {
206: session.message = msg;
207: }
208:
209: /**
210: * Get an upload status for the current user session.
211: * @param uploadId the upload id
212: * @return the upload status
213: */
214: public UploadStatus getUploadStatus(String uploadId) {
215: return session.getUpload(uploadId);
216: }
217:
218: }
|