diff --git a/pom.xml b/pom.xml
index a1cf69a0f6..fb171d4ed6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.data
spring-data-mongodb-parent
- 2.2.0.BUILD-SNAPSHOT
+ 2.2.0.DATAMONGO-2240-SNAPSHOT
pom
Spring Data MongoDB
diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml
index bb7d9f03cc..2af296057c 100644
--- a/spring-data-mongodb-benchmarks/pom.xml
+++ b/spring-data-mongodb-benchmarks/pom.xml
@@ -7,7 +7,7 @@
org.springframework.data
spring-data-mongodb-parent
- 2.2.0.BUILD-SNAPSHOT
+ 2.2.0.DATAMONGO-2240-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml
index b32dcba387..d98c188c1a 100644
--- a/spring-data-mongodb-distribution/pom.xml
+++ b/spring-data-mongodb-distribution/pom.xml
@@ -14,7 +14,7 @@
org.springframework.data
spring-data-mongodb-parent
- 2.2.0.BUILD-SNAPSHOT
+ 2.2.0.DATAMONGO-2240-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml
index b611cf01a8..da1fbb45d0 100644
--- a/spring-data-mongodb/pom.xml
+++ b/spring-data-mongodb/pom.xml
@@ -11,7 +11,7 @@
org.springframework.data
spring-data-mongodb-parent
- 2.2.0.BUILD-SNAPSHOT
+ 2.2.0.DATAMONGO-2240-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java
index a090969abc..19c646e214 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java
@@ -48,7 +48,7 @@ public class GridFsResource extends InputStreamResource {
/**
* Creates a new, absent {@link GridFsResource}.
- *
+ *
* @param filename filename of the absent resource.
* @since 2.1
*/
@@ -116,7 +116,7 @@ public InputStream getInputStream() throws IOException, IllegalStateException {
public long contentLength() throws IOException {
verifyExists();
- return file.getLength();
+ return getGridFSFile().getLength();
}
/*
@@ -125,7 +125,7 @@ public long contentLength() throws IOException {
*/
@Override
public String getFilename() throws IllegalStateException {
- return filename;
+ return this.filename;
}
/*
@@ -134,7 +134,7 @@ public String getFilename() throws IllegalStateException {
*/
@Override
public boolean exists() {
- return file != null;
+ return this.file != null;
}
/*
@@ -145,7 +145,7 @@ public boolean exists() {
public long lastModified() throws IOException {
verifyExists();
- return file.getUploadDate().getTime();
+ return getGridFSFile().getUploadDate().getTime();
}
/*
@@ -167,7 +167,16 @@ public Object getId() {
Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));
- return file.getId();
+ return getGridFSFile().getId();
+ }
+
+ /**
+ * @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
+ * @since 2.2
+ */
+ @Nullable
+ public GridFSFile getGridFSFile() {
+ return this.file;
}
/**
@@ -185,8 +194,9 @@ public String getContentType() {
return Optionals
.firstNonEmpty(
- () -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
- () -> Optional.ofNullable(file.getContentType()))
+ () -> Optional.ofNullable(getGridFSFile().getMetadata())
+ .map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
+ () -> Optional.ofNullable(getGridFSFile().getContentType()))
.orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file"));
}
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java
index 6f2d742957..4946d6d0d5 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsResource.java
@@ -17,12 +17,12 @@
import reactor.core.publisher.Flux;
-import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.reactivestreams.Publisher;
+
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
@@ -39,9 +39,6 @@
*/
public class ReactiveGridFsResource extends AbstractResource {
- static final String CONTENT_TYPE_FIELD = "_contentType";
- private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
-
private final @Nullable GridFSFile file;
private final String filename;
private final Flux content;
@@ -104,7 +101,7 @@ public InputStream getInputStream() throws IllegalStateException {
public long contentLength() throws IOException {
verifyExists();
- return file.getLength();
+ return getGridFSFile().getLength();
}
/*
@@ -113,7 +110,7 @@ public long contentLength() throws IOException {
*/
@Override
public String getFilename() throws IllegalStateException {
- return filename;
+ return this.filename;
}
/*
@@ -122,7 +119,7 @@ public String getFilename() throws IllegalStateException {
*/
@Override
public boolean exists() {
- return file != null;
+ return this.file != null;
}
/*
@@ -133,7 +130,7 @@ public boolean exists() {
public long lastModified() throws IOException {
verifyExists();
- return file.getUploadDate().getTime();
+ return getGridFSFile().getUploadDate().getTime();
}
/*
@@ -155,7 +152,16 @@ public Object getId() {
Assert.state(exists(), () -> String.format("%s does not exist.", getDescription()));
- return file.getId();
+ return getGridFSFile().getId();
+ }
+
+ /**
+ * @return the underlying {@link GridFSFile}. Can be {@literal null} if absent.
+ * @since 2.2
+ */
+ @Nullable
+ public GridFSFile getGridFSFile() {
+ return file;
}
/**
@@ -168,7 +174,7 @@ public Flux getDownloadStream() {
if (!exists()) {
return Flux.error(new FileNotFoundException(String.format("%s does not exist.", getDescription())));
}
- return content;
+ return this.content;
}
private void verifyExists() throws FileNotFoundException {
diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java
index b912eaa5ca..cbd5eb5c88 100644
--- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java
+++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java
@@ -45,6 +45,15 @@ public void shouldReadContentTypeCorrectly() {
assertThat(resource.getContentType()).isEqualTo("text/plain");
}
+ @Test // DATAMONGO-2240
+ public void shouldReturnGridFSFile() {
+
+ GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", new Document());
+ GridFsResource resource = new GridFsResource(file);
+
+ assertThat(resource.getGridFSFile()).isSameAs(file);
+ }
+
@Test // DATAMONGO-1850
public void shouldThrowExceptionOnEmptyContentType() {
@@ -89,6 +98,5 @@ public void shouldReturnFilenameForAbsentResource() {
assertThat(absent.exists()).isFalse();
assertThat(absent.getDescription()).contains("GridFs resource [foo]");
assertThat(absent.getFilename()).isEqualTo("foo");
-
}
}
diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java
index 2afbd08916..3825e729d6 100644
--- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java
+++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplateTests.java
@@ -16,11 +16,10 @@
package org.springframework.data.mongodb.gridfs;
import static org.assertj.core.api.Assertions.*;
-import static org.springframework.data.mongodb.core.query.Criteria.where;
+import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.*;
-import org.springframework.dao.IncorrectResultSizeDataAccessException;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
@@ -32,12 +31,14 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@@ -143,7 +144,7 @@ public void getResourceShouldRetrieveContentByIdentity() throws IOException {
.verifyComplete();
}
- @Test // DATAMONGO-1855
+ @Test // DATAMONGO-1855, DATAMONGO-2240
public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws IOException {
AsyncInputStream upload1 = AsyncStreamHelper.toAsyncInputStream(resource.getInputStream());
@@ -155,10 +156,25 @@ public void shouldEmitFirstEntryWhenFindFirstRetrievesMoreThanOneResult() throws
operations.findFirst(query(where("filename").regex("foo*"))) //
.flatMap(operations::getResource) //
.as(StepVerifier::create) //
- .expectNextCount(1) //
+ .assertNext(actual -> {
+
+ assertThat(actual.getGridFSFile()).isNotNull();
+ })
.verifyComplete();
}
+ @Test // DATAMONGO-2240
+ public void shouldReturnNoGridFsFileWhenAbsent() {
+
+ operations.getResource("absent") //
+ .as(StepVerifier::create) //
+ .assertNext(actual -> {
+
+ assertThat(actual.exists()).isFalse();
+ assertThat(actual.getGridFSFile()).isNull();
+ }).verifyComplete();
+ }
+
@Test // DATAMONGO-1855
public void shouldEmitErrorWhenFindOneRetrievesMoreThanOneResult() throws IOException {