Browse Source

DATAMONGO-1813 - Polishing.

Add since tag. Add non-null guard. Refactor conditional resource mapping to Optional. Apply code formatter.

Optimize array construction from List.

Original pull request: #543.
pull/544/head
Mark Paluch 8 years ago
parent
commit
88805d0743
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
  2. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
  3. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

2
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}. * Returns the {@link GridFsResource} for a {@link com.mongodb.client.gridfs.model.GridFSFile}.
*
* @param file must not be {@literal null}. * @param file must not be {@literal null}.
* @return the resource for the file. * @return the resource for the file.
* @since 2.1
*/ */
GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file); GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file);

10
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) * @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
*/ */
public GridFsResource getResource(String location) { public GridFsResource getResource(String location) {
return Optional.ofNullable(findOne(query(whereFilename().is(location)))).map(this::getResource).orElse(null);
GridFSFile file = findOne(query(whereFilename().is(location)));
return file != null ? getResource(file) : null;
} }
/* /*
@ -238,6 +236,8 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
*/ */
public GridFsResource getResource(GridFSFile file) { public GridFsResource getResource(GridFSFile file) {
Assert.notNull(file, "GridFSFile must not be null!");
return new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())); return new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()));
} }
@ -256,13 +256,13 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
if (path.isPattern()) { if (path.isPattern()) {
GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex()))); GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex())));
List<GridFsResource> resources = new ArrayList<GridFsResource>(); List<GridFsResource> resources = new ArrayList<>();
for (GridFSFile file : files) { for (GridFSFile file : files) {
resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()))); 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) }; return new GridFsResource[] { getResource(locationPattern) };

2
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.ArrayList;
import java.util.List; import java.util.List;
import org.assertj.core.api.Assertions;
import org.bson.BsonObjectId; import org.bson.BsonObjectId;
import org.bson.Document; import org.bson.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -233,7 +232,6 @@ public class GridFsTemplateIntegrationTests {
Document metadata = new Document("key", "value"); Document metadata = new Document("key", "value");
ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata); ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
GridFSFile file = operations.findOne(query(whereMetaData("key").is("value"))); GridFSFile file = operations.findOne(query(whereMetaData("key").is("value")));
GridFsResource result = operations.getResource(file); GridFsResource result = operations.getResource(file);

Loading…
Cancel
Save