Example usage for com.mongodb.client.gridfs.model GridFSFile getMetadata

List of usage examples for com.mongodb.client.gridfs.model GridFSFile getMetadata

Introduction

In this page you can find the example usage for com.mongodb.client.gridfs.model GridFSFile getMetadata.

Prototype

@Nullable
public Document getMetadata() 

Source Link

Document

Any additional metadata stored along with the file

Usage

From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java

License:Apache License

private JSONObject executeFind(GridFSBucket gridFS, String query, String sortBy, String limit, String skip)
        throws JSONException {
    Document queryObj = Document.parse(query);
    Document sortObj = Document.parse(sortBy);
    int filesLimit = Integer.parseInt(limit);
    int filesSkip = Integer.parseInt(skip);
    // Partial Keys cant be fetched for a file

    MongoCursor<GridFSFile> it = gridFS.find(queryObj).sort(sortObj).skip(filesSkip).limit(filesLimit)
            .iterator();//from w  ww .  j a  v a  2 s.co m

    JSONArray fileList = new JSONArray();
    while (it.hasNext()) {
        GridFSFile fsFile = it.next();

        JSONObject file = new JSONObject();

        file.put("_id", fsFile.getId().asObjectId().getValue());
        file.put("fileName", fsFile.getFilename());
        file.put("length", fsFile.getLength());
        file.put("chunkSize", fsFile.getChunkSize());
        file.put("uploadDate", fsFile.getUploadDate());
        file.put("md5", fsFile.getMD5());
        if (fsFile.getMetadata() != null) {
            file.put("metadata", fsFile.getMetadata());
        }

        fileList.put(file);

    }
    JSONObject result = new JSONObject();
    long count = fileList.length();
    result.put("documents", fileList);
    result.put("editable", true);
    result.put("count", count);
    return result;
}

From source file:org.apache.nifi.processors.mongodb.gridfs.FetchGridFS.java

License:Apache License

private void handleFile(GridFSBucket bucket, ProcessSession session, ProcessContext context, FlowFile parent,
        GridFSFile input, String query) {
    Map<String, String> attrs = new HashMap<>();
    attrs.put(METADATA_ATTRIBUTE, input.getMetadata() != null ? input.getMetadata().toJson() : "{}");
    if (context.getProperty(QUERY_ATTRIBUTE).isSet()) {
        String key = context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(parent).getValue();
        attrs.put(key, query);//from w  ww.j  a  v  a2 s  .  co m
    }
    attrs.put(CoreAttributes.FILENAME.key(), input.getFilename());
    FlowFile output = parent != null ? session.create(parent) : session.create();
    output = session.write(output, out -> bucket.downloadToStream(input.getObjectId(), out));
    output = session.putAllAttributes(output, attrs);
    session.transfer(output, REL_SUCCESS);
    session.getProvenanceReporter().receive(output, getTransitUri(input.getObjectId(), output, context));
}

From source file:org.apache.nifi.processors.mongodb.gridfs.GridFSITTestBase.java

License:Apache License

public boolean fileHasProperties(String name, String bucketName, Map<String, String> attrs) {
    GridFSBucket bucket = GridFSBuckets.create(client.getDatabase(DB), bucketName);
    MongoCursor it = bucket.find(Document.parse(String.format("{ \"filename\": \"%s\" }", name))).iterator();
    boolean retVal = false;

    if (it.hasNext()) {
        GridFSFile file = (GridFSFile) it.next();
        Document metadata = file.getMetadata();
        if (metadata != null && metadata.size() == attrs.size()) {
            retVal = true;//from  w  w w .  j  ava 2  s . c o  m
            for (Map.Entry<String, Object> entry : metadata.entrySet()) {
                Object val = attrs.get(entry.getKey());
                if (val == null || !entry.getValue().equals(val)) {
                    retVal = false;
                    break;
                }
            }
        }
    }

    it.close();

    return retVal;
}

From source file:org.restheart.db.GridFsDAO.java

License:Open Source License

@Override
public OperationResult deleteFile(final Database db, final String dbName, final String bucketName,
        final BsonObjectId fileId, final String requestEtag, final boolean checkEtag) {
    final String bucket = extractBucketName(bucketName);

    GridFSBucket gridFSBucket = GridFSBuckets.create(db.getDatabase(dbName), bucket);

    GridFSFile file = gridFSBucket.find(eq("_id", fileId)).limit(1).iterator().tryNext();

    if (file == null) {
        return new OperationResult(HttpStatus.SC_NOT_FOUND);
    } else if (checkEtag) {
        Object oldEtag = file.getMetadata().get("_etag");

        if (oldEtag != null) {
            if (requestEtag == null) {
                return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
            } else if (!Objects.equals(oldEtag.toString(), requestEtag)) {
                return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
            }//  w  w  w.ja va  2 s. c o m
        }
    }

    gridFSBucket.delete(fileId.asObjectId().getValue());

    return new OperationResult(HttpStatus.SC_NO_CONTENT);
}

From source file:org.restheart.handlers.files.GetFileBinaryHandler.java

License:Open Source License

private boolean checkEtag(HttpServerExchange exchange, GridFSFile dbsfile) {
    if (dbsfile != null) {
        Object etag;//from  w w w . j  a  v  a  2 s.c om

        if (dbsfile.getMetadata() != null && dbsfile.getMetadata().containsKey("_etag")) {
            etag = dbsfile.getMetadata().get("_etag");
        } else {
            etag = null;
        }

        if (etag != null && etag instanceof ObjectId) {
            ObjectId _etag = (ObjectId) etag;

            BsonObjectId __etag = new BsonObjectId(_etag);

            // in case the request contains the IF_NONE_MATCH header with the current etag value,
            // just return 304 NOT_MODIFIED code
            if (RequestHelper.checkReadEtag(exchange, __etag)) {
                exchange.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
                exchange.endExchange();
                return true;
            }
        }
    }

    return false;
}

From source file:org.restheart.handlers.files.GetFileBinaryHandler.java

License:Open Source License

private void sendBinaryContent(final GridFSBucket gridFSBucket, final GridFSFile file,
        final HttpServerExchange exchange) throws IOException {
    LOGGER.trace("Filename = {}", file.getFilename());
    LOGGER.trace("Content length = {}", file.getLength());

    if (file.getMetadata() != null && file.getMetadata().get("contentType") != null) {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
                file.getMetadata().get("contentType").toString());
    } else if (file.getExtraElements() != null && file.getExtraElements().get("contentType") != null) {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
                file.getExtraElements().get("contentType").toString());
    } else {/*from   www .  j  a  v  a2s.  com*/
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, APPLICATION_OCTET_STREAM);
    }

    exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, file.getLength());

    exchange.getResponseHeaders().put(Headers.CONTENT_DISPOSITION,
            String.format("inline; filename=\"%s\"", extractFilename(file)));

    exchange.getResponseHeaders().put(Headers.CONTENT_TRANSFER_ENCODING, CONTENT_TRANSFER_ENCODING_BINARY);

    ResponseHelper.injectEtagHeader(exchange, file.getMetadata());

    exchange.setStatusCode(HttpStatus.SC_OK);

    gridFSBucket.downloadToStream(file.getId().asObjectId().getValue(), exchange.getOutputStream());

    exchange.endExchange();
}