Example usage for com.mongodb.gridfs GridFSDBFile getInputStream

List of usage examples for com.mongodb.gridfs GridFSDBFile getInputStream

Introduction

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

Prototype

public InputStream getInputStream() 

Source Link

Document

Returns an InputStream from which data can be read.

Usage

From source file:com.photon.phresco.service.impl.DbService.java

License:Apache License

protected InputStream getFileFromDB(String id) throws PhrescoException {
    if (isDebugEnabled) {
        LOGGER.debug("DbService.getFileFromDB:Entry");
        if (StringUtils.isEmpty(id)) {
            LOGGER.warn("DbService.getFileFromDB", STATUS_BAD_REQUEST, "message=\"id is empty\"");
            throw new PhrescoException("id is empty");
        }//from www . ja va 2s .c o m
        LOGGER.info("DbService.getFileFromDB", "id=\"" + id + "\"");
    }
    GridFSDBFile imageForOutput = getGridFs().findOne(id);
    InputStream inputStream = imageForOutput.getInputStream();
    if (isDebugEnabled) {
        LOGGER.debug("DbService.getFileFromDB:Exit");
    }
    return inputStream;
}

From source file:com.pubkit.platform.persistence.impl.ApplicationDaoImpl.java

License:Open Source License

public InputStream getFileAsStream(String fileId) {
    GridFS gridFs = new GridFS(mongoTemplate.getDb(), PK_FILES_BUCKET);
    GridFSDBFile savedFile = gridFs.find(new ObjectId(fileId));

    return savedFile.getInputStream();
}

From source file:com.seajas.search.contender.service.storage.StorageService.java

License:Open Source License

/**
 * Retrieve a CompositeEntry document by its ID.
 *
 * @param id//www. j  ava2  s  .c om
 * @return CompositeEntry
 */
public CompositeEntry retrieveEntryById(final ObjectId id) {
    CompositeEntry entry = mongoTemplate.findById(id, CompositeEntry.class, defaultCollection);

    if (entry != null && !EnumSet.of(CompositeState.Source, CompositeState.SourceElement)
            .contains(entry.getCurrentState())) {
        if (entry.getOriginalContent() != null) {
            GridFSDBFile file = gridFs.find(entry.getOriginalContent().getId());

            entry.getOriginalContent().setContent(new BufferedInputStream(file.getInputStream()));
        } else if (entry.getModifiedContent() != null) {
            GridFSDBFile file = gridFs.find(entry.getModifiedContent().getId());

            entry.getModifiedContent().setContent(new BufferedInputStream(file.getInputStream()));
        } else
            logger.error(
                    "Content retrieval is relevant given this entry's current state, but no content has been stored");
    }

    return entry;
}

From source file:com.seajas.search.contender.service.storage.StorageService.java

License:Open Source License

/**
 * Retrieve a CompositeEntry document by its ID.
 *
 * @param compositeUrl//ww  w. jav  a2s  .  c  o m
 * @return CompositeEntry
 */
public CompositeEntry retrieveEntryByCompositeUrl(final String compositeUrl) {
    Query query = new Query().addCriteria(where("enricherElement._id").is(compositeUrl));

    CompositeEntry entry = mongoTemplate.findOne(query, CompositeEntry.class, defaultCollection);

    if (entry != null && !EnumSet.of(CompositeState.Source, CompositeState.SourceElement)
            .contains(entry.getCurrentState())) {
        if (entry.getOriginalContent() != null) {
            GridFSDBFile file = gridFs.find(entry.getOriginalContent().getId());

            entry.getOriginalContent().setContent(new BufferedInputStream(file.getInputStream()));
        } else if (entry.getModifiedContent() != null) {
            GridFSDBFile file = gridFs.find(entry.getModifiedContent().getId());

            entry.getModifiedContent().setContent(new BufferedInputStream(file.getInputStream()));
        } else
            logger.error(
                    "Content retrieval is relevant given this entry's current state, but no content has been stored");
    }

    return entry;
}

From source file:com.seajas.search.profiler.service.repository.RepositoryService.java

License:Open Source License

/**
 * Retrieve a resource using the given document path.
 * /* w w w.  jav a  2  s  .com*/
 * @param entryId
 * @return RepositoryContent
 */
public RepositoryContent getResource(final String entryId) {
    CompositeEntry entry = mongoTemplate.findById(new ObjectId(entryId), CompositeEntry.class,
            defaultCollection);

    if (entry.getOriginalContent() == null)
        throw new IllegalArgumentException("No original content was set for resource identified by " + entryId);

    GridFSDBFile dbFile = gridFs.find(entry.getOriginalContent().getId());

    return new RepositoryContent(dbFile.getInputStream(), entry.getOriginalContent().getMediaType());
}

