Merge pull request #44366 from nosan
* pr/44366: Add spring.data.mongodb.protocol property Closes gh-44366
This commit is contained in:
commit
bde4fb0b38
|
@ -51,6 +51,11 @@ public class MongoProperties {
|
|||
*/
|
||||
public static final String DEFAULT_URI = "mongodb://localhost/test";
|
||||
|
||||
/**
|
||||
* Protocol to be used for the MongoDB connection. Ignored if 'uri' is set.
|
||||
*/
|
||||
private String protocol = "mongodb";
|
||||
|
||||
/**
|
||||
* Mongo server host. Ignored if 'uri' is set.
|
||||
*/
|
||||
|
@ -117,6 +122,14 @@ public class MongoProperties {
|
|||
*/
|
||||
private Boolean autoIndexCreation;
|
||||
|
||||
public void setProtocol(String protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
public String getProtocol() {
|
||||
return this.protocol;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
|
|
@ -51,11 +51,11 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails
|
|||
|
||||
@Override
|
||||
public ConnectionString getConnectionString() {
|
||||
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
|
||||
// protocol://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
|
||||
if (this.properties.getUri() != null) {
|
||||
return new ConnectionString(this.properties.getUri());
|
||||
}
|
||||
StringBuilder builder = new StringBuilder("mongodb://");
|
||||
StringBuilder builder = new StringBuilder(getProtocol()).append("://");
|
||||
if (this.properties.getUsername() != null) {
|
||||
builder.append(encode(this.properties.getUsername()));
|
||||
builder.append(":");
|
||||
|
@ -83,6 +83,14 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails
|
|||
return new ConnectionString(builder.toString());
|
||||
}
|
||||
|
||||
private String getProtocol() {
|
||||
String protocol = this.properties.getProtocol();
|
||||
if (StringUtils.hasText(protocol)) {
|
||||
return protocol;
|
||||
}
|
||||
return "mongodb";
|
||||
}
|
||||
|
||||
private String encode(String input) {
|
||||
return URLEncoder.encode(input, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
|
|
@ -2924,6 +2924,22 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "spring.data.mongodb.protocol",
|
||||
"values": [
|
||||
{
|
||||
"value": "mongodb"
|
||||
},
|
||||
{
|
||||
"value": "mongodb+srv"
|
||||
}
|
||||
],
|
||||
"providers": [
|
||||
{
|
||||
"name": "any"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "spring.data.redis.lettuce.read-from",
|
||||
"values": [
|
||||
|
|
|
@ -24,6 +24,7 @@ import com.mongodb.MongoCredential;
|
|||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import com.mongodb.client.internal.MongoClientImpl;
|
||||
import com.mongodb.connection.ClusterConnectionMode;
|
||||
import com.mongodb.connection.SslSettings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -98,6 +99,22 @@ class MongoAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresProtocol() {
|
||||
this.contextRunner.withPropertyValues("spring.data.mongodb.protocol=mongodb+srv").run((context) -> {
|
||||
MongoClientSettings settings = getSettings(context);
|
||||
assertThat(settings.getClusterSettings().getMode()).isEqualTo(ClusterConnectionMode.MULTIPLE);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultProtocol() {
|
||||
this.contextRunner.run((context) -> {
|
||||
MongoClientSettings settings = getSettings(context);
|
||||
assertThat(settings.getClusterSettings().getMode()).isEqualTo(ClusterConnectionMode.SINGLE);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuresWithoutSslWhenDisabledWithBundle() {
|
||||
this.contextRunner
|
||||
|
|
|
@ -86,6 +86,13 @@ class PropertiesMongoConnectionDetailsTests {
|
|||
assertThat(connectionString.getDatabase()).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void protocolCanBeConfigured() {
|
||||
this.properties.setProtocol("mongodb+srv");
|
||||
ConnectionString connectionString = this.connectionDetails.getConnectionString();
|
||||
assertThat(connectionString.getConnectionString()).startsWith("mongodb+srv://");
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticationDatabaseCanBeConfigured() {
|
||||
this.properties.setUsername("user");
|
||||
|
|
Loading…
Reference in New Issue