protected Builder getClientOptions(final SSLContext sslContext) { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.sslEnabled(true); builder.socketFactory(sslContext.getSocketFactory()); return builder; }
protected MongoClientOptions.Builder getClientOptions(final SSLContext sslContext) { MongoClientOptions.Builder builder = MongoClientOptions.builder(); builder.sslEnabled(true); builder.socketFactory(sslContext.getSocketFactory()); return builder; }
@Singleton @Provides public static MongoSession createMongoSession(TypeManager typeManager, MongoClientConfig config) { requireNonNull(config, "config is null"); MongoClientOptions.Builder options = MongoClientOptions.builder(); options.connectionsPerHost(config.getConnectionsPerHost()) .connectTimeout(config.getConnectionTimeout()) .socketTimeout(config.getSocketTimeout()) .socketKeepAlive(config.getSocketKeepAlive()) .sslEnabled(config.getSslEnabled()) .maxWaitTime(config.getMaxWaitTime()) .minConnectionsPerHost(config.getMinConnectionsPerHost()) .readPreference(config.getReadPreference().getReadPreference()) .writeConcern(config.getWriteConcern().getWriteConcern()); if (config.getRequiredReplicaSetName() != null) { options.requiredReplicaSetName(config.getRequiredReplicaSetName()); } MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build()); return new MongoSession( typeManager, client, config); } }
builder.sslEnabled(sslEnabled); builder.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier); return builder.build();
if (sslContext == null) { if (config.ssl != null) { optionsBuilder.sslEnabled(config.ssl.booleanValue()); optionsBuilder.sslEnabled(true); optionsBuilder.sslContext(sslContext);
@Bean public MongoClientOptions mongoClientOptions(MongoOptionProperties mongoOptionProperties) { if (mongoOptionProperties == null) { return new MongoClientOptions.Builder().build(); } return new MongoClientOptions.Builder() .minConnectionsPerHost(mongoOptionProperties.getMinConnectionPerHost()) .connectionsPerHost(mongoOptionProperties.getMaxConnectionPerHost()) .threadsAllowedToBlockForConnectionMultiplier(mongoOptionProperties.getThreadsAllowedToBlockForConnectionMultiplier()) .serverSelectionTimeout(mongoOptionProperties.getServerSelectionTimeout()) .maxWaitTime(mongoOptionProperties.getMaxWaitTime()) .maxConnectionIdleTime(mongoOptionProperties.getMaxConnectionIdleTime()) .maxConnectionLifeTime(mongoOptionProperties.getMaxConnectionLifeTime()) .connectTimeout(mongoOptionProperties.getConnectTimeout()) .socketTimeout(mongoOptionProperties.getSocketTimeout()) .socketKeepAlive(mongoOptionProperties.getSocketKeepAlive()) .sslEnabled(mongoOptionProperties.getSslEnabled()) .sslInvalidHostNameAllowed(mongoOptionProperties.getSslInvalidHostNameAllowed()) .alwaysUseMBeans(mongoOptionProperties.getAlwaysUseMBeans()) .heartbeatFrequency(mongoOptionProperties.getHeartbeatFrequency()) .minConnectionsPerHost(mongoOptionProperties.getMinConnectionPerHost()) .heartbeatConnectTimeout(mongoOptionProperties.getHeartbeatConnectTimeout()) .heartbeatSocketTimeout(mongoOptionProperties.getSocketTimeout()) .localThreshold(mongoOptionProperties.getLocalThreshold()) .build(); }
@Singleton @Provides public static MongoSession createMongoSession(TypeManager typeManager, MongoClientConfig config) { requireNonNull(config, "config is null"); MongoClientOptions.Builder options = MongoClientOptions.builder(); options.connectionsPerHost(config.getConnectionsPerHost()) .connectTimeout(config.getConnectionTimeout()) .socketTimeout(config.getSocketTimeout()) .socketKeepAlive(config.getSocketKeepAlive()) .sslEnabled(config.getSslEnabled()) .maxWaitTime(config.getMaxWaitTime()) .minConnectionsPerHost(config.getMinConnectionsPerHost()) .readPreference(config.getReadPreference().getReadPreference()) .writeConcern(config.getWriteConcern().getWriteConcern()); if (config.getRequiredReplicaSetName() != null) { options.requiredReplicaSetName(config.getRequiredReplicaSetName()); } MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build()); return new MongoSession( typeManager, client, config); } }
.sslEnabled(true) .sslContext(sslContext) .sslInvalidHostNameAllowed(true)
/** * Replica Set with Username/Password authentication (Mongo Atlas) * @param url * @param port * @param dbName * @param user * @param password * @param replicaSet * @throws UnknownHostException * @throws MongoException */ public void initConnection(String url, String port, String dbName, String user, String password, String replicaSet, String authSource) throws UnknownHostException, MongoException { String[] listOfHosts = url.split(","); List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>(); for (String host : listOfHosts) { serverAddressList.add(new ServerAddress(host, Integer.parseInt(port))); } MongoCredential mongoCredential = MongoCredential.createCredential(user, authSource, password.toCharArray()); List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>(); mongoCredentialList.add(mongoCredential); MongoClientOptions mongoOptions = MongoClientOptions.builder().sslEnabled(Boolean.TRUE).requiredReplicaSetName(replicaSet).build(); MongoClient m = new MongoClient(serverAddressList, mongoCredentialList, mongoOptions); db = m.getDatabase(dbName); }
private Builder getMongoClientOptionsBuilder(final CodecRegistry codecRegistry) { return builder() .sslEnabled(sslEnabled) .codecRegistry(codecRegistry) .readPreference(ReadPreference.valueOf(readPreference)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout) .serverSelectionTimeout(serverSelectionTimeout) .cursorFinalizerEnabled(true) .maxWaitTime(maxWaitTime) .maxConnectionLifeTime(connectionpool.getMaxLifeTime()) .threadsAllowedToBlockForConnectionMultiplier(connectionpool.getBlockedConnectionMultiplier()) .maxConnectionIdleTime(connectionpool.getMaxIdleTime()) .minConnectionsPerHost(connectionpool.getMinSize()) .connectionsPerHost(connectionpool.getMaxSize()); }
public MongoClientOptions toMongoClientOptions(final CodecRegistry codecRegistry) { return builder() .sslEnabled(sslEnabled) .codecRegistry(codecRegistry) .readPreference(ReadPreference.valueOf(readPreference)) .connectTimeout(connectTimeout) .serverSelectionTimeout(serverSelectionTimeout) .cursorFinalizerEnabled(true) .maxWaitTime(maxWaitTime) .maxConnectionLifeTime(connectionpool.getMaxLifeTime()) .threadsAllowedToBlockForConnectionMultiplier(connectionpool.getBlockedConnectionMultiplier()) .maxConnectionIdleTime(connectionpool.getMaxIdleTime()) .minConnectionsPerHost(connectionpool.getMinSize()) .connectionsPerHost(connectionpool.getMaxSize()) .build(); }
public MongoPlugin ssl() { this.options.sslEnabled(true); return this; }
/** * Class for MySQL database connections using the settings provided * @param dbSettings - database settings */ MongoDBDatabaseConnector(DatabaseConnectionSettingsImpl dbSettings) { this.dbSettings = dbSettings; MongoCredential credential = MongoCredential.createCredential(dbSettings.getUsername(), dbSettings.getDatabaseName(), dbSettings.getPassword().toCharArray()); MongoClientOptions options = MongoClientOptions.builder().sslEnabled(false).build(); client = new MongoClient(new ServerAddress(dbSettings.getHost(), dbSettings.getPort()), credential,options); }
public MongoPlugin ssl() { this.options.sslEnabled(true); return this; }
@Override public AbstractConnection connect(String host, String user, String password, String database, int port) { MongoCredential credential = MongoCredential.createCredential(user, database, password.toCharArray()); MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build(); client = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), options); this.database = client.getDatabase(database); connected = true; return this; }
private MongoClientOptions toMongoClientOptions(MongoClientConfiguration configuration) { MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); if (configuration.isSslEnabled()) { optionsBuilder.sslEnabled(configuration.isSslEnabled()); if (configuration.getSocketFactory() != null) { optionsBuilder.socketFactory(configuration.getSocketFactory()); optionsBuilder.sslInvalidHostNameAllowed(configuration.isSslAllowInvalidHostnames()); } } return optionsBuilder.build(); }
private MongoClientOptions toMongoClientOptions(MongoClientConfiguration configuration) { MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); if (configuration.isSslEnabled()) { optionsBuilder.sslEnabled(configuration.isSslEnabled()); if (configuration.getSocketFactory() != null) { optionsBuilder.socketFactory(configuration.getSocketFactory()); optionsBuilder.sslInvalidHostNameAllowed(configuration.isSslAllowInvalidHostnames()); } } return optionsBuilder.build(); }
private MongoClient createMongoClient(ServerConfiguration configuration) { List<String> serverUrls = configuration.getServerUrls(); if (serverUrls.isEmpty()) { throw new ConfigurationException("server host is not set"); } List<ServerAddress> serverAddresses = new LinkedList<>(); if (SshTunnelingConfiguration.isEmpty(configuration.getSshTunnelingConfiguration())) { for (String serverUrl : serverUrls) { ServerConfiguration.HostAndPort hostAndPort = ServerConfiguration.extractHostAndPort(serverUrl); serverAddresses.add(new ServerAddress(hostAndPort.host, hostAndPort.port)); } } else { serverAddresses.add(new ServerAddress(DEFAULT_TUNNEL_LOCAL_HOST, DEFAULT_TUNNEL_LOCAL_PORT)); } MongoClientOptions options = MongoClientOptions.builder() .sslEnabled(configuration.isSslConnection()) .readPreference(configuration.getReadPreference()) .codecRegistry(MongoClient.getDefaultCodecRegistry()) .build(); if (StringUtils.isEmpty(configuration.getUsername())) { return new MongoClient(serverAddresses, options); } else { MongoCredential credential = getMongoCredential(configuration); return new MongoClient(serverAddresses, Collections.singletonList(credential), options); } }
builder.sslEnabled(sslEnabled);
/** * @param config the configuration */ public ConnectionContext(Configuration config) { this.config = config; this.useHostsAsSeeds = config.getBoolean(MongoDbConnectorConfig.AUTO_DISCOVER_MEMBERS); final String username = config.getString(MongoDbConnectorConfig.USER); final String password = config.getString(MongoDbConnectorConfig.PASSWORD); final String adminDbName = ReplicaSetDiscovery.ADMIN_DATABASE_NAME; final boolean useSSL = config.getBoolean(MongoDbConnectorConfig.SSL_ENABLED); final boolean sslAllowInvalidHostnames = config.getBoolean(MongoDbConnectorConfig.SSL_ALLOW_INVALID_HOSTNAMES); // Set up the client pool so that it ... MongoClients.Builder clientBuilder = MongoClients.create(); if (username != null || password != null) { clientBuilder.withCredential(MongoCredential.createCredential(username, adminDbName, password.toCharArray())); } if (useSSL) { clientBuilder.options().sslEnabled(true).sslInvalidHostNameAllowed(sslAllowInvalidHostnames); } pool = clientBuilder.build(); this.replicaSets = ReplicaSets.parse(hosts()); final int initialDelayInMs = config.getInteger(MongoDbConnectorConfig.CONNECT_BACKOFF_INITIAL_DELAY_MS); final long maxDelayInMs = config.getLong(MongoDbConnectorConfig.CONNECT_BACKOFF_MAX_DELAY_MS); this.primaryBackoffStrategy = DelayStrategy.exponential(initialDelayInMs, maxDelayInMs); }