List of usage examples for com.mongodb.gridfs GridFS GridFS
public GridFS(final DB db, final String bucket)
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Saves a file to the database by file name. This is used during a form upload. We use tika to * determine the content type./* w ww .j a va2 s.co m*/ * * TODO: Refactor this mess * * @param databaseName the name of the database * @param bucketName the name of the bucket * @param fileName the name of the file * @param overwrite whether to overwrite an existing file with the same name * @param stream the file byte stream * @return the Mongo ID of the file * @throws DatasourceException * @throws ExistsException * @throws NotFoundException */ @Override public String addByForm(String databaseName, String bucketName, String fileName, boolean overwrite, InputStream stream) throws DatasourceException, ExistsException, NotFoundException { String fileId = null; try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); GridFSDBFile gfsFile = gfsBucket.findOne(fileName); if (gfsFile == null) { // the file does not exist -- create GridFSInputFile dbFile = gfsBucket.createFile(stream, fileName); dbFile.setContentType(tika.detect(fileName)); dbFile.save(); fileId = dbFile.getId().toString(); } else { // the file exists if (overwrite) { // overwrite the existing file gfsBucket.remove(gfsFile); GridFSInputFile inputFile = gfsBucket.createFile(stream, fileName); inputFile.setContentType(tika.detect(fileName)); inputFile.save(); fileId = inputFile.getId().toString(); } else { throw new ExistsException("The file already exists in the bucket"); } } } catch (MongoException ex) { logger.error("Could not persist entity to bucket", ex); throw new DatasourceException("Could not persist file to bucket"); } if (fileId == null || fileId.isEmpty()) { throw new DatasourceException("Could not persist file to bucket"); } return fileId; }
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Saves a document to the database by file name. If the document already exists this request * will be dropped and the existing file will not be overwritten. * * @param databaseName the database// ww w . j a v a2 s .co m * @param bucketName the bucket * @param fileName the file name * @param inputStream the binary payload * @return the identifier of the file * @throws DatasourceException * @throws ExistsException * @throws NotFoundException */ @Override public String addByFileName(String databaseName, String bucketName, String fileName, InputStream inputStream) throws DatasourceException, ExistsException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); if (gfsBucket.findOne(fileName) != null) { throw new ExistsException("The file already exists"); } GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName); inputFile.setContentType(tika.detect(fileName)); inputFile.save(); return inputFile.getId().toString(); } catch (MongoException ex) { logger.error("An error occured while adding the file", ex); throw new DatasourceException("An error occured while adding the file"); } }
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Updates a file in the database. If the file exists in the database it will be updated. If the * file doesn't exist it will be created. * * @param databaseName the database//from w w w . j a va2s . co m * @param bucketName the bucket * @param fileName the file name * @param inputStream the binary payload * @return the identifier of the file * @throws DatasourceException * @throws NotFoundException */ @Override public String updateByFileName(String databaseName, String bucketName, String fileName, InputStream inputStream) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); GridFSDBFile gfsFile = gfsBucket.findOne(fileName); if (gfsFile == null) { GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName); inputFile.setContentType(tika.detect(fileName)); inputFile.save(); return inputFile.getId().toString(); } else { gfsBucket.remove(gfsFile); GridFSInputFile inputFile = gfsBucket.createFile(inputStream, fileName); inputFile.setContentType(tika.detect(fileName)); inputFile.save(); return inputFile.getId().toString(); } } catch (MongoException ex) { logger.error("An error occured while updating the file", ex); throw new DatasourceException("An error occured while updating the file"); } }
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Returns the file with the given file name. * * @param databaseName the database/* w ww .ja v a 2 s. c om*/ * @param bucketName the bucket * @param fileName the file name * @return the file in with the given file name * @throws DatasourceException * @throws IOException * @throws NotFoundException */ @Override public FileEnvelope getByFileName(String databaseName, String bucketName, String fileName) throws DatasourceException, IOException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); GridFSDBFile gfsFile = gfsBucket.findOne(fileName); if (gfsFile == null) { throw new NotFoundException("The file doesnt exist"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); gfsFile.writeTo(baos); return new FileEnvelope(baos.toByteArray(), gfsFile.getContentType(), fileName); } catch (MongoException ex) { logger.error("An error occured while retrieving the file", ex); throw new DatasourceException("An error occured while retrieving the file"); } }
From source file:org.bananaforscale.cormac.dao.gridfs.GridFsDataServiceImpl.java
License:Apache License
/** * Removes a file in the database.//from w w w. ja v a 2 s . c o m * * @param databaseName the database * @param bucketName the bucket * @param fileName the file to delete * @return a status message with the outcome of the operation * @throws DatasourceException * @throws NotFoundException */ @Override public boolean removeByFileName(String databaseName, String bucketName, String fileName) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } DB mongoDatabase = mongoClient.getDB(databaseName); GridFS gfsBucket = new GridFS(mongoDatabase, bucketName); GridFSDBFile gfsFile = gfsBucket.findOne(fileName); if (gfsFile == null) { throw new NotFoundException("The file doesnt exist"); } gfsBucket.remove(gfsFile); return true; } catch (MongoException ex) { logger.error("An error occured while removing the file", ex); throw new DatasourceException("An error occured while removing the file"); } }
From source file:org.exist.mongodb.xquery.gridfs.Get.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {//w ww . ja v a2 s. com // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String bucket = args[2].itemAt(0).getStringValue(); String documentId = args[3].itemAt(0).getStringValue(); boolean forceBinary = args[4].itemAt(0).toJavaObject(Boolean.class); // Get database DB db = client.getDB(dbname); // Creates a GridFS instance for the specified bucket GridFS gfs = new GridFS(db, bucket); // Find one document by id or by filename GridFSDBFile gfsFile = (isCalledAs(FIND_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId)) : gfs.findOne(documentId); // TODO: find latest if (gfsFile == null) { throw new XPathException(this, GridfsModule.GRFS0004, String.format("Document '%s' could not be found.", documentId)); } Sequence retVal = get(gfsFile, forceBinary); return retVal; } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } }
From source file:org.exist.mongodb.xquery.gridfs.ListDocuments.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/*w w w. ja v a 2 s . com*/ // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String bucket = args[2].itemAt(0).getStringValue(); // Get database DB db = client.getDB(dbname); // Creates a GridFS instance for the specified bucket GridFS gfs = new GridFS(db, bucket); return ContentSerializer.getDocuments(gfs); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } //return Sequence.EMPTY_SEQUENCE; }
From source file:org.exist.mongodb.xquery.gridfs.Properties.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {//from w ww . j av a 2 s.c o m // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String bucket = args[2].itemAt(0).getStringValue(); String documentId = args[3].itemAt(0).getStringValue(); // Get database DB db = client.getDB(dbname); // Creates a GridFS instance for the specified bucket GridFS gfs = new GridFS(db, bucket); // Find one document by id or by filename GridFSDBFile gfsFile = (isCalledAs(PROPS_BY_OBJECTID)) ? gfs.findOne(new ObjectId(documentId)) : gfs.findOne(documentId); if (gfsFile == null) { throw new XPathException(this, GridfsModule.GRFS0004, String.format("Document '%s' could not be found.", documentId)); } return ContentSerializer.getReport(gfsFile); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } }
From source file:org.exist.mongodb.xquery.gridfs.Remove.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/* w ww . j av a 2 s .c o m*/ // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String bucket = args[2].itemAt(0).getStringValue(); String documentId = args[3].itemAt(0).getStringValue(); // Get database DB db = client.getDB(dbname); // Create a GridFS instance for the specified bucket GridFS gfs = new GridFS(db, bucket); // Remove document by id or by filename if (isCalledAs(REMOVE_BY_OBJECTID)) { gfs.remove(new ObjectId(documentId)); } else { gfs.remove(documentId); } return new StringValue(documentId); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } }
From source file:org.exist.mongodb.xquery.gridfs.Store.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/* w ww . j a v a2 s . c om*/ // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String bucket = args[2].itemAt(0).getStringValue(); String documentName = args[3].itemAt(0).getStringValue(); String contentType = getMimeType(args[4], documentName); LOG.info(String.format("Storing document %s (%s)", documentName, contentType)); // Actual content: File object, doc() element, base64... Item content = args[5].itemAt(0); // Get database DB db = client.getDB(dbname); // Creates a GridFS instance for the specified bucket GridFS gfs = new GridFS(db, bucket); // Create file GridFSInputFile gfsFile = gfs.createFile(); // Set meta data gfsFile.setFilename(documentName); gfsFile.setContentType(contentType); StopWatch stopWatch = new StopWatch(); // Write data if (StringUtils.endsWithAny(documentName, nonCompressables)) { writeRaw(gfsFile, stopWatch, content); } else { int dataType = content.getType(); writeCompressed(gfsFile, stopWatch, content, dataType); } LOG.info(String.format("serialization time: %s", stopWatch.getTime())); // Report identifier return new StringValue(gfsFile.getId().toString()); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } }