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.
This commit is contained in:
Mark Paluch
2018-07-06 13:01:09 +02:00
parent e18f506edd
commit 0ec82e1f2e
3 changed files with 32 additions and 2 deletions

View File

@@ -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<GridFsResource> 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]);

View File

@@ -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<ObjectId, Resource> fixture = new LinkedHashMap<>();
fixture.put(reference1, resource);
fixture.put(reference2, secondResource);
for (Entry<ObjectId, Resource> 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;
}

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<empty/>