List of usage examples for com.mongodb.gridfs GridFS findOne
public GridFSDBFile findOne(final DBObject query)
From source file:org.mongodb.demos.binary.GridFSDemo.java
License:Apache License
/** * Read the file stored in GridFS and save it into the fileToSave location * @param id//from www .j a v a 2s . com * @param fileToSave * @throws Exception */ public void readLargeFile(Object id, String fileToSave) throws Exception { GridFS fs = new GridFS(db); GridFSDBFile out = fs.findOne(new BasicDBObject("_id", id)); out.writeTo(fileToSave); System.out.println("File saved into " + fileToSave); }
From source file:org.openspotlight.storage.mongodb.MongoStorageSessionImpl.java
License:Open Source License
public byte[] readAsGridFS(final Partition partition, final Property property) throws Exception { final String key = getFileName(partition, property); final GridFS fs = getCachedGridFSForPartition(partition); final GridFSDBFile file = fs.findOne(key); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(file.getInputStream(), baos); return baos.toByteArray(); }
From source file:org.s1.mongodb.cluster.GridFSFileStorage.java
License:Apache License
@Override public FileStorage.FileReadBean read(Id id) throws NotFoundException { GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection()); GridFSDBFile o = fs.findOne(id.getEntity()); if (o == null) throw new NotFoundException("GridFS file not found: " + id); FileStorage.FileMetaBean fb = new FileStorage.FileMetaBean(); fb.fromMap(MongoDBFormat.toMap(o.getMetaData())); fb.setSize(o.getLength());//from ww w . j a v a 2 s . c om fb.setContentType(o.getContentType()); if (LOG.isDebugEnabled()) LOG.debug("Read file: " + id + ", meta:" + fb.toMap()); return new FileStorage.FileReadBean(o.getInputStream(), fb); }
From source file:org.sipfoundry.commons.userdb.profile.UserProfileServiceImpl.java
License:Open Source License
@Override public InputStream getAvatar(String userName) { GridFS avatarFS = new GridFS(m_template.getDb()); GridFSDBFile imageForOutput = avatarFS.findOne(String.format(AVATAR_NAME, userName)); if (imageForOutput != null) { return imageForOutput.getInputStream(); }//from ww w .ja va 2 s.c o m // try default avatar imageForOutput = avatarFS.findOne(String.format(AVATAR_NAME, "default")); if (imageForOutput != null) { return imageForOutput.getInputStream(); } return null; }
From source file:org.sipfoundry.commons.userdb.profile.UserProfileServiceImpl.java
License:Open Source License
@Override public void saveAvatar(String userName, InputStream originalIs, boolean overwriteIfExists) throws AvatarUploadException { GridFS avatarFS = new GridFS(m_template.getDb()); GridFSDBFile imageForOutput = avatarFS.findOne(String.format(AVATAR_NAME, userName)); if (imageForOutput == null || (imageForOutput != null && overwriteIfExists)) { saveAvatar(userName, originalIs); }//w w w. j a v a2 s. c o m }
From source file:org.teiid.translator.mongodb.MongoDBExecutionFactory.java
License:Open Source License
/** * @param field//from w w w .j a v a 2s . c om * @param expectedClass * @return * @throws TranslatorException */ public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException { if (value == null) { return null; } if (value.getClass().equals(expectedClass)) { return value; } if (value instanceof DBRef) { Object obj = ((DBRef) value).getId(); if (obj instanceof BasicDBObject) { BasicDBObject bdb = (BasicDBObject) obj; return bdb.get(colName); } return obj; } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) { return new java.sql.Date(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) { return new java.sql.Timestamp(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) { return new java.sql.Time(((java.util.Date) value).getTime()); } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) { return new BigDecimal((String) value); } else if (value instanceof String && expectedClass.equals(BigInteger.class)) { return new BigInteger((String) value); } else if (value instanceof String && expectedClass.equals(Character.class)) { return new Character(((String) value).charAt(0)); } else if (value instanceof String && expectedClass.equals(BinaryType.class)) { return new BinaryType(((String) value).getBytes()); } else if (value instanceof String && expectedClass.equals(Blob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof String && expectedClass.equals(Clob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new ClobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }, -1); } else if (value instanceof String && expectedClass.equals(SQLXML.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new SQLXMLImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof BasicDBList) { BasicDBList arrayValues = (BasicDBList) value; //array if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) { Class arrayType = expectedClass.getComponentType(); Object array = Array.newInstance(arrayType, arrayValues.size()); for (int i = 0; i < arrayValues.size(); i++) { Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName); Array.set(array, i, arrayItem); } value = array; } } else if (value instanceof org.bson.types.ObjectId) { org.bson.types.ObjectId id = (org.bson.types.ObjectId) value; value = id.toStringBabble(); } else { Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass); if (transform != null) { try { value = transform.transform(value, expectedClass); } catch (TransformationException e) { throw new TranslatorException(e); } } } return value; }
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
@Override public Boolean storeBlob(CallingContext context, String docPath, InputStream newContent, Boolean append) { GridFS gridFS = getGridFS(); GridFSInputFile file;/* w w w .j a v a2 s .c o m*/ if (!append) { gridFS.remove(docPath); file = createNewFile(docPath, newContent); } else { GridFSDBFile existing = gridFS.findOne(docPath); if (existing != null) { try { file = updateExisting(context, docPath, newContent, gridFS, existing); } catch (IOException e) { file = null; log.error(String.format("Error while appending to docPath %s: %s", docPath, ExceptionToString.format(e))); } } else { file = createNewFile(docPath, newContent); } } return file != null; }
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
@Override public Boolean deleteBlob(CallingContext context, String docPath) { GridFS gridFS = getGridFS(); String lockKey = createLockKey(gridFS, docPath); LockHandle lockHandle = grabLock(context, lockKey); boolean retVal = false; try {/* w w w.ja v a 2 s . c om*/ if (gridFS.findOne(docPath) != null) { gridFS.remove(docPath); retVal = true; } } finally { releaseLock(context, lockKey, lockHandle); } return retVal; }
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
@Override public InputStream getBlob(CallingContext context, String docPath) { GridFS gridFS = getGridFS(); String lockKey = createLockKey(gridFS, docPath); LockHandle lockHandle = grabLock(context, lockKey); InputStream retVal = null;//from w w w . j av a 2s . com try { GridFSDBFile file = gridFS.findOne(docPath); if (file != null) { retVal = file.getInputStream(); } } finally { releaseLock(context, lockKey, lockHandle); } return retVal; }
From source file:rmi_video.VideoServer.java
@Override public VideoData getVideo(String id) throws RemoteException { try {/*from w w w . j a v a 2s .co m*/ //Cria conexao com MongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); //Recupera o video atraves do ID GridFS gfsVideo = new GridFS(db, "video"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("videoId", id); GridFSDBFile videoForOutput = gfsVideo.findOne(whereQuery); String filename = videoForOutput.getFilename(); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + filename)) { videoForOutput.writeTo(fos); } Path path = Paths.get("/Users/philcr/Documents/" + filename); byte[] data = Files.readAllBytes(path); File videoFile = new File("/Users/philcr/Documents/" + filename); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("Video could not be deleted!"); } mongoClient.close(); return new VideoData(data, filename, id); } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } }