Browse Source

DATAMONGO-1850 - Guard GridFsResource.getContentType() against absent file metadata.

We now consider fall back to GridFS.getContentType() if GridFS metadata is absent to prevent null dereference.

Original Pull Request: #527
pull/530/merge
Mark Paluch 8 years ago committed by Christoph Strobl
parent
commit
acb68f3ca4
  1. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsResource.java
  2. 54
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsResourceUnitTests.java

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

@ -18,10 +18,13 @@ package org.springframework.data.mongodb.gridfs; @@ -18,10 +18,13 @@ package org.springframework.data.mongodb.gridfs;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.data.util.Optionals;
import com.mongodb.MongoGridFSException;
import com.mongodb.client.gridfs.model.GridFSFile;
/**
@ -30,6 +33,7 @@ import com.mongodb.client.gridfs.model.GridFSFile; @@ -30,6 +33,7 @@ import com.mongodb.client.gridfs.model.GridFSFile;
* @author Oliver Gierke
* @author Christoph Strobl
* @author Hartmut Lang
* @author Mark Paluch
*/
public class GridFsResource extends InputStreamResource {
@ -104,8 +108,11 @@ public class GridFsResource extends InputStreamResource { @@ -104,8 +108,11 @@ public class GridFsResource extends InputStreamResource {
@SuppressWarnings("deprecation")
public String getContentType() {
String contentType = file.getMetadata().get(CONTENT_TYPE_FIELD, String.class);
return contentType != null ? contentType : file.getContentType();
return Optionals
.firstNonEmpty(
() -> Optional.ofNullable(file.getMetadata()).map(it -> it.get(CONTENT_TYPE_FIELD, String.class)),
() -> Optional.ofNullable(file.getContentType()))
.orElseThrow(() -> new MongoGridFSException("No contentType data for this GridFS file"));
}
}

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

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.gridfs;
import static org.assertj.core.api.Assertions.*;
import java.util.Date;
import org.bson.BsonObjectId;
import org.bson.Document;
import org.junit.Test;
import com.mongodb.MongoGridFSException;
import com.mongodb.client.gridfs.model.GridFSFile;
/**
* Unit tests for {@link GridFsResource}.
*
* @author Mark Paluch
*/
public class GridFsResourceUnitTests {
@Test // DATAMONGO-1850
public void shouldReadContentTypeCorrectly() {
Document metadata = new Document(GridFsResource.CONTENT_TYPE_FIELD, "text/plain");
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", metadata);
GridFsResource resource = new GridFsResource(file);
assertThat(resource.getContentType()).isEqualTo("text/plain");
}
@Test // DATAMONGO-1850
public void shouldThrowExceptionOnEmptyContentType() {
GridFSFile file = new GridFSFile(new BsonObjectId(), "foo", 0, 0, new Date(), "foo", null);
GridFsResource resource = new GridFsResource(file);
assertThatThrownBy(resource::getContentType).isInstanceOf(MongoGridFSException.class);
}
}
Loading…
Cancel
Save