Exemple #1
0
  public List<String> findDocuments(String id) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);
      String key = "metadata." + DocumentConnector.ID;
      BasicDBObject query = new BasicDBObject(key, id);
      List<GridFSDBFile> gridFSDBFiles = gridFS.find(query);

      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      List<String> objects = new ArrayList<String>();
      for (GridFSDBFile gridFSDBFile : gridFSDBFiles) {
        ObjectId objectId = (ObjectId) gridFSDBFile.getId();
        objects.add(objectId.toStringMongod());
      }

      return objects;
    } catch (Exception e) {
      log.error("findDocuments error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #2
0
  public Photo saveImg(FileItem item) throws Exception {
    Photo photo = new Photo();
    String filename = item.getName();
    if (filename == null || filename.length() == 0) {
      log.error("img name illegal");
      return null;
    }
    int index = filename.lastIndexOf(".");
    String type = filename.substring(index + 1);
    if (!ImgTools.checkImgFormatValidata(type)) {
      log.error("img type illegal");
      return null;
    }
    ObjectId id = ObjectIdGenerator.generate();
    // filename = new ObjectId() + filename.substring(index);
    photo.setId(id.toString());
    photo.setType(type);

    GridFS mphoto = new GridFS(MongoDBPool.getInstance().getDB(), collection);
    GridFSInputFile in = null;
    in = mphoto.createFile(item.getInputStream());
    in.setId(id);
    in.setFilename(id.toString());
    in.setContentType(type);
    in.save();
    item.getInputStream().close();
    return photo;
  }
Exemple #3
0
  public String newDocument(InputStream in, String json) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      GridFSInputFile gridFSInputFile = gridFS.createFile(in);
      ObjectId objectId = (ObjectId) gridFSInputFile.getId();
      String GUID = objectId.toStringMongod();

      DBObject dbObject = (DBObject) JSON.parse(json);

      gridFSInputFile.setFilename((String) dbObject.get(NAME));
      gridFSInputFile.setContentType((String) dbObject.get(CONTENT_TYPE));
      gridFSInputFile.setMetaData(dbObject);
      gridFSInputFile.save();
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      return GUID;
    } catch (Exception e) {
      log.error("newDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
 @Override
 public void saveAvatar(String userName, InputStream originalIs) throws AvatarUploadException {
   ByteArrayOutputStream os = null;
   InputStream is = null;
   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);
   }
 }
Exemple #5
0
  @Override
  public void reset() {
    super.reset();
    try {
      String ritePropertiesFilename = Rite.getInstance().getProperty(Rite.PropertyKeys.HOST);
      Properties hostProps = new Properties();
      hostProps.load(new FileInputStream(ritePropertiesFilename));
      String hostname = hostProps.getProperty("hostname");
      int port = Integer.parseInt(hostProps.getProperty("port"));
      String dbname = hostProps.getProperty("dbname");
      boolean auth = Boolean.parseBoolean(hostProps.getProperty("auth"));
      Mongo mongo = new Mongo(hostname, port);
      DB db = mongo.getDB(dbname);
      if (auth) {
        String user = hostProps.getProperty("user");
        String pass = hostProps.getProperty("pass");
        db.authenticate(user, pass.toCharArray());
      }

      GridFS gfs = new GridFS(db);
      String filename = getFileName();
      File f = new File(filename);
      f.delete();
      gfs.remove(filename);
      mongo.close();
    } catch (Exception e) {
      this.fail();
      this.complete();
    }
  }
Exemple #6
0
  public String getDocument(String GUID, OutputStream out) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      ObjectId key = new ObjectId(GUID);
      GridFSDBFile gridFSDBFile = gridFS.find(key);

      if (gridFSDBFile == null) {
        throw new DocumentException("No existe el documento");
      }
      if (out != null) gridFSDBFile.writeTo(out);
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      DBObject dbObject = gridFSDBFile.getMetaData();
      return JSON.serialize(dbObject);
    } catch (Exception e) {
      log.error("getDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #7
0
  public String newDocument(InputStream in, Map<String, String> properties)
      throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      GridFSInputFile gridFSInputFile = gridFS.createFile(in);
      ObjectId id = (ObjectId) gridFSInputFile.getId();
      String GUID = id.toStringMongod();

      gridFSInputFile.setFilename(properties.get(DocumentConnector.NAME));
      gridFSInputFile.setContentType(properties.get(DocumentConnector.CONTENT_TYPE));
      gridFSInputFile.put(DocumentConnector.ID, properties.get(DocumentConnector.ID));
      gridFSInputFile.put(
          DocumentConnector.DOCUMENT_TYPE, properties.get(DocumentConnector.DOCUMENT_TYPE));
      gridFSInputFile.save();
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      return GUID;
    } catch (Exception e) {
      log.error("newDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
  @Test
  public void testImportAttachment() throws Exception {
    logger.debug("*** testImportAttachment ***");
    byte[] content =
        copyToBytesFromClasspath(
            "/test/elasticsearch/plugin/river/mongodb/gridfs/test-attachment.html");
    logger.debug("Content in bytes: {}", content.length);
    GridFS gridFS = new GridFS(mongoDB);
    GridFSInputFile in = gridFS.createFile(content);
    in.setFilename("test-attachment.html");
    in.setContentType("text/html");
    in.save();
    in.validate();

    String id = in.getId().toString();
    logger.debug("GridFS in: {}", in);
    logger.debug("Document created with id: {}", id);

    GridFSDBFile out = gridFS.findOne(in.getFilename());
    logger.debug("GridFS from findOne: {}", out);
    out = gridFS.findOne(new ObjectId(id));
    logger.debug("GridFS from findOne: {}", out);
    Assert.assertEquals(out.getId(), in.getId());

    Thread.sleep(wait);
    refreshIndex();

    CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet();
    logger.debug("Index total count: {}", countResponse.getCount());
    assertThat(countResponse.getCount(), equalTo(1l));

    countResponse =
        getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", id))).actionGet();
    logger.debug("Index count for id {}: {}", id, countResponse.getCount());
    assertThat(countResponse.getCount(), equalTo(1l));

    SearchResponse response =
        getNode()
            .client()
            .prepareSearch(getIndex())
            .setQuery(QueryBuilders.queryString("Aliquam"))
            .execute()
            .actionGet();
    logger.debug("SearchResponse {}", response.toString());
    long totalHits = response.getHits().getTotalHits();
    logger.debug("TotalHits: {}", totalHits);
    assertThat(totalHits, equalTo(1l));

    gridFS.remove(new ObjectId(id));

    Thread.sleep(wait);
    refreshIndex();

    countResponse =
        getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", id))).actionGet();
    logger.debug("Count after delete request: {}", countResponse.getCount());
    assertThat(countResponse.getCount(), equalTo(0L));
  }
Exemple #9
0
 public GridFSDBFile getImg(String id) {
   log.info("getting img:" + id + " from mongo");
   if (id == null || id.length() == 0) {
     log.info("img filename null");
     return null;
   }
   GridFS photo = new GridFS(MongoDBPool.getInstance().getDB(), collection);
   return photo.findOne(id);
 }
 @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);
   }
 }
  /**
   * Update the GridFSDBFile in the associated DB with the key/values in updateKeys
   *
   * @param updateKeys Map of new tag data
   * @param file GridFSDBFile to update with tag data
   * @param db
   * @param songId ID of Song to update with tag data
   * @return
   */
  public static boolean updateFile(
      Map<String, String> updateKeys,
      GridFSDBFile file,
      DB db,
      ObjectId songId) { // TODO updateKeys?
    File audioFile = null;
    try {
      audioFile = File.createTempFile("tmp", ".mp3");
    } catch (IOException e) {
      log.error("tmp file not created", e);
    }

    audioFile.deleteOnExit();
    AudioFile f = null;
    ObjectId id = (ObjectId) file.getId();
    ObjectId oid = null;
    try {
      file.writeTo(audioFile);
      f = AudioFileIO.read(audioFile);
      Tag tag = f.getTagOrCreateAndSetDefault();
      DBObject q = new BasicDBObject("_id", songId);
      DBObject o = new BasicDBObject("$set", new BasicDBObject(updateKeys));

      if (updateKeys.get("artist") != null) {
        tag.setField(FieldKey.ARTIST, updateKeys.get("artist"));
      }
      if (updateKeys.get("album") != null) {
        tag.setField(FieldKey.ALBUM, updateKeys.get("album"));
      }
      if (updateKeys.get("title") != null) {
        tag.setField(FieldKey.TITLE, updateKeys.get("title"));
      }
      if (updateKeys.get("track") != null) {
        tag.setField(FieldKey.TRACK, updateKeys.get("track"));
      }
      if (updateKeys.get("year") != null) {
        tag.setField(FieldKey.YEAR, updateKeys.get("year"));
      }
      AudioFileIO.write(f);
      GridFS myFS = new GridFS(db);
      myFS.remove(id);
      GridFSInputFile inputFile =
          putSongFileInDB(f.getFile(), db, file.getContentType(), file.getFilename(), id);
      oid = (ObjectId) inputFile.getId();
      if (oid.equals(id)) {
        db.getCollection("songs").update(q, o);
      }
    } catch (KeyNotFoundException knfe) {
      log.error("key not found", knfe);
    } catch (FieldDataInvalidException fdie) {
      log.error("tried to set field with invalid value", fdie);
    } catch (Exception e) {
      log.error("error reading/writing file", e);
    }
    return (oid.equals(id));
  }
  @Override
  public FileEntity findFileByObjectId(String filePath, String objectId) {
    GridFS gridFS = new GridFS(db, filePath);

    GridFSDBFile dbFile = gridFS.find(new ObjectId(objectId));
    if (dbFile == null) {
      return null;
    }

    FileEntity entity = new FileEntity(dbFile);
    return entity;
  }
 @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();
   }
   // try default avatar
   imageForOutput = avatarFS.findOne(String.format(AVATAR_NAME, "default"));
   if (imageForOutput != null) {
     return imageForOutput.getInputStream();
   }
   return null;
 }
