@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));
  }
Esempio n. 2
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());
    }
  }
Esempio n. 3
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());
    }
  }
  /**
   * 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));
  }
Esempio n. 5
0
  @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());
  }
  /**
   * Put the Song and data file associated with fileLocation in the database.
   *
   * @param song
   * @param fileLocation
   * @param contentType
   * @param db
   * @return
   */
  public static GridFSInputFile putSongInDB(
      Song song, String fileLocation, String contentType, DB db) {
    DBCollection collection = db.getCollection("songs");

    Object id = null;
    File file = new File(fileLocation.replace(".tmp", ".mp3"));
    //	"."+contentType.substring(contentType.indexOf("/")+1)));

    GridFSInputFile gridFSInputFile =
        putSongFileInDB(file, db, contentType, song.getFilename(), id);

    song.setFileId(gridFSInputFile.getId());
    collection.insert(song); // insert corresponding json doc
    log.info("song inserted: " + song.toString());

    if (!file.delete()) { // delete the tmp file
      log.warn("file not deleted: " + file.getPath());
    }
    return gridFSInputFile;
  }
Esempio n. 7
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() + "\"}"));
  }
  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(); */
  }
Esempio n. 10
0
 public String getId() {
   return input.getId().toString();
 }
  @Test
  public void testImportAttachment() throws Exception {
    logger.debug("*** testImportAttachment ***");
    try {
      // createDatabase();
      byte[] content = copyToBytesFromClasspath(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));

      GetResponse getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get();
      logger.debug("Get request for id {}: {}", id, getResponse.isExists());
      assertThat(getResponse.isExists(), equalTo(true));
      //            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();

      getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get();
      logger.debug("Get request for id {}: {}", id, getResponse.isExists());
      assertThat(getResponse.isExists(), equalTo(false));
      //            countResponse =
      // getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id",
      // id))).actionGet();
      //            logger.debug("Count after delete request: {}", countResponse.getCount());
      //            assertThat(countResponse.getCount(), equalTo(0L));
    } catch (Throwable t) {
      logger.error("testImportAttachment failed.", t);
      Assert.fail("testImportAttachment failed", t);
    } finally {
      // cleanUp();
    }
  }
Esempio n. 12
0
  private void uploadMapToServer(RoomMap newMap) {

    Mongo mongo = null;
    DB database;
    DBCollection sampleData;

    String databaseAddress = DEFAULT_DATABASE_ADDRESS;

    try { // read parameters from the configuration file
      databaseAddress = Utilities.getConfigurationValue(DATABASE_ADDRESS);

    } catch (NumberFormatException exception) { // reading has failed, use default values
      logger.info(
          "Reading parameters from configuration file failed. "
              + "Using default values for database_address instead.");
    }

    try {
      mongo = new Mongo(databaseAddress);
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    }

    database = mongo.getDB("rssiSystem");
    sampleData = database.getCollection("map_records");
    // remove all maps
    sampleData.drop();

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    String strDate = simpledateformat.format(new Date());

    // -------------------
    // Load our image
    byte[] imageBytes = null;
    try {
      imageBytes = Utilities.LoadImageAsBytes(newMap.getPath());
    } catch (Exception e1) {
      e1.printStackTrace();
    }

    // ---------------------

    // Create GridFS object
    GridFS fs = new GridFS(database);
    // Save image into database
    GridFSInputFile in = fs.createFile(imageBytes);
    in.save();
    Object mapIdObject = in.getId();
    // System.out.println(mapIdObject);

    try {

      DBObject documentDetail = new BasicDBObject();

      documentDetail.put("_cls", "mapRecords"); // for mongoEngine ORM users
      documentDetail.put("image", mapIdObject);
      documentDetail.put("mapId", newMap.getId());
      documentDetail.put("width", newMap.getWidthInMeters());
      documentDetail.put("height", newMap.getHeightInMeters());
      documentDetail.put("offsetX", newMap.getLowerLeftMarkerOffsetXInPixels());
      documentDetail.put("offsetY", newMap.getLowerLeftMarkerOffsetYInPixels());
      documentDetail.put("offset2X", newMap.getUpperRightMarkerOffsetXInPixels());
      documentDetail.put("offset2Y", newMap.getUpperRightMarkerOffsetYInPixels());
      documentDetail.put("scalingX", newMap.getRatioWidth());
      documentDetail.put("scalingY", newMap.getRatioHeight());
      documentDetail.put("title", newMap.getTitle());
      documentDetail.put("updateTime", strDate);

      sampleData.insert(documentDetail);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }