diff options
author | Simon Rettberg | 2015-06-23 19:10:48 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-06-23 19:10:48 +0200 |
commit | 8ad025dd99468f71d2fa5c49e0bcf359b055ec97 (patch) | |
tree | 6208ff7d598116e36b21882e5a018a99d2506103 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java | |
parent | fix bwLehrpool logo not beeing read from the resources (diff) | |
download | tutor-module-8ad025dd99468f71d2fa5c49e0bcf359b055ec97.tar.gz tutor-module-8ad025dd99468f71d2fa5c49e0bcf359b055ec97.tar.xz tutor-module-8ad025dd99468f71d2fa5c49e0bcf359b055ec97.zip |
[server] More methods implemented
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java | 66 |
1 files changed, 52 insertions, 14 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java index bf444a20..3fdadfb1 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/SessionManager.java @@ -1,12 +1,19 @@ package org.openslx.bwlp.sat.thrift; +import java.sql.SQLException; +import java.util.Iterator; import java.util.Map; +import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; +import org.openslx.bwlp.sat.database.mappers.DbUser; +import org.openslx.bwlp.sat.permissions.User; +import org.openslx.bwlp.sat.util.QuickTimer; import org.openslx.bwlp.thrift.iface.AuthorizationError; import org.openslx.bwlp.thrift.iface.TAuthorizationException; +import org.openslx.bwlp.thrift.iface.TInvalidTokenException; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.thrifthelper.ThriftManager; @@ -31,10 +38,29 @@ public class SessionManager { public void touch(long now) { this.validUntil = now + SESSION_TIMEOUT; } + + public boolean isTooOld(long now) { + return validUntil < now; + } } // saves the current tokens and the mapped userdata, returning from the server - private static Map<String, Entry> tokenManager = new ConcurrentHashMap<>(); + private static final Map<String, Entry> tokenManager = new ConcurrentHashMap<>(); + + static { + // Clean cached session periodically + QuickTimer.scheduleAtFixedDelay(new TimerTask() { + @Override + public void run() { + final long now = System.currentTimeMillis(); + for (Iterator<Entry> it = tokenManager.values().iterator(); it.hasNext();) { + Entry e = it.next(); + if (e == null || e.isTooOld(now)) + it.remove(); + } + } + }, 60000, 1200600); + } /** * Get the user corresponding to the given token. @@ -61,17 +87,16 @@ public class SessionManager { */ 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; + if (e == null) + return getRemote(token); + // User session already cached + final long now = System.currentTimeMillis(); + if (e.isTooOld(now)) { + tokenManager.remove(token); + return getRemote(token); } - return getRemote(token); + e.touch(now); + return e.user; } /** @@ -83,19 +108,32 @@ public class SessionManager { tokenManager.remove(token); } + /** + * Get {@link UserInfo} from master server. + * + * @param token token of user to get + * @return + */ private static UserInfo getRemote(String token) { UserInfo ui = null; try { ui = ThriftManager.getMasterClient().getUserFromToken(token); + } catch (TInvalidTokenException ite) { + return null; // Token not known to master server } catch (Exception e) { LOGGER.warn("Could not reach master server to query for user token of a client!", e); } - if (ui == null) + // Valid reply, check if user is allowed to communicate with this satellite server + if (!User.canLogin(ui)) + return null; + // Is valid, insert/update db record + try { + DbUser.writeUserOnLogin(ui); + } catch (SQLException e) { return null; + } tokenManager.put(token, new Entry(ui)); return ui; } - // TODO: Clean map of old entries periodically - } |