Exemple #14
0
 @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);
   }
   return Response.ok(
           org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType())
       .build();
 }
  @Test
  public void shouldStoreFileInMultipleChunks() throws Exception {
    final byte[] data = new byte[] {1, 2, 3, 4, 5};

    final GridFSInputFile file = fs.createFile(data);
    file.setChunkSize(3); // chunk size is less than data size in order to get more than one chunk
    file.save();

    final GridFSDBFile result = fs.findOne((ObjectId) file.getId());

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    assertEquals(data.length, result.writeTo(out));

    assertArrayEquals(data, out.toByteArray());
  }
 private GridFSDBFile getFile(final MD5 md5) {
   final GridFSDBFile out;
   final DBObject query = new BasicDBObject();
   query.put(Fields.MONGO_ID, md5.getMD5());
   out = gfs.findOne(query);
   return out;
 }
Exemple #17
0
  public void deleteDocument(String GUID) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      ObjectId key = new ObjectId(GUID);
      gridFS.remove(key);
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }
    } catch (Exception e) {
      log.error("deleteDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
  @Override
  public List<FileEntity> findFileByName(
      String filePath, String fileName, EnumQueryPattern qpattern) {
    GridFS gridFS = new GridFS(db, filePath);
    BasicDBObject query = new BasicDBObject();
    Pattern pattern =
        Pattern.compile(
            EnumQueryPattern.getMatchPatternStr(qpattern, fileName), Pattern.CASE_INSENSITIVE);
    query.append("filename", pattern);
    List<GridFSDBFile> dbFiles = gridFS.find(query);
    List<FileEntity> files = new ArrayList<FileEntity>();
    for (GridFSDBFile dbFile : dbFiles) {
      FileEntity file = new FileEntity(dbFile);
      files.add(file);
    }

    return files;
  }
 @Override
 public void removeBlob(MD5 md5) throws BlobStoreCommunicationException {
   final DBObject query = new BasicDBObject();
   query.put(Fields.MONGO_ID, md5.getMD5());
   try {
     gfs.remove(query);
   } catch (MongoException me) {
     throw new BlobStoreCommunicationException("Could not write to the mongo database", me);
   }
 }
Exemple #20
0
  @Override
  public Operation call() throws Exception {
    try {
      String ritePropertiesFilename = Rite.getInstance().getProperty(Rite.PropertyKeys.HOST);
      Properties hostProps = new Properties();
      hostProps.load(new FileInputStream(ritePropertiesFilename));
      String hostname = hostProps.getProperty("hostname");
      int port = Integer.parseInt(hostProps.getProperty("port"));
      String dbname = hostProps.getProperty("dbname");
      boolean auth = Boolean.parseBoolean(hostProps.getProperty("auth"));
      Mongo mongo = new Mongo(hostname, port);
      DB db = mongo.getDB(dbname);
      if (auth) {
        String user = hostProps.getProperty("user");
        String pass = hostProps.getProperty("pass");
        db.authenticate(user, pass.toCharArray());
      }

      GridFS gfs = new GridFS(db);
      String filename = getFileName();
      File f = new File(filename);
      if (!f.exists()) {
        throw new Exception("The file " + filename + " does not exist locally!");
      }
      int filesInDb = gfs.find(filename).size();
      if (filesInDb > 0) {
        throw new Exception("The file " + filename + " already exists in the database!");
      }
      GridFSInputFile gsampleFile = gfs.createFile(f);
      gsampleFile.setFilename(f.getName());
      gsampleFile.save();
      mongo.close();
    } catch (Exception e) {
      this.setProperty(
          GenericOperation.PropertyKeys.ERROR, OperationUtilities.getStackTraceAsString(e));
      this.fail();
      this.complete();
      return this;
    }
    this.complete();
    return this;
  }
Exemple #21
0
 public static Result getBeerImg(String id) {
   GridFSDBFile img = gfs.find(new ObjectId(id));
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try {
     img.writeTo(baos);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return ok(baos.toByteArray()).as("image/png");
 }
Exemple #22
0
  public static void main(String[] args) throws Exception {

    MongoClient client = new MongoClient();
    DB db = client.getDB("course");
    FileInputStream inputStream = null;

    GridFS videos =
        new GridFS(db, "video"); // does not create new GridFS it just gives object to manipulate it
    // returns bucket named video
    try {

      inputStream = new FileInputStream("BinarySearch.mp4");

    } catch (FileNotFoundException e) {

      e.printStackTrace();
    }

    GridFSInputFile video = videos.createFile(inputStream, "BinarySearch.mp4");

    BasicDBObject meta = new BasicDBObject("description", "Binary Search");

    ArrayList<String> tags = new ArrayList<String>();
    tags.add("search");
    tags.add("data structures");

    meta.append("tags", tags);

    video.setMetaData(meta);
    video.save();

    System.out.println(video.get("_id"));
    System.out.println("file saved");
    System.out.println("reading file from mongo");

    GridFSDBFile dbFile = videos.findOne(new BasicDBObject("filename", "BinarySearch.mp4"));

    FileOutputStream outputStream = new FileOutputStream("BinarySearch_copy.mp4");

    dbFile.writeTo(outputStream);
  }
  /**
   * @param file
   * @param db
   * @param contentType
   * @param filename
   * @param id
   * @return
   */
  private static GridFSInputFile putSongFileInDB(
      File file, DB db, String contentType, String filename, Object id) {
    byte[] songAsBytes = null;
    GridFS myFS = new GridFS(db);
    GridFSInputFile gridFSInputFile = null;
    try {
      songAsBytes = FileUtils.readFileToByteArray(file);
      gridFSInputFile = myFS.createFile(songAsBytes);
      gridFSInputFile.setFilename(filename);
      gridFSInputFile.setContentType(contentType);
      if (id != null) {
        gridFSInputFile.put("_id", id);
      }

      gridFSInputFile.save(); // insert file
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return gridFSInputFile;
  }
  public FileEntity saveFile(
      String filePath,
      String fileName,
      String contentType,
      InputStream inputStream,
      String objectId) {

    if (filePath == null) filePath = "fs";
    GridFS gridFS = new GridFS(db, filePath);
    GridFSInputFile gfsFile = gridFS.createFile(inputStream);
    gfsFile.setFilename(fileName);
    gfsFile.setContentType(contentType);
    if (objectId != null) gfsFile.setId(new ObjectId(objectId));
    // String objectId = gfsFile.getId().toString();
    gfsFile.save();

    GridFSDBFile dbFile = gridFS.findOne(new ObjectId(gfsFile.getId().toString()));

    FileEntity entity = new FileEntity(dbFile);

    return entity;
  }
  /**
   * @param args
   * @throws UnknownHostException
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("test");
    String newFileName = "mkyong-java-image5";
    File imageFile = new File("c:\\mongodb.jpg");
    GridFS gfsPhoto = new GridFS(db);
    GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
    gfsFile.setFilename(newFileName);
    gfsFile.setContentType("image/jpeg");
    gfsFile.save();
    /// System.out.println(gfsFile.getId());

    gfsFile = gfsPhoto.createFile(imageFile);
    gfsFile.setFilename(newFileName);
    gfsFile.setContentType("image/jpeg");
    gfsFile.save();
    System.out.println(gfsFile.getId());

    /*  RegistryKey rg = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(suffix);
    object obj=rg.GetValue("Content Type");
    result =obj!=null?obj.ToString():string.Empty;
    rg.Close(); */
  }
 @Override
 public List<DependencyStatus> status() {
   // note failures are tested manually for now, if you make changes test
   // things still work
   // TODO TEST add tests exercising failures
   final String version;
   try {
     final CommandResult bi = gfs.getDB().command("buildInfo");
     version = bi.getString("version");
   } catch (MongoException e) {
     LoggerFactory.getLogger(getClass()).error("Failed to connect to MongoDB", e);
     return Arrays.asList(
         new DependencyStatus(
             false, "Couldn't connect to MongoDB: " + e.getMessage(), "GridFS", "Unknown"));
   }
   return Arrays.asList(new DependencyStatus(true, "OK", "GridFS", version));
 }
 @Override
 public void saveBlob(final MD5 md5, final InputStream data, final boolean sorted)
     throws BlobStoreCommunicationException {
   if (data == null || md5 == null) {
     throw new NullPointerException("Arguments cannot be null");
   }
   if (getFile(md5) != null) {
     return; // already exists
   }
   final GridFSInputFile gif = gfs.createFile(data, true);
   gif.setId(md5.getMD5());
   gif.setFilename(md5.getMD5());
   gif.put(Fields.GFS_SORTED, sorted);
   try {
     gif.save();
   } catch (DuplicateKeyException dk) {
     // already here, done
   } catch (MongoException me) {
     throw new BlobStoreCommunicationException("Could not write to the mongo database", me);
   }
 }
Exemple #28
0
  @BodyParser.Of(BodyParser.Json.class)
  public static Result newBeer() {
    GridFSInputFile gfsImg = null;
    ObjectMapper mapper = new ObjectMapper();
    Beer beer = null;
    try {
      beer = mapper.readValue(request().body().asJson(), Beer.class);
      beers.save(beer);
    } catch (JsonParseException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (JsonMappingException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {
      BufferedImage img =
          ImageIO.read(new URL("http://wwwimages.harpoonbrewery.com/SummerBeer-2013-Modal.jpg"));
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ImageIO.write(img, "jpg", baos);
      baos.flush();
      gfsImg = gfs.createFile(baos.toByteArray());
      gfsImg.setFilename("bestbeer.jpg");
      gfsImg.save();
      beer.imgId = gfsImg.getId().toString();
      beers.save(beer);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return ok(Json.parse("{\"beerId\":\"" + beer.getId() + "\"}"));
  }
 @Override
 public void deleteFileByFileName(String filePath, String fileName) {
   GridFS gridFS = new GridFS(db, filePath);
   gridFS.remove(fileName);
 }
 @Override
 public void deleteFileByObjectId(String filePath, String objectId) {
   GridFS gridFS = new GridFS(db, filePath);
   gridFS.remove(new ObjectId(objectId));
 }