Example usage for com.mongodb.gridfs GridFS GridFS

List of usage examples for com.mongodb.gridfs GridFS GridFS

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS GridFS.

Prototype

public GridFS(final DB db, final String bucket) 

Source Link

Document

Creates a GridFS instance for the specified bucket in the given database.

Usage

From source file:org.exist.mongodb.xquery.gridfs.Stream.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {//w  w w .  j a va 2s.c o m
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String bucket = args[2].itemAt(0).getStringValue();
        String documentId = args[3].itemAt(0).getStringValue();
        Boolean setDisposition = args[4].itemAt(0).toJavaObject(Boolean.class);

        // Get database
        DB db = client.getDB(dbname);

        // Creates a GridFS instance for the specified bucket
        GridFS gfs = new GridFS(db, bucket);

        // Find one document by id or by filename
        GridFSDBFile gfsFile = (isCalledAs(FIND_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId))
                : gfs.findOne(documentId);

        stream(gfsFile, documentId, setDisposition);

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage());
    }

    return Sequence.EMPTY_SEQUENCE;

}

From source file:org.i3xx.step.mongo.core.impl.FileStoreImpl.java

License:Apache License

public FileStoreImpl(DB dbs, String bucket) {
    fs = new GridFS(dbs, bucket);
}

From source file:org.mule.transport.mongodb.MongoDBMessageDispatcher.java

License:Open Source License

protected Object doDispatchToBucket(MuleEvent event, String bucket) throws Exception {
    DB db;/*  w ww.j  a va 2s  .  com*/

    if (StringUtils.isNotBlank(connector.getMongoURI().getDatabase())) {
        db = connector.getMongo().getDB(connector.getMongoURI().getDatabase());

    } else {
        db = connector.getMongo().getDB(connector.getMongoURI().getDatabase());
    }

    GridFS gridFS = new GridFS(db, bucket);

    db.requestStart();

    Object payload = event.getMessage().getPayload();

    GridFSInputFile file = null;

    if (payload instanceof File) {
        file = gridFS.createFile((File) payload);
    }

    if (payload instanceof InputStream) {
        file = gridFS.createFile((InputStream) payload);
    }

    if (payload instanceof byte[]) {
        file = gridFS.createFile((byte[]) payload);
    }

    if (payload instanceof String) {
        file = gridFS.createFile(((String) payload).getBytes());
    }

    if (file == null) {
        throw new MongoDBException(
                String.format("Cannot persist objects of type %s to GridFS", payload.getClass()));
    }

    String filename = event.getMessage().getOutboundProperty(MongoDBConnector.PROPERTY_FILENAME, "");

    if (StringUtils.isNotBlank(filename)) {
        logger.debug("Setting filename on GridFS file to: " + filename);
        file.setFilename(filename);
    }

    String contentType = event.getMessage().getOutboundProperty(MongoDBConnector.PROPERTY_CONTENT_TYPE, "");

    if (StringUtils.isBlank(contentType) && event.getEndpoint().getProperties().containsKey("contentType")) {
        contentType = (String) event.getEndpoint().getProperty("contentType");
    }

    if (StringUtils.isNotBlank(contentType)) {
        logger.debug("Setting contentType on GridFS file to: " + contentType);
        file.setContentType(contentType);
    }

    logger.debug("Attempting to save file: " + file.getFilename());

    Date startTime = new Date();
    file.save();
    Date endTime = new Date();

    long elapsed = endTime.getTime() - startTime.getTime();

    logger.debug(String.format("GridFS file %s saved in %s seconds", file.getId(), elapsed / 1000.0));

    try {
        file.validate();
    } catch (MongoException ex) {
        if (ex.getMessage().startsWith("md5 differ")) {
            logger.error("MD5 checksum mismatch while saving the file. "
                    + "This may be the real deal or is possibly an issue that keeps recurring with"
                    + " some releases of the Java driver ");
        }
    }

    ObjectId id = (ObjectId) file.getId();
    event.getMessage().setOutboundProperty(MongoDBConnector.PROPERTY_OBJECT_ID, id.toStringMongod());

    db.requestDone();

    return file;
}

From source file:org.mule.transport.mongodb.MongoDBMessageReceiver.java

License:Open Source License

