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:org.teiid.translator.mongodb.MongoDBExecutionFactory.java

License:Open Source License

/**
 * @param field/*from  w  w w .j  a  va 2s .co  m*/
 * @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:org.waveprotocol.box.server.persistence.mongodb.MongoDbStore.java

License:Apache License

@Override
public AttachmentData getAttachment(AttachmentId attachmentId) {

    final GridFSDBFile attachment = getAttachmentGrid().findOne(attachmentId.serialise());

    if (attachment == null) {
        return null;
    } else {//  w ww  .  j  a  va  2s . c  om
        return new AttachmentData() {

            @Override
            public InputStream getInputStream() throws IOException {
                return attachment.getInputStream();
            }

            @Override
            public long getSize() {
                return attachment.getLength();
            }
        };
    }
}

From source file:org.waveprotocol.wave.examples.fedone.persistence.mongodb.MongoDbStore.java

License:Apache License

@Override
public AttachmentData getAttachment(String id) {
    final GridFSDBFile attachment = getAttachmentGrid().findOne(id);

    if (attachment == null) {
        return null;
    } else {//from   w  ww  . j a v a  2  s  .c  o  m
        return new AttachmentData() {
            @Override
            public void writeDataTo(OutputStream out) throws IOException {
                attachment.writeTo(out);
            }

            @Override
            public Date getLastModifiedDate() {
                return attachment.getUploadDate();
            }

            @Override
            public long getContentSize() {
                return attachment.getLength();
            }

            @Override
            public InputStream getInputStream() {
                return attachment.getInputStream();
            }
        };
    }
}

From source file:piecework.content.concrete.GridFsContentResource.java

License:Educational Community License

@Override
public InputStream getInputStream() throws IOException {
    Query query = query(GridFsCriteria.whereFilename().is(location));
    if (uploadDate != null)
        query.addCriteria(GridFsCriteria.where("uploadDate").is(uploadDate));
    GridFSDBFile file = gridFsOperations.findOne(query);
    return file.getInputStream();
}

From source file:piecework.util.ContentUtility.java

License:Educational Community License

public static ContentResource toContent(GridFSDBFile file) {
    if (file == null)
        return null;

    String fileId = file.getId().toString();
    DBObject metadata = file.getMetaData();
    String originalFileName = metadata != null ? String.class.cast(metadata.get("originalFilename")) : null;

    return new BasicContentResource.Builder().contentId(fileId).contentType(file.getContentType())
            .filename(originalFileName).location(file.getFilename()).inputStream(file.getInputStream())
            .lastModified(file.getUploadDate()).length(Long.valueOf(file.getLength()))
            //                .md5(file.getMD5())
            .build();//from  ww  w.j  a  va2  s  .  c  o  m
}

From source file:rapture.blob.mongodb.GridFSBlobHandler.java

License:Open Source License

@Override
public InputStream getBlob(CallingContext context, String docPath) {
    GridFS gridFS = getGridFS();//from  ww w  .java 2 s  . co  m
    String lockKey = createLockKey(gridFS, docPath);
    LockHandle lockHandle = grabLock(context, lockKey);
    InputStream retVal = null;
    try {
        GridFSDBFile file = gridFS.findOne(docPath);
        if (file != null) {
            retVal = file.getInputStream();
        }
    } finally {
        releaseLock(context, lockKey, lockHandle);
    }
    return retVal;
}

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

License:Open Source License

public GridFsSharedInputStream(GridFSDBFile gridFSDBFile, long offset, long length) {
    LOG.trace("GridFsSharedInputStream(GridFSDBFile, {}, {})", offset, length);
    this.inputStream = new BufferedInputStream(gridFSDBFile.getInputStream());
    try {/*from  w ww . j  av  a 2 s.  co m*/
        this.inputStream.skip(offset);
    } catch (IOException e) {
        // TODO which exception should we throw?
        throw new OtherErrorException("Failed to create stream", e);
    }
    this.gridFSDBFile = gridFSDBFile;
    this.endPosition = Math.min(offset + length, gridFSDBFile.getLength());
    this.offset = offset;
    this.position = offset;
}

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

License:Apache License

@Override
public FileContent findById(String id) {
    GridFSDBFile fileEntry = gridFs.findOne(id);

    try {//from  w  w w . j  av a2 s.  co m
        FileContent fileContent = new FileContent();
        fileContent.setId(id);
        fileContent.setData(IOUtils.toByteArray(fileEntry.getInputStream()));

        return fileContent;
    } catch (IOException ex) {
        LOG.error("Error while retrieving file: " + ex.getMessage());

        return null;
    }
}

From source file:tango.mongo.ImageManager.java

License:Open Source License

public static ImageHandler createImage(GridFSDBFile file) {

    TiffDecoder td = new TiffDecoder(file.getInputStream(), file.getFilename());
    try {//from   w  w w  .ja v  a 2s  .c o  m
        FileInfo[] info = td.getTiffInfo();
        ImagePlus imp = null;
        //System.out.println("opening file: depth:"+info.length+ " info0:"+info[0].toString());
        if (info.length > 1) { // try to open as stack
            Opener o = new Opener();
            o.setSilentMode(true);
            imp = o.openTiffStack(info);
            imp.setTitle(file.getFilename());
            if (file.containsField("pixelDepth"))
                imp.getCalibration().pixelDepth = (Double) file.get("pixelDepth");
            if (file.containsField("unit"))
                imp.getCalibration().setUnit((String) file.get("unit"));
            file.getInputStream().close();
            if (imp != null)
                return ImageHandler.wrap(imp);
        } else {
            // FIXME not tested!!
            Opener o = new Opener();
            imp = o.openTiff(file.getInputStream(), file.getFilename());
            file.getInputStream().close();
            if (imp != null)
                return ImageHandler.wrap(imp);
        }
    } catch (Exception e) {
        exceptionPrinter.print(e, "", Core.GUIMode);
    }
    return null;
}

From source file:tango.mongo.ImageManager.java

License:Open Source License

private void transferFiles(String queryField, ObjectId queryValue, GridFS gfsSource, GridFS gfsDestination) {
    BasicDBObject query = new BasicDBObject(queryField, queryValue);
    List<GridFSDBFile> files = gfsSource.find(query); // FIXME charge tout en mmoire?
    for (GridFSDBFile file : files) {
        GridFSInputFile gfi = gfsDestination.createFile(file.getInputStream(), file.getFilename());
        gfi.put(queryField, queryValue);
        gfi.put("fileIdx", file.get("fileIdx"));
        gfi.put("fileType", file.get("fileType"));
        gfi.put("pixelDepth", file.get("pixelDepth"));
        gfi.put("unit", file.get("unit"));
        gfi.save();//w  ww  . ja v  a2s .  c om
        if (gfi != null)
            try {
                gfi.getOutputStream().close();
            } catch (IOException ex) {
                Logger.getLogger(ImageManager.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
    gfsSource.remove(query);
}