diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java index 0dcb340bd..b7b5c6463 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java @@ -160,8 +160,10 @@ public interface GridFsOperations extends ResourcePatternResolver { /** * Returns the {@link GridFsResource} for a {@link com.mongodb.client.gridfs.model.GridFSFile}. + * * @param file must not be {@literal null}. * @return the resource for the file. + * @since 2.1 */ GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java index b4bfc2f35..a5d107c61 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java @@ -227,9 +227,7 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver * @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String) */ public GridFsResource getResource(String location) { - - GridFSFile file = findOne(query(whereFilename().is(location))); - return file != null ? getResource(file) : null; + return Optional.ofNullable(findOne(query(whereFilename().is(location)))).map(this::getResource).orElse(null); } /* @@ -238,6 +236,8 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver */ public GridFsResource getResource(GridFSFile file) { + Assert.notNull(file, "GridFSFile must not be null!"); + return new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())); } @@ -256,13 +256,13 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver if (path.isPattern()) { GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); - List resources = new ArrayList(); + List resources = new ArrayList<>(); for (GridFSFile file : files) { resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()))); } - return resources.toArray(new GridFsResource[resources.size()]); + return resources.toArray(new GridFsResource[0]); } return new GridFsResource[] { getResource(locationPattern) }; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java index d66ec7ac5..5ddf1f24d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.assertj.core.api.Assertions; import org.bson.BsonObjectId; import org.bson.Document; import org.bson.types.ObjectId; @@ -233,7 +232,6 @@ public class GridFsTemplateIntegrationTests { Document metadata = new Document("key", "value"); ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata); - List files = new ArrayList(); GridFSFile file = operations.findOne(query(whereMetaData("key").is("value"))); GridFsResource result = operations.getResource(file);