From source file:com.trenako.web.images.WebImageServiceImpl.java

License:Apache License

@Override
public ResponseEntity<byte[]> renderImage(String imageSlug) {
    GridFSDBFile img = repo.findFileBySlug(imageSlug);
    if (img == null) {
        throw new NotFoundException();
    }/*from   ww  w.j  a v  a 2s.co m*/

    InputStream in = img.getInputStream();
    MediaType mediaType = parse(img.getContentType());

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(mediaType);

    try {
        return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
    } catch (IOException ioEx) {
        throw new UploadRenderingException("Error occurred rendering the file.", ioEx);
    }
}

From source file:data.repository.pragma.DataObjectController.java

License:Apache License

@RequestMapping("/DO/update/metadata")
@ResponseBody//from  w  ww .j av a  2 s . com
public MessageResponse setMetadata(@RequestParam(value = "ID", required = true) String id,
        @RequestBody String metadata) {
    // Connect to MongoDB and set metadata as curation purposes
    // Return a new document ID

    try {
        // Get original Grid FS file
        GridFSDBFile doc = Staging_repository.findDOByID(id);

        // Form updated metadata object
        DBObject metadataObject = (DBObject) JSON.parse(metadata);
        // Push back updated DO to MongoDB and get a new doc ID
        String updated_id = Staging_repository.addDO(doc.getInputStream(), doc.getFilename(),
                doc.getContentType(), metadataObject);
        MessageResponse response = new MessageResponse(true, updated_id);
        return response;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        MessageResponse response = new MessageResponse(false, null);
        return response;
    }
}

From source file:data.repository.pragma.DataObjectController.java

License:Apache License

@RequestMapping(value = "/DO/add", method = RequestMethod.POST)
@ResponseBody/*from w w  w.  ja  va  2  s  . c  o  m*/
public MessageResponse DOadd(@RequestParam(value = "ID", required = true) String id) {

    // Connect to Mongo staging DB and get DO information

    try {
        if (Staging_repository.existDOByID(id)) {
            GridFSDBFile doc = Staging_repository.findDOByID(id);

            // Transfer DO from staging database to permanent repository
            // DO in repo can create and read, but update and delete
            // operation is disallowed.
            String repo_id = permanent_repository.addDO(doc.getInputStream(), doc.getFilename(),
                    doc.getContentType(), doc.getMetaData());

            // Return message response with registered PID record
            MessageResponse response = new MessageResponse(true, repo_id);
            return response;
        } else {
            MessageResponse response = new MessageResponse(false, null);
            return response;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println(e.toString());
        MessageResponse response = new MessageResponse(false, null);
        return response;
    }
}

From source file:de.fhg.igd.mongomvcc.impl.DefaultConvertStrategy.java

License:Open Source License

@Override
public Object convert(long oid) throws IOException {
    GridFSDBFile file = _gridFS.findOne(new BasicDBObject(MongoDBVLargeCollection.OID, oid));
    if (file == null) {
        return null;
    }//from www  .j av  a 2  s  .c om
    int type = (Integer) file.get(BINARY_TYPE);
    Object r;
    if (type == BYTEARRAY) {
        r = toByteArray(file);
    } else if (type == INPUTSTREAM) {
        r = file.getInputStream();
    } else if (type == BYTEBUFFER) {
        r = ByteBuffer.wrap(toByteArray(file));
    } else if (type == FLOATARRAY) {
        r = toFloatArray(file);
    } else if (type == FLOATBUFFER) {
        r = FloatBuffer.wrap(toFloatArray(file));
    } else {
        //no information. simply forward the input stream
        r = file.getInputStream();
    }
    return r;
}

From source file:de.fhg.igd.mongomvcc.impl.DefaultConvertStrategy.java

License:Open Source License

/**
 * Converts the contents of a GridFS file to a byte array
 * @param file the file//from  w w w  . ja  va  2s  . co m
 * @return the byte array
 * @throws IOException if the file could not be read
 */
private byte[] toByteArray(GridFSDBFile file) throws IOException {
    InputStream is = file.getInputStream();
    int len = (int) file.getLength();
    int pos = 0;
    byte[] b = new byte[len];
    while (len > 0) {
        int read = is.read(b, pos, len);
        pos += read;
        len -= read;
    }
    return b;
}