From 31f8a63a171c59b64361c4d77d6eebfa1637b5b5 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 16 Apr 2019 13:24:57 +0200 Subject: [PATCH] DATAMONGO-2240 - Polishing. Consistently use this for field access. Access GridFSFile through getter. Original Pull Request: #741 --- .../data/mongodb/gridfs/GridFsResource.java | 17 +++++++++-------- .../mongodb/gridfs/ReactiveGridFsResource.java | 16 ++++++---------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java index 9cf6a6fa4..19c646e21 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java @@ -116,7 +116,7 @@ public class GridFsResource extends InputStreamResource { public long contentLength() throws IOException { verifyExists(); - return file.getLength(); + return getGridFSFile().getLength(); } /* @@ -125,7 +125,7 @@ public class GridFsResource extends InputStreamResource { */ @Override public String getFilename() throws IllegalStateException { - return filename; + return this.filename; } /* @@ -134,7 +134,7 @@ public class GridFsResource extends InputStreamResource { */ @Override public boolean exists() { - return file != null; + return this.file != null; } /* @@ -145,7 +145,7 @@ public class GridFsResource extends InputStreamResource { public long lastModified() throws IOException { verifyExists(); - return file.getUploadDate().getTime(); + return getGridFSFile().getUploadDate().getTime(); } /* @@ -167,7 +167,7 @@ public class GridFsResource extends InputStreamResource { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); - return file.getId(); + return getGridFSFile().getId(); } /** @@ -176,7 +176,7 @@ public class GridFsResource extends InputStreamResource { */ @Nullable public GridFSFile getGridFSFile() { - return file; + return this.file; } /** @@ -194,8 +194,9 @@ public class GridFsResource extends InputStreamResource { return Optionals .firstNonEmpty( - () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), - () -> Optional.ofNullable(file.getContentType())) + () -> Optional.ofNullable(getGridFSFile().getMetadata()) + .map(it -> it.get(CONTENT_TYPE_FIELD, String.class)), + () -> Optional.ofNullable(getGridFSFile().getContentType())) .orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file")); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java index 495ab2487..4946d6d0d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java @@ -17,7 +17,6 @@ package org.springframework.data.mongodb.gridfs; import reactor.core.publisher.Flux; -import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -40,9 +39,6 @@ import com.mongodb.client.gridfs.model.GridFSFile; */ public class ReactiveGridFsResource extends AbstractResource { - static final String CONTENT_TYPE_FIELD = "_contentType"; - private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]); - private final @Nullable GridFSFile file; private final String filename; private final Flux content; @@ -105,7 +101,7 @@ public class ReactiveGridFsResource extends AbstractResource { public long contentLength() throws IOException { verifyExists(); - return file.getLength(); + return getGridFSFile().getLength(); } /* @@ -114,7 +110,7 @@ public class ReactiveGridFsResource extends AbstractResource { */ @Override public String getFilename() throws IllegalStateException { - return filename; + return this.filename; } /* @@ -123,7 +119,7 @@ public class ReactiveGridFsResource extends AbstractResource { */ @Override public boolean exists() { - return file != null; + return this.file != null; } /* @@ -134,7 +130,7 @@ public class ReactiveGridFsResource extends AbstractResource { public long lastModified() throws IOException { verifyExists(); - return file.getUploadDate().getTime(); + return getGridFSFile().getUploadDate().getTime(); } /* @@ -156,7 +152,7 @@ public class ReactiveGridFsResource extends AbstractResource { Assert.state(exists(), () -> String.format("%s does not exist.", getDescription())); - return file.getId(); + return getGridFSFile().getId(); } /** @@ -178,7 +174,7 @@ public class ReactiveGridFsResource extends AbstractResource { if (!exists()) { return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription()))); } - return content; + return this.content; } private void verifyExists() throws FileNotFoundException {