void pollBucket(String bucket) throws Exception {

    MongoDBConnector mongoConnector = (MongoDBConnector) connector;

    DB db;/*from  w w  w. ja  v  a2  s  .co m*/

    if (StringUtils.isNotBlank(mongoConnector.getMongoURI().getDatabase())) {
        db = mongoConnector.getMongo().getDB(mongoConnector.getMongoURI().getDatabase());

    } else {
        db = mongoConnector.getMongo().getDB(mongoConnector.getMongoURI().getDatabase());
    }

    db.requestStart();

    GridFS gridFS = new GridFS(db, bucket);

    DBObject query = (DBObject) JSON.parse((String) endpoint.getProperty("query"));

    List<GridFSDBFile> results = gridFS.find(query);

    logger.debug(String.format("Query %s on bucket %s returned %d results", query, bucket, results.size()));

    db.requestDone();

    routeMessage(createMuleMessage(results));
}

From source file:org.nuxeo.ecm.core.storage.mongodb.GridFSBinaryManager.java

License:Apache License

@Override
public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
    super.initialize(blobProviderId, properties);
    this.properties = properties;
    String server = properties.get(SERVER_PROPERTY);
    if (StringUtils.isBlank(server)) {
        throw new NuxeoException(
                "Missing server property in GridFS Binary Manager descriptor: " + blobProviderId);
    }/*  w  ww.  j  a v a  2  s . co  m*/
    String dbname = properties.get(DBNAME_PROPERTY);
    if (StringUtils.isBlank(dbname)) {
        throw new NuxeoException(
                "Missing dbname property in GridFS Binary Manager descriptor: " + blobProviderId);
    }
    String bucket = properties.get(BUCKET_PROPERTY);
    if (StringUtils.isBlank(bucket)) {
        bucket = blobProviderId + ".fs";
    }
    if (server.startsWith("mongodb://")) {
        client = new MongoClient(new MongoClientURI(server));
    } else {
        client = new MongoClient(new ServerAddress(server));
    }
    gridFS = new GridFS(client.getDB(dbname), bucket);
    garbageCollector = new GridFSBinaryGarbageCollector();
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public FileStorage.FileReadBean read(Id id) throws NotFoundException {
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    GridFSDBFile o = fs.findOne(id.getEntity());
    if (o == null)
        throw new NotFoundException("GridFS file not found: " + id);
    FileStorage.FileMetaBean fb = new FileStorage.FileMetaBean();
    fb.fromMap(MongoDBFormat.toMap(o.getMetaData()));
    fb.setSize(o.getLength());/*from  w w w.  j  a va2s .  c o  m*/
    fb.setContentType(o.getContentType());
    if (LOG.isDebugEnabled())
        LOG.debug("Read file: " + id + ", meta:" + fb.toMap());
    return new FileStorage.FileReadBean(o.getInputStream(), fb);
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public FileStorage.FileWriteBean createFileWriteBean(Id id, FileStorage.FileMetaBean meta) {
    meta.setLastModified(new Date());
    meta.setCreated(new Date());
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    fs.remove(id.getEntity());/*from   w  w  w .  j a  v  a  2 s.c  o m*/

    GridFSInputFile gfsFile = fs.createFile(id.getEntity());
    gfsFile.setContentType(meta.getContentType());
    gfsFile.setMetaData(MongoDBFormat.fromMap(meta.toMap()));

    GridFSFileWriteBean gridFSFileWriteBean = new GridFSFileWriteBean(id, gfsFile.getOutputStream(), meta);
    gridFSFileWriteBean.gfsFile = gfsFile;
    return gridFSFileWriteBean;
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public void remove(Id id) {
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    fs.remove(id.getEntity());/* w  w w . j  a  v a  2 s . c  o  m*/
    if (LOG.isDebugEnabled())
        LOG.debug("File removed successfully:" + id);
}

From source file:org.sakaiproject.nakamura.lite.storage.mongo.GridFSContentHelper.java

License:Apache License

public GridFSContentHelper(DB mongodb, RowHasher rowHasher, Map<String, Object> properties) {
    this.rowHasher = rowHasher;

    String bucket = StorageClientUtils.getSetting(properties.get(MongoClientPool.PROP_BUCKET),
            GridFS.DEFAULT_BUCKET);//from  w  w w  .  j  av a 2s . com
    this.contentBodies = new GridFS(mongodb, bucket);
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

public ROServicesImpl() {
    db = MongoDB.getServicesDB();/*from   w w  w.ja  v  a  2s  .c o m*/
    metaDb = MongoDB.geMetaGenDB();
    oreDb = MongoDB.geOreDB();
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    repositoriesCollection = db.getCollection(MongoDB.repositories);
    oreMapCollection = metaDb.getCollection(MongoDB.oreMap);
    oreMapBucket = new GridFS(oreDb, MongoDB.oreMap);
    fgdcCollection = metaDb.getCollection(MongoDB.fgdc);
    control.setNoCache(true);
}