Package com.mongodb.gridfs

Examples of com.mongodb.gridfs.GridFS


  public FileDAO(DB db, String bucket)
  {
    this.db = db;
    this.bucket = bucket;
    this.chunkSize = DEFAULT_CHUNK_SIZE_BYTES;
    fs = new GridFS(db, bucket);
  }
View Full Code Here


{
  public static void main(String args[]) throws IOException, DBException
  {
    DB db = DBUtils.connectDB("localhost", 27017, "test");

    GridFS fs = new GridFS(db);
   
    GridFSInputFile inputFile = fs.createFile(new File("./foo.txt"));
    System.out.println("GridFS file: "+inputFile.getFilename());


  }
View Full Code Here

            String errorCode = "OK";
            String errorDesc = null;
            String _id = documentIdentifiers[i];
            String version = versions[i];
            getSession();
            GridFS gfs = new GridFS(session, bucket);

            RepositoryDocument rd = new RepositoryDocument();
            if (Logging.connectors.isDebugEnabled()) {
                Logging.connectors.debug("GridFS: Processing document _id = " + _id);
            }

            GridFSDBFile document = gfs.findOne(new ObjectId(_id));

            if (document == null) {
                activities.deleteDocument(_id);
                i++;
                continue;
            }

            DBObject metadata = document.getMetaData();
            if (metadata == null) {
                Logging.connectors.warn("GridFS: Document " + _id + " has a null metadata - skipping.");
                i++;
                continue;
            }

            String urlValue = document.getMetaData().get(this.url) == null
                    ? StringUtils.EMPTY
                    : document.getMetaData().get(this.url).toString();
            if (!StringUtils.isEmpty(urlValue)) {
                if (!scanOnly[i]) {
                    boolean validURL;
                    try {
                        new java.net.URI(urlValue);
                        validURL = true;
                    } catch (java.net.URISyntaxException e) {
                        validURL = false;
                    }
                    if (validURL) {
                        long fileLenght = document.getLength();
                        InputStream is = document.getInputStream();
                        try {
                            Date indexingDate = new Date();
                            rd.setBinary(is, fileLenght);
                            rd.setCreatedDate(document.getUploadDate());
                            rd.setFileName(document.getFilename());
                            rd.setIndexingDate(indexingDate);
                            rd.setMimeType(document.getContentType());
                            String[] aclsArray = null;
                            String[] denyAclsArray = null;
                            if (acl != null) {
                                try {
                                    Object aclObject = document.getMetaData().get(acl);
                                    if (aclObject != null) {
                                        List<String> acls = (List<String>) aclObject;
                                        aclsArray = (String[]) acls.toArray();
                                    }
                                } catch (ClassCastException e) {
                                    // This is bad because security will fail
                                    Logging.connectors.warn("GridFS: Document " + _id + " metadata ACL field doesn't contain List<String> type.");
                                    throw new ManifoldCFException("Security decoding error: "+e.getMessage(),e);
                                }
                            }
                            if (denyAcl != null) {
                                try {
                                    Object denyAclObject = document.getMetaData().get(denyAcl);
                                    if (denyAclObject != null) {
                                        List<String> denyAcls = (List<String>) denyAclObject;
                                        denyAcls.add(GLOBAL_DENY_TOKEN);
                                        denyAclsArray = (String[]) denyAcls.toArray();
                                    }
                                } catch (ClassCastException e) {
                                    // This is bad because security will fail
                                    Logging.connectors.warn("GridFS: Document " + _id + " metadata DenyACL field doesn't contain List<String> type.");
                                    throw new ManifoldCFException("Security decoding error: "+e.getMessage(),e);
                                }
                            }
                            rd.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT,aclsArray,denyAclsArray);
                            try {
                                activities.ingestDocumentWithException(_id, version, urlValue, rd);
                            } catch (IOException e) {
                                handleIOException(e);
                            }
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                handleIOException(e);
                            }
                        }
                        gfs.getDB().getMongo().getConnector().close();
                        session = null;
                        activities.recordActivity(startTime, ACTIVITY_FETCH,
                                fileLenght, _id, errorCode, errorDesc, null);
                    } else {
                        Logging.connectors.warn("GridFS: Document " + _id + " has a invalid URL: " + urlValue + " - skipping.");
View Full Code Here

        String[] versions = new String[documentIdentifiers.length];
        getSession();
        int i = 0;
        while (i < versions.length) {
            String _id = documentIdentifiers[i];
            GridFS gridfs = new GridFS(session, bucket);
            GridFSDBFile document = gridfs.findOne(new ObjectId(_id));
            if (document == null) {
                versions[i] = null;
            } else {
                DBObject metadata = document.getMetaData();
                versions[i] = document.getMD5() + "+" + metadata != null
View Full Code Here

   
    //Connect and create/get database &  Create namespace
    this.connectMongo();
   
    DB mdb = this.mongoConn.getDatabase(this.collection);
    GridFS gfsFile = new GridFS(mdb, this.namespace);
   
    // Load in a GridFS file the Inptustream
    GridFSInputFile gfsInputFile = gfsFile.createFile(is);
   
    // set a filename for identification purposes. We use the previously created UUID
    gfsInputFile.setFilename(uuid);
   
    // save the file file into mongoDB and disconnect
View Full Code Here

   
    //Connect and create/get database &  Create namespace
    this.connectMongo();
   
    DB mdb = this.mongoConn.getDatabase(this.collection);
    GridFS gfsFile = new GridFS(mdb, this.namespace);
 
    // get file file by it's filename
    GridFSDBFile fileForOutput = null;
    fileForOutput = gfsFile.findOne(id);
    if (fileForOutput == null)
    {
      this.logger.error("[File][GridFS] Can not retrieve item: {}", id);
      throw new IllegalStateException(id + " Item not found in Mongo database");
    }
View Full Code Here

 
    //Connect and create/get database &  Create namespace
    this.connectMongo();
   
    DB mdb = this.mongoConn.getDatabase(this.collection);
    GridFS gfsFile = new GridFS(mdb, this.namespace);
   
    // Delete it
    gfsFile.remove(id);

    this.mongoConn.disconnect();
   
    this.logger.info("[File][GridFS] Item {} deleted", id);
  }
View Full Code Here

        MongoClient client = new MongoClient();
        DB db = client.getDB("course");
        FileInputStream inputStream = null;


        GridFS videos = new GridFS(db, "videos"); // returns GridFS bucket named "videos"

        try {
            inputStream = new FileInputStream("video.mp4");
        } catch (FileNotFoundException e) {
            System.out.println("Can't open file");
            System.exit(1);
        }

        GridFSInputFile video  = videos.createFile(inputStream, "video.mp4");

        // create some meta data for the object
        BasicDBObject meta = new BasicDBObject("description", "Jennifer Singing");
        ArrayList<String> tags = new ArrayList<String>();
        tags.add("Singing");
        tags.add("Opera");
        meta.append("tags", tags);

        video.setMetaData(meta);
        video.save();

        System.out.println("Object ID in Files Collection: " +  video.get("_id"));


        System.out.println("Saved the file to MongoDB");
        System.out.println("Now lets read it back out");

        GridFSDBFile gridFile = videos.findOne(new BasicDBObject("filename", "video.mp4"));

        FileOutputStream outputStream = new FileOutputStream("video_copy.mp4");
        gridFile.writeTo(outputStream);

        System.out.println("Write the file back out");
View Full Code Here

        }

        if (_useGridFS) {
            if (StringUtils.isNotBlank(_gridFSDatabaseName)) {
                _sLog.info("Using grid FS with DB " + _gridFSDatabaseName);
                _gridFS = new GridFS(_mongo.getDB(_gridFSDatabaseName));
            } else {
                _sLog.info("Using grid FS with DB " + _databaseName);
                _gridFS = new GridFS(_mongo.getDB(_databaseName));
            }
        }
    }
View Full Code Here

     *
     * @param db The DB.
     */
    public MongoGridFSBlobStore(DB db) {
        commandExecutor = new DefaultCommandExecutor();
        gridFS = new GridFS(db);
    }
View Full Code Here

TOP

Related Classes of com.mongodb.gridfs.GridFS

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.