Permalink
Browse files

JAVA-673: Added support for ssl=true|false to connection string

  • Loading branch information...
1 parent f515070 commit 10d8a0bf20edaad32a905ea0d27b7b9880f8deec @jyemin jyemin committed Nov 14, 2012
Showing with 16 additions and 0 deletions.
  1. +7 −0 src/main/com/mongodb/MongoClientURI.java
  2. +9 −0 src/test/com/mongodb/MongoClientURITest.java
@@ -17,6 +17,7 @@
package com.mongodb;
+import javax.net.ssl.SSLSocketFactory;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
@@ -62,6 +63,7 @@
* </ul>
* <p>Connection Configuration:</p>
* <ul>
+ * <li>{@code ssl=true|false}: Whether to connect using SSL.</li>
* <li>{@code connectTimeoutMS=ms}: How long a connection can take to be opened before timing out.</li>
* <li>{@code socketTimeoutMS=ms}: How long a send or receive on a socket can take before timing out.</li>
* </ul>
@@ -254,6 +256,7 @@ public MongoClientURI(final String uri) {
generalOptionsKeys.add("sockettimeoutms");
generalOptionsKeys.add("sockettimeoutms");
generalOptionsKeys.add("autoconnectretry");
+ generalOptionsKeys.add("ssl");
readPreferenceKeys.add("slaveok");
readPreferenceKeys.add("readpreference");
@@ -301,6 +304,10 @@ private MongoClientOptions createOptions(Map<String, List<String>> optionsMap, M
builder.socketTimeout(Integer.parseInt(value));
} else if (key.equals("autoconnectretry")) {
builder.autoConnectRetry(_parseBoolean(value));
+ } else if (key.equals("ssl")) {
+ if (_parseBoolean(value)) {
+ builder.socketFactory(SSLSocketFactory.getDefault());
+ }
}
}
@@ -22,6 +22,7 @@
import javax.net.SocketFactory;
import java.net.UnknownHostException;
+import javax.net.ssl.SSLSocketFactory;
public class MongoClientURITest extends TestCase {
@@ -151,6 +152,14 @@ public void testWriteConcern() {
}
+ @Test
+ public void testSSLOption() {
+ MongoClientURI uri = new MongoClientURI("mongodb://localhost/?ssl=false");
+ assertFalse(uri.getOptions().getSocketFactory() instanceof SSLSocketFactory);
+
+ uri = new MongoClientURI("mongodb://localhost/?ssl=true");
+ assertTrue(uri.getOptions().getSocketFactory() instanceof SSLSocketFactory);
+ }
@Test()
public void testOptions() {

0 comments on commit 10d8a0b

Please sign in to comment.