/** * Returns the {@link Resource}'s content type. * * @return never {@literal null}. * @throws com.mongodb.MongoGridFSException in case no content type declared on {@link GridFSFile#getMetadata()} nor * provided via {@link GridFSFile#getContentType()}. * @throws IllegalStateException if the file does not {@link #exists()}. */ @SuppressWarnings("deprecation") public String getContentType() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return Optionals .firstNonEmpty( () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), () -> Optional.ofNullable(file.getContentType())) .orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file")); }
@Override @SuppressWarnings("deprecation") public void encode(final BsonWriter writer, final GridFSFile value, final EncoderContext encoderContext) { BsonDocument bsonDocument = new BsonDocument(); bsonDocument.put("_id", value.getId()); bsonDocument.put("filename", new BsonString(value.getFilename())); bsonDocument.put("length", new BsonInt64(value.getLength())); bsonDocument.put("chunkSize", new BsonInt32(value.getChunkSize())); bsonDocument.put("uploadDate", new BsonDateTime(value.getUploadDate().getTime())); if (value.getMD5() != null) { bsonDocument.put("md5", new BsonString(value.getMD5())); } Document metadata = value.getMetadata(); if (metadata != null) { bsonDocument.put("metadata", new BsonDocumentWrapper<Document>(metadata, documentCodec)); } Document extraElements = value.getExtraElements(); if (extraElements != null) { bsonDocument.putAll(new BsonDocumentWrapper<Document>(extraElements, documentCodec)); } bsonDocumentCodec.encode(writer, bsonDocument, encoderContext); }
/** * Returns the {@link Resource}'s content type. * * @return never {@literal null}. * @throws com.mongodb.MongoGridFSException in case no content type declared on {@link GridFSFile#getMetadata()} nor * provided via {@link GridFSFile#getContentType()}. * @throws IllegalStateException if the file does not {@link #exists()}. */ @SuppressWarnings("deprecation") public String getContentType() { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); return Optionals .firstNonEmpty( () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), () -> Optional.ofNullable(file.getContentType())) .orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file")); }
private boolean checkEtag(HttpServerExchange exchange, GridFSFile dbsfile) { if (dbsfile != null) { Object etag; 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; }
Document metadata = gridFSFile.getMetadata();
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.getMetadata() != null && file.getMetadata().get("contentType") != null) { exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, file.getMetadata().get("contentType").toString()); } else { exchange.getResponseHeaders().put( CONTENT_TRANSFER_ENCODING_BINARY); ResponseHelper.injectEtagHeader(exchange, file.getMetadata());
@Override public InputStream getAssociatedDocumentStream(String uniqueId, String fileName) { GridFSBucket gridFS = createGridFSConnection(); GridFSFile file = gridFS.find(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName))).first(); if (file == null) { return null; } InputStream is = gridFS.openDownloadStream(file.getObjectId()); ; Document metadata = file.getMetadata(); if (metadata.containsKey(COMPRESSED_FLAG)) { boolean compressed = (boolean) metadata.remove(COMPRESSED_FLAG); if (compressed) { is = new InflaterInputStream(is); } } return is; }
Document metadata = file.getMetadata(); if (metadata != null) { Object oldEtag = metadata.get("_etag");
if (file != null) { InputStream inputStream = getGridFs().openDownloadStream(file.getId()); Document metadata = file.getMetadata();
private AssociatedDocument loadGridFSToAssociatedDocument(GridFSBucket gridFS, GridFSFile file, FetchType fetchType) throws IOException { AssociatedDocument.Builder aBuilder = AssociatedDocument.newBuilder(); aBuilder.setFilename(file.getFilename()); Document metadata = file.getMetadata(); boolean compressed = false; if (metadata.containsKey(COMPRESSED_FLAG)) { compressed = (boolean) metadata.remove(COMPRESSED_FLAG); } long timestamp = (long) metadata.remove(TIMESTAMP); aBuilder.setCompressed(compressed); aBuilder.setTimestamp(timestamp); aBuilder.setDocumentUniqueId((String) metadata.remove(DOCUMENT_UNIQUE_ID_KEY)); for (String field : metadata.keySet()) { aBuilder.addMetadata(Metadata.newBuilder().setKey(field).setValue((String) metadata.get(field))); } if (FetchType.FULL.equals(fetchType)) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); gridFS.downloadToStream(file.getObjectId(), byteArrayOutputStream); byte[] bytes = byteArrayOutputStream.toByteArray(); if (null != bytes) { if (compressed) { bytes = CommonCompression.uncompressZlib(bytes); } aBuilder.setDocument(ByteString.copyFrom(bytes)); } } aBuilder.setIndexName(indexName); return aBuilder.build(); }
@Override @SuppressWarnings("deprecation") public void encode(final BsonWriter writer, final GridFSFile value, final EncoderContext encoderContext) { BsonDocument bsonDocument = new BsonDocument(); bsonDocument.put("_id", value.getId()); bsonDocument.put("filename", new BsonString(value.getFilename())); bsonDocument.put("length", new BsonInt64(value.getLength())); bsonDocument.put("chunkSize", new BsonInt32(value.getChunkSize())); bsonDocument.put("uploadDate", new BsonDateTime(value.getUploadDate().getTime())); if (value.getMD5() != null) { bsonDocument.put("md5", new BsonString(value.getMD5())); } Document metadata = value.getMetadata(); if (metadata != null) { bsonDocument.put("metadata", new BsonDocumentWrapper<Document>(metadata, documentCodec)); } Document extraElements = value.getExtraElements(); if (extraElements != null) { bsonDocument.putAll(new BsonDocumentWrapper<Document>(extraElements, documentCodec)); } bsonDocumentCodec.encode(writer, bsonDocument, encoderContext); }