Skip to content

Commit 0ec82e1

Browse files
committed
DATAMONGO-2021 - Polishing.
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.
1 parent e18f506 commit 0ec82e1

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Christoph Strobl
5353
* @author Mark Paluch
5454
* @author Hartmut Lang
55+
* @author Niklas Helge Hanft
5556
*/
5657
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
5758

@@ -228,7 +229,8 @@ public ClassLoader getClassLoader() {
228229
*/
229230
public GridFsResource getResource(String location) {
230231

231-
return Optional.ofNullable(findOne(query(whereFilename().is(location)))).map(this::getResource)
232+
return Optional.ofNullable(findOne(query(whereFilename().is(location)))) //
233+
.map(this::getResource) //
232234
.orElseGet(() -> GridFsResource.absent(location));
233235
}
234236

@@ -261,7 +263,7 @@ public GridFsResource[] getResources(String locationPattern) {
261263
List<GridFsResource> resources = new ArrayList<>();
262264

263265
for (GridFSFile file : files) {
264-
resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())));
266+
resources.add(getResource(file));
265267
}
266268

267269
return resources.toArray(new GridFsResource[0]);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
import java.io.IOException;
2424
import java.util.ArrayList;
25+
import java.util.LinkedHashMap;
2526
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
2629

2730
import org.bson.BsonObjectId;
2831
import org.bson.Document;
@@ -38,6 +41,7 @@
3841
import org.springframework.data.mongodb.core.query.Query;
3942
import org.springframework.test.context.ContextConfiguration;
4043
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44+
import org.springframework.util.StreamUtils;
4145

4246
import com.mongodb.MongoGridFSException;
4347
import com.mongodb.client.gridfs.GridFSFindIterable;
@@ -51,6 +55,7 @@
5155
* @author Thomas Darimont
5256
* @author Martin Baumgartner
5357
* @author Hartmut Lang
58+
* @author Mark Paluch
5459
*/
5560
@RunWith(SpringJUnit4ClassRunner.class)
5661
@ContextConfiguration("classpath:gridfs/gridfs.xml")
@@ -239,6 +244,27 @@ public void convertFileToResource() throws IOException {
239244
assertThat(((BsonObjectId) result.getId()).getValue()).isEqualTo(reference);
240245
}
241246

247+
@Test // DATAMONGO-2021
248+
public void getResourceShouldRetrieveContentByIdentity() throws IOException {
249+
250+
ClassPathResource secondResource = new ClassPathResource("gridfs/another-resource.xml");
251+
252+
ObjectId reference1 = operations.store(resource.getInputStream(), "foo.xml");
253+
ObjectId reference2 = operations.store(secondResource.getInputStream(), "foo.xml");
254+
255+
Map<ObjectId, Resource> fixture = new LinkedHashMap<>();
256+
fixture.put(reference1, resource);
257+
fixture.put(reference2, secondResource);
258+
259+
for (Entry<ObjectId, Resource> entry : fixture.entrySet()) {
260+
261+
GridFsResource fsFile = operations.getResource(operations.findOne(query(where("_id").is(entry.getKey()))));
262+
byte[] content = StreamUtils.copyToByteArray(fsFile.getInputStream());
263+
264+
assertThat(content).isEqualTo(StreamUtils.copyToByteArray(entry.getValue().getInputStream()));
265+
}
266+
}
267+
242268
class Metadata {
243269
String version;
244270
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<empty/>

0 commit comments

Comments
 (0)