Search in sources :

Example 36 with BasicDBObject

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());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 37 with BasicDBObject

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();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject)

Example 38 with BasicDBObject

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();
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SessionData(org.eclipse.jetty.server.session.SessionData) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 39 with BasicDBObject

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());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 40 with BasicDBObject

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;
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)566 DBObject (com.mongodb.DBObject)338 Test (org.junit.Test)175 DBCollection (com.mongodb.DBCollection)77 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)68 ApiOperation (io.swagger.annotations.ApiOperation)65 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)64 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)54 ArrayList (java.util.ArrayList)53 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)52 ObjectId (org.bson.types.ObjectId)43 DBCursor (com.mongodb.DBCursor)41 HashMap (java.util.HashMap)35 BasicDBList (com.mongodb.BasicDBList)32 List (java.util.List)30 Map (java.util.Map)26 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)18