diff options
| author | Simon Rettberg | 2015-06-11 18:40:49 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2015-06-11 18:40:49 +0200 |
| commit | e0005ceecfd9281230c4add7575b18ee88307774 (patch) | |
| tree | a73bbcfc213df478c701aac120ae2b7c6e52bb1b /dozentenmodulserver/src/main/java/server/SessionManager.java | |
| parent | [server] db stuff, new interface, ... (diff) | |
| download | tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.gz tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.tar.xz tutor-module-e0005ceecfd9281230c4add7575b18ee88307774.zip | |
[server] On mah way (lots of restructuring, some early db classes, sql dump of current schema)
Diffstat (limited to 'dozentenmodulserver/src/main/java/server/SessionManager.java')
| -rw-r--r-- | dozentenmodulserver/src/main/java/server/SessionManager.java | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/dozentenmodulserver/src/main/java/server/SessionManager.java b/dozentenmodulserver/src/main/java/server/SessionManager.java deleted file mode 100644 index 3f4d4257..00000000 --- a/dozentenmodulserver/src/main/java/server/SessionManager.java +++ /dev/null @@ -1,101 +0,0 @@ -package server; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; - -import org.apache.log4j.Logger; -import org.openslx.bwlp.thrift.iface.AuthorizationError; -import org.openslx.bwlp.thrift.iface.TAuthorizationException; -import org.openslx.bwlp.thrift.iface.UserInfo; -import org.openslx.thrifthelper.ThriftManager; - -/** - * Manages user sessions. Mainly used to map tokens to users. - * - */ -public class SessionManager { - - private static final Logger LOGGER = Logger.getLogger(SessionManager.class); - - private static class Entry { - private static final long SESSION_TIMEOUT = TimeUnit.DAYS.toMillis(1); - private final UserInfo user; - private long validUntil; - - private Entry(UserInfo user) { - this.user = user; - this.validUntil = System.currentTimeMillis() + SESSION_TIMEOUT; - } - - public void touch(long now) { - this.validUntil = now + SESSION_TIMEOUT; - } - } - - // saves the current tokens and the mapped userdata, returning from the server - private static Map<String, Entry> tokenManager = new ConcurrentHashMap<>(); - - /** - * Get the user corresponding to the given token. - * - * @param token user's token - * @return UserInfo for the matching user - * @throws TAuthorizationException if the token is not known or the session - * expired - */ - public static UserInfo getOrFail(String token) throws TAuthorizationException { - UserInfo ui = get(token); - if (ui != null) - return ui; - throw new TAuthorizationException(AuthorizationError.NOT_AUTHENTICATED, - "Your session token is not known to the server"); - } - - /** - * Get the user corresponding to the given token. Returns null if the token - * is not known, or the session already timed out. - * - * @param token user's token - * @return UserInfo for the matching user - */ - public static UserInfo get(String token) { - Entry e = tokenManager.get(token); - if (e != null) { - // User session already cached - final long now = System.currentTimeMillis(); - if (e.validUntil < now) { - tokenManager.remove(token); - return getRemote(token); - } - e.touch(now); - return e.user; - } - return getRemote(token); - } - - /** - * Remove session matching the given token - * - * @param token - */ - public static void remove(String token) { - tokenManager.remove(token); - } - - private static UserInfo getRemote(String token) { - UserInfo ui = null; - try { - ui = ThriftManager.getMasterClient().getUserFromToken(token); - } catch (Exception e) { - LOGGER.warn("Could not reach master server to query for user token of a client!", e); - } - if (ui == null) - return null; - tokenManager.put(token, new Entry(ui)); - return ui; - } - - // TODO: Clean map of old entries periodically - -} |
