From 0ec82e1f2eee795b4a6e7e308351c68382607fc6 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 6 Jul 2018 13:01:09 +0200 Subject: [PATCH] DATAMONGO-2021 - Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adapt getResources(…) to use the file id and no longer the file name when opening a download stream. Add author tag. Add test to verify content retrieval by identity. Original pull request: #581. --- .../data/mongodb/gridfs/GridFsTemplate.java | 6 +++-- .../GridFsTemplateIntegrationTests.java | 26 +++++++++++++++++++ .../resources/gridfs/another-resource.xml | 2 ++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 spring-data-mongodb/src/test/resources/gridfs/another-resource.xml 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 e3fa48150..e5a9a901e 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 @@ -52,6 +52,7 @@ import com.mongodb.client.gridfs.model.GridFSUploadOptions; * @author Christoph Strobl * @author Mark Paluch * @author Hartmut Lang + * @author Niklas Helge Hanft */ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver { @@ -228,7 +229,8 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver */ public GridFsResource getResource(String location) { - return Optional.ofNullable(findOne(query(whereFilename().is(location)))).map(this::getResource) + return Optional.ofNullable(findOne(query(whereFilename().is(location)))) // + .map(this::getResource) // .orElseGet(() -> GridFsResource.absent(location)); } @@ -261,7 +263,7 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver List resources = new ArrayList<>(); for (GridFSFile file : files) { - resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()))); + resources.add(getResource(file)); } return resources.toArray(new GridFsResource[0]); 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 5a3d28d95..334356f67 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 @@ -22,7 +22,10 @@ import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*; import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.bson.BsonObjectId; import org.bson.Document; @@ -38,6 +41,7 @@ import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.StreamUtils; import com.mongodb.MongoGridFSException; import com.mongodb.client.gridfs.GridFSFindIterable; @@ -51,6 +55,7 @@ import com.mongodb.client.gridfs.model.GridFSFile; * @author Thomas Darimont * @author Martin Baumgartner * @author Hartmut Lang + * @author Mark Paluch */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:gridfs/gridfs.xml") @@ -239,6 +244,27 @@ public class GridFsTemplateIntegrationTests { assertThat(((BsonObjectId) result.getId()).getValue()).isEqualTo(reference); } + @Test // DATAMONGO-2021 + public void getResourceShouldRetrieveContentByIdentity() throws IOException { + + ClassPathResource secondResource = new ClassPathResource("gridfs/another-resource.xml"); + + ObjectId reference1 = operations.store(resource.getInputStream(), "foo.xml"); + ObjectId reference2 = operations.store(secondResource.getInputStream(), "foo.xml"); + + Map fixture = new LinkedHashMap<>(); + fixture.put(reference1, resource); + fixture.put(reference2, secondResource); + + for (Entry entry : fixture.entrySet()) { + + GridFsResource fsFile = operations.getResource(operations.findOne(query(where("_id").is(entry.getKey())))); + byte[] content = StreamUtils.copyToByteArray(fsFile.getInputStream()); + + assertThat(content).isEqualTo(StreamUtils.copyToByteArray(entry.getValue().getInputStream())); + } + } + class Metadata { String version; } diff --git a/spring-data-mongodb/src/test/resources/gridfs/another-resource.xml b/spring-data-mongodb/src/test/resources/gridfs/another-resource.xml new file mode 100644 index 000000000..7217ac474 --- /dev/null +++ b/spring-data-mongodb/src/test/resources/gridfs/another-resource.xml @@ -0,0 +1,2 @@ + +