Example usage for com.mongodb.gridfs GridFSDBFile writeTo

List of usage examples for com.mongodb.gridfs GridFSDBFile writeTo

Introduction

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

Prototype

public long writeTo(final OutputStream out) throws IOException 

Source Link

Document

Writes the file's data to an OutputStream.

Usage

From source file:com.cyslab.craftvm.rest.mongo.GridfsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.trace("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);/*from w w w.j av  a 2  s. c om*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    String op = req.getParameter("op");
    if (op == null)
        op = "get";

    StringBuilder buf = tl.get();
    // reset buf
    buf.setLength(0);

    // list
    if ("get".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("file does not exists"));
            return;
        }

        res.setContentLength((int) db_file.getLength());
        String ct = db_file.getContentType();
        if (ct != null)
            res.setContentType(ct);
        OutputStream os = res.getOutputStream();
        long l;
        while ((l = db_file.writeTo(os)) > 0)
            ;
        os.flush();
        os.close();

    }
    // list
    else if ("list".equals(op)) {

        DBCursor c = fs.getFileList();
        if (c == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        int no = 0;
        buf.append("[");
        while (c.hasNext()) {

            DBObject o = c.next();
            JSON.serialize(o, buf);
            buf.append(",");
            no++;

        }

        if (no > 0)
            buf.setCharAt(buf.length() - 1, ']');
        else
            buf.append(']');

        out_str(req, buf.toString(), "application/json");

    }
    // info
    else if ("info".equals(op)) {

        String file_name = req.getParameter("filename");
        if (file_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }

        GridFSDBFile db_file = fs.findOne(file_name);
        if (db_file == null) {
            error(res, SC_NOT_FOUND, Status.get("no documents found"));
            return;
        }

        buf.append("{");
        buf.append(String.format("\"ContentType\":%s,", db_file.getContentType()));
        buf.append(String.format("\"Length\":%d,", db_file.getLength()));
        buf.append(String.format("\"MD5\":%s", db_file.getMD5()));
        buf.append("}");

        out_str(req, buf.toString(), "application/json");

    } else
        res.sendError(SC_BAD_REQUEST);

}

From source file:com.edgytech.umongo.DbPanel.java

License:Apache License

public void downloadFile(final ButtonBase button) {
    final DB db = getDbNode().getDb();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.downloadQuery)).getDBObject();
    final String fname = getStringFieldValue(Item.downloadFileName);
    final String dpath = getStringFieldValue(Item.downloadFilePath);
    if (dpath.isEmpty()) {
        return;/*from  ww w. j a  v a2s .c o m*/
    }
    final File dfile = new File(dpath);

    new DbJob() {

        @Override
        public Object doRun() throws IOException {
            GridFSDBFile dbfile = null;
            if (query != null) {
                dbfile = getGridFS().findOne(query);
            } else {
                dbfile = getGridFS().findOne(fname);
            }
            if (dbfile == null) {
                throw new MongoException("GridFS cannot find file " + fname);
            }
            dbfile.writeTo(dfile);
            return dbfile;
        }

        @Override
        public String getNS() {
            return db.getName();
        }

        @Override
        public String getShortName() {
            return "Download File";
        }

        @Override
        public DBObject getRoot(Object result) {
            BasicDBObject obj = new BasicDBObject("filename", fname);
            obj.put("query", query);
            obj.put("path", dpath);
            return obj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

From source file:com.fliker.Modal.OSMPreview.java

public byte[] imagefromid(String imageId) {

    CoursePreview courseprev = new CoursePreview();
    GridFSDBFile imagecontent = courseprev.getFiles(imageId);
    System.out.println("imagecontent ++" + imagecontent.getInputStream());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from   w w w.  jav  a  2s  .c  o m
        imagecontent.writeTo(baos);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] imageblob = baos.toByteArray();

    return imageblob;
}

From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_LibraryJars.java

License:Apache License

protected static void copyFile(final String binary_id, final String path, final IStorageService aleph2_fs,
        final GridFS share_fs) throws IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        final GridFSDBFile file = share_fs.find(new ObjectId(binary_id));
        file.writeTo(out);
        final FileContext fs = aleph2_fs.getUnderlyingPlatformDriver(FileContext.class, Optional.empty()).get();
        final Path file_path = fs.makeQualified(new Path(path));
        try (FSDataOutputStream outer = fs.create(file_path,
                EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE),
                org.apache.hadoop.fs.Options.CreateOpts.createParent())) {
            outer.write(out.toByteArray());
        }/*from   w w  w  .j a  va  2s.  c  o m*/
    }
}

