Search in sources :

Example 1 with BasicDBObject

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

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

the class MongoDbEndpoint method ensureIndex.

/**
     * Add Index
     *
     * @param collection
     */
public void ensureIndex(MongoCollection<BasicDBObject> collection, List<BasicDBObject> dynamicIndex) {
    if (dynamicIndex != null && !dynamicIndex.isEmpty()) {
        for (BasicDBObject index : dynamicIndex) {
            LOG.debug("create BDObject Index {}", index);
            collection.createIndex(index);
        }
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject)

Example 3 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 4 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 5 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)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)497 DBObject (com.mongodb.DBObject)289 Test (org.junit.Test)166 DBCollection (com.mongodb.DBCollection)72 ArrayList (java.util.ArrayList)48 ObjectId (org.bson.types.ObjectId)40 DBCursor (com.mongodb.DBCursor)38 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)38 ApiOperation (io.swagger.annotations.ApiOperation)37 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 HashMap (java.util.HashMap)32 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)30 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)29 BasicDBList (com.mongodb.BasicDBList)28 List (java.util.List)24 Map (java.util.Map)23 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)18 IOException (java.io.IOException)15