List of usage examples for com.mongodb.gridfs GridFSInputFile setContentType
public void setContentType(final String contentType)
From source file:com.cognifide.aet.vs.artifacts.ArtifactsDAOMongoDBImpl.java
License:Apache License
@Override public String saveArtifact(DBKey dbKey, InputStream data, String contentType) { String resultObjectId = null; GridFS gfs = getGridFS(dbKey);/*from w ww . j av a 2 s .c om*/ if (gfs != null) { GridFSInputFile file = gfs.createFile(data); if (file != null) { file.setContentType(contentType); file.save(); resultObjectId = file.getId().toString(); } } return resultObjectId; }
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 w w . j av a2 s.c om 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);// ww w . ja v a 2s . c om 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.edgytech.umongo.DbPanel.java
License:Apache License
public void uploadFile(final ButtonBase button) { final DbNode dbNode = getDbNode(); final DB db = dbNode.getDb(); final String path = getStringFieldValue(Item.uploadFilePath); if (path.isEmpty()) { return;/*from w ww. j ava2 s .co m*/ } final File src = new File(path); final String fileName = getStringFieldValue(Item.uploadFileName); final String contentType = getStringFieldValue(Item.uploadContentType); final DBObject metadata = ((DocBuilderField) getBoundUnit(Item.uploadMetadata)).getDBObject(); new DbJob() { @Override public Object doRun() throws IOException { final GridFSInputFile file = getGridFS().createFile(src); if (!fileName.isEmpty()) { file.setFilename(fileName); } if (!contentType.isEmpty()) { file.setContentType(contentType); } if (metadata != null) { file.setMetaData(metadata); } file.save(); return file; } @Override public String getNS() { return db.getName(); } @Override public String getShortName() { return "Upload File"; } @Override public DBObject getRoot(Object result) { return new BasicDBObject("path", path); } @Override public void wrapUp(Object res) { super.wrapUp(res); // may have new collections dbNode.structureComponent(); } @Override public ButtonBase getButton() { return button; } }.addJob(); }
From source file:com.englishtown.integration.java.IntegrationTestHelper.java
License:Open Source License
public static String createFile(JsonObject config, String bucket) { GridFS gridFS = IntegrationTestHelper.getGridFS(config, bucket); GridFSInputFile inputFile = gridFS .createFile(IntegrationTestHelper.class.getResourceAsStream("/EF_Labs_ENG_logo.JPG")); inputFile.setContentType(DEFAULT_CONTENT_TYPE); inputFile.setFilename(DEFAULT_FILENAME); inputFile.setChunkSize(DEFAULT_CHUNK_SIZE); inputFile.setMetaData(DEFAULT_METADATA); inputFile.save();/* ww w .j a v a 2s . c om*/ return inputFile.getId().toString(); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
/** * @param attachmentContentStream//from w w w .j ava 2 s .c om * @return */ @Override public AttachmentContentMetadata createAttachmentContent(String name, String contentType, InputStream attachmentContentStream) { // Do not specify a bucket (so the data will be stored in fs.files and fs.chunks) GridFSInputFile gfsFile = gridFS.createFile(attachmentContentStream); ObjectId id = new ObjectId(); gfsFile.setContentType(contentType); gfsFile.setId(id); String filename = id.toString(); gfsFile.setFilename(filename); gfsFile.save(); return new AttachmentContentMetadata(gfsFile.getFilename(), gfsFile.getLength()); }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for uploading a file to GridFS. * * @param dbName Name of Database//from w w w. j av a 2 s .co m * @param bucketName Name of GridFS Bucket * @param formData formDataBodyPart of the uploaded file * @param inputStream inputStream of the uploaded file * @param dbInfo Mongo Db Configuration provided by user to connect to. * @returns Success message with additional file details such as name, size, * download url & deletion url as JSON Array string. */ public JSONArray insertFile(String dbName, String bucketName, String dbInfo, InputStream inputStream, FormDataBodyPart formData) throws DatabaseException, CollectionException, DocumentException, ValidationException { mongoInstance = mongoInstanceProvider.getMongoInstance(); if (dbName == null) { throw new EmptyDatabaseNameException("Database name is null"); } if (dbName.equals("")) { throw new EmptyDatabaseNameException("Database Name Empty"); } if (bucketName == null) { throw new EmptyCollectionNameException("Bucket name is null"); } if (bucketName.equals("")) { throw new EmptyCollectionNameException("Bucket Name Empty"); } JSONArray result = new JSONArray(); FormDataContentDisposition fileData = formData.getFormDataContentDisposition(); try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); GridFSInputFile fsInputFile = gridFS.createFile(inputStream, fileData.getFileName()); fsInputFile.setContentType(formData.getMediaType().toString()); fsInputFile.save(); JSONObject obj = new JSONObject(); obj.put("name", fsInputFile.getFilename()); obj.put("size", fsInputFile.getLength()); obj.put("url", String.format("services/%s/%s/gridfs/getfile?id=%s&download=%s&dbInfo=%s&ts=%s", dbName, bucketName, fsInputFile.getId().toString(), false, dbInfo, new Date())); obj.put("delete_url", String.format("services/%s/%s/gridfs/dropfile?id=%s&dbInfo=%s&ts=%s", dbName, bucketName, fsInputFile.getId().toString(), dbInfo, new Date().getTime())); obj.put("delete_type", "GET"); result.put(obj); } catch (Exception e) { CollectionException ce = new CollectionException(ErrorCodes.UPLOAD_FILE_EXCEPTION, "UPLOAD_FILE_EXCEPTION", e.getCause()); throw ce; } return result; }
From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java
License:Open Source License
void setFileMetaData(GridFSInputFile newFile, Document document, int shardKey) { newFile.setContentType(document.getMimeType()); // store the encoding as meta-data for EncodedDocuments DBObject metaData = newFile.getMetaData(); if (metaData == null) { metaData = new BasicDBObject(); }/*from w w w .j a va2 s .co m*/ metaData.put(INDEX_STATUS_KEY, STATUS_UNINDEXED); metaData.put(FILE_EXTENSION_KEY, document.getFileExtension()); metaData.put(BINARY_ENCODING_KEY, document.getBinaryEncoding()); metaData.put(INDEX_SHARD_KEY, shardKey); newFile.setMetaData(metaData); }
From source file:com.mongo.gridfs.GridFSFileLoader.java
License:Open Source License
public void loadFile(String fileToLoad, String contentType, Map<String, String> metaData) { InputStream is = new GridFSFileValidator().loadFile(fileToLoad); GridFSInputFile file = gridfs.createFile(is); file.setContentType(contentType); file.setFilename(fileToLoad);//from w w w .j ava 2 s . c o m BasicDBObject metadata = new BasicDBObject(); if (metaData != null) { for (Entry<String, String> entry : metaData.entrySet()) { metadata.put(entry.getKey(), entry.getValue()); } } file.setMetaData(metadata); file.save(); }
From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java
License:Open Source License
public String saveFile(byte[] fileData, String fileName, String contentType) { GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET); GridFSInputFile gfsFile = gridFs.createFile(fileData); gfsFile.setFilename(fileName);/*from w ww.j a v a2 s . c o m*/ gfsFile.setContentType(contentType); gfsFile.save(); LOG.info("Saved new file :" + fileName); return gfsFile.getId().toString(); }