From source file:com.ikanow.infinit.e.api.social.sharing.ShareHandler.java

License:Open Source License

/**
 * Finds the gridfile given by id and returns the bytes
 * //from   w w w  .j  ava  2 s.c o  m
 * @param id the object id of the gridfile to lookup (stored in sharepojo)
 * @return bytes of file stored in gridfile
 */
private byte[] getGridFile(ObjectId id) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        GridFSDBFile file = DbManager.getSocial().getShareBinary().find(id);
        file.writeTo(out);
        byte[] toReturn = out.toByteArray();
        out.close();
        return toReturn;
    } catch (Exception ex) {
    }
    return null;
}

From source file:com.ikanow.infinit.e.api.social.sharing.ShareHandler.java

License:Open Source License

@SuppressWarnings("unused")
private ByteArrayOutputStream getGridStream(ObjectId id) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from w  w  w  .j  a v  a  2  s .c o  m*/
        GridFSDBFile file = DbManager.getSocial().getShareBinary().find(id);
        file.writeTo(out);
    } catch (Exception ex) {
    }
    return out;
}

From source file:com.ikanow.infinit.e.core.mapreduce.HadoopJobRunner.java

License:Open Source License

/**
 * Downloads jar file from web using URL call.  Typically
 * the jar files we be kept in our /share store so we will
 * be calling our own api./*from  w ww  . j  ava  2 s  . c om*/
 * 
 * @param jarURL
 * @return
 * @throws Exception 
 */
private String downloadJarFile(String jarURL, List<ObjectId> communityIds) throws Exception {
    String shareStringOLD = "$infinite/share/get/";
    String shareStringNEW = "$infinite/social/share/get/";
    String tempFileName = assignNewJarLocation();
    OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFileName));
    if (jarURL.startsWith(shareStringOLD) || jarURL.startsWith(shareStringNEW)) {
        //jar is local use id to grab jar (skips authentication)
        String shareid = null;
        if (jarURL.startsWith(shareStringOLD)) {
            shareid = jarURL.substring(shareStringOLD.length());
        } else {
            shareid = jarURL.substring(shareStringNEW.length());
        }
        BasicDBObject query = new BasicDBObject(SharePojo._id_, new ObjectId(shareid));
        query.put(ShareCommunityPojo.shareQuery_id_, new BasicDBObject(MongoDbManager.in_, communityIds));

        SharePojo share = SharePojo.fromDb(DbManager.getSocial().getShare().findOne(query), SharePojo.class);
        if (null == share) {
            throw new RuntimeException("Can't find JAR file or insufficient permissions");
        }
        if (share.getBinaryId() != null) {
            GridFSDBFile file = DbManager.getSocial().getShareBinary().find(share.getBinaryId());
            file.writeTo(out);
        } else {
            out.write(share.getBinaryData());
        }
    } else {
        if (jarURL.startsWith("$infinite")) {
            jarURL = jarURL.replace("$infinite", "http://localhost:8080");
        } else if (jarURL.startsWith("file://")) {
            // Can't access the file system, except for this one nominated file:
            if (!jarURL
                    .equals("file:///opt/infinite-home/lib/plugins/infinit.e.hadoop.prototyping_engine.jar")) {
                throw new RuntimeException("Can't find JAR file or insufficient permissions");
            }
        }

        //download jar from external site
        URL url = new URL(jarURL);

        URLConnection ucon = url.openConnection();
        InputStream in = ucon.getInputStream();
        byte[] buf = new byte[1024];
        int byteRead = 0;
        while ((byteRead = in.read(buf)) != -1) {
            out.write(buf, 0, byteRead);
        }
        in.close();
    }
    out.close();
    return tempFileName;
}

