Browse Source

DATAMONGO-1903 - Align database name check in SimpleMongoDbFactory with MongoDB limitations.

We now test database names against the current (3.6) MongoDB specifications for database names.

Original pull request: #546.
pull/553/head
George Moraitis 8 years ago committed by Mark Paluch
parent
commit
b69ddd43ac
  1. 29
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java
  2. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java

29
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/SimpleMongoDbFactory.java

@ -36,7 +36,7 @@ import com.mongodb.WriteConcern;
/** /**
* Factory to create {@link DB} instances from a {@link Mongo} instance. * Factory to create {@link DB} instances from a {@link Mongo} instance.
* *
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
@ -55,7 +55,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Create an instance of {@link SimpleMongoDbFactory} given the {@link Mongo} instance and database name. * Create an instance of {@link SimpleMongoDbFactory} given the {@link Mongo} instance and database name.
* *
* @param mongo Mongo instance, must not be {@literal null}. * @param mongo Mongo instance, must not be {@literal null}.
* @param databaseName database name, not be {@literal null} or empty. * @param databaseName database name, not be {@literal null} or empty.
* @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClient, String)}. * @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClient, String)}.
@ -67,7 +67,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password * Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password
* *
* @param mongo Mongo instance, must not be {@literal null}. * @param mongo Mongo instance, must not be {@literal null}.
* @param databaseName Database name, must not be {@literal null} or empty. * @param databaseName Database name, must not be {@literal null} or empty.
* @param credentials username and password. * @param credentials username and password.
@ -80,7 +80,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password * Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password
* *
* @param mongo Mongo instance, must not be {@literal null}. * @param mongo Mongo instance, must not be {@literal null}.
* @param databaseName Database name, must not be {@literal null} or empty. * @param databaseName Database name, must not be {@literal null} or empty.
* @param credentials username and password. * @param credentials username and password.
@ -95,7 +95,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}. * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}.
* *
* @param uri must not be {@literal null}. * @param uri must not be {@literal null}.
* @throws MongoException * @throws MongoException
* @throws UnknownHostException * @throws UnknownHostException
@ -110,7 +110,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}. * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClientURI}.
* *
* @param uri must not be {@literal null}. * @param uri must not be {@literal null}.
* @throws UnknownHostException * @throws UnknownHostException
* @since 1.7 * @since 1.7
@ -121,7 +121,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}. * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoClient}.
* *
* @param mongoClient must not be {@literal null}. * @param mongoClient must not be {@literal null}.
* @param databaseName must not be {@literal null}. * @param databaseName must not be {@literal null}.
* @since 1.7 * @since 1.7
@ -140,8 +140,8 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
Assert.notNull(mongo, "Mongo must not be null"); Assert.notNull(mongo, "Mongo must not be null");
Assert.hasText(databaseName, "Database name must not be empty"); Assert.hasText(databaseName, "Database name must not be empty");
Assert.isTrue(databaseName.matches("[\\w-]+"), Assert.isTrue(databaseName.matches("[^/\\\\.$\"]+"),
"Database name must only contain letters, numbers, underscores and dashes!"); "Database name must not contain any of the symbols[" + "[^/\\\\.$\"]+" + "]");
this.mongo = mongo; this.mongo = mongo;
this.databaseName = databaseName; this.databaseName = databaseName;
@ -163,8 +163,13 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
*/ */
private SimpleMongoDbFactory(MongoClient client, String databaseName, boolean mongoInstanceCreated) { private SimpleMongoDbFactory(MongoClient client, String databaseName, boolean mongoInstanceCreated) {
Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
String validNamePattern = isWindows ? "[^/\\\\.$*<>:|?\"]+" : "[^/\\\\.$\"]+";
Assert.notNull(client, "MongoClient must not be null!"); Assert.notNull(client, "MongoClient must not be null!");
Assert.hasText(databaseName, "Database name must not be empty!"); Assert.hasText(databaseName, "Database name must not be empty!");
Assert.isTrue(databaseName.matches(validNamePattern),
"Database name must not contain any of the symbols[" + (isWindows ? "/\\.$*<>:|?\"" : "/\\.$\"") + "]");
this.mongo = client; this.mongo = client;
this.databaseName = databaseName; this.databaseName = databaseName;
@ -176,7 +181,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Configures the {@link WriteConcern} to be used on the {@link DB} instance being created. * Configures the {@link WriteConcern} to be used on the {@link DB} instance being created.
* *
* @param writeConcern the writeConcern to set * @param writeConcern the writeConcern to set
*/ */
public void setWriteConcern(WriteConcern writeConcern) { public void setWriteConcern(WriteConcern writeConcern) {
@ -211,7 +216,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
/** /**
* Clean up the Mongo instance if it was created by the factory itself. * Clean up the Mongo instance if it was created by the factory itself.
* *
* @see DisposableBean#destroy() * @see DisposableBean#destroy()
*/ */
public void destroy() throws Exception { public void destroy() throws Exception {
@ -224,7 +229,7 @@ public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
return chars == null ? null : String.valueOf(chars); return chars == null ? null : String.valueOf(chars);
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator() * @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator()
*/ */

2
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SimpleMongoDbFactoryUnitTests.java

@ -52,7 +52,7 @@ public class SimpleMongoDbFactoryUnitTests {
@Test // DATADOC-254 @Test // DATADOC-254
public void rejectsIllegalDatabaseNames() { public void rejectsIllegalDatabaseNames() {
rejectsDatabaseName("foo.bar"); rejectsDatabaseName("foo.bar");
rejectsDatabaseName("foo!bar"); rejectsDatabaseName("foo$bar");
} }
@Test // DATADOC-254 @Test // DATADOC-254

Loading…
Cancel
Save