Browse Source

DATAMONGO-503 - Added support for content type on GridFsOperations.

Added methods to take the content type of the file to be created. Clarified nullability of arguments in JavaDoc.

Pull request: #19.
pull/21/merge
Philipp Schneider 13 years ago committed by Oliver Gierke
parent
commit
3a9dc2b98c
  1. 40
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java
  2. 51
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
  3. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIIntegrationTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import com.mongodb.gridfs.GridFSFile;
* Collection of operations to store and read files from MongoDB GridFS. * Collection of operations to store and read files from MongoDB GridFS.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider
*/ */
public interface GridFsOperations extends ResourcePatternResolver { public interface GridFsOperations extends ResourcePatternResolver {
@ -42,27 +43,60 @@ public interface GridFsOperations extends ResourcePatternResolver {
*/ */
GridFSFile store(InputStream content, String filename); GridFSFile store(InputStream content, String filename);
/**
* Stores the given content into a file with the given name and content type.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @param contentType can be {@literal null}.
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename, String contentType);
/** /**
* Stores the given content into a file with the given name using the given metadata. The metadata object will be * Stores the given content into a file with the given name using the given metadata. The metadata object will be
* marshalled before writing. * marshalled before writing.
* *
* @param content must not be {@literal null}. * @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty. * @param filename must not be {@literal null} or empty.
* @param metadata * @param metadata can be {@literal null}.
* @return the {@link GridFSFile} just created * @return the {@link GridFSFile} just created
*/ */
GridFSFile store(InputStream content, String filename, Object metadata); GridFSFile store(InputStream content, String filename, Object metadata);
/**
* Stores the given content into a file with the given name and content type using the given metadata. The metadata
* object will be marshalled before writing.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @param contentType can be {@literal null}.
* @param metadata can be {@literal null}
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename, String contentType, Object metadata);
/** /**
* Stores the given content into a file with the given name using the given metadata. * Stores the given content into a file with the given name using the given metadata.
* *
* @param content must not be {@literal null}. * @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty. * @param filename must not be {@literal null} or empty.
* @param metadata must not be {@literal null}. * @param metadata can be {@literal null}.
* @return the {@link GridFSFile} just created * @return the {@link GridFSFile} just created
*/ */
GridFSFile store(InputStream content, String filename, DBObject metadata); GridFSFile store(InputStream content, String filename, DBObject metadata);
/**
* Stores the given content into a file with the given name and content type using the given metadata.
*
* @param content must not be {@literal null}.
* @param filename must not be {@literal null} or empty.
* @param contentType can be {@literal null}.
* @param metadata can be {@literal null}.
* @return the {@link GridFSFile} just created
*/
GridFSFile store(InputStream content, String filename, String contentType, DBObject metadata);
/** /**
* Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the * Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the
* {@link Query} will not be regarded as MongoDB does not support ordering for GridFS file access. * {@link Query} will not be regarded as MongoDB does not support ordering for GridFS file access.

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,7 @@ import com.mongodb.gridfs.GridFSInputFile;
* {@link GridFsOperations} implementation to store content into MongoDB GridFS. * {@link GridFsOperations} implementation to store content into MongoDB GridFS.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider
*/ */
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver { public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
@ -87,15 +88,37 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
return store(content, filename, (Object) null); return store(content, filename, (Object) null);
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String)
*/
public GridFSFile store(InputStream content, String filename, String contentType) {
return store(content, filename, contentType, (Object) null);
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object) * @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object)
*/ */
public GridFSFile store(InputStream content, String filename, Object metadata) { public GridFSFile store(InputStream content, String filename, Object metadata) {
DBObject dbObject = new BasicDBObject(); return store(content, filename, null, metadata);
converter.write(metadata, dbObject); }
return store(content, filename, dbObject);
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String, java.lang.Object)
*/
public GridFSFile store(InputStream content, String filename, String contentType, Object metadata) {
DBObject dbObject = null;
if (metadata != null) {
dbObject = new BasicDBObject();
converter.write(metadata, dbObject);
}
return store(content, filename, contentType, dbObject);
} }
/* /*
@ -103,16 +126,30 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, com.mongodb.DBObject) * @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, com.mongodb.DBObject)
*/ */
public GridFSFile store(InputStream content, String filename, DBObject metadata) { public GridFSFile store(InputStream content, String filename, DBObject metadata) {
return this.store(content, filename, null, metadata);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, com.mongodb.DBObject)
*/
public GridFSFile store(InputStream content, String filename, String contentType, DBObject metadata) {
Assert.notNull(content); Assert.notNull(content);
Assert.hasText(filename); Assert.hasText(filename);
Assert.notNull(metadata);
GridFSInputFile file = getGridFs().createFile(content); GridFSInputFile file = getGridFs().createFile(content);
file.setFilename(filename); file.setFilename(filename);
file.setMetaData(metadata);
file.save();
if (metadata != null) {
file.setMetaData(metadata);
}
if (contentType != null) {
file.setContentType(contentType);
}
file.save();
return file; return file;
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2011 the original author or authors. * Copyright 2011-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import com.mongodb.gridfs.GridFSFile;
* Integration tests for {@link GridFsTemplate}. * Integration tests for {@link GridFsTemplate}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Philipp Schneider
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:gridfs/gridfs.xml") @ContextConfiguration("classpath:gridfs/gridfs.xml")
@ -77,6 +78,19 @@ public class GridFsTemplateIIntegrationTests {
assertSame(result.get(0), reference); assertSame(result.get(0), reference);
} }
/**
* @see DATAMONGO-503
*/
@Test
public void storesContentType() throws IOException {
GridFSFile reference = operations.store(resource.getInputStream(), "foo2.xml", "application/xml");
List<GridFSDBFile> result = operations.find(query(whereContentType().is("application/xml")));
assertThat(result.size(), is(1));
assertSame(result.get(0), reference);
}
@Test @Test
public void marshalsComplexMetadata() throws IOException { public void marshalsComplexMetadata() throws IOException {

Loading…
Cancel
Save