Example usage for com.mongodb.gridfs GridFSInputFile getOutputStream

List of usage examples for com.mongodb.gridfs GridFSInputFile getOutputStream

Introduction

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

Prototype

public OutputStream getOutputStream() 

Source Link

Document

After retrieving this java.io.OutputStream , this object will be capable of accepting successively written data to the output stream.

Usage

From source file:cfel.service.DatabaseServive.java

License:Open Source License

public OutputStream getFileOutputStream(String id, String contentType) {
    if (mFS != null) {
        try {//from   w  w  w  .  jav a 2  s.c om
            deleteFile(id);
            GridFSInputFile dbFile = mFS.createFile(id);
            if (contentType != null) {
                dbFile.setContentType(contentType);
            }
            return dbFile.getOutputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.andreig.jetty.GridfsServlet.java

License:GNU General Public License

@SuppressWarnings("unused")
@Override//from w  w w.j a v  a  2s.co  m
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPost()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);
        return;
    }

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    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";

    String file_name = req.getParameter("filename");

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

    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);
    }

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

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n = 0, bytesno = 0;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
        bytesno += n;
    }
    os.close();

    if (is != null)
        is.close();

    out_json(req, Status.OK);

}

From source file:com.andreig.jetty.GridfsServlet.java

License:GNU General Public License

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

    log.fine("doPut()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);//from w  ww.j  a v  a  2  s.com
        return;
    }

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    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";

    String file_name = req.getParameter("filename");

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

    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);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old != null) {
        error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    @SuppressWarnings("unused")
    int n = 0, bytesno = 0;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
        bytesno += n;
    }
    os.flush();
    os.close();

    if (is != null)
        is.close();

    out_json(req, Status.OK);

}

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

License:GNU General Public License

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

    log.trace("doPost()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);//from w  ww. j  a v a2 s. com
        return;
    }

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    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";

    String file_name = req.getParameter("filename");

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

    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);
    }

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

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n = 0, bytesno = 0;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
        bytesno += n;
    }
    os.close();

    if (is != null)
        is.close();

    out_json(req, Status.OK);

}

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

License:GNU General Public License

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

    log.trace("doPut()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);/*  www.  ja v a2s .  c  o  m*/
        return;
    }

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    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";

    String file_name = req.getParameter("filename");

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

    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);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old != null) {
        error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n = 0, bytesno = 0;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
        bytesno += n;
    }
    os.flush();
    os.close();

    if (is != null)
        is.close();

    out_json(req, Status.OK);

}

From source file:com.wavemaker.tools.io.filesystem.mongo.MongoFileSystem.java

License:Open Source License

private void touch(GridFSInputFile file) {
    try {//from   ww w .  jav a2  s. c o m
        file.getOutputStream().close();
    } catch (IOException e) {
        throw new ResourceException(e);
    }
}

From source file:com.wavemaker.tools.io.filesystem.mongo.MongoFileSystem.java

License:Open Source License

@Override
public OutputStream getOutputStream(MongoFileSystemKey key) {
    delete(key);//w w w.  j  a  v  a 2s .  com
    GridFSInputFile file = create(key, ResourceType.FILE);
    return file.getOutputStream();
}

From source file:com.wavemaker.tools.io.mongo.MongoResourceStore.java

License:Open Source License

protected GridFSInputFile create(Type type, boolean createEmptyFile) {
    Assert.notNull(type, "Type must not be null");
    GridFSInputFile file = this.fs.createFile(getFilename(getPath()));
    JailedResourcePath parent = this.path.getParent();
    if (parent != null) {
        file.put(PARENT, parent.getUnjailedPath().toString());
    }//from w ww . j  a v  a2s.c  o  m
    file.put(RESOURCE_TYPE, type.name());
    if (createEmptyFile) {
        try {
            file.getOutputStream().close();
        } catch (IOException e) {
            throw new ResourceException(e);
        }
    }
    return file;
}

From source file:edu.umn.biomedicus.uima.xmi.MongoDbXmiWriter.java

License:Apache License

@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    JCas systemView;/*ww w. j a  v a2s .c  o m*/
    try {
        systemView = jCas.getView(Views.SYSTEM_VIEW);
    } catch (CASException e) {
        throw new AnalysisEngineProcessException(e);
    }

    String documentId = null;
    JFSIndexRepository jfsIndexRepository = systemView.getJFSIndexRepository();
    if (jfsIndexRepository != null) {
        FSIterator<DocumentId> it = jfsIndexRepository.getAllIndexedFS(DocumentId.type);
        if (it.hasNext()) {
            DocumentId documentAnnotation = it.next();
            documentId = documentAnnotation.getDocumentId();
        }
    }

    if (documentId == null) {
        documentId = UUID.randomUUID().toString();
    }

    GridFSInputFile file = gridFS.createFile(documentId + ".xmi");

    try (OutputStream outputStream = file.getOutputStream()) {
        XmiCasSerializer.serialize(jCas.getCas(), outputStream);
    } catch (IOException | SAXException e) {
        throw new AnalysisEngineProcessException(e);
    }

}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

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

    log.fine("doPost()");

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    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];/*w  w  w.j av a  2 s. c  o  m*/
            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";

    String file_name = req.getParameter("filename");

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

    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);
    }

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

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
    }
    os.close();

    is.close();

    out_json(req, Status.OK);

}