Example usage for com.mongodb.gridfs GridFSInputFile setMetaData

List of usage examples for com.mongodb.gridfs GridFSInputFile setMetaData

Introduction

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

Prototype

public void setMetaData(final DBObject metadata) 

Source Link

Document

Gets the file metadata.

Usage

From source file:org.apache.camel.component.gridfs.GridFsProducer.java

License:Apache License

public void process(Exchange exchange) throws Exception {
    String operation = endpoint.getOperation();
    if (operation == null) {
        operation = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_OPERATION, String.class);
    }//  w  w w . j a  v a  2s.  c om
    if (operation == null || "create".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        Long chunkSize = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_CHUNKSIZE, Long.class);

        InputStream ins = exchange.getIn().getMandatoryBody(InputStream.class);
        GridFSInputFile gfsFile = endpoint.getGridFs().createFile(ins, filename, true);
        if (chunkSize != null && chunkSize > 0) {
            gfsFile.setChunkSize(chunkSize);
        }
        final String ct = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        if (ct != null) {
            gfsFile.setContentType(ct);
        }
        String metaData = exchange.getIn().getHeader(GridFsEndpoint.GRIDFS_METADATA, String.class);
        DBObject dbObject = (DBObject) JSON.parse(metaData);
        gfsFile.setMetaData(dbObject);
        gfsFile.save();
        exchange.getIn().setHeader(Exchange.FILE_NAME_PRODUCED, gfsFile.getFilename());
    } else if ("remove".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        endpoint.getGridFs().remove(filename);
    } else if ("findOne".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        GridFSDBFile file = endpoint.getGridFs().findOne(filename);
        if (file != null) {
            exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
            exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
            exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
            exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
            exchange.getIn().setBody(file.getInputStream(), InputStream.class);
        } else {
            throw new FileNotFoundException("No GridFS file for " + filename);
        }
    } else if ("listAll".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(new DBCursorFilenameReader(cursor), Reader.class);
    } else if ("count".equals(operation)) {
        final String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
        DBCursor cursor;
        if (filename == null) {
            cursor = endpoint.getGridFs().getFileList();
        } else {
            cursor = endpoint.getGridFs().getFileList(new BasicDBObject("filename", filename));
        }
        exchange.getIn().setBody(cursor.count(), Integer.class);
    }

}

From source file:org.exist.mongodb.xquery.gridfs.Store.java

License:Open Source License

void writeCompressed(GridFSInputFile gfsFile, StopWatch stopWatch, Item content, int dataType)
        throws NoSuchAlgorithmException, IOException, XPathException {
    // Store data compressed, add statistics
    try (OutputStream stream = gfsFile.getOutputStream()) {
        MessageDigest md = MessageDigest.getInstance("MD5");
        CountingOutputStream cosGZ = new CountingOutputStream(stream);
        GZIPOutputStream gos = new GZIPOutputStream(cosGZ);
        DigestOutputStream dos = new DigestOutputStream(gos, md);
        CountingOutputStream cosRaw = new CountingOutputStream(dos);

        stopWatch.start();/*ww  w  .  j  a  va 2 s. c o  m*/
        ContentSerializer.serialize(content, context, cosRaw);
        cosRaw.flush();
        cosRaw.close();
        stopWatch.stop();

        long nrBytesRaw = cosRaw.getByteCount();
        long nrBytesGZ = cosGZ.getByteCount();
        String checksum = Hex.encodeHexString(dos.getMessageDigest().digest());

        BasicDBObject info = new BasicDBObject();
        info.put(Constants.EXIST_COMPRESSION, GZIP);
        info.put(Constants.EXIST_ORIGINAL_SIZE, nrBytesRaw);
        info.put(Constants.EXIST_ORIGINAL_MD5, checksum);
        info.put(Constants.EXIST_DATATYPE, dataType);
        info.put(Constants.EXIST_DATATYPE_TEXT, Type.getTypeName(dataType));

        gfsFile.setMetaData(info);

        LOG.info("original_md5:" + checksum);
        LOG.info("compression ratio:" + ((100l * nrBytesGZ) / nrBytesRaw));

    }
}

