1 /*
2  * Copyright 2008 Les Hazlewood
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 module hunt.shiro.session.mgt.NativeSessionManager;
17 
18 import hunt.shiro.session.mgt.SessionKey;
19 import hunt.shiro.session.mgt.SessionManager;
20 
21 import hunt.shiro.Exceptions;
22 import hunt.shiro.session.Session;
23 import hunt.collection;
24 // import java.util.Date;
25 
26 /**
27  * A {@code Native} session manager is one that manages sessions natively - that is, it is directly responsible
28  * for the creation, persistence and removal of {@link hunt.shiro.session.Session Session} instances and their
29  * lifecycles.
30  *
31  */
32 interface NativeSessionManager : SessionManager {
33 
34     /**
35      * Returns the time the associated {@code Session} started (was created).
36      *
37      * @param key the session key to use to look up the target session.
38      * @return the time the specified {@code Session} started (was created).
39      * @see hunt.shiro.session.Session#getStartTimestamp()
40      */
41     Date getStartTimestamp(SessionKey key);
42 
43     /**
44      * Returns the time the associated {@code Session} last interacted with the system.
45      *
46      * @param key the session key to use to look up the target session.
47      * @return time the session last accessed the system
48      * @see hunt.shiro.session.Session#getLastAccessTime()
49      * @see hunt.shiro.session.Session#touch()
50      */
51     Date getLastAccessTime(SessionKey key);
52 
53     /**
54      * Returns {@code true} if the associated session is valid (it exists and is not stopped nor expired),
55      * {@code false} otherwise.
56      *
57      * @param key the session key to use to look up the target session.
58      * @return {@code true} if the session is valid (exists and is not stopped or expired), {@code false} otherwise.
59      */
60     bool isValid(SessionKey key);
61 
62     /**
63      * Returns quietly if the associated session is valid (it exists and is not stopped or expired) or throws
64      * an {@link hunt.shiro.session.InvalidSessionException} indicating that the session id is invalid.  This
65      * might be preferred to be used instead of {@link #isValid} since any exception thrown will definitively explain
66      * the reason for invalidation.
67      *
68      * @param key the session key to use to look up the target session.
69      * @throws hunt.shiro.session.InvalidSessionException
70      *          if the session id is invalid (it does not exist or it is stopped or expired).
71      */
72     void checkValid(SessionKey key);
73 
74     /**
75      * Returns the time in milliseconds that the associated session may remain idle before expiring.
76      * <ul>
77      * <li>A negative return value means the session will never expire.</li>
78      * <li>A non-negative return value (0 or greater) means the session expiration will occur if idle for that
79      * length of time.</li>
80      * </ul>
81      *
82      * @param key the session key to use to look up the target session.
83      * @return the time in milliseconds that the associated session may remain idle before expiring.
84      * @throws hunt.shiro.session.InvalidSessionException
85      *          if the session has been stopped or expired prior to calling this method.
86      */
87     long getTimeout(SessionKey key);
88 
89     /**
90      * Sets the time in milliseconds that the associated session may remain idle before expiring.
91      * <ul>
92      * <li>A negative return value means the session will never expire.</li>
93      * <li>A non-negative return value (0 or greater) means the session expiration will occur if idle for that
94      * length of time.</li>
95      * </ul>
96      *
97      * @param key                 the session key to use to look up the target session.
98      * @param maxIdleTimeInMillis the time in milliseconds that the associated session may remain idle before expiring.
99      * @throws hunt.shiro.session.InvalidSessionException
100      *          if the session has been stopped or expired prior to calling this method.
101      */
102     void setTimeout(SessionKey key, long maxIdleTimeInMillis);
103 
104     /**
105      * Updates the last accessed time of the session identified by <code>sessionId</code>.  This
106      * can be used to explicitly ensure that a session does not time out.
107      *
108      * @param key the session key to use to look up the target session.
109      * @throws hunt.shiro.session.InvalidSessionException
110      *          if the session has been stopped or expired prior to calling this method.
111      * @see hunt.shiro.session.Session#touch
112      */
113     void touch(SessionKey key);
114 
115     /**
116      * Returns the host name or IP string of the host where the session was started, if known.  If
117      * no host name or IP was specified when starting the session, this method returns {@code null}
118      *
119      * @param key the session key to use to look up the target session.
120      * @return the host name or ip address of the host where the session originated, if known.  If unknown,
121      *         this method returns {@code null}.
122      */
123     string getHost(SessionKey key);
124 
125     /**
126      * Explicitly stops the associated session, thereby releasing all of its resources.
127      *
128      * @param key the session key to use to look up the target session.
129      * @throws InvalidSessionException if the session has stopped or expired prior to calling this method.
130      * @see hunt.shiro.session.Session#stop
131      */
132     void stop(SessionKey key);
133 
134     /**
135      * Returns all attribute keys maintained by the target session or an empty collection if there are no attributes.
136      *
137      * @param sessionKey the session key to use to look up the target session.
138      * @return all attribute keys maintained by the target session or an empty collection if there are no attributes.
139      * @throws InvalidSessionException if the associated session has stopped or expired prior to calling this method.
140      * @see hunt.shiro.session.Session#getAttributeKeys()
141      */
142     Object[] getAttributeKeys(SessionKey sessionKey);
143 
144     /**
145      * Returns the object bound to the associated session identified by the specified attribute key.  If there
146      * is no object bound under the attribute key for the given session, {@code null} is returned.
147      *
148      * @param sessionKey   session key to use to look up the target session.
149      * @param attributeKey the unique name of the object bound to the associated session
150      * @return the object bound under the {@code attributeKey} or {@code null} if there is no object bound.
151      * @throws InvalidSessionException if the specified session has stopped or expired prior to calling this method.
152      * @see hunt.shiro.session.Session#getAttribute(Object key)
153      */
154     Object getAttribute(SessionKey sessionKey, Object attributeKey);
155 
156     /**
157      * Binds the specified {@code value} to the associated session uniquely identified by the {@code attributeKey}.
158      * If there is already a session attribute bound under the {@code attributeKey}, that existing object will be
159      * replaced by the new {@code value}.
160      * <p/>
161      * If the {@code value} parameter is null, it has the same effect as if the
162      * {@link #removeAttribute(SessionKey sessionKey, Object attributeKey)} method was called.
163      *
164      * @param sessionKey   the session key to use to look up the target session.
165      * @param attributeKey the key under which the {@code value} object will be bound in this session
166      * @param value        the object to bind in this session.
167      * @throws InvalidSessionException if the specified session has stopped or expired prior to calling this method.
168      * @see hunt.shiro.session.Session#setAttribute(Object key, Object value)
169      */
170     void setAttribute(SessionKey sessionKey, Object attributeKey, Object value);
171 
172     /**
173      * Removes (unbinds) the object bound to associated {@code Session} under the given {@code attributeKey}.
174      *
175      * @param sessionKey   session key to use to look up the target session.
176      * @param attributeKey the key uniquely identifying the object to remove
177      * @return the object removed or {@code null} if there was no object bound under the specified {@code attributeKey}.
178      * @throws InvalidSessionException if the specified session has stopped or expired prior to calling this method.
179      * @see hunt.shiro.session.Session#removeAttribute(Object key)
180      */
181     Object removeAttribute(SessionKey sessionKey, Object attributeKey);
182 
183 }