From 5bc7882190fe56a675aa1796440389f4509b4b2d Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 16 Mar 2017 14:27:48 +1100 Subject: [PATCH] Issue #1398 --- .../session/GCloudSessionDataStore.java | 32 +++++++++++++++++++ .../nosql/mongodb/MongoSessionDataStore.java | 21 +++++++++--- .../server/session/FileSessionDataStore.java | 1 + 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java b/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java index 7e6179b52ea..98bfe17dddf 100644 --- a/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java +++ b/jetty-gcloud/jetty-gcloud-session-manager/src/main/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStore.java @@ -98,6 +98,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore public static final String EXPIRY = "expiry"; public static final String MAXINACTIVE = "maxInactive"; public static final String ATTRIBUTES = "attributes"; + public static final String LASTSAVED = "lastSaved"; public static final String KIND = "GCloudSession"; protected String _kind = KIND; @@ -107,6 +108,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore protected String _accessed = ACCESSED; protected String _lastAccessed = LASTACCESSED; protected String _lastNode = LASTNODE; + protected String _lastSaved = LASTSAVED; protected String _createTime = CREATETIME; protected String _cookieSetTime = COOKIESETTIME; protected String _expiry = EXPIRY; @@ -302,6 +304,23 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore _attributes = attributes; } + /** + * @return the lastSaved + */ + public String getLastSaved() + { + return _lastSaved; + } + + /** + * @param lastSaved the lastSaved to set + */ + public void setLastSaved(String lastSaved) + { + checkNotNull(lastSaved); + _lastSaved = lastSaved; + } + /** * @see java.lang.Object#toString() */ @@ -868,6 +887,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore .set(_model.getLastNode(),session.getLastNode()) .set(_model.getExpiry(), session.getExpiry()) .set(_model.getMaxInactive(), session.getMaxInactiveMs()) + .set(_model.getLastSaved(), session.getLastSaved()) .set(_model.getAttributes(), BlobValue.newBuilder(Blob.copyFrom(baos.toByteArray())).setExcludeFromIndexes(true).build()).build(); @@ -902,6 +922,17 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore long createTime = entity.getLong(_model.getCreateTime()); long cookieSet = entity.getLong(_model.getCookieSetTime()); String lastNode = entity.getString(_model.getLastNode()); + + long lastSaved = 0; + //for compatibility with previously saved sessions, lastSaved may not be present + try + { + lastSaved = entity.getLong(_model.getLastSaved()); + } + catch (DatastoreException e) + { + LOG.ignore(e); + } long expiry = entity.getLong(_model.getExpiry()); long maxInactive = entity.getLong(_model.getMaxInactive()); Blob blob = (Blob) entity.getBlob(_model.getAttributes()); @@ -912,6 +943,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore session.setVhost(vhost); session.setCookieSet(cookieSet); session.setLastNode(lastNode); + session.setLastSaved(lastSaved); session.setExpiry(expiry); try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) { diff --git a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java index 7e6f8725447..9ed9e279170 100644 --- a/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java +++ b/jetty-nosql/src/main/java/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.java @@ -117,6 +117,12 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore */ public final static String __VERSION = __METADATA + ".version"; + + public final static String __LASTSAVED = __METADATA + ".lastSaved"; + + + public final static String __LASTNODE = __METADATA + ".lastNode"; + /** * Last access time of session */ @@ -202,14 +208,15 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore if (valid == null || !valid) return; - Object version = getNestedValue(sessionDocument, getContextSubfield(__VERSION)); + Long lastSaved = (Long)getNestedValue(sessionDocument, getContextSubfield(__LASTSAVED)); + String lastNode = (String)getNestedValue(sessionDocument, getContextSubfield(__LASTNODE)); Long created = (Long)sessionDocument.get(__CREATED); Long accessed = (Long)sessionDocument.get(__ACCESSED); Long maxInactive = (Long)sessionDocument.get(__MAX_IDLE); - Long expiry = (Long)sessionDocument.get(__EXPIRY); - + Long expiry = (Long)sessionDocument.get(__EXPIRY); + NoSqlSessionData data = null; // get the session for the context @@ -228,6 +235,8 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore data.setExpiry(expiry); data.setContextPath(_context.getCanonicalContextPath()); data.setVhost(_context.getVhost()); + data.setLastSaved(lastSaved); + data.setLastNode(lastNode); HashMap attributes = new HashMap<>(); for (String name : sessionSubDocumentForContext.keySet()) @@ -427,7 +436,7 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore */ @Override public void doStore(String id, SessionData data, long lastSaveTime) throws Exception - { + { NoSqlSessionData nsqd = (NoSqlSessionData)data; // Form query for upsert @@ -449,12 +458,16 @@ public class MongoSessionDataStore extends NoSqlSessionDataStore sets.put(__CREATED,nsqd.getCreated()); sets.put(__VALID,true); sets.put(getContextSubfield(__VERSION),version); + sets.put(getContextSubfield(__LASTSAVED), data.getLastSaved()); + sets.put(getContextSubfield(__LASTNODE), data.getLastNode()); sets.put(__MAX_IDLE, nsqd.getMaxInactiveMs()); sets.put(__EXPIRY, nsqd.getExpiry()); nsqd.setVersion(version); } else { + sets.put(getContextSubfield(__LASTSAVED), data.getLastSaved()); + sets.put(getContextSubfield(__LASTNODE), data.getLastNode()); version = new Long(((Number)version).longValue() + 1); nsqd.setVersion(version); update.put("$inc",_version_1); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java index 74c2f2bff5f..4642894dcf2 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/FileSessionDataStore.java @@ -196,6 +196,7 @@ public class FileSessionDataStore extends AbstractSessionDataStore try (FileInputStream in = new FileInputStream(file)) { SessionData data = load(in); + data.setLastSaved(file.lastModified()); //delete restored file file.delete(); reference.set(data);