use of com.mongodb.BasicDBObject in project jetty.project by eclipse.
the class SessionExpiryTest method getSessionMaxInactiveInterval.
public long getSessionMaxInactiveInterval(DBCollection sessions, String id) throws Exception {
assertNotNull(sessions);
assertNotNull(id);
DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID, id));
assertNotNull(o);
Long inactiveInterval = (Long) o.get(MongoSessionDataStore.__MAX_IDLE);
return (inactiveInterval == null ? null : inactiveInterval.longValue());
}
use of com.mongodb.BasicDBObject in project jetty.project by eclipse.
the class MongoSessionDataStore method doStart.
@Override
protected void doStart() throws Exception {
if (_dbSessions == null)
throw new IllegalStateException("DBCollection not set");
_version_1 = new BasicDBObject(getContextSubfield(__VERSION), 1);
ensureIndexes();
super.doStart();
}
use of com.mongodb.BasicDBObject in project jetty.project by eclipse.
the class MongoSessionDataStore method load.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#load(String)
*/
@Override
public SessionData load(String id) throws Exception {
final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
Runnable r = new Runnable() {
public void run() {
try {
DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id));
if (LOG.isDebugEnabled())
LOG.debug("id={} loaded={}", id, sessionDocument);
if (sessionDocument == null)
return;
Boolean valid = (Boolean) sessionDocument.get(__VALID);
if (LOG.isDebugEnabled())
LOG.debug("id={} valid={}", id, valid);
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);
NoSqlSessionData data = null;
// get the session for the context
DBObject sessionSubDocumentForContext = (DBObject) getNestedValue(sessionDocument, getContextField());
if (LOG.isDebugEnabled())
LOG.debug("attrs {}", sessionSubDocumentForContext);
if (sessionSubDocumentForContext != null) {
if (LOG.isDebugEnabled())
LOG.debug("Session {} present for context {}", id, _context);
//only load a session if it exists for this context
data = (NoSqlSessionData) newSessionData(id, created, accessed, accessed, maxInactive);
data.setVersion(version);
data.setExpiry(expiry);
data.setContextPath(_context.getCanonicalContextPath());
data.setVhost(_context.getVhost());
data.setLastSaved(lastSaved);
data.setLastNode(lastNode);
HashMap<String, Object> attributes = new HashMap<>();
for (String name : sessionSubDocumentForContext.keySet()) {
//skip special metadata attribute which is not one of the actual session attributes
if (__METADATA.equals(name))
continue;
String attr = decodeName(name);
Object value = decodeValue(sessionSubDocumentForContext.get(name));
attributes.put(attr, value);
}
data.putAllAttributes(attributes);
} else {
if (LOG.isDebugEnabled())
LOG.debug("Session {} not present for context {}", id, _context);
}
reference.set(data);
} catch (Exception e) {
exception.set(e);
}
}
};
_context.run(r);
if (exception.get() != null)
throw exception.get();
return reference.get();
}
use of com.mongodb.BasicDBObject in project jetty.project by eclipse.
the class MongoSessionDataStore method exists.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String)
*/
@Override
public boolean exists(String id) throws Exception {
DBObject fields = new BasicDBObject();
fields.put(__EXPIRY, 1);
fields.put(__VALID, 1);
DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id), fields);
if (sessionDocument == null)
//doesn't exist
return false;
Boolean valid = (Boolean) sessionDocument.get(__VALID);
if (!valid)
//invalid - nb should not happen
return false;
Long expiry = (Long) sessionDocument.get(__EXPIRY);
if (expiry.longValue() <= 0)
//never expires, its good
return true;
//expires later
return (expiry.longValue() > System.currentTimeMillis());
}
use of com.mongodb.BasicDBObject in project jetty.project by eclipse.
the class MongoSessionDataStore method delete.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#delete(String)
*/
@Override
public boolean delete(String id) throws Exception {
if (LOG.isDebugEnabled())
LOG.debug("Remove:session {} for context ", id, _context);
/*
* Check if the session exists and if it does remove the context
* associated with this session
*/
BasicDBObject mongoKey = new BasicDBObject(__ID, id);
//DBObject sessionDocument = _dbSessions.findOne(mongoKey,_version_1);
DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id));
if (sessionDocument != null) {
DBObject c = (DBObject) getNestedValue(sessionDocument, __CONTEXT);
if (c == null) {
//delete whole doc
_dbSessions.remove(mongoKey, WriteConcern.SAFE);
return false;
}
Set<String> contexts = c.keySet();
if (contexts.isEmpty()) {
//delete whole doc
_dbSessions.remove(mongoKey, WriteConcern.SAFE);
return false;
}
if (contexts.size() == 1 && contexts.iterator().next().equals(getCanonicalContextId())) {
//delete whole doc
_dbSessions.remove(new BasicDBObject(__ID, id), WriteConcern.SAFE);
return true;
}
//just remove entry for my context
BasicDBObject remove = new BasicDBObject();
BasicDBObject unsets = new BasicDBObject();
unsets.put(getContextField(), 1);
remove.put("$unset", unsets);
WriteResult result = _dbSessions.update(mongoKey, remove, false, false, WriteConcern.SAFE);
return true;
} else {
return false;
}
}
Aggregations