From source file:com.ikanow.infinit.e.harvest.HarvestController.java

License:Open Source License

/**
 * Downloads jar file from web using URL call.  Typically
 * the jar files we be kept in our /share store so we will
 * be calling our own api.//from  w w w .  j  a  va2  s  .c o m
 * 
 * @param jarURL
 * @return
 * @throws Exception 
 */
public static String maintainJarFileCache(SharePojo share) throws Exception {
    String tempFileName = System.getProperty("java.io.tmpdir") + "/" + share.get_id() + ".cache.jar";
    File tempFile = new File(tempFileName);

    // Compare dates (if it exists) to see if we need to update the cache) 

    if (!tempFile.exists() || (tempFile.lastModified() < share.getModified().getTime())) {
        OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFileName));
        if (share.getBinaryId() != null) {
            GridFSDBFile file = DbManager.getSocial().getShareBinary().find(share.getBinaryId());
            file.writeTo(out);
        } else {
            out.write(share.getBinaryData());
        }
        out.flush();
        out.close();
    } //TESTED

    return tempFileName;
}

From source file:com.ikanow.infinit.e.processing.custom.utils.InfiniteHadoopUtils.java

License:Open Source License

/**
 * Downloads jar file from web using URL call.  Typically
 * the jar files we be kept in our /share store so we will
 * be calling our own api./* w  ww. ja  va  2 s . c  om*/
 * 
 * @param jarURL
 * @return
 * @throws Exception 
 */
public static String downloadJarFile(String jarURL, List<ObjectId> communityIds, PropertiesManager prop_custom,
        ObjectId submitterId) throws Exception {
    String shareStringOLD = "$infinite/share/get/";
    String shareStringNEW = "$infinite/social/share/get/";
    //jar is local use id to grab jar (skips authentication)
    String shareid = null;
    try {
        new ObjectId(jarURL);
        shareid = jarURL;
    } catch (Exception e) {
    } // that's fine it's just not a raw ObjectId

    if (jarURL.startsWith(shareStringOLD) || jarURL.startsWith(shareStringNEW) || (null != shareid)) {
        if (null == shareid) {
            if (jarURL.startsWith(shareStringOLD)) {
                shareid = jarURL.substring(shareStringOLD.length());
            } else {
                shareid = jarURL.substring(shareStringNEW.length());
            }
        }
        BasicDBObject query = new BasicDBObject(SharePojo._id_, new ObjectId(shareid));
        query.put(ShareCommunityPojo.shareQuery_id_, new BasicDBObject(MongoDbManager.in_, communityIds));

        SharePojo share = SharePojo.fromDb(DbManager.getSocial().getShare().findOne(query), SharePojo.class);

        if (null == share) {
            throw new RuntimeException(
                    "Can't find JAR file or share or custom table or source, or insufficient permissions: "
                            + shareid);
        }

        // The JAR owner needs to be an admin:
        //TODO (INF-2118): At some point would like there to be a choice ... if not admin then must inherit the Infinit.e sandbox version
        // ... there seemed to be some issues with that however so for now will just allow all admin jars and no non-admin jars
        // (see other INF-2118 branch)
        if (prop_custom.getHarvestSecurity()) {
            if (!AuthUtils.isAdmin(share.getOwner().get_id())) {
                throw new RuntimeException("Permissions error: only administrators can upload custom JARs");
            }
        } //TESTED (by hand)

        String extension = ".cache";
        if ((null != share.getMediaType()) && (share.getMediaType().contains("java-archive"))) {
            extension = ".cache.jar";
        } else if ((null != share.getMediaType()) && (share.getMediaType().contains("gzip"))) {
            extension = ".cache.tgz";
        } else if ((null != share.getMediaType()) && (share.getMediaType().contains("zip"))) {
            extension = ".cache.zip";
        }
        String tempFileName = assignNewJarLocation(prop_custom, shareid + extension);
        File tempFile = new File(tempFileName);

        // Compare dates (if it exists) to see if we need to update the cache) 

        if (!tempFile.exists() || (tempFile.lastModified() < share.getModified().getTime())) {
            if (null != share.getDocumentLocation()) {
                tempFileName = share.getDocumentLocation().getCollection(); // ie just return the pointer               
                if (!(new File(tempFileName).exists())) { // (this is really only when debugging)
                    // Try looking in temp path
                    tempFileName = tempFileName.substring(1 + tempFileName.lastIndexOf('/'));
                    tempFileName = assignNewJarLocation(prop_custom, tempFileName);
                }
            } //TESTED
            else {
                OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFileName));
                if (share.getBinaryId() != null) {
                    GridFSDBFile file = DbManager.getSocial().getShareBinary().find(share.getBinaryId());
                    file.writeTo(out);
                } else {
                    out.write(share.getBinaryData());
                }
                out.flush();
                out.close();
            }
        } //TESTED         

        return tempFileName;
    } else // Not an infinit.e share - either a local file or served externally
    {
        if (jarURL.startsWith("$infinite")) { // Local web server
            if (prop_custom.getHarvestSecurity()) {
                if (!AuthUtils.isAdmin(submitterId)) {
                    throw new RuntimeException(
                            "Permissions error: only administrators can run custom JARs served from a web server (users can run custom JARs when uploaded by an admin to the share store)");
                }
            } //TOTEST            
            jarURL = jarURL.replace("$infinite", "http://localhost:8080");
        } //TESTED (by hand) 
        else if (!jarURL.startsWith("http")) {
            // Can't access the file system, except for this one nominated file:
            if (!jarURL.equals(BUILT_IN_JOB_PATH)) {
                throw new RuntimeException("Can't find JAR file or insufficient permissions: " + jarURL);
            }
            jarURL = BUILT_IN_JOB_PATH.substring(7);
            if (!(new File(jarURL).exists())) { // (this is really only when debugging)
                // Try looking in temp path
                jarURL = assignNewJarLocation(prop_custom, BUILT_IN_JOB_NAME);
            }
            return jarURL;
        } //TESTED
        else { // Access a JAR from an external web server, can only do this if admin
            if (prop_custom.getHarvestSecurity()) {
                if (!AuthUtils.isAdmin(submitterId)) {
                    throw new RuntimeException(
                            "Permissions error: only administrators can run custom JARs served from a web server (users can run custom JARs when uploaded by an admin to the share store)");
                }
            } //TOTEST            
        } //TESTED (by hand)

        String tempFileName = assignNewJarLocation(prop_custom, null);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(tempFileName));

        //download jar from external site
        URL url = new URL(jarURL);

        URLConnection ucon = url.openConnection();
        InputStream in = ucon.getInputStream();
        byte[] buf = new byte[1024];
        int byteRead = 0;
        while ((byteRead = in.read(buf)) != -1) {
            out.write(buf, 0, byteRead);
        }
        in.close();
        out.close();
        return tempFileName;
    } //(end share - first clause - or served externally - second clause)
}

From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for retrieving the specified file stored in GridFS.
 *
 * @param dbName     Name of Database/*from w  ww.jav  a2  s.com*/
 * @param bucketName Name of GridFS Bucket
 * @param id         ObjectId of the file to be retrieved
 * @returns Requested multipartfile for viewing or download based on 'download' param.
 */
public File getFile(String dbName, String bucketName, String id)
        throws ValidationException, DatabaseException, CollectionException {
    mongoInstance = mongoInstanceProvider.getMongoInstance();

    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database Name Is Null");
    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }
    File tempFile = null;
    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException(

                    "Database with dbName [ " + dbName + "] does not exist");
        }

        GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);
        GridFSDBFile gridFSDBFile = gridFS.findOne(new ObjectId(id));
        String tempDir = System.getProperty("java.io.tmpdir");
        tempFile = new File(tempDir + "/" + gridFSDBFile.getFilename());
        gridFSDBFile.writeTo(tempFile);

    } catch (MongoException m) {
        CollectionException e = new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION,
                "GET_FILE_EXCEPTION", m.getCause());
        throw e;
    } catch (IOException e) {
        CollectionException ce = new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION,
                "GET_FILE_EXCEPTION", e.getCause());
        throw ce;
    }
    return tempFile;
}