List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
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/* w ww . j a va 2 s . c o m*/ * @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; }
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/* ww w .ja va 2 s . c o 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.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for dropping a file from GridFS. * * @param dbName Name of Database/*from www . j av a 2s. com*/ * @param bucketName Name of GridFS Bucket * @param id Object id of file to be deleted * @returns Status message. */ public String deleteFile(String dbName, String bucketName, ObjectId id) throws DatabaseException, 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"); } String result = null; GridFSDBFile gridFSDBFile = null; try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST"); } if (id == null) { throw new EmptyDocumentDataException("File is empty"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); gridFSDBFile = gridFS.findOne(id); if (gridFSDBFile == null) { throw new UndefinedDocumentException("DOCUMENT_DOES_NOT_EXIST"); } gridFS.remove(id); } catch (MongoException e) { throw new DeleteDocumentException("FILE_DELETION_EXCEPTION"); } result = "Deleted File : [" + gridFSDBFile.getFilename() + "]"; return result; }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for dropping all files from a GridFS bucket. * * @param dbName Name of Database//w w w . j ava2 s. c o m * @param bucketName Name of GridFS Bucket * @returns Status message. */ public String dropBucket(String dbName, String bucketName) throws DatabaseException, 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"); } String result = null; try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException("DB [" + dbName + "] DOES NOT EXIST"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); gridFS.remove(new BasicDBObject()); } catch (MongoException e) { throw new DeleteDocumentException("FILES_DELETION_EXCEPTION"); } result = "All files in [" + bucketName + "] have been deleted"; return result; }
From source file:com.khubla.cbean.mongokv.MongoKVService.java
License:Open Source License
@Override public byte[] load(CBeanKey cBeanKey) throws KVServiceException { MongoClient mongoClient = null;// ww w .j av a2s. c o m try { /* * get a pooled mongo connection */ mongoClient = mongoClientPool.borrowObject(); final DB db = mongoClient.getDB(cBeanUrl.getClusterName()); new GridFS(db, ""); // List<> gfs.find(url); // final Location location = new Location(new Namespace(clusterName), url); // final FetchValue fv = new FetchValue.Builder(location).build(); // final FetchValue.Response response = riakClient.execute(fv); /* * response */ // if (null != response) { // final RiakObject riakObject = response.getValue(RiakObject.class); // if (null != riakObject) { // final BinaryValue binaryValue = riakObject.getValue(); // if (null != binaryValue) { // logger.info("Loaded asset '" + url + "'"); // return binaryValue.getValue(); // } // } // } logger.info("Unable to load asset '" + cBeanKey.getKey() + "'"); return null; } catch (final Exception e) { throw new KVServiceException(e); } finally { try { if (null != mongoClient) { mongoClientPool.returnObject(mongoClient); } } catch (final Exception e) { e.printStackTrace(); } } }
From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java
License:Open Source License
public MongoGridDocStoreService(DB db, String bucketName) { this(new GridFS(db, bucketName), db.getCollection(bucketName + GRID_FS_FILES_COLLECTION_SUFFIX)); }
From source file:com.lowereast.guiceymongo.guice.spi.GridFSProviderModule.java
License:Apache License
private void cacheGuiceyBucket() throws Exception { String bucketKey = ((GuiceyMongoBucket) ((Key<?>) key).getAnnotation()).value(); String clonedConfiguration = getInstance(_injector, Key.get(String.class, AnnotationUtil.clonedConfiguration(_configuration))); String bucket;//from w w w. j ava 2 s . c o m if (clonedConfiguration == null) bucket = _injector .getInstance(Key.get(String.class, AnnotationUtil.configuredBucket(_configuration, bucketKey))); else bucket = _injector.getInstance( Key.get(String.class, AnnotationUtil.configuredBucket(clonedConfiguration, bucketKey))); _cachedGridFS = new GridFS(_databaseProvider.get(), bucket); }
From source file:com.lowereast.guiceymongo.GuiceyBucket.java
License:Apache License
public GuiceyBucket(DB database, String bucket) { this(new GridFS(database, bucket)); }
From source file:com.mattinsler.guiceymongo.guice.spi.GridFSProviderModule.java
License:Apache License
private void cacheGuiceyBucket() throws Exception { String bucketKey = ((MongoBucket) ((Key<?>) key).getAnnotation()).value(); String clonedConfiguration = getInstance(_injector, Key.get(String.class, AnnotationUtil.clonedConfiguration(_configuration))); String bucket;/*from w w w. jav a 2 s. com*/ if (clonedConfiguration == null) bucket = _injector .getInstance(Key.get(String.class, AnnotationUtil.configuredBucket(_configuration, bucketKey))); else bucket = _injector.getInstance( Key.get(String.class, AnnotationUtil.configuredBucket(clonedConfiguration, bucketKey))); _cachedGridFS = new GridFS(_databaseProvider.get(), bucket); }
From source file:com.photon.phresco.service.impl.DbService.java
License:Apache License
private GridFS getGridFs() throws PhrescoException { if (isDebugEnabled) { LOGGER.debug("DbService.getGridFs:Entry"); }/* w w w . j a v a 2s . co m*/ try { Mongo mongo = new Mongo(serverConfig.getDbHost(), serverConfig.getDbPort()); DB db = mongo.getDB(serverConfig.getDbName()); GridFS gfsPhoto = new GridFS(db, "icons"); if (isDebugEnabled) { LOGGER.debug("DbService.getGridFs:Exit"); } return gfsPhoto; } catch (UnknownHostException e) { if (isDebugEnabled) { LOGGER.error("DbService.getGridFs", STATUS_FAILURE, MESSAGE_EQUALS + "\"" + e.getLocalizedMessage() + "\""); } throw new PhrescoException(e); } catch (MongoException e) { if (isDebugEnabled) { LOGGER.error("DbService.getGridFs", STATUS_FAILURE, MESSAGE_EQUALS + "\"" + e.getLocalizedMessage() + "\""); } throw new PhrescoException(e); } }