Example usage for com.mongodb.gridfs GridFS GridFS

List of usage examples for com.mongodb.gridfs GridFS GridFS

Introduction

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

Prototype

public GridFS(final DB db) 

Source Link

Document

Creates a GridFS instance for the default bucket "fs" in the given database.

Usage

From source file:org.sipfoundry.commons.userdb.profile.UserProfileServiceImpl.java

License:Open Source License

@Override
public void saveAvatar(String userName, InputStream originalIs) throws AvatarUploadException {
    ByteArrayOutputStream os = null;
    InputStream is = null;//from  www  .j  a v  a 2 s . com
    try {
        BufferedImage originalImage = ImageIO.read(originalIs);
        BufferedImage thumbnail = Thumbnails.of(originalImage).crop(Positions.CENTER).size(128, 128)
                .asBufferedImage();
        os = new ByteArrayOutputStream();
        ImageIO.write(thumbnail, "png", os);
        is = new ByteArrayInputStream(os.toByteArray());
        String fileName = String.format(AVATAR_NAME, userName);
        GridFS avatarFS = new GridFS(m_template.getDb());
        avatarFS.remove(fileName);
        GridFSInputFile gfsFile = avatarFS.createFile(is);
        gfsFile.setFilename(fileName);
        gfsFile.save();
    } catch (Exception ex) {
        throw new AvatarUploadException(ex);
    } finally {
        IOUtils.closeQuietly(originalIs);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

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);
    }/*from   w  ww  . j av a 2  s .c  om*/
}

From source file:org.springframework.data.mongodb.gridfs.GridFsTemplate.java

License:Apache License

private GridFS getGridFs() {
    DB db = dbFactory.getDb();
    return bucket == null ? new GridFS(db) : new GridFS(db, bucket);
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreService.java

License:Open Source License

@Autowired(required = true)
public MongoMessageStoreService(MongoDbFactory mongoDbFactory) {
    gridFs = new GridFS(mongoDbFactory.getDb());

    this.shsMessageMarshaller = new ShsMessageMarshaller();
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

@Test(groups = "largeTests", enabled = true)
public void testSave() {
    final ShsMessageEntry entry = make(a(ShsMessageEntryMaker.ShsMessageEntry));
    final se.inera.axel.shs.mime.ShsMessage shsMessage = make(a(ShsMessage));

    messageStore.save(entry, shsMessage);

    mongoOperations.execute(new DbCallback<Object>() {
        @Override//  ww  w .j a  v  a 2  s .  co m
        public Object doInDB(DB db) throws MongoException, DataAccessException {
            GridFS gridFs = new GridFS(db);

            assertNotNull(gridFs.findOne(entry.getId()), "Saved file was not found in grid");

            return null;
        }
    });
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

@Test(groups = "largeTests", enabled = true)
public void existingFileShouldBeDeleted() {
    messageStore.delete(entry1);/*from   w  w w.j a va2  s  . c  o  m*/

    mongoOperations.execute(new DbCallback<Object>() {
        @Override
        public Object doInDB(DB db) throws MongoException, DataAccessException {
            GridFS gridFs = new GridFS(db);

            assertNull(gridFs.findOne(entry1.getId()), "Entry was not deleted");

            return null;
        }
    });
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

@Test(groups = "largeTests", enabled = true)
public void deletingNonExistingFileShouldBeANoOp() {
    messageStore.delete(make(a(ShsMessageEntryMaker.ShsMessageEntry)));

    mongoOperations.execute(new DbCallback<Object>() {
        @Override//from   w w  w . j  a v  a2  s  .  c om
        public Object doInDB(DB db) throws MongoException, DataAccessException {
            GridFS gridFs = new GridFS(db);
            DBCursor dbCursor = gridFs.getFileList();

            assertThat(dbCursor.count(), is(0));

            return null;
        }
    });
}

From source file:se.inera.axel.shs.broker.messagestore.internal.MongoMessageStoreServiceIT.java

License:Open Source License

private void saveMessage(ShsMessageEntry entry, ShsMessage message, DB db) {
    GridFS gridFs = new GridFS(db);
    GridFSInputFile inputFile = gridFs.createFile(entry.getId());

    OutputStream out = null;/*  w w  w.  jav a  2  s  .co  m*/
    try {
        out = inputFile.getOutputStream();

        new ShsMessageMarshaller().marshal(message, out);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

From source file:streamflow.datastore.mongodb.impl.MongoGridFsFileContentDao.java

License:Apache License

@Inject
public MongoGridFsFileContentDao(Mongo mongo, DatastoreConfig datastoreConfig) {
    String dbName = (String) datastoreConfig.properties().get("dbName");
    if (dbName == null || dbName.isEmpty()) {
        dbName = "streamflow";
    }/*from  w w w  .j  a v  a2 s .  c  o  m*/

    DB db = mongo.getDB(dbName);

    gridFs = new GridFS(db);
}

From source file:xbdd.webapp.resource.feature.Attachment.java

License:Apache License

@GET
@Path("/{id}")
public javax.ws.rs.core.Response getAttachment(@PathParam("id") final String id) throws IOException {
    final DB db = this.client.getDB("grid");
    final GridFS gridFS = new GridFS(db);
    final GridFSDBFile file = gridFS.findOne(id);
    // log.info(file);
    if (file == null) {
        throw new WebApplicationException(404);
    }/*from w  w  w . ja  v a2  s  .c o m*/
    return Response.ok(org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType())
            .build();

}