From source file:org.mule.module.mongo.api.MongoClientImpl.java

License:Open Source License

public DBObject createFile(final InputStream content, final String filename, final String contentType,
        final DBObject metadata) {
    Validate.notNull(filename);//from   w w  w .ja va 2 s .  c  o m
    Validate.notNull(content);
    final GridFSInputFile file = getGridFs().createFile(content);
    file.setFilename(filename);
    file.setContentType(contentType);
    if (metadata != null) {
        file.setMetaData(metadata);
    }
    file.save();
    return file;
}

From source file:org.s1.mongodb.cluster.GridFSFileStorage.java

License:Apache License

@Override
public FileStorage.FileWriteBean createFileWriteBean(Id id, FileStorage.FileMetaBean meta) {
    meta.setLastModified(new Date());
    meta.setCreated(new Date());
    GridFS fs = new GridFS(MongoDBConnectionHelper.getConnection(id.getDatabase()), id.getCollection());
    fs.remove(id.getEntity());//from www .j  ava 2  s .  co m

    GridFSInputFile gfsFile = fs.createFile(id.getEntity());
    gfsFile.setContentType(meta.getContentType());
    gfsFile.setMetaData(MongoDBFormat.fromMap(meta.toMap()));

    GridFSFileWriteBean gridFSFileWriteBean = new GridFSFileWriteBean(id, gfsFile.getOutputStream(), meta);
    gridFSFileWriteBean.gfsFile = gfsFile;
    return gridFSFileWriteBean;
}

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

License:Apache License

public GridFSFile store(InputStream content, String filename, String contentType, DBObject metadata) {

    Assert.notNull(content);/*from w w  w.j  a va  2  s  . co m*/

    GridFSInputFile file = getGridFs().createFile(content);

    if (filename != null) {
        file.setFilename(filename);
    }

    if (metadata != null) {
        file.setMetaData(metadata);
    }

    if (contentType != null) {
        file.setContentType(contentType);
    }

    file.save();
    return file;
}

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

License:Apache License

/**
 * go through all the embedded content, store it to GridFS, replace the doc embeddings with a hyperlink to the saved content.
 *//*  w  w  w. ja  v a2s . c  om*/
protected void embedSteps(final DBObject feature, final GridFS gridFS, final Coordinates coordinates) {
    final BasicDBList elements = (BasicDBList) feature.get("elements");
    final String featureId = (String) feature.get("_id");
    if (elements != null) {
        for (int j = 0; j < elements.size(); j++) {
            final DBObject scenario = (DBObject) elements.get(j);
            final String scenarioId = (String) scenario.get("_id");
            final BasicDBList steps = (BasicDBList) scenario.get("steps");
            if (steps != null) {
                for (int k = 0; k < steps.size(); k++) {
                    final DBObject step = (DBObject) steps.get(k);
                    final BasicDBList embeddings = (BasicDBList) step.get("embeddings");
                    if (embeddings != null) {
                        for (int l = 0; l < embeddings.size(); l++) {
                            final DBObject embedding = (DBObject) embeddings.get(l);
                            final GridFSInputFile image = gridFS.createFile(
                                    Base64.decodeBase64(((String) embedding.get("data")).getBytes()));
                            image.setFilename(guid());
                            final BasicDBObject metadata = new BasicDBObject()
                                    .append("product", coordinates.getProduct())
                                    .append("major", coordinates.getMajor())
                                    .append("minor", coordinates.getMinor())
                                    .append("servicePack", coordinates.getServicePack())
                                    .append("build", coordinates.getBuild()).append("feature", featureId)
                                    .append("scenario", scenarioId);
                            image.setMetaData(metadata);
                            image.setContentType((String) embedding.get("mime_type"));
                            image.save();
                            embeddings.put(l, image.getFilename());
                        }
                    }
                }
            }
        }
    }
}