Search in sources :

Example 1 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class GridFsConsumer method run.

@Override
public void run() {
    DBCursor c = null;
    java.util.Date fromDate = null;
    QueryStrategy s = endpoint.getQueryStrategy();
    boolean usesTimestamp = s != QueryStrategy.FileAttribute;
    boolean persistsTimestamp = s == QueryStrategy.PersistentTimestamp || s == QueryStrategy.PersistentTimestampAndFileAttribute;
    boolean usesAttribute = s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute || s == QueryStrategy.PersistentTimestampAndFileAttribute;
    DBCollection ptsCollection = null;
    DBObject persistentTimestamp = null;
    if (persistsTimestamp) {
        ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
        // ensure standard indexes as long as collections are small
        try {
            if (ptsCollection.count() < 1000) {
                ptsCollection.createIndex(new BasicDBObject("id", 1));
            }
        } catch (MongoException e) {
        //TODO: Logging
        }
        persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
        if (persistentTimestamp == null) {
            persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
            fromDate = new java.util.Date();
            persistentTimestamp.put("timestamp", fromDate);
            ptsCollection.save(persistentTimestamp);
        }
        fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
    } else if (usesTimestamp) {
        fromDate = new java.util.Date();
    }
    try {
        Thread.sleep(endpoint.getInitialDelay());
        while (isStarted()) {
            if (c == null || c.getCursorId() == 0) {
                if (c != null) {
                    c.close();
                }
                String queryString = endpoint.getQuery();
                DBObject query;
                if (queryString == null) {
                    query = new BasicDBObject();
                } else {
                    query = (DBObject) JSON.parse(queryString);
                }
                if (usesTimestamp) {
                    query.put("uploadDate", new BasicDBObject("$gt", fromDate));
                }
                if (usesAttribute) {
                    query.put(endpoint.getFileAttributeName(), null);
                }
                c = endpoint.getFilesCollection().find(query);
            }
            boolean dateModified = false;
            while (c.hasNext() && isStarted()) {
                GridFSDBFile file = (GridFSDBFile) c.next();
                GridFSDBFile forig = file;
                if (usesAttribute) {
                    file.put(endpoint.getFileAttributeName(), "processing");
                    DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null).get();
                    forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false, file, true, false);
                }
                if (forig != null) {
                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));
                    Exchange exchange = endpoint.createExchange();
                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
                    try {
                        getProcessor().process(exchange);
                        //System.out.println("Processing " + file.getFilename());
                        if (usesAttribute) {
                            forig.put(endpoint.getFileAttributeName(), "done");
                            endpoint.getFilesCollection().save(forig);
                        }
                        if (usesTimestamp) {
                            if (file.getUploadDate().compareTo(fromDate) > 0) {
                                fromDate = file.getUploadDate();
                                dateModified = true;
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (persistsTimestamp && dateModified) {
                persistentTimestamp.put("timestamp", fromDate);
                ptsCollection.save(persistentTimestamp);
            }
            Thread.sleep(endpoint.getDelay());
        }
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (c != null) {
        c.close();
    }
}
Also used : MongoException(com.mongodb.MongoException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) MongoException(com.mongodb.MongoException) Exchange(org.apache.camel.Exchange) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile)

Example 2 with DBObject

use of com.mongodb.DBObject in project jetty.project by eclipse.

the class MongoSessionDataStore method getNestedValue.

/*------------------------------------------------------------ */
/**
     * Dig through a given dbObject for the nested value
     */
private Object getNestedValue(DBObject dbObject, String nestedKey) {
    String[] keyChain = nestedKey.split("\\.");
    DBObject temp = dbObject;
    for (int i = 0; i < keyChain.length - 1; ++i) {
        temp = (DBObject) temp.get(keyChain[i]);
        if (temp == null) {
            return null;
        }
    }
    return temp.get(keyChain[keyChain.length - 1]);
}
Also used : DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 3 with DBObject

use of com.mongodb.DBObject 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 4 with DBObject

use of com.mongodb.DBObject 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 5 with DBObject

use of com.mongodb.DBObject 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

DBObject (com.mongodb.DBObject)545 BasicDBObject (com.mongodb.BasicDBObject)386 Test (org.junit.Test)214 DBCollection (com.mongodb.DBCollection)83 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)54 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)52 ApiOperation (io.swagger.annotations.ApiOperation)47 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)46 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)41 DBCursor (com.mongodb.DBCursor)40 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)38 List (java.util.List)31 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)31 Map (java.util.Map)26 ObjectId (org.bson.types.ObjectId)26 BasicDBList (com.mongodb.BasicDBList)24 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 BSONObject (org.bson.BSONObject)19 MongoException (com.mongodb.